Go Down

Topic: Plotting ADXL345 Accelerometer Data on MATLAB (Read 3333 times) previous topic - next topic


Hey All,

I'm reading in data from an ADXL345 three-axis accelerometer, and plotting live in MATLAB. I sometimes get this error "Warning: A Timeout Occurred Before Terminator Was Reached" - other times, plotting works perfectly fine. I have searched through similar problems, but have not been able to find a solution specific to mine. I use fscanf to read in the data coming from the accelerometer as float data types. There are typically three values one each for x, y, and z value, but sometimes there will be fewer than three values - I thought this might have something to do with it, but for the times that plotting does work, it works indefinitely.

I show my MATLAB code below:

Anyone have an idea what could be wrong?

Thanks for your help!
Code: [Select]

// For Arduino

// This is what I am using to print data over serial port.

 Serial.print(x, DEC);
 Serial.print(' ');
 Serial.print(y, DEC);
 Serial.print(' ');
 Serial.println(z, DEC);

Code: [Select]

%% Plots data from serial port in real time

% variables
time = 0;
data1 = 0;
data2 = 0;
data3 = 0;
index = 0;
scrollWidth = 10;
delay = 0.005;
min = -100;
max = 100;

graph1 = plot(time, data1, 'm-');
hold on;
graph2 = plot(time, data2, 'c-');
hold on;
graph3 = plot(time, data3, 'g-');

% setup serial port

s = serial('/dev/tty.usbmodem1411');
set(s, 'InputBufferSize', 1024);
set(s, 'FlowControl', 'hardware');
% set(s, 'BaudRate', 57600);
set(s, 'BaudRate', 115200);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout', 10);


% start time

loop = 0;
while ishandle(graph1)
   stream = fscanf(s, '%f %f %f')';
   if (loop < 10)
       loop = loop + 1;
   elseif (loop == 10)
       loop = 0;
       index = index + 1;
       time(index) = toc;
       if (numel(stream) < 3)
           data1(index) = stream(1, 1);
           data2(index) = stream(1, 2);
           data3(index) = stream(1, 3);
       if(scrollWidth > 0)
           selindex = time > time(index) - scrollWidth;
           set(graph1,'XData', time(selindex), 'YData', data1(selindex));
           set(graph2,'XData', time(selindex), 'YData', data2(selindex));
           set(graph3,'XData', time(selindex), 'YData', data3(selindex));
           axis([time(index) - scrollWidth time(index) min max]);
           set(graph1,'XData', time, 'YData', data1);
           set(graph2, 'XData', time, 'YData', data2);
           set(graph3, 'Xdata', time, 'YData', data3);
           axis([0 time(index) min max]);



Hi CS_accel,

I have had a play around with this code and have a few questions for you. It seems to be working fine, but please could you (or anyone else) explain how the following delay time was derived ?

   if (loop < 10)
       loop = loop + 1;
   elseif (loop == 10)
       loop = 0;


Go Up