Main Content

Compute Orientation from Recorded IMU Data

Load the rpy_9axis file into the workspace. The file contains recorded accelerometer, gyroscope, and magnetometer sensor data from a device oscillating in pitch (around the y-axis), then yaw (around the z-axis), and then roll (around the x-axis). The file also contains the sample rate of the recording.

load("rpy_9axis.mat","sensorData")

Read the acceleration and gyroscope readings from the recorded sensor data.

accel = sensorData.Acceleration;
gyro = sensorData.AngularVelocity;

Example Model

Open the Simulink® model.

modelname = "ComputeOrientationFromIMUData";
open_system(modelname)

The model reads the accelerometer readings and gyroscope readings from the MATLAB® workspace by using the Constant block. The sample rate of the Constant block is set to the sampling rate of the sensor. In this example, the sample rate is set to 0.005. The model uses the custom MATLAB Function block readSamples to input one sample of sensor data to the IMU Filter block at each simulation time step. Then, the model computes an estimate of the sensor body orientation by using an IMU Filter block with these parameters:

  • Reference frameNED

  • Orientation formatquaternion

  • Decimation factor1

  • Initial process noiseimufilter.defaultProcessNoise

  • Accelerometer noise0.0001924722

  • Gyroscope noise9.1385e-5

  • Gyroscope drift noise3.0462e-13

  • Linear acceleration noise0.0096236100000000012

  • Linear acceleration decay factor0.5

  • Simulate usingInterpreted execution

By default, the IMU Filter block outputs the orientation as a vector of quaternions. The model uses the custom MATLAB Function block hquat2eul to convert the quaternion angles to Euler angles.

Simulate Model

Set the start time to 0.005 seconds and the stop time to 8 seconds. You can compute the stop time as $\,sample\, rate \times number\, of\, samples$.

set_param(modelname,"StartTime","0.005","StopTime","8")

Run the model. The IMU Filter block combines the data from the accelerometer and gyroscope readings and computes the sensor body orientation along the x-, y-, and z-directions. The model then plots the computed results. The model uses the Scope block to plot the computed sensor body orientation over time.

sim(modelname);