Data Visualization (Error) via sensor

조회 수: 3 (최근 30일)
Haris Bin Yousaf
Haris Bin Yousaf 2022년 3월 24일
답변: Ryan Salvo 2022년 10월 10일
Heyyy, I am working on getting plots of Accelration, Angular Velocity and Magnetic Field and i am pretty close in obtaining them, I am stuck in this error that says:
Index in position 1 exceeds array bounds (must not exceed 10).
I guess that is because the sample rate for my sensor is 10 and it stores 10 values in matrices and my loop exceeds that limit. I just want help in resolving this error. I would be very pleased if someone could modify below code into error free code.
CODE:
%Clearing up ports and all objects
clc
clear all
%Creating Arduino Object
ard = arduino()
%Defining sample rate
fs = 100;
%Creating IMU sensor object
my_imu = mpu9250(ard, 'SampleRate', fs, 'OutputFormat', 'matrix')
%Reading the raw data form sensor and storing them in variables
[my_Accel, my_AngVel, my_Mag] = read(my_imu) %Matrix form
%Provide time frame in seconds
senseFrame = 60;
%Measure approximate execution time of a single read cycle
tic;
[Accel, AngVel, Mag] = read(my_imu);
tDelta = toc;
%Number of samples to be calculated in sensorFrame time frame
numSamples = floor(senseFrame/tDelta);
%Time Vector
tVector = linspace(0, senseFrame, numSamples);
tCorrection = 0;
recordedData = zeros(numSamples, 3, 3);
%Plotting the Data
subplot(3, 1, 1)
hold on
% Create handle to X-axis acceleration animatedline object
hAx = animatedline('color', 'r', 'linewidth', 1.25);
% Create handle to Y-axis acceleration animatedline object
hAy = animatedline('color', 'k', 'linewidth', 1.25);
% Create handle to Z-axis acceleration animatedline object
hAz = animatedline('color', 'b', 'linewidth', 1.25);
legend('A_x (m/s^2)','A_y (m/s^2)','A_z (m/s^2)');
ylabel('Acceleration (m/s^2)');xlabel('Time (s)');
title('Reading Accelerometer values from MPU9250 sensor', 'fontsize', 12);
axis([0 senseFrame -30 30]);
hold off
grid minor
subplot(3, 1, 2)
% Create handle to X-axis angular velocity animatedline object
hVx = animatedline('color', 'r', 'linewidth', 1.25);
% Create handle to Y-axis angular velocity animatedline object
hVy = animatedline('color', 'k', 'linewidth', 1.25);
% Create handle to Z-axis angular velocity animatedline object
hVz = animatedline('color', 'b', 'linewidth', 1.25);
legend('\omega_x (rad/s)','\omega_y (rad/s)','\omega_z (rad/s)');
ylabel('Angular Velocity (rad/s)');xlabel('Time (s)');
title('Reading Angular velocity values from MPU9250 sensor', 'fontsize', 12);
axis([0 senseFrame -10 10]);
hold off
grid minor
subplot(3, 1, 3)
% Create handle to X-axis magnetic field animatedline object
hMagx = animatedline('color', 'r', 'linewidth', 1.25);
% Create handle to Y-axis magnetic field animatedline object
hMagy = animatedline('color', 'k', 'linewidth', 1.25);
% Create handle to Z-axis magnetic field animatedline object
hMagz = animatedline('color', 'b', 'linewidth', 1.5);
legend('\mu_x (\muT)','\mu_y (\muT)','\mu_z (\muT)');
ylabel('Magnetic Field (\muT)');xlabel('Time (s)');
title('Reading Magnetometer values from MPU9250 sensor', 'fontsize', 12);
axis([0 senseFrame -50 50]);
hold off
grid minor
%Read and plot the data from sensor
for i = 1:numSamples
% Read Acceleration, Angular Velocity and Mangnetic field...
% values from the sensor in matrix form
[AccelVal, AngVelVal, MagVal] = read(my_imu);
tic;
addpoints(hAx, tVector(i) + tCorrection, AccelVal(1));
addpoints(hAy, tVector(i) + tCorrection, AccelVal(2));
addpoints(hAz, tVector(i) + tCorrection, AccelVal(3));
addpoints(hVx, tVector(i) + tCorrection, AngVelVal(1));
addpoints(hVy, tVector(i) + tCorrection, AngVelVal(2));
addpoints(hVz, tVector(i) + tCorrection, AngVelVal(3));
addpoints(hMagx, tVector(i) + tCorrection, MagVal(1));
addpoints(hMagy, tVector(i) + tCorrection, MagVal(2));
addpoints(hMagz, tVector(i) + tCorrection, MagVal(3));
recordedData(i, :, 1) = AccelVal(i,:);
recordedData(i, :, 2) = AngVelVal(i,:);
recordedData(i, :, 3) = MagVal(i,:);
tCorrection = toc;
drawnow;
end

답변 (1개)

Ryan Salvo
Ryan Salvo 2022년 10월 10일
Hi Haris,
The AccelVal, AngVelVal, and MagVal variables are incorrectly indexed at the end of final for-loop.
recordedData(i, :, 1) = AccelVal(i,:);
recordedData(i, :, 2) = AngVelVal(i,:);
recordedData(i, :, 3) = MagVal(i,:);
These values are of size SamplesPerRead-by-3. Since the indexing variable i is increasing from 1 to numSamples, once it is greater than the default SamplesPerRead value of 10, the code errors.
Thanks,
Ryan

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by