Anyone can help me to understand this code?

1 visualización (últimos 30 días)
WAN NOR NAZIRA MUSTAPA KAMAL
WAN NOR NAZIRA MUSTAPA KAMAL el 30 de En. de 2021
Comentada: Walter Roberson el 30 de En. de 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);

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de En. de 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 comentarios
WAN NOR NAZIRA MUSTAPA KAMAL
WAN NOR NAZIRA MUSTAPA KAMAL el 30 de En. de 2021
"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 el 30 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Arduino Hardware en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by