In my previous article about my quadcopter, I described my project. Now comes the first step where I explain how to get pitch, roll and yaw information from my inertial measurement unit (IMU) on my Arduino card.
For this part, I will be only using the accelerometer and the gyroscope of my MPU9150 (so it’s the same as a MPU6150).
We’ll first see why we need both an accelerometer and a gyroscope, then I’ll give you my wiring and finally I will talk quickly about the fusion algorithm.
Why do we need accelerometer AND gyroscope ?
The accelerometer will measure acceleration, and in our case we are interested by the acceleration caused by gravity. If your sensor is not moving it will give you a 3D vector pointing at the ground, so you can know your attitude (pitch, roll
and yaw). This is what is used in your smartphone to rotate what is displayed on your screen when you tilt your phone. Unfortunately, an accelerometer is sensible to movement and vibrations which are two elements that you have on a drone so you can’t use it alone.
The gyroscope will measure an angle velocity on every axis. By integrating this, you will be able to know what is your attitude. But you have to know your first attitude at the beginning (either by starting on a known position or by using data from another system like an accelerometer of course !). The gyroscope comes with another problem : it tends to drift over time so you need to reset its position from time to time.
This graph shows you calculated roll of my sensor if you only use an accelerometer or a gyroscope :
You can see that the accelerometer is noisy and that the gyroscope drift at the end. The point of a fusion algorithm is to get the advantages of both sensors in order to have a nice attitude indication.
Wiring to the Arduino
Like most of the IMU sensors, the MPU9150 is using the I2C port. This bus is quite useful because it is using only two wires for data (actually one for data and one for the clock). You can also connect multiple devices on the same port. For instance you can easily add a barometer to get your altitude.
The wiring is quite simple, VCC to 3.3V (check your sensor datasheet, it can change from one manufacturer to an other). GND to GND of course and SDL (data line) to A4 and SCL (clock line) to A5.
If like me you never remember if A5 is for data or clock, here is a stupid mnemonic trick : the clock is always ticking fast, so it the greater number is for the clock (SCL -> A5).
You need one more thing to make it work correctly. As you can see there are some resistors on the schematic above : those are called pull-up resistors. Thanks to them the signal is less sensible to interferences.
The sensor can work without pull-up resistors, but as soon you as you have interferences your code can freeze. This is what happened with my drone. When the motors were disconnected everything was going fine. But when I did the same tests with the motor turning my program was freezing … which can be inconvenient when you’re building a flying drone ! This is because the brushless motors are creating electrical interferences on the I²C wires.
Fusion and code
Once your sensor is connected, you can start reading the values from it in order to have the information you are looking for. You have two solutions :
- Implement your own fusion algorithm. It is hard to do it but it can be done using kalman filter. Unfortunately this is beyond my capacity (for now … 🙂 ). You can also implement easier but still good methods like the complementary filter. This is well explained on this website.
- The other solution is easier, this is why I prefer it ! Nowadays some chips have bult-in calculation system which will take care of the fusion. Not only does this spares you some work but also the calculation is done by the IMU itself and not your Arduino. Here is the code that I use to get this information : link.
So far we’ve seen how to get the attitude of you drone which is a very important information to have when controlling a quadcopter. But there is still a lot of work to do.
I have already implement other features on my drone, it is almost ready to make its first flight ! In the future I will explain other important part of the development of my flight controller (reading RX signal, PID controll of the motors etc …)