How to find the orientation of the microcontroller board having an on-board IMU in real-time ?

1 次查看(过去 30 天)
Greetings of the day!
I am trying to use the example scripts provided in the documentation of Sensor Fusion and Tracking Toolbox. However, in those scripts, the IMU data which is already recorded. However, I want to find the orientation using the data that is being transmitted in real-time throught serial port. Please note that I am reading the serial data in the MATLAB script.
I request suggestions and tips on how to process this data in real-time to determine the orientation of the device and which would be the suitable choice of filters like kalman, imufilter etc.

回答(1 个)

Aravind
Aravind 2025-4-8
To determine the orientation of a microcontroller board with an on-board IMU in real-time using MATLAB, you can follow these steps:
Reading IMU Data from the Serial Port: First, you need to read the IMU data in real-time from the serial port. Create a “serialport” object and use the “read” function to read data in a while loop. You can find more information about the “serialport” object and the “read” function at these documentation pages: https://www.mathworks.com/help/instrument/serialport.html and https://www.mathworks.com/help/instrument/serialport.read.html.
After reading a line of data from the serial port, parse it to obtain the IMU data, specifically the accelerometer and gyroscope readings for all three axes.
Processing the Read IMU Data to Determine Orientation: Once you have the accelerometer and gyroscope readings, fuse them to obtain the device's orientation. This is done using a sensor fusion filter. Various options are available, and you can find more information about which filter to use in different scenarios and their assumptions or limitations here: https://www.mathworks.com/help/fusion/ug/introduction-on-choosing-inertial-sensor-fusion-filters.html.
For determining orientation using gyroscope and accelerometer readings (IMU readings), the best filter to use is the “imufilter” in the Sensor Fusion and Tracking Toolbox. More information about this filter can be found here: https://www.mathworks.com/help/fusion/ref/imufilter-system-object.html. Using the “imufilter” object, you can continuously fuse the IMU data from the serial port to obtain the microcontroller’s orientation. The “imufilter” object provides the orientation as a quaternion, which can be converted into degrees using the “eulerd” function. More information about this function is available here: https://www.mathworks.com/help/nav/ref/quaternion.eulerd.html.
Here is a basic code skeleton for implementing the above solution:
% Initialize the IMU filter
imuFilt = imufilter('SampleRate', 100); % Set your actual sampling rate
% Initialize serial communication
s = serialport("COM3", 115200); % Adjust port and baud rate
while true
% Read IMU data from serial port
% Parse IMU data to obtain accelerometer and gyroscope values
% Assuming that the variable accel contains accelerometer readings
% Assuming that the variable gyro contains gyroscope readings
% Update orientation using IMU filter
quat = imuFilt(accel, gyro);
% Convert quaternion to Euler Angles
eulAngles = eulerd(quat, 'ZYX', 'frame');
% Display the values
disp(eulAngles);
end
To get started with sensor fusion, you can refer to the following documentation for examples on how to use different filters for sensor fusion: https://www.mathworks.com/help/fusion/gs/determine-orientation-through-sensor-fusion.html.
I hope this helps!

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by