Anyone can help me to understand this code?

1 次查看(过去 30 天)
a = arduino();
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
figure;
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
title('Acceleration values from mpu6050');
x_val = animatedline('Color','r');
y_val = animatedline('Color','g');
z_val = animatedline('Color','b');
stop_time = 10; % time in seconds
count = 1;
tic;
while(toc < stop_time)
data = read(imu);
addpoints(x_val,count:(count+imu.SamplesPerRead-1),data.Acceleration(:,1));
addpoints(y_val,count:(count+imu.SamplesPerRead-1),data.Acceleration(:,2));
addpoints(z_val,count:(count+imu.SamplesPerRead-1),data.Acceleration(:,3));
count = count + imu.SamplesPerRead;
pause(0.001);
end
release(imu);

采纳的回答

Walter Roberson
Walter Roberson 2021-1-30
It configures an arduino. It configures an accelerometer attached to the arduino. It initializes some variables.
Then it loops for a set amount of clock time, reading 10 accelerometer samples at a time, each with 3 channels, X Y Z. It adds the received data to the corresponding graphics line. It pauses for one millisecond. In this context, really the pause is there to tell the graphics system it is okay to update the display.
  2 个评论
WAN NOR NAZIRA MUSTAPA KAMAL
"Then it loops for a set amount of clock time", what do you meant by that? And actually I am confused where I tried to run the code, the stop time is 10 second, but why the graph is stop at 500 second? Do you know?
Walter Roberson
Walter Roberson 2021-1-30
Each time the statement
while(toc < stop_time)
is executed, it figures out the elapsed clock time since the tic statement; the value is measured in seconds. When the configures stop_time is reached (so 10 clock seconds have passed since the tic) then the loop exits.
The x axis of the plots are not seconds: they are sample numbers. You configured 50 samples per second, and 50 samples/s * 10 seconds = 500 samples.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by