Building my own drone aircraft

How to build an Arduino and Raspberry Pi based Quadcopter


Table of contents

Introduction
How a quad works
Components
The theory behind a quadcopter

To be continued...

Introduction

Reading some articles and watching youtube videos I got excited about the idea of building my own quadcopter. Not only this lets you learn a lot about embedded development but also gives an opportunity to build your own flying robot :). What I want to do is build it completely from scratch, not using any existing flight controllers. This way you can add whatever sensors you want and your possibilities are limited only with your quad's battery life :)

How this works

A quad is a 4-motor machine, and its main task is to make sure it stays stable in the air, which is done by constantly taking measurements from sensors (accelerometer, gyro, pressure altitude sensor, possibly some others like ultrasonic distance sensors) and quickly correcting the rotations of its motors to compensate for any rising instability. As simple as it looks from the outside, in reality a drone does hundreds of corrections per second.

As the drone stays in the air, it constantly takes measurements of its current position changes using mainly an accelerometer and a gyro sensors.

Based on these measurements, drone position corrections are calculated by a unit called Flight Controller. Then the Flight Controller signals each of the four motors about the speed they should rotate in through Electronic Speed Controllers (ESCs). The ESCs just transform the controller pulse-width-modulated signals into high-current pulses that the motor's coils "understand".

Additionally the Flight Controller can read from other sensors, like ultrasonic sensors, to instantly react when the quad approaches a wall or any other obstacle, and prevent collisions and damaging props or those objects.

As an input the Flight Controller accepts high-level control commands which tell it where to fly, what altitude to maintain, and so on.

This is all the Flight Controller does. It is like a peripheral nervous system of a human, where all actions are based on reflexes, without giving much thought to the actions it does.

The Flight Controller must be very fast-reacting, which is why it is usually implemented using a real-time microcontroller, instead of a full multi-process computer. This is because the regular (non-realtime) Linux-based computer can get busy doing some system stuff like paging when it has to do some critical motor-speed adjustment instead, which will result in a crash. I am planning to use Arduino Pro Mini as a Flight Controller, because it is small, light-weight, doesn't take much power and is easy to program. Having all these advantages, Arduino has only 30KB of Flash Memory and 8/16 MHz clock. In case Arduino turns out to be not enough, I'll later port the code to an STM32 ARM-based MCU.

The actual "brains" of the quad will be a Minicomputer Raspberry PI Zero. Among other single board computers, I found this one to be the cheapest, most light weight and the most low power one (65 milliamps when idle, 80-140 ma when running GUI).

Unlike a Flight Controller, which acts as a peripheral nervous system, the Minicomputer will act as a central nervous system, allowing the drone to think before doing :). The Minicomputer will have high level sensors attached to it, like GPS, camera and possibly control receiver, and will give high level commands to the Flight Controller.

When building a drone the primary concern is the weight. The essential stuff, like motors, ESCs, the battery already take about 0.5 kgs, so it's important to reduce the remaining weight load as much as possible. Every extra gram requires the motors to rotate faster to stay on the same height, which results in a higher power consumption, faster draining of the battery and eventually a lower flight time.

Components to build a quad

Here are the components I ordered for the quadcopter:


And some nice-to-have's:

Now I have to wait about a month for all these orders to be delivered, and we can start...

The theory behind a quadcopter

Controlling the quad movements

A quad has four vertically mounted motors with stiffly attached props to it, and normally the only control we have is separately adjusting the rotation speed of each of these motors.

To compensate the angular momentum of each other, two motors rotate clockwise, the other two rotate counter-clockwise, but these motors use different props, so the lifting force is always pointing upwards

Even though the quad is symmetric, one of its sides is considered to be "front". All rotations of the quad in space are relative to this front.

As any aircraft, the quad can rotate along one of 3 axis, and these rotations are called Yaw, Rol and Pitch.

Yaw is the rotation of the quad parallel to the ground. If you look at the quad from the top and spin it in front of you, this will be yawwing

Roll is a left-right tilt, this is what an airplane does when it wants to turn left or right. If you stay behind the quad and tilt it to the left or right, this will be rolling. The yaw change makes the quad fly towards one of its sides (left or right).

And the Pitch is pointing the "nose" of the quad lower or higher, this is what an airplane does when gaining or losing altitude. It is just like rolling, but around the other axis, the one that goes "through" the left and right sides of the quad. The pitch change actually makes the quad fly either forward or backwards, just like helicopters

The only remaining control we have is the thrust of the motors. If we add more thrust to all 4 engines simultaneously, we will perform an uplift so the quad will keep going upwards without changing it's orientation. If we reduce the thrust, the quad will do a downfall and will eventually land (or crash, depending on how fast it falls).

All these operations can be done by just changing the rotation speed of one or more motors, which is what the flight controller is about.

So how do we actually control these 4 operations (yaw, roll, pitch, uplift/downfall) on a drone? Suppose we have variables motor1, motor2, motor3, motor4 that control the motors thrust in percents (0%-100%). To peform the uplift/downfall, we just need to increment/decrement all 4 variables altogether.

To control the other parameters, we will rotate the motors at different speeds. Intuitively, to bank left, we can just slightly decrease the power of two motors on the left (1 and 3), and increase the power of motors on the right (motors 2 and 4). This will be rolling.

The same goes for the pitching: we can increase the power of the motors on the back (motors 3, 4) and decrease the power of the motors on the front (1, 2). Or vise versa, depending on the direction we want to go in.

As for the yawwing, we use the fact that the motors create an angular momentum on the body of the quad. Precisely for this reason two of the motors rotate clockwise, and the other two - counterclockwise, to compensate each other's effect. So if we reduce the power of those motors that rotate clockwise, and increase the power of the motors that rotate counterclockwise, the whole quad will start spinning clockwise, producing the yaw. And vise versa.

The picture shows how I numbered the motors of the quad, as well as the direction in which the motors spin.

The table below shows how we should change the motors rotation speed to control the quad. + means increase the power, - means decrease it. In order to do the opposite maneuvre (say, roll to the left instead of rolling to the right) we just need to invert all the signs in the appropriate column (in our case, Roll).

MotorYaw clockwiseRoll to the rightPitch downThrust up
1 + + - +
2 - - - +
3 - + + +
4 + - + +

Stabilizing the quad

A flying quad is an unstable system, which means that even a slight blow of wind can make it go haywire, flip upside down and crash. Therefore some kind of self-stabilization algorithm is needed to make sure the quad can safely handle air flow fluctuations and other disturbances and return to its "normal" position - the position that the pilot of the Minicomputer wants it to be in

In most cases this is achieved by using a gyroscope and an accelerometer sensors and a proportional-integral-derivative (PID) algorithm. The sensors notify the Flight Controller that the position of the quad has changed, and the PID algorithm makes the engines rotate in such a way that the quad automatically compensates for this unwanted change and evens itself out.



To be continued...



References

  • http://blog.tekkiewalk.com/
  • http://rotorbuilds.com/build/1260