Locking Your Heading: Using IMU for Heading Estimation
RAiV includes a 6 degree of freedom (DoF) MEMS motion tracking sensor which combines a 3-axis MEMS gyroscope and a 3-axis MEMS accelerometer. In this post we briefly introduce MEMS gyroscopes and accelerometers. Then, we show how you can detect heading with example code.
Warning: For dead reckoning please use external magnetometer and/or odometer with this 6DoF IMU (Inertial Measurement Unit).
What is a 6DoF IMU
The combination of 3-axis gyroscope and a 3-axis accelerometer is technically known as 6DoF IMU. This sensor is good at detecting short duration motions.
Gyroscope and its uses
Gyroscope measures angular velocity (rate of rotation). The output of the sensor shows the velocity and direction of the rotation around the X, Y, and Z axes (Roll, Pitch, and Yaw, respectively).
This sensor can be used for:
- Digital Image Stabilization - image frames are digitally shifted, rotated, or warped to compensate for the detected motion.
- Robotics Navigation - robots can turn in exact angles or keep their heading steady.
Accelerometer and its uses
Accelerometer measures acceleration along X, Y, Z axes.
This sensor can be used for:
- Tilt sensing - robots can check their alignment.
- Activity recognition - robots can check their motion and stationary states.
Gyro Utilization
One of the main uses of gyroscope is heading detection. Here we will show a simple algorithm for heading estimation.
Prepare & Upload the Code
In the example below we will:
- Calibrate both gyroscope and accelerometer for bias.
- Create a heading estimation thread
- Get the heading estimate periodically
You can find this example in our Github Repository with all the necessary modules. Please download the example code from the github repository and upload it to RAiV via the web interface.
# For accessing data pipeline
from qCU_Data import qCUData
# For gyro operations
from headingEstim import HeadingEstimator
from headingEstim import estimate_IMU_bias
import threading
def main():
# Create interface
theQCUData = qCUData()
# Initialize shared memory
if not theQCUData.init():
print("Failed to initialize shared memory")
return
event = threading.Event()
# Get gyro and accelerator biases
maxCount = 200
accel_bias, gyro_bias = estimate_IMU_bias(theQCUData, maxCount)
# Start heading estimator thread
headingEstimator = HeadingEstimator(theQCUData=theQCUData, gyro_bias=gyro_bias)
headingEstimator.start()
# Enter object detection loop
try:
# Main loop to get data
loop_count = 0
max_loops = 500 # Limit for demonstration
while loop_count < max_loops:
curHeading = headingEstimator.get_heading()
print(f"Heading: {curHeading:.3f}");
# Sleep 1 seconds
event.wait(1.0)
finally:
print("Cleanup completed")
# Stop heading estimator
headingEstimator.stop()
if __name__ == "__main__":
main()
Live Action: It’s Our Turn
Rotate the RAiV (or the robotic platform RAiV mounted on) around itself. Check the PC script output for heading direction in degrees.
What is Next?
Check our Python SDK:
RAiV Python SDKCheck our Github Repository For Sample Codes:
Our Github Repository