storing arduino serial data to cell array

조회 수: 76(최근 30일)
Ritter
Ritter 2014년 8월 1일
댓글: harold 2018년 5월 14일
I have an Arduino Mega 2560 that reads and outputs raw the x, y and z accelerometer sensor values of 3 different acceleromters(shown below):
void loop()
{
int hip_x = analogRead(hip_xpin);
int hip_y = analogRead(hip_ypin);
int hip_z = analogRead(hip_zpin);
int knee_x = analogRead(knee_xpin);
int knee_y = analogRead(knee_ypin);
int knee_z = analogRead(knee_zpin);
int ankle_x = analogRead(ankle_xpin);
int ankle_y = analogRead(ankle_ypin);
int ankle_z = analogRead(ankle_zpin);
if(digitalRead(pulse)== LOW)
{
Serial.print(hip_x);
Serial.print("\t");
Serial.print(hip_y);
Serial.print("\t");
Serial.print(hip_z);
Serial.print("\t");
Serial.print(knee_x);
Serial.print("\t");
Serial.print(knee_y);
Serial.print("\t");
Serial.print(knee_z);
Serial.print("\t");
Serial.print(ankle_x);
Serial.print("\t");
Serial.print(ankle_y);
Serial.print("\t");
Serial.print(ankle_z);
Serial.print("\n");
count=count+1;
while (digitalRead(pulse)== LOW)
{
//do nothing
}
}
}
The Arduino is meant to synchronize with a motion capture system running at 60 frames per second and then take accelerometer values once per frame. I wrote a Matlab script to read and store the values into a cell array.
%clear all;
s1 = serial('COM9'); %define serial port
s1.BaudRate=115200; %define baud rate
%data=zeros(490<)
%open serial port
%set(s1, 'terminator', 'LF');
fopen(s1);
s1.ReadAsyncMode = 'continuous';
count =1;
%data = cell(480,10);
%evalin('data', 's1');
readasync(s1);
while(s1.BytesAvailable <= 0) %wait until Arduino outputs data
end
while (1)
%while(s1.BytesAvailable > 0)
data = fscanf(s1); %read sensor
flushinput(s1);
disp(data);
%disp(count)
%count = count+1;
% end
end
% close the serial port!
fclose(s1);
However no matter how I play with the 'fscanf' function to store the incoming values into an array I keep getting a dimension mismatch error. When I display the incoming serial values I get this:
515 511 620 515 514 622 502 495 624
515 511 620 515 514 622 502 495 624
corresponding to the x, y and z values of each acceleromter respectively. I would like to keep this format but have Matlab store it in a cell array for further processing. Can anyone explain how I may accomplish this?

답변(2개)

Juan B. Gutierrez
Juan B. Gutierrez 2015년 4월 4일
Ritter, it has been a few months since you asked this, but I ran into it yesterday. The trick is to know that the serial gives you a string, and you have to split it using strsplit. Here is a solution:
In your Arduino code, send the values separated by tab (any other character works as well):
Serial.print(accelerationX);
// print a tab character:
Serial.print("\t"); // Use this character in MATLAB
Serial.print(accelerationY);
Serial.println();
The following MATLAB code loads it into a matrix:
sFile = 'AcceDualAxis.csv';
if exist(sFile) == 0 ; % if the file does not exists, read instrumentation
delete(instrfindall); %pre-emptively close all ports
s1 = serial('COM5'); %define serial port to read the Arduino
s1.BaudRate=115200; %define baud rate
fopen(s1);
s1.ReadAsyncMode = 'continuous';
readasync(s1);
while(s1.BytesAvailable <= 0) %wait until Arduino outputs data
%
end
mData = [];
for i=1:1000 %while if constant acquisition is needed.
sSerialData = fscanf(s1); %read sensor
flushinput(s1);
t = strsplit(sSerialData,'\t'); % same character as the Arduino code
mData(i,1) = str2double(t(1));
mData(i,2) = str2double(t(2));
end
delete(instrfindall); % close the serial port
csvwrite(sFile,mData); % save the data to a CSV file
else % if the file exists, load it
mData = csvread(sFile);
end
  댓글 수: 3
aqsa aqsa
aqsa aqsa 2016년 9월 21일
Hi, James. i am working on a matlab code which recieves values in the form of string from arduino. And there are four values in a string i recieve. Now i have to split all four of these in a cell array. But after storing first two values i am recieving the error of Index exceeds matrix dimensions. Please guide me how u manage to read the values in an array of 1000x3.

댓글을 달려면 로그인하십시오.


Chirag Gupta
Chirag Gupta 2014년 8월 1일
Have you taken a look at the Serial_Monitor? Might solve your issue http://www.mathworks.com/matlabcentral/fileexchange/45839-serial-monitor--debugger-
  댓글 수: 2
harold
harold 2018년 5월 14일
Hey, did you solved the situation? If so, would you mind sharing the code? Because I face with a similar problem. Thx.

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by