Quantcast
Channel: Instrument Control (GPIB, Serial, VISA, IVI) topics
Viewing all articles
Browse latest Browse all 5625

NI DAQ and Arduino in Matlab (control obj in a callback function triggered by another obj)

$
0
0

(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!


Viewing all articles
Browse latest Browse all 5625

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>