What should I change in this code for me to get in the command window a 10 data of acceleration with 1 data for 1 second?

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

imu = mpu6050(a,'SampleRate',1,'SamplesPerRead',1,'ReadMode','Latest');

8 commentaires

I cannot do that as there is an error in the command window like this : Expected SampleRate to be a scalar with value >= 10.
imu = mpu6050(a,'SampleRate',10,'SamplesPerRead',10,'ReadMode','Latest');
data = read(imu);
addpoints(x_val,count,data.Acceleration(1,1));
addpoints(y_val,count,data.Acceleration(1,2));
addpoints(z_val,count,data.Acceleration(1,3));
count = count + 1;
pause(0.001);
end
By the way, can you explain to me what does it meant by this line of code:
addpoints(x_val,count,data.Acceleration(1,1));
because you change the line it is, 1 at front it is meaning for 1 second?
The hardware does not let you set a rate less than 10 Hz, but you only need 1 Hz. Obvious work around: tell the hardware you want 10 Hz, but ignore everything except the first sample out of the group of 10.
When you read the samples, the first column is X, the second is Y, the third column is Z.
The first row is for the time you started reading; the second is 1/SampleRate seconds later; the third row is 1/SampleRate seconds later, and so on. So with SampleRate 10, the first row is the time you started reading, the second row is time + 0.1 seconds, the third row is time + 0.2 seconds. You asked for 10 SamplesPerRead so the last row would be time + 0.9 seconds. But you only care about data once per second, so just ignore everything other than the first row.
So, the one in the graph plotting, it is only plot the first row of data? like that?
The code plots the first row out of each group. The groups are taken at 10 Hz and 10 are read at one time, so one second of data is read at a time, but you do not bother to plot the data corresponding to .1,.2,.3,.4,.5,.6,.7,.8,.9 of the second, just the .0 of each second.
I see. But why I observed that the graph is not compatible compare with the data in the table obtained? I meant the value in table is not the same as in the graph

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by