Anyone can help me to understand this code?

조회 수: 1 (최근 30일)
WAN NOR NAZIRA MUSTAPA KAMAL
WAN NOR NAZIRA MUSTAPA KAMAL 2021년 1월 30일
댓글: Walter Roberson 2021년 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
WAN NOR NAZIRA MUSTAPA KAMAL 2021년 1월 30일
"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에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by