Anyone can help me to understand this code?

2 vues (au cours des 30 derniers jours)
WAN NOR NAZIRA MUSTAPA KAMAL
Commenté : Walter Roberson le 30 Jan 2021
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);

Réponse acceptée

Walter Roberson
Walter Roberson le 30 Jan 2021
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 commentaires
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 le 30 Jan 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by