In previous posts I described a setup to use a digital compass to measure orientation of a telescope. After assembly and writing code, a test indicated that using magnetometer data alone did not provide accurate results. With an added accelerometer an estimate for altitude can be made, so the magnetometer is only required to provide the azimuth.
Integrated circuits combining magnetometer, accelerometers and gyroscopes are called ‘intertial measurement units’ (IMU). Richard’s tech IMU library is a software library that can integrate the results and provide the user with useful data. From the website: “RTIMULib2 is an easy way to connect a 9-dof, 10-dof or 11-dof IMU to an embedded Linux system and obtain RTQF or Kalman-filtered quaternion or Euler angle pose data.” Dof, or ‘degrees of freedom’, means the number of independent variables the sensor measures. Kalman filters are a sort of dynamic bayesian networks used in signal processing to smooth the raw sensor data. Quaternions and Euler angles are two alternatives of a rotation matrix to represent a rotation, that you will also find in a telephone attitude API.
The website lists a number of IMU’s. Two IMU’s comparable in price are interesting:
- LSM9DS1 – this IMU is used in the Astro Pi’s SenseHat, shown on the left.
- Bosch BNO055 – this IMU has on-chip ‘fusion’: on the chip calibration and processing is performed, so the user can directly query absolute values, rather than having to process the raw sensor data himself.
Initially the Bosch looked like to way to go: just read all processed values, which makes the user code easy to write and understand. There is even one sensor readout boolean that indicates if the magnetometer is calibrated correctly. However, the Pi I2C interface is reported to have issues with clock stretching that is required by the Bosch. This is solved by accessing the values with the BNO055 in serial UART mode, or by following the instruction on lowering the i2c clock speed from the bno055-python-i2c library.
The two big benefits of the SenseHat is its bigger install base with Pi users – people have already discussed online how to calibrate the magnetometer with the RTIMULib software – and that the sensor is a HAT: Hardware Attached on Top of the Pi. There are also cases available for the SenseHat + Pi.