storing arduino serial data to cell array
조회 수: 5 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (2개)
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
James Hornby
2015년 4월 11일
편집: James Hornby
2015년 4월 11일
Hi, Iv sort of managed to get it to work by copying and pasting the line:
mData(i,1) = str2double(t(1));
Replacing it with:
mData(i,1) = hex2dec(t(1));
and Iv managed to select just the 12, 20th and 21st byte of the packet.
However, the function never runs to completion and to generate my .csv file, it only manages between 20 and 70 cycles before it crashes out and produces the error message:
"Error using serial/fscanf (line 154) Unsuccessful read: Index exceeds matrix dimensions.
Error in RouterPacketIn (line 14) sSerialData = fscanf(s1); %read sensor"
Any thoughts on what is causing the crash? If its something to do with errors in the comms is there any way of just telling the code to ignoore it and carry on?
Thanks again
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
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!