IMU measurements
Read all the information from the SBC Inertial Measurement Unit and play with its parameters.
You can find more information about the Imu
class here: IMU – Inertial Measurement Unit.
Print accelerometer, gyroscope and temperature readings
This example prints all reported values by the IMU (3xaccelerometers, 3xgyroscopes, 1xtemperature).
>>> import sbc
>>> imu = sbc.Imu()
>>> print('IMU accelerometers readings: ', imu.read_acc_g(), '[G]')
IMU accelerometers readings: [-9.155273e-05, 0.008605957, 0.9963684] [G]
>>> print('IMU gyroscopes readings: ', imu.read_gyro_rad_s(), '[rad/s]')
IMU gyroscopes readings: [0.003329053, 0.00692443, -0.0007989727] [rad/s]
>>> print('IMU temperature sensor reading: ', imu.read_temp_c(), '[degC]')
IMU temperature sensor reading: 35.5 [degC]
Print Euler angles estimated from the IMU sensors
This example shows how to filter accelerometer measurements with Mahony AHRS filter algorithm.
Since the IMU does not have a magnetometer, it is not possible to estimate the yaw angle.
The code runs the Mahony filter inside a 10Hz timer.
Only pitch and roll angles should be taken into account.
>>> import sbc
>>> import MahonyAhrs
>>> import machine
>>> def cb( tmr ):
>>> gyro = imu.read_gyro_rad_s()
>>> acc = imu.read_acc_g()
>>> ahrs.update( gyro, acc, 0.1 )
>>> euler_angles = ahrs.get_roll_pitch_yaw()
>>> print( 'Pitch:', euler_angles[0], '[deg] Roll: ', euler_angles[1], '[deg]')
>>> imu = sbc.Imu()
>>> imu.acc_config( imu.ACC_CONF_ODR_1600, imu.ACC_RANGE_3_G )
>>> imu.gyro_config( imu.GYRO_ODR_2000, imu.GYRO_RANGE_125_DEG_S )
>>> ahrs = MahonyAhrs.Ahrs( 1.5, 0.01 )
>>> tim = machine.Timer( -1 , period=100, callback=cb )
Pitch: 0.5193999 [deg] Roll: -0.5416162 [deg]
Pitch: 0.6081545 [deg] Roll: -0.5968983 [deg]
Pitch: 0.7136583 [deg] Roll: -0.5773315 [deg]
Pitch: 0.5892505 [deg] Roll: -0.3142482 [deg]
...
Change accelerometer frequency and range/resolution
Example to change IMU accelerometers configuration to 200Hz and 6G.
>>> import sbc
>>> imu = sbc.Imu()
>>> imu.acc_config( imu.ACC_CONF_ODR_200, imu.ACC_RANGE_6_G )