## The quadcopter : control the orientation

The quadcopter orientation can be defined by three angles : Pitch, Roll, and Yaw. These angles determine orientation and therefore the direction the quadcopter will take. Basically, changing the pitch will make the quadcopter go forward/backward, the roll  bend to the left/right, and finally the yaw will make it rotate around its vertical axis.

So, to fully control a quadcopter- i.e make it go where you want over your neighbors' garden to spy on them- you just need to control these 3 angles. Do you? Almost ;). You need a 4th parameter, the throttle of the rotors. Obviously, if the propellers are not spinning, it would be hard to take off, wouldn't it? 😉 But let's forget about that and assume that the throttle is sufficient enough so the drone takes off.

#### Control individually pitch, roll and yaw angles

In order to change the pitch and roll angles , the main idea is to change each motors speed so the quadcopter starts bending in the desired direction. Let's formalize things a bit and define the motors "power"  in  C++ language style :

int motor1Power, motor3Power; // Motors on the X axis

//=>; changing their speeds will affect the pitch

int motor2Power, motor4Power; // Motors on the Y axis

//=>; changing their speeds will affect the roll


As I said in the previous paragraph, the motors power depends on the throttle you can give them with a remote control for example. A simple implementation of this in an Arduino-like sketch would look like this


void loop(){

motor1Power = throttle;

motor2Power = throttle;

motor3Power = throttle;

motor4Power = throttle;

}



What is done here is just simply redirecting the throttle input (coming from a remote control for example) to the motors, which will just concretely change the altitude/ vertical speed of the drone.

Now, let's think a bit about how to make the quadcopter move forward/ backward . This motion corresponds to the pitch angle. In order to change the pitch, we only need to change the speed of the two motors on the X axis. Indeed, affecting the speed of the motors 2 and 4 (on the Y axis) would just start a rotation around the X axis (therefore changing the roll angle).

###### But how to change the first and third motors speeds?

If you try adding the same value offset to the motors speed, it won't change anything because what you need in order to incline the aircraft is a difference in the motors speed. Imagine you attach two weights of one kg to the two opposite motors, what happens to the pitch? Nothing, you would need to attach different weights to incline the quadcopter. It's very intuitive and works exactly the same with the motors speed!

The best way to get different motors speed is to add the offset value one one motor output, and to subtract it on the other, so the "overall throttle " of the axis remains constant and therefore changing the pitch won't affect the quadcopter's "vertical speed"

By the same method, you can change the roll angle by changing motors speeds on the Y axis (in our example motors 2 and 4) and therefore control separately pitch and roll! Let's look at the updated pseudo-code introducing two new inputs pitchOffset and rollOffset

void loop(){

motor1Power = throttle + pitchOffset;

motor2Power = throttle + rollOffset;

motor3Power = throttle - pitchOffset;

motor4Power = throttle - rollOffset;

}


And that's it! This is how the pitch and roll are controlled.

###### The yaw..

Why is the yaw different? Because it's not a matter of inclination here, so changing two motors speed won't make it. But i forgot  to tell you a little detail about quadcopters 😉 Two propellers turn clockwise, the two others turn counterclockwise. And how does it help you? The answer is the torque . When an object rotates around an axis, it creates a torque, creating a rotation movement of the body holding the propeller, and that's why traditional helicopters have an anti torque rotor on their tail, keeping the rotorcraft from spinning until the pilot pukes (it is more likely he dies in an awful crash). With opposite rotations, the quadcopter doesn't need and anti-torque tail, one axis compensating the torque of the other.

Changing the torque and therefore the yaw seems now really simple,doesn't it? Just add a yawOffset to the speed of two motors on the same axis, while you subtract it from the speeds on the other axis (so the overall throttle doesn't change). The final code looks like this:

void loop(){

motor1Power = throttle + pitchOffset + yawOffset;

motor2Power = throttle + rollOffset  - yawOffsett;

motor3Power = throttle - pitchOffset + yawOffsett;

motor4Power = throttle - rollOffset  - yawOffsett;

}


(Y)awesome.

And that's it for the basics of how to control a quadcopter, hope it was clear and detailed enough. If you want to take it to the next level and auto stabilize your quadcopter, it's right here 😉