How can I change this code to get the line when plotting the graph? Because I tried adding commands related but still there is no line when I am running the codes

1 回表示 (過去 30 日間)
function [] = i2c_sensor()
a = arduino('COM3', 'UNO');
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
for i=1:10
accelReadings(i,:) = readAcceleration(imu);
display(accelReadings(i,:));
pause(1);
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');
end
end

採用された回答

dpb
dpb 2021 年 2 月 2 日
You're creating a new animatedline object every time, not adding points to the ones intended...that's not how the examples in the documentation show using it.
function [] = i2c_sensor()
a = arduino('COM3', 'UNO');
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
title('Acceleration values from mpu6050');
hx_val = animatedline('Color','r');
hy_val = animatedline('Color','g');
hz_val = animatedline('Color','b');
for i=1:10
accelReadings(i,:) = readAcceleration(imu);
t(i)=????
display(accelReadings(i,:));
addpoints(hx_val(t(i),accelReadings(i,1));
addpoints(hy_val(t(i),accelReadings(i,2));
addpoints(hz_val(t(i),accelReadings(i,3));
drawnow
pause(1);
end
end
ASSUMPTIONS:
  1. x,y,z components of acceleration are in a 3-vector returned from the readAcceleration routine,
  2. can return the sample time for each reading in some manner into variable t which is needed for an abscissa plotting location by addpoints
Read the documentation for both animatedline and addpoints and look at all the examples carefully for further enhancements in using.
  3 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 3 日
addpoints(hx_val, t(i),accelReadings(i,1));
addpoints(hy_val, t(i),accelReadings(i,2));
addpoints(hz_val, t(i),accelReadings(i,3));

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by