Real Time MULTIPLE Plot Data Accelerometer STM32 F4 Discovery
22 次查看(过去 30 天)
显示 更早的评论
Dear all,
my name is FIlippo from Bologna, Italy. I am using the STM32 F4 Discovery microcontroller that has a built-in accelerometer. I am streaming the three acceleration values through the serial port (Via USART) in this format: Value_x,Value_y,Value_z . I would like to stream these datas in a Matlab plot that refreshes continuosly. I made some researches on the web, and I found out a code that works well. I modified a little bit it. The problem is that this code allows to stream only ONE data (in my case, only one axis). As you can see, in the variable dat = fscanf(s,['%d,%d,%d']); i can correctly read all three acceleration, but actually i do not now how to plot three values instead of only one.
I just need three run-time plot, every form of plot is good: 3xsubplots, 3x different figure, 1 plot with three lines...everything will be ok.
Thank you very much.
Best regards Filippo
clear
clc
%User Defined Properties
serialPort = 'COM9'; % define COM port #
plotTitle = 'Serial Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Data'; % y-axis label
plotGrid = 'on'; % 'off' to turn off grid
min = -2010; % set y-min
max = 2010; % set y-max
scrollWidth = 10; % display period in plot, plot entire data log if <= 0
delay = .01; % make sure sample faster than resolution
%Define Function Variables
time = 0;
data_x = 0;
count = 0;
%Set up Plot
plotGraphx = plot(time,data_x,'-mo',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s = serial(serialPort)
disp('Close Plot to End Session');
fopen(s);
tic
while ( ishandle(plotGraphx) ) %Loop when Plot is Active && ishandle(plotGraphy) && ishandle(plotGraphz)
dat = fscanf(s,['%d,%d,%d']); %Read Data from Serial as Float: PLEASE NOTE: Here i modified the code, in order to adjust the correct format of data in input.
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
count = count + 1;
time(count) = toc; %Extract Elapsed Time
data_x(count) = dat(1); %Extract 1st Data Element
%Set Axis according to Scroll Width
if(scrollWidth > 0)
set(plotGraphx,'XData',time(time > time(count)-scrollWidth),'YData',data_x(time > time(count)-scrollWidth));
axis([time(count)-scrollWidth time(count) min max]);
else
set(plotGraphx,'XData',time,'YData',data_x);
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min plotGraph plotGrid plotTitle s ...
scrollWidth serialPort xLabel yLabel;
disp('Session Terminated...');
1 个评论
Mansi Gupta
2018-6-26
U ended the code with two ends, is it necessary to end communication when we are uploading the program to Arduino or other microcontroller.
回答(1 个)
Ameer Hamza
2018-5-21
Refer to my answer here, which deals with exactly the same problem. The main point is, you can have several lines on a single axis and obtain their handles. Then you add data to those handles in a similar way as you are doing now. Here is a general sketch of what you need to do
plotGraphx = plot(time,data_x,'-mo',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
hold on; % it will hold all the lines on same axis
plotGraphy = plot(time,data_y,'-mo',... <---- define data_y before this
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
plotGraphz = plot(time,data_x,'-mo',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
Then in the while loop, do something like this
data_x(count) = dat(1);
data_y(count) = dat(2);
data_x(count) = dat(3);
and for adding the points, use the respective handles
set(plotGraphx,'XData',time(time > time(count)-scrollWidth),'YData',data_x(time > time(count)-scrollWidth));
set(plotGraphy,'XData',time(time > time(count)-scrollWidth),'YData',data_y(time > time(count)-scrollWidth));
set(plotGraphz,'XData',time(time > time(count)-scrollWidth),'YData',data_z(time > time(count)-scrollWidth));
similar for the else statements.
5 个评论
Francesco Lucarelli
2021-6-10
Hi all,
i would like to read continuos data from an accelerometer.Before i was using hterm but it does not support long recording.
I was wondering if i can do that with matlab
My port is COM15 with a baud 8200000, it should create a new line every 13 characters (i have 1 byte to identy the accelerometer, 4 for time, 2 for x 2 for y 2 for z 2 for the current)
Can anyone help me with this?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!