Goal of this repo is to implement IMU sensor fusion on real hardware (Arduino Uno R3) using the complementary filter. The complementary filter combines data from both gyroscope and accelerometer applying first order HPF and LPF, respectively. The final result is an angular orientation output, which we can use to rotate a 3d-box made in JavaScript using the p5.js library.
- Arduino Uno
- 6-DOF MPU-6050 with GY-521
Complementary filtering consists of using low-pass and high-pass filters in a complementary way. The basic idea is to combine the long-term stability of the accelerometer and the short-term stability of the gyroscope: the accelerometer signal is filtered with a low-pass filter to limit the effects of vibrations; conversely, the gyroscope signal is filtered with a high-pass filter to reduce the effect of long-term drift.
I made a small pdf file including a little bit of theory behind this project.
- [ENG] Orientation Estimation using IMU
- [ITA] Stima dell'orientamento utilizzando sistemi ineriziali IMU
- Download a p5.serialapp release on this website
- Connect Arduino with the MPU-6050 as you can see below
- Connect Arduino to your PC via USB
- Upload the Arduino code with ArduinoIDE or gcc-avrdude.exe. Once it is uploaded, close Arduino IDE and make sure the COM connection is closed
- Open p5.serialapp and click the "open" button to establish a connection
- Run the JavaScript code on a local server. If this doesn't work, make sure that in main.js file your 'COM' port is selected.
Here you can see the final result obtained. The box is really stable and responsive. Since magnetometer is missing from the MPU-6050 module, it's impossible to retrieve a correct Z-axis rotation due to the lack of a fixed frame. However, the yaw angle is calculated by the gyroscope anyway in order to show this issue. In the JavaScript file it is possible to enable rotation on the Z axis by uncommenting the corresponding line
Inside the Arduino directory you can find the code used to implement complementary filter directly on hardware. Roll, pitch and yaw are calculated and sent via serial communication to PC. JavaScript code is used to generate and rotate the 3d-box, according to the stream of data incoming. In order to build this project you need the p5.serialApp made by Shawn Van Every to connect Arduino and the p5.js sketch.