01-17-2014 08:31 AM
(I am aware that this may not be the most appropriate place to ask, but I'm pretty desperate...)
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
All comments/suggestions are welcome.
Many thanks in advance!
01-20-2014
03:14 AM
- last edited on
04-16-2025
03:34 PM
by
Content Cleaner
Hello hemm,
As you noted this is most likely not the appropriate place to past these non-NI Development environment related questions.
Especially since the issue does not seem to be related to the interface with the NI DAQ card.
Maybe you could add some Message Tags so that people that are following specific tags might be able to detect your question and help you.
Would it be an option for you to use NI LabVIEW?
If this is the case then it might be interesting to note that many of the Arduino users over here are using the NI LabVIEW Interface for Arduino Toolkit:
https://www.ni.com/en/support/downloads/tools-network/download.labview-hobbyist-toolkit.html
This is however being supported from the following page:
https://forums.ni.com/t5/LabVIEW-Interface-for-Arduino/ct-p/7008