Create a real-time control in DAQ Matlab

21 次查看(过去 30 天)
I'm using the Data Acquisition Toolbox for acquiring and generating data with High-Speed Dynamic Signal Analyzers (Modell DT9847 from data translation).
I have a Mini SmartShaker™ with Intergrated Power Amplifier (MODEL K2007E01) connecting to output channel as acutator;
and one Dynamic Force Sensor (MODEL PCB-288D01) connecting to input channel 0&1 for collecting data.
Now I have built up an open loop which containing one output channel and two input channels. The acquisition and generation rates set to
s.Rate = 100000;
%%Construct the output data
%sweep sine wave source
t = linspace(0, 60, 6000000)';
outputSignal1= chirp(t, 100, t(end), 500)*1/10;
%fixed frequency sine wave source
data = linspace(0,pi*100,s.Rate)';
outputSignal2 = sin(data)*1/10 ;
%%Queue the output data
% Use the |queueOutputData| command to queue the data.
%first queue 60sec of sweep signal(60sec), after that queue fixed frequency signal.
queueOutputData(s,outputSignal1);
queueOutputData(s,outputSignal2);
%Listener fires every 1/10 sec
s.NotifyWhenDataAvailableExceeds = 10000;
s.IsContinuous = true;
s.startBackground;
Then I got the result by calling two listeners;
dataAvailableListener = addlistener(s,'DataAvailable',...
@(src,event)plot(event.TimeStamps, event.Data));
lh = addlistener(s,'DataAvailable',@(src, event)logData(src, event, fid1));
function logData(src, evt, fid)
data = [evt.TimeStamps, evt.Data]' ;
fwrite(fid,data,'double');
end
Figure 1 Plot data during the acquisition (dataAvailableListener )
Figure 2 Plot data of the whole measurement from log file (lh)
In the first 60sec, the result is from the sweep signal outputsignal1. In the 60-70sec the result is from the outputsignal2 with fixed frequency.
You can see from figure 2, the amplitude of the sweep signal changing with fluctuations when the signal frequency increasing from 100Hz to 500Hz.
Now I would like to build a control loop, which could influence the output signal(amplitude) by setting a trigger for the input channel.
hk = addlistener(s,'DataAvailable',...
@inputReceived);
function inputReceived(src,event)
if any(event.Data(:,2) > 0.4)
disp('Too high')
%%control algorithm%%
end
if min(event.Data(:,2) < 0.4)
disp('Too low')
%%control algorithm%%
end
end
I would like to ask you about the possibility to realize the control loop or do you have any experience on building a control loop in matlab? I'm not a professtional in matlab, so could you give me some tools or algorithm to achieve the goal in real-time control? Thank you!
  3 个评论
Kyle Heintz
Kyle Heintz 2018-8-1
Each call to the "chirp" function creates a vector which starts at the value of 1 at time of 0. If you inspect the last values in the "outputSignal1" vector, they are different than the beginning values of "outputSignal2". The queued data ultimately has jumps in values at these points which is likely causing the spikes of noise you are seeing at the ends of each buffer.
A better way to handle this would be to call "chirp" only once for the entire range of time, or develop your own frequency sweep function. You could then extract only the points you want to be queued to the output channel, for example:
queueOutputData(s, outputSignalFull(1:1e6))
han xiao
han xiao 2018-8-6
Good morning, based on my last request on the noise in DAQ system, I've found out a solution to it.
Now I've been clear, that the noise came from the definition of t = linspace(0, 1, 100000)' of chirp function. Refer to the introduction of "linspace"(Generate linearly spaced vector),
y = linspace(x1,x2,n) generates n points. The spacing between the points is (x2-x1)/(n-1). (from 1. https://ww2.mathworks.cn/help/matlab/ref/linspace.html?lang=en )
we must choose two points x1 and x2 with an interval bigger or equal than 2, which means, we have to select two random points with x2-x1≥2.
For example, t = linspace(0, 2, 100000)' or t = linspace(1, 3, 100000)' etc.
In order to not influene the sample interval of sweep sine wave, we have to modify the total amount of scans from each buffer. Based on the theory of configuration, we have to enlarge the total number of scans.
Sample interval=0.00001=(x2-x1)/(n-1). x2=2,x1=0,n=200000;
If the the acquisition and generation rates is set to s.Rate = 100000; Each buffer will run for 2 seconds ( double the duration of time).
Now we change the acquisition and generation rates to s.Rate = 200000,
According to the QuickDAQ sweep function generation ,If we set the acquisition time equal to 60 sec, the sample interval will be 0,000005 Sec and the number of scans will be 12000000.
s.Rate = 200000;
t = linspace(0, 60, 12000000)';
outputSignal1= chirp(t, 200, t(end), 500)*1/10;
If we divided the chirp signal into 60 equivalence intervals, and we don't change the sample interval at the same time, we have to define 400000 scans per block;
t = linspace(0, 2, 400000)';
Sample interval=0.000005=(x2-x1)/(n-1)=(2-0)/(400000-1). These can lead to a double duration of the acquisition. Each block runs 2 seconds. If we create 60 new signals, the total duration will be 120 seconds.
The figure shows above represented 60 equivalence interval blocks multiplied with respective factors.
Until now, I've realized the basic control loop by adding factors to a group of new signals in workspace, but it worked a little bit complicated. I have to copy each factor in command window to the corresponding signal maunally,because the listener is called once per second, so each second the listener will generate a new factor in command window. I created a new .m file"sweep_signal_source_200000Rate" to save the new signals and then copy to my workspace"close_loop_design_in_real_time" later on.These two files are in my attachment.
Do you know a algorithm in Matlab, which can automatically copy and paste each factor sequentially? This may save a lot of time and reduce the chance of making mistakes. If you have an idea how to deal with it, I'll be very grateful!
Thank you very much in advance!

请先登录,再进行评论。

回答(1 个)

Kyle Heintz
Kyle Heintz 2018-7-30
MATLAB and the Data Acquisition Toolbox operate in a non real-time environment, and for this reason true deterministic real-time control is not possible.
If your control loop is not required to be true real-time, you may be able to program the control logic in a loop in MATLAB. It appears that you have already started building the framework for this control loop in MATLAB code. Refer to the following MATLAB Answers post for more information on this workflow:
If you are familiar with Simulink or willing to learn, our products Simulink Real-Time and Simulink Desktop Real-Time are designed for this type of application.
  2 个评论
han xiao
han xiao 2018-7-31
Thank you for your answer, but unfortunately I left the same question in that workflow. I tried with the simulink but it doesn't support my hardware. I have updated new situation in my question, please have a look at it! :D
hurrch
hurrch 2019-10-19
Good day
sorry i have to sneak into this conversation but i really need your expertise Sire.
I am a final year student of an institution in Africa and I have issues with my chosen project cause I do not have prior knowledge of coding or programming.
Please, I really need help with this project of mine as it will be due in 1 weeks’ time (1/11/2019), I don’t mind being assigned to someone good with the matlab DAQ toolbox for an extra fee. This is the crux of my project.
I am to write a code that will read data from two NI 9237 modules (6 channels in total). the modules are to read data from a force balance (strain due to force). The final result should be different plots for different force values against time. I will explain with a diagram and a flow chart, please do correct the flow chart if it is wrong. Some where in the code, there should be a scaling coefficient that will convert strain to force as needed for the equations. The code should plot the data in real time and also at the end of the acquisition. And the plots are individual force values against time. E.g N against time, M against time etc.
This is what I just did playing around with the code but I’m technically stuck afterwards.
daq.getVendors
daq.getDevices
s = daq.createSession ('ni');
ch1 = s.addAnalogInputChannel('Dev1', 'ai0', 'Bridge');
ch2 = s.addAnalogInputChannel('Dev1', 'ai1', 'Bridge');
ch3 = s.addAnalogInputChannel('Dev1', 'ai2', 'Bridge');
ch4 = s.addAnalogInputChannel('Dev2', 'ai0', 'Bridge');
ch5 = s.addAnalogInputChannel('Dev2', 'ai1', 'Bridge');
ch6 = s.addAnalogInputChannel('Dev2', 'ai2', 'Bridge');
ch1.BridgeMode = 'Full';
ch1.ExcitationVoltage = 10;
ch1.NominalBridgeResistance = 350;
ch2.BridgeMode = 'Full';
ch2.ExcitationVoltage = 10;
ch2.NominalBridgeResistance = 350;
ch3.BridgeMode = 'Full';
ch3.ExcitationVoltage = 10;
ch3.NominalBridgeResistance = 350;
ch4.BridgeMode = 'Full';
ch4.ExcitationVoltage = 10;
ch4.NominalBridgeResistance = 350;
ch5.BridgeMode = 'Full';
ch5.ExcitationVoltage = 10;
ch5.NominalBridgeResistance = 350;
ch6.BridgeMode = 'Full';
ch6.ExcitationVoltage = 10;
ch6.NominalBridgeResistance = 350;
attached below is the document to help understand what exactly i am doing and how far i have gone. it also has the flow chart, diagrams and equations.
i really do anticipate your reply.
thank you so much

请先登录,再进行评论。

产品


版本

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by