RASPBERRY PI AND SENSE HAT(II): IMU (INERTIAL MANAGEMENT UNIT) WITH ACCELEROMETER, GYROSCOPE AND MAGNETOMETER
- Layout for this exercise:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXBeegAbEG2iI3-zlMp9MdyYhc_ZKX3UyjgYjUSjGdRgQgcGhnFLWDd013UtQa4zls6NoAMJDcsHT7AmmDuxt-U_QgS63-LKEwfW54eIqyrAKRcSdBMu4m04O3o5eU9lOPtqpzCdEsXGek/s1600/layout_sense_hat.jpg)
1 - Introduction
- The Sense HAT has a movement sensor called an IMU (Inertial Management Unit) which can measure the type of movement it is experiencing:
https://www.raspberrypi.org/learning/astro-pi-guide/sensors/movement.md
- An IMU is an electronic device that measures and reports a body's specific force, angular rate, and sometimes the magnetic field surrounding the body, using a combination of accelerometers and gyroscopes, sometimes also magnetometers.
- An IMU is built basically with 3 sensors in one. The readings for these 3 sensors are combined to have an accurate orientation of the Sense HAT module:
a) gyroscope: measures momentum and rotation.
b) accelerometer: measures acceleration forces and can be used to find the direction of gravity.
c) magnetometer: measures the Earth's own magnetic field, so it's a bit like a compass.
- An IMU sensor is used on all manned and unmanned spacecraft to track movements and maintain an understanding of orientation.
- All objects in space have three axes around which they can rotate. The three axes are:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEina0TUrWVNDhNODh6itE6qyimu92JA4bDP4EVZXVNJLbbjbokq2_kr-VOrq6Jc3j5SOaGACNMlfJ-xDFxevRVZj_Txz2MpnpprS0iioL8h5GvuitiLUK9G6doo1xuRhUQ-nrylKOUeok_l/s640/screenshot.1.jpg)
- Pitch, roll, yaw are terms used in aviation and airplane control to describe the movement around the 3 axes:
a) pitch: like a plane taking off
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxt6ZYwRi8x0fw7BnQPrYczJuvhPLxzMIlGb200elaaZhqxxhSU3vniudBPtJ0XOfJmGpt1qwqA2CS78nWREPGNM84s3qDsTlAR45uG3oeQmz3WGcOSYUox0Obgk3uzrZrssV8aQZnqEoJ/s640/screenshot.2.jpg)
b) roll: the plane doing a victory roll
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIkY2yAIjCjB6N4k72xGmzTuKCBubQ-ncCcEiFESpw-fJqAhq9wglyMMQm3ihOEq7IQiIqVxJfPJ3mhi9OHum6dWTUnm3fYPD411YGhCUAdAGKaELsf3-eGez0ORsxsgdKjHHjeNSfGItK/s640/screenshot.5.jpg)
c) yaw: steering the plane like a car
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6D0cC-PNJPgrs_BEmVrCPVoZyC9MtATHkvbvXOi-E1V9CuA83aUQmFllFzUt-MaacvaIaL2IA7t51AbX6rH2Zgyd9eQpN6rMpBTC5P4e0J2VwDs99mimZ2L5xPBPVWtp0e8hODeas1xi2/s640/screenshot.4.jpg)
- This image shows where these axes are in relation to the Sense HAT:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqqV9fFlkTu3nLVM7jOvH7NckBkrINevNik0nlNN9nE4Q_lVqfi3hMMc_RhoibQpLHYCHdj3nC0XT0czm36tyFWVUAPurg46az33PYYn5boNmbsMv6gaj7J3EW25SRE1DWoGMiCCOG4aG9/s640/screenshot.6.jpg)
2 - Programming Raspberry Pi and Sense Hat to determine its orientation
- The next program offers accurate information about the orientation of the Raspberry Pi and Sense Hat module.
- The function set_imu_config(True,True,True) sets the magnetometer, gyroscope and accelerometer (in that order) to make measurements about the position (determined by the pitch, roll and yaw output parameters):
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyzJ2vFs1yc0gdmqrow_r6ueDtxdqjEPW84LEBW1YJEU4WFEhRSNR082UhN_6HT9j3W34J9m0L-vG4NHM8PXxBjkOXi3x4TFbQkajKzG_x_U1qGAWb0OvehSEtk16l9FaWrzKoSJke12hW/s1600/screenshot.7.jpg)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMznE5jK1tHhm1bX_9zxI-rpuvzWBz0KNBfF03Dw-Oa3ef16rVPBZeYuZl98sl8V2Ek9xOFZELM6xqCJTXE7FS5oa4MzYhyQRSBYRofEs8s4fHC0oziuAWSms9lgVp9-zuqDUCS7rQluCS/s1600/screenshot.16.jpg)
- Giving permissions of execution:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBoJjyi-h8CdlA5eVogfuylXorqbPw7sYGSWsbUNMzNmyUxFE1bXdjrjOMGMOID0FFcuJ1uKbzHPwZOM2j6_qu0n_hX6R6QI_V_CacuZBx0PiXsil88XvJRsGw1s4OQnidN2A9Alv8Eymr/s1600/screenshot.9.jpg)
- The initial position of the Raspberry Pi and Sense Hat is flat on a table:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjb7BAg1-wQfXHuqGnhcqYObwZ12A3hJZTcW_-uUy4PrEEtx0AHhRqxd8W8XPeWon7HYXCXPMEp5o0QU0nL-smVHR89D6ur8u_RDE-rxiNddvGVk5d7onVUMD6i2ZaPgaWdoNlxObnCIczq/s400/screenshot.21.jpg)
- Running the program:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9PeqkLsFPZPR7DDjYfieHHDeDnvEw_lUiR8LkHnp_2RgIjPKgVT1JqFMumhB0mXVZWQjEwxXfIitxxk8RzM2pBG7VPTUOSNH5e-HCdFfitB3Su5GK7bCdGr3hsJWQR0LDQWkfCSPjOt4L/s1600/screenshot.23.jpg)
- Tilting up the Raspberry Pi and the Sense Hat modules:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1XKDANPM9VUBPZTrVSEjYMxGJJVbe-eBYFduIXgLtSqOtwCKfjgrAcgF5JviGeaam-I4iuprTH3MCCGwFgSa7D7prAcgGnk40TdjeEUQaexZG828gQ9f5Nt-N058HgRfDZd_Q4px2oB2S/s400/screenshot.22.jpg)
- After tilting up the module a variation for the pitch is detected while roll and yaw remain almost equal:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5feUXzc2VBcr8Nm66UMf_TBAwEdovp5kgW2f5edA9ewqzGcI_TvB8R2pibeGLIwMqqjzYkSlegkjN2c7mzJ2ODka7V3LpT-tsgPEJjUUHuCgdf9nzQFh1pSwhmcN_K9r7bhOTBI6lIhhB/s1600/screenshot.24.jpg)