%% Generate IMU Data from Stationary Input
% Use the imuSensor System object™ to model receiving data from a stationary ideal IMU containing an accelerometer, gyroscope, and magnetometer.
% Create an ideal IMU sensor model that contains an accelerometer, gyroscope, and magnetometer.

IMU = imuSensor('accel-gyro-mag')

%
% Define the ground-truth, underlying motion of the IMU you are modeling. The acceleration and angular velocity are defined relative to the local NED coordinate system.

numSamples = 1000;
acceleration = zeros(numSamples,3);
angularVelocity = zeros(numSamples,3);

%
% Call IMU with the ground-truth acceleration and angular velocity. The object outputs accelerometer readings, gyroscope readings, and magnetometer readings, as modeled by the properties of the imuSensor System object. The accelerometer readings, gyroscope readings, and magnetometer readings are relative to the IMU sensor body coordinate system.

[accelReading,gyroReading,magReading] = IMU(acceleration,angularVelocity);

%
% Plot the accelerometer readings, gyroscope readings, and magnetometer readings.

t = (0:(numSamples-1))/IMU.SampleRate;
subplot(3,1,1)
plot(t,accelReading)
legend('X-axis','Y-axis','Z-axis')
title('Accelerometer Readings')
ylabel('Acceleration (m/s^2)')

subplot(3,1,2)
plot(t,gyroReading)
legend('X-axis','Y-axis','Z-axis')
title('Gyroscope Readings')
ylabel('Angular Velocity (rad/s)')

subplot(3,1,3)
plot(t,magReading)
legend('X-axis','Y-axis','Z-axis')
title('Magnetometer Readings')
xlabel('Time (s)')
ylabel('Magnetic Field (uT)')

%
% Orientation is not specified and the ground-truth motion is stationary, so the IMU sensor body coordinate system and the local NED coordinate system overlap for the entire simulation.
% Accelerometer readings: The z-axis of the sensor body corresponds to the Down-axis. The 9.8 m/s^2 acceleration along the z-axis is due to gravity.
% Gyroscope readings: The gyroscope readings are zero along each axis, as expected.
% Magnetometer readings: Because the sensor body coordinate system is aligned with the local NED coordinate system, the magnetometer readings correspond to the MagneticField property of imuSensor. The MagneticField property is defined in the local NED coordinate system.
% Copyright 2018 The MathWorks, Inc.