How to control an object in a callback function triggered by another object.
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hartwin
el 17 de En. de 2014
Comentada: Image Analyst
el 24 de En. de 2014
I am writing a script to acquire online data from a DAQ card and to control an Arduino microcontroller based on the output of the DAQ card data.
The acquiring of the DAQ card data works fine, but I'm having problems with (1) the online processing of these data and (2) the corresponding controlling of the Arduino microcontroller. I'll try to explain both problems as clear as possible, the matlab code that has already been written can be found at the bottom of this question.
I'm using a 64-bit Matlab, so I can only use the Session-Based Interface.
1) Online processing
Data are collected at a rate of 1000 samples/s. Each time 250 new samples are available, a callback function is triggered to process these data (using a listener triggered by the 'DataAvailable' event). This processing consists of spectral analysis, using the 'spectrogram' function. The problem is that the spectrogram function should use a window of 500 samples and an overlap of 250 samples. So, new samples should be available for processing every 250 samples, but not only the new 250 samples but also the previous 250 samples are needed to accomplish the overlap. Basically, I need 500 samples for each callback function run, but this function has to be ran every 250 samples.
2) Controlling the Arduino
The biggest problem lies in the controlling of the Arduino. After spectral analysis, the calculated value is compared to a predefined treshold and if this value exceeds the treshold, the output of the Arduino should be put to one, else the output should be put to zero. The problem here is that, since the callback function is an external function, it doesn't recognize the Arduino object. The Arduino has been connected to Matlab at the start of the script, but is only available in the 'base' workspace. Connecting the Arduino on each function run isn't a possibility, since this should be done every 250 ms.
Attempted solutions
I have tried to make the function into a nested function, so that it can use the same variables as the ones in the script, but Matlab doesn't allow this. When I write a nested function, I get the following error: "A nested function cannot be defined inside a control statement (if, while, for, switch or try/catch)." However, I have never defined one of these statements, so the function isn't inside a control statement. I don't understand why this doesn't work.
Matlab code
Script
%%Preparing Arduino
% Connecting the arduino chip to Matlab
a=arduino('COM4');
% Defining the 13th pin as an output
a.pinMode(13,'output')
%%Preparing the DAQ card
% Searching the DAQ card
devices = daq.getDevices;
% Create a session
s = daq.createSession('ni');
% Define the input channel
s.addAnalogInputChannel('Dev1', 0, 'Voltage');
% Define the total measuring and stimulation time
s.DurationInSeconds = 4;
% Define the interval for processing
s.NotifyWhenDataAvailableExceeds = 500;
% Loading the optimal stimulation parameters
load('parameters.mat')
setappdata(s,'freq',parameters(1))
setappdata(s,'treshold',parameters(2))
% Define a listener triggered by the DataAvailable event
lh = s.addlistener('DataAvailable',@stimulation);
% Save the start time
starttime=clock;
% Start the measurements
LFP=s.startForeground;
Callback function
function stimulation(src,event)
freq=getappdata(src,'freq');
treshold=getappdata(src,'treshold');
LFP=event.Data;
[S,F,T,P]=spectrogram(LFP,500,250,[],1000);
PSD=log(P(freq));
if PSD>=treshold
a.digitalWrite(13,1)
else
a.digitalWrite(13,0)
end
end
0 comentarios
Respuesta aceptada
Image Analyst
el 17 de En. de 2014
Have you tried all the methods in the FAQ yet? http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
3 comentarios
Image Analyst
el 24 de En. de 2014
The second function does not need to be nested , it can be at the same level in the same m-file following the first function. Nested functions can be seen only by the function they're inside, whereas unnested functions can be seen by any other function.
Más respuestas (0)
Ver también
Categorías
Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!