Incorrect analog input reading in legacy interface matlab - matlab

I tried to use analog input signal for triggering purpose, however I have a problem with analog input reading. For example, when I send 6 V, I can read only 0.5 V, and signal form shown in the link is supposed to be square pulse but obviously, it is not.
My Daq card is NI PCI 6120. I used MAX software to check if it is a hardware issue, but it gives correct value and signal form, and as I try a session based matlab code to read only one analog input channel, I can get correct signal.
There should be a mistake in my matlab triggering code. Any suggestion ?
dig= digitalio('nidaq','Dev1');
line = addline(dio,0:1,'Out');
ai = analoginput('nidaq','Dev1');
channel = addchannel(ai,0:1);
set(ai,'SampleRate',fs);
set(ai,'SamplesPerTrigger',N);
set(ai,'Timeout',10000)
set(ai,'TriggerChannel',channel(1));
set(ai,'TriggerType','Software');
set(ai,'TriggerCondition','Rising');
set(ai,'TriggerConditionValue',0.5);
set(ai,'TriggerDelayUnits','Samples');
set(ai,'TriggerDelay',-3000);
set(ai,'LogFileName','file00.daq')
set(ai,'LoggingMode','Disk&Memory')
putvalue(dig,1)
start (ai)
[data t] = getdata(ai);
putvalue(dio,0)
delete(ai);
delete(dig);
enter image description here

Related

Digital Output Using NI BNC-2110 connect to NI USB-6255

I currently have NI BNC-2110 connect to the NI USB-6255 which is then connected to my computer.
My goal is to generate a digital output through the NI BNC-2110 and then read the output on an analog input on the same NI BNC-2110. (The purpose of this is to make sure that I know how to properly output a digital signal and can input an analog signal, and I figured this would just check both things simultaneously.)
The setup works when generating a digital signal and reading an analog when I use the software NI MAX, my problem is when I try and do the same thing in Matlab.
Here is my current code in Matlab:
d = daq.getDevices
s = daq.createSession('ni');
addAnalogInputChannel(s,'dev1', 'ai0', 'Voltage');
s.Rate = 8000;
q = daq.createSession('ni');
addDigitalChannel(q,'dev1','Port2/Line0:0','OutputOnly');
for p = 1:1:100
outputSingleScan(q,1)
pause(0.1)
data = s.inputSingleScan;
data
outputSingleScan(q,0)
pause(0.1)
data = s.inputSingleScan;
data
end
My current results are an analog input of ~0 Volts, and what I expect is a square wave from my analog input.
Any help on this would be much appreciated.
I am expecting something with a syntax in Matlab with port to be the problem, but not to sure.

How to take a photo inside a While-loop with a webcam in MATLAB?

Situation of the problem:
The Arduino measures the length of an object. If the length is between a pre-determined interval then the Arduino serial writes a '1' on a particular COM-port. MATLAB will read on the same COM-port so we can read the ‘1’ inside MATLAB. For each '1' (read by Matlab) a photo is taken by a webcam. The following While-loop gives us the opportunity to read the ‘1’ inside MATLAB.
clear all
clc
arduino = serial('/dev/tty.usbmodem1411','BaudRate',9600);
fopen(arduino);
Sensor = true
cam = webcam(2);
while (Sensor)
A = fscanf(arduino,'%d')
if A == 1
img = snapshot(cam);
imshow(img);
end
end
fclose(arduino);
But the webcam doesn’t take the picture we want to take.
We have the following problems:
The first time that a '1' is read (by Matlab), no photo is taken. The second time there is a photo taken. By the third ‘1’, previous photo changes a bit (but Matlab gives not the photo which is taken by the third ‘1’. Then by the fourth ‘1’, Matlab gives the photo that is taken by the third.
Anybody knows how I can fix this?

Serial Comunnication between matlab and arduino

I am trying to send data from MATLAB to ARDUINO using the following code for arduino and the second one for the matlab. Both code work fine and when i press 1 led lights up and when press 2 led become off. But actually what i am trying to do is when matlab run code it automatically send 1 to arduino and the led become on. i have tried may changes but cant able to do. when iam trying to run the third code (given below) the arduino status led blink that show it receive some thing but my actual led which is connected to pin 13 still off.
int ledPin=13;
int matlabData;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0) // if there is data to read
{
matlabData=Serial.read(); // read data
if(matlabData==1)
digitalWrite(ledPin,HIGH); // turn light on
else if(matlabData==2)
digitalWrite(ledPin,LOW); // turn light off
}
}
(MATLAB)
1.
clear all
2.clc
3.
4.answer=1; % this is where we'll store the user's answer
5.arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
6.
7.fopen(arduino); % initiate arduino communication
8.
9.while answer
10. fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
11. answer=input('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
12.end
13.
14.fclose(arduino); % end communication with arduino
(MY edit code)
1.
clear all
2.clc
3.
4.answer=1; % this is where we'll store the user's answer
5.arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
6.
7.fopen(arduino); % initiate arduino communication
8.
9.%while answer
10. fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
11. answer='1'%('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
12.%end
13.
14.fclose(arduino); % end communication with arduino
The difference is the following:
answer = input('bla')
yields an answer that is a number, i.e. answer is of type double. In your third case, you wrote answer='1' which is a char, so in fact, the variable answer is different in both cases. Try and change the code in the third part to this here:
answer = 1;
or change the fprintf command to
fprintf(arduino, '%s', str2num(answer));

How can I get signal dimensions in Simulink model

I have a question.
After simulate a simulink model I need to get signal dimensions of each line using MATLAB command.
I get line handles by following
line_h = find_system(gcs, 'FindAll', 'on','SearchDepth', 1, 'Type', 'Line')
then how can I get signal dimensions from line handles
** When check 'signal dimensions' in Format menu -> Port/Signal Displays
After simulate a model number of signal dimensions will show on nonscalar line.
I need to get it using MATLAB command.
Sorry for my English skill
Thank you
Alternatively, you can find the signal dimensions and signal widths of each block they originate from, using:
get_param(<block_path>,'CompiledPortDimensions')
get_param(<block_path>,'CompiledPortWidths')
Replacing <block_path> with the appropriate block path for each block of interest. The model must be compiled before you can run these commands, but since you indicate doing this after running the model, that shouldn't be a problem.
If you have a set of line handles from your find_system command you can use the following command to get the block connected to the signal.
hblkSrc = get_param(h(k),'SrcBlockHandle');
You can then use get_param(hblkSrc,'CompiledPortDimensions') as suggested by am304 to get the dimensions.
You can solve it the following way.
Enable signal logging for the desired signals (Properties). For
example set the name to custom and signalone.
If you actually don't want to log the signal, set Limit data points to last to 1, so you avoid storing unused data.
Go to SImulink preferences and enable signal logging, default output name is logsout
after simulation you'll get a dataset logsout in your workspace
now evaluate this dataset as follows:
% returns data, if data limit is set to 1 it's a coloumn
% vector with just the last value
data = logsout.get('signalone').Values.Data
you can now just use the size of this vector and you know the dimension of the signal
[~,dim]=size(data)
or in one line:
[~,dim]=size(logsout.get('signalone').Values.Data)
If you have a a lot of signals and you want to evaluate them at once, give your signals convenient output-names and use a loop for iterating through a string vector with all your signal names.
As you say you want the dimensions of "all" (are you sure?) signals I think it is more convenient to just check "Enable signal logging" in each signal property and do all further definitions in the Simulink preferences where you have a list to manage all signals.

Can you synchronize the data acquisition toolbox and the image acquisition toolbox of Matlab?

I'd like to simultaneously get data from a camera (i.e. an image) and an analog voltage using matlab. For the camera I use the imaq toolbox, for reading the voltage I use the daq toolbox (reading NI-USB device), with a following code:
clear all
% Prepare camera
vid = videoinput('gentl', 1, 'Mono8');
src = getselectedsource(vid);
vid.FramesPerTrigger = 1;
vid.TriggerRepeat = Inf;
triggerconfig(vid, 'hardware', 'DeviceSpecific', 'DeviceSpecific');
src.FrameStartTriggerMode = 'On';
src.FrameStartTriggerActivation = 'RisingEdge';
% prepare DAQ
s=daq.createSession('ni');
s.addAnalogInputChannel('Dev1','ai1','Voltage');
fid = fopen('log.txt','w');
lh = s.addlistener('DataAvailable',#(src,event)SaveData(fid,event));
s.IsContinuous = true;
% Take data
s.startBackground();
start(vid)
N=10;
for ii=1:N
im(:,:,ii)=getsnapshot(vid);
end
% end code
delete(lh );
fclose('all');
stop(vid)
delete(vid)
where the function SaveData is:
function SaveData(fid,event)
time = event.TimeStamps;
data = event.Data;
fprintf(fid, '%f,%f\n ', [time data]);
end
I do get images and a log.txt file with the daq trace (time and data), but how can I use the external triggering (that trigger the camera) or some other clock to synchronize the two?
For this example, the daq reads the camera triggering TTL signal (# 50 Hz), so I want to assign each TTL pulse to an image.
Addendum:
I've been searching and have found a few discussions (like this one) on the subject, and read the examples that are found in the Mathworks website, but haven't found an answer. The documentation shows how to Start a Multi-Trigger Acquisition on an External Event, but the acquisition discussed is only relevant for the DAQ based input, not a camera based input (it is also working in the foreground).
This will not entirely solve your problem, but it might be good enough. Since the synchronization signal you are after in at 50 Hz, you can use clock in order to create time stamps for both types of your data (camera image and analog voltage). Since the function clock takes practically no time (i.e. below 1e-7 sec), you can try edit to your SaveData function accordingly:
fprintf(fid, '%f,%f\n ', [clock time data]);
And in the for loop add:
timestamp(i,:)=clock;
Can you use the sync to trigger the AD board? From the USB-6009 manual...
Using PFI 0 as a Digital Trigger--
When an analog input task is defined, you can configure PFI 0 as a digital trigger input. When the digital trigger is enabled, the AI task waits for a rising or falling edge on PFI 0 before starting the acquisition. To use AI Start Trigger (ai/StartTrigger) with a digital source, specify PFI 0 as the source and select a rising or falling edge.
My experience suggests that delay between trigger and AQ is very short
I'm sorry I use Python or C for this, so I can't give you MatLab code, but you want to look at functions like.
/* Select trigger source */
Select_Signal(deviceNumber, ND_IN_START_TRIGGER, ND_PFI_0, ND_HIGH_TO_LOW);
/* specify that a start trigger is to be used */
DAQ_Config(deviceNumber, startTrig, extConv); // set startTrig = 1
/* start the acquisition */
DAQ_Start(deviceNumber, …)
If you want to take this route you could get more ideas from:
http://www.ni.com/white-paper/4326/en
Hope this helps,
Carl
This is yet no complete solution, but some thoughts that might be useful.
I do get images and a log.txt file with the daq trace (time and data), but how can I use the external triggering (that trigger the camera) or some other clock to synchronize the two?
Can you think of a way to calibrate your setup? I.e. modify your experiment and create a distinct event in both your image stream and voltage measurements, which can be used for synchronization? Just like this ...