How to design your own quadcopter. How to make a quadcopter based on arduino We write a program for a quadcopter on arduino

Hello, our dear readers. In this article we will talk about how to assemble a quadcopter using Arduino. This is not the easiest, although very exciting task, the result of which will be the appearance of a small drone, designed, assembled, and configured with my own hands. Let’s immediately make a reservation that we are talking about the cheapest drone possible from the most affordable components.

Before you start assembling the quadcopter with your own hands, you need to acquire all the necessary parts. The brain of our homemade product will be the flight controller Arduino Uno. Its capabilities are more than enough to control a drone.

In addition to the microcontroller, we will need:

  • Battery (preferably several) 3.7V
  • MPU-6050 board (gyroscope and accelerometer installed on it)
  • Transistor ULN2003A
  • Commutator motors with hollow rotor 0820
  • Wires

A few points need to be made. Since we collect, our choice fell on commutator motors with a hollow rotor (so-called coreless motors). They are not nearly as reliable as brushless motors, but they are much cheaper. In addition, you can do without additional speed controllers.

But it is impossible to do without a gyroscope and accelerometer. The gyroscope is necessary to ensure that the quadcopter can maintain a given direction of movement, while the accelerometer is used to measure acceleration. Without these devices, controlling the copter would be much more difficult (if not impossible), since they provide data for the signal that regulates the speed of rotation of the propellers.

We did not include the frame in the list of required parts. You can purchase it, or you can 3D print the frame, beams and mounts for the engines. The second option seems preferable to us, especially since you can easily find quadcopter projects on the Internet.

The frame printed on the printer will be not only light, but also durable. But if you don’t have access to a 3D printer, you can order a frame.

Step-by-step assembly instructions

How to print a frame and fasteners

3D printers can be found in many universities, laboratories, and coworking spaces. Often access to them is free. You can create models for printing yourself using, for example, Solidworks. Or you can use ready-made solutions, changing the parameters if necessary.

How to set up the gyroscope accelerometer

To configure the accelerometer-gyroscope (I2C), we recommend using the following library. Under no circumstances connect the board to 5V voltage, otherwise you will instantly damage it.


Let's briefly tell you why an I2C board with sensors is interesting. It is noticeably different from a conventional accelerometer board with three analog outputs for the X, Y, Z axes. I2C is an interface bus that allows the transfer of significant amounts of data through logical digital pulses.

There are not many analog outputs on the board, and this is a big plus of I2C, because otherwise we would have to use all the ports on the Arduino to receive data from the gyroscope and accelerometer.

Connection diagram to Arduino

Before the I2C board can communicate with the Arduino, it needs to be connected to the controller.

We are interested in the I2C scanner code sketch, or rather, its code.

Copy the program code, paste it into an empty sketch, and then run it. Make sure that the connection is established to 9600 (to do this, run Arduino IDE via Tools-Serial Monitor). An I2C device with address 0x68 or 0x69 should appear. Write down or remember the address. If the address is not assigned, most likely the problem is in the connection to the Arduino electronics.

Next, we need a sketch that can process gyroscope and accelerometer data. There are many options on the Internet, and finding the right one is not a problem. Most likely, it will be in archived form. Unzip the downloaded archive, open the Arduino IDE and add the library (sketch-import library-add library). We will need the MPU6050 and I2Cdev folders.

Open MPU6050_DMP6 and carefully review the code. You won’t have to perform any complicated actions, but if the address 0x60 was assigned, then you need to uncomment the line at the top (it can be found under #includes) and write the correct address. Initially tav is specified as 0×68.

We download the program, open the monitor window through 115200 and simply follow the instructions. In a few moments you will receive data from the gyroscope/accelerometer. Then the sensors should be calibrated.

Place the board on a flat surface and run the sketch MPU6050_calibration.ino (easy to find on the Internet). Review the code, the default address is 0x68. After starting the program, you will have information on deviations (offset). Write it down, we will need it in the MPU6050_DMP6 sketch.

That's it, you got a functioning gyroscope and accelerometer.

Program for Arduino

The program will help you control the drone. The algorithm used for stabilization is based on two PID controllers. One is for roll, the other is for pitch.

The difference in the rotation speeds of pairs of propellers 1 and 2 is equal to the difference in the speeds of pairs of propellers 3 and 4. The same is true for pairs 1, 3 and 2, 4. The PID controller changes the difference in speed, after which roll and pitch become zero .

Pay attention to the Arduino digital pins for the motors and don't forget to change the sketch.

Connection to the controller

In order to control the copter, we need to gain control of the motors by connecting them to the Arduino. The controller outputs only a small voltage and current, so connecting motors directly makes no sense. Instead, you can install several transistors to increase the voltage.


To create a diagram we need:

  • Arduino
  • Engines
  • Transistors

All this is assembled on a circuit board and connected by connectors.

The first step is to connect 4 PWM outputs (marked ~) to the transistor. Then connect the connectors to the motors connected to the power supply. In our case, we use a 5V battery, but a 3-5V battery will also work.

The transistors must be grounded and the ground on the Arduino board must be connected to the battery ground. The motors must rotate in the correct direction, that is, they must work to lift the copter, and not to roll it.

By switching the motor contact from 5V to the transistor, you will see that the rotor will change direction of rotation. Once you have made the adjustment, you will no longer have to return to changing the direction of rotation of the rotor. Now we are interested in speed.

After running and testing the accelerometer, we install our circuit on the ProtoBoard. In its absence, you can use a regular circuit board, having previously soldered the rails for the controller on it.

Before soldering the accelerometer to the board, it is necessary to calibrate it on a horizontal surface. This will help achieve more precise work sensor in the future.

How else can you upgrade your quad?

The bottleneck of the copter is its collector engines. If you search, you can find slightly larger and more powerful motors than those proposed in our article, but there will not be a significant gain in performance.


However, our goal was to assemble an inexpensive quadcopter with our own hands, and that is why cheap motors were used. Brushless motors are noticeably more expensive, but they will give you noticeably more power and reliability. They will also have to be purchased with speed controllers, but this is a truly effective upgrade.

The choice of the Arduino Uno board is due to the fact that you can quite easily remove the chip from it and put it on the ProtoBoard. This allows you to reduce the weight of the drone by 30 grams, but you will have to include additional capacitors in the circuit. Suitable Arduino board Pro Mini.

Concerning Arduino programs, then it can be relatively easily changed and supplemented with new functions. The main thing is that with its help the drone is capable of automatic mode stabilize your position.

The main idea of ​​the project is to create an inexpensive quadcopter that has autonomous power supply and a flight stabilization algorithm using Arduino.

In addition to Arduino, we will need a gyroscope/accelerometer and brush motors. The above project does not consider methods for controlling the flight of a quadcopter, but they can definitely be added. The cost of this quadcopter is approximately $60.

What is a quadcopter?

I am sure that most of those reading this article already know what a quadcopter is. If not, here is a brief description of these wonderful devices.

A quadcopter is a flying device with four “legs”, each of which has a motor with a propeller. Quadcopters are essentially similar to helicopters, but their movement, rotation, and tilt are ensured by the synchronous operation of four propellers. In addition, quadcopters have such a concept as “pitch” - rotation around the longitudinal axis. In order to stabilize the quadcopter's flight, two propellers rotate in one direction (clockwise), and two propellers rotate in the opposite direction (counterclockwise). Thanks to this ability to hover in one position in the air, quadcopters are primarily used for aerial photography and video filming. Of course, quadcopters and other similar devices with multiple engines are used in rescue operations, police, military, etc. IN Lately the cost of components for the production of quadcopters has decreased significantly and many companies have begun to produce them. Buying a finished product today will not be a problem.

So, you have already figured out a little what a quadcopter is, let's now move on to brief description the process of its manufacture.

The first thing we did was Google shopping in search of the components that we would need to make it.

In most cases, microcontrollers and brushless (valve) motors are used. It was decided to use Arduino as the controller, since it is an ideal platform in terms of price. The first problem that arose was brushless motors. Remember, we are focusing on a budget of $60. And the cost of one brushless motor, which can be used in our quadcopter design, ranges from $20 to $60! In addition, the use of these motors requires the installation of additional controllers - speedcontrollers. So it was decided to use brushed motors. The dimensions of our quadcopter are small, so we purchased motors with relatively low torque. Google suggested that quadcopters with similar drives exist. The found motors can lift up to 55 grams of weight, which suits us quite well. The next step is to solve the problems of stabilizing motors using gyroscopes and accelerometers. A gyroscope is a device that uses the Earth's gravity to determine the angle of inclination (orientation) in space. The classic design of a gyroscope consists of a freely rotating disk called a rotor. The rotor is mounted on an axle that is located in the center of a larger, more stable wheel. When the axis rotates, the rotor remains in a static state, which corresponds to the center of gravity. An accelerometer is a compact device that is used to measure acceleration. When an object leaves a state of rest (starts to move), the accelerometer records the vibrations that arise during this movement. Accelerometers use microscopic crystals that generate voltage when impacted. This voltage is removed and the acceleration value is formed. These two sensors are required in a quadcopter. It is on the basis of their readings that a control signal is generated, which regulates the rotation speed of the motors to ensure roll, movement or stabilization of our quadcopter.

Necessary components, parts and equipment

For the Arduino-controlled quadcopter project we will need:

  • - wires;
  • - lithium batteries at 3.7 V;
  • - transistor: ULN2003A Darlington Transistor (you can take a transistor that supports larger loads);
  • - motors: 0820 Coreless Motors;
  • - microcontroller: Arduino Uno;
  • - accelerometer/gyroscope: MPU-6050 board (cheap and cheerful all-in-one option);
  • - 3D printer or access to it for printing parts of the quadcopter design;
  • - tools (including a soldering iron and the ability to use it!).
  • BUY 3.7 V lithium batteries (Be careful, among the products offered you need to find exactly the one that suits you!);

3-D printing of quadcopter parts

One of the first steps is to create the frame of our quadcopter. It was decided to take the path of least resistance and print the frame on a 3D printer. In addition to ease of manufacture, the frame printed on a 3D printer is quite lightweight thanks to honeycomb printing. The parts were designed in Solidworks. Below are all the solid models. You can easily download them and send them to print. Details are saved in .stl format. If you want, you can safely modify and change them using the same Solidworks. The models are parametric, so if you decide to use other motors, you just need to change a few parameters in the model and you will get a ready-made frame for your quadcopter dimensions.


As a result you will get something like this:



Setting up the accelerometer-gyroscope (I2C)


This example used the MPU6050 board from SparkFun. It costs about $10 on Amazon and works well. Similar China on Aliexpress or Ebay offers similar boards for up to $5. Works great too.

What is I2C?

On simple boards accelerometer, everything is logical and clear: it has separate analog outputs for the X, Y and Z axes. Each output corresponds to a separate accelerometer axis. If you now look at the I2C board, you will understand that everything is somewhat more confusing. I2C is a communication standard in which large amounts of information are transferred using digital logic pulses instead of analog outputs. MPU6050 gives you 6 controlled axes (3 for gyroscope and 3 for accelerometer). If they were all analog, we would have to use all the analog ports on the Arduino Uno. With the I2C protocol we use far fewer pins to connect.

Arduino connection diagram

The connection diagram for the MPU6050 board is shown below. Please note that the Arduino library assumes the use of these pins. As a rule, even if you have a board from a different manufacturer, the pins are labeled the same, therefore, the connection diagram remains the same.

If you power it at 5V, the board may be damaged, so be careful and use 3.3V. Some MPU6050 boards have a voltage regulator that acts as a fuse, but it's still not worth the risk. If your board has an AD0 pin, it must be connected to ground (GND). In our case, the VIO pin is connected to AD0 on the board itself, so there is no need to connect the AD0 pin.

Sketch for Arduino

At this point you will need some knowledge of Arduino programming. If you don't understand something, stop there and try to figure it out. The explanations below will help you with many questions, but it is impossible to describe all possible nuances.

Once you have connected the MPU-6050 to your Arduino, turn it on and download the I2C scanner code sketch.

Copy the program code, paste it into an empty sketch and run it. Open serial Arduino monitor IDE (Tools->Serial Monitor) and make sure you are connected to the 9600 (bottom left).

If you did everything correctly, the I2C device should be detected and assigned an address of 0x68 or 0x69. Write it down. If errors appear, check your connection.

Now you need to upload a sketch that processes information from the accelerometer/gyroscope. There are more than one version of similar sketches floating around the Internet, we recommend using this one. After following the link, click “Download Zip”. After downloading, unzip the archive. Then open the Arduino IDE. Go to sketch->import library -> add library. You will need to add both folders: I2Cdev and MPU6050.

Once you have installed the libraries, open the MPU6050_DMP6 file (MPU6050 -> Examples). I recommend that you watch it, even if you are not particularly knowledgeable in code. If you have assigned the address 0x69, you need to uncomment one line at the top of the code (after #includes), since the default is 0x68. The program should now compile.

Download the program, open the serial monitor window (this time with 115200) and follow the instructions. Congratulations, as you should now be able to get the values ​​from the accelerometer/gyroscope via the Arduino!

Now run the calibration sketch, which can be downloaded here: MPU6050_calibration.ino (again, the default port is 0x68, but you can change it). Record the offset data you receive. You will use this data in the MPU6050_DMP6 sketch (and in the subsequent program for the quadcopter).

Now you have a working, certainly useful, accelerometer/gyroscope.

Connecting to Arduino

We looked at connecting the accelerometer. The next step is to get the Arduino to control the motors. The Arduino board provides no output great importance current and voltage, so instead of connecting motors directly to the board's digital outputs, we use transistors to "amplify" the voltage.

Let's start assembling the electrical circuit. At this stage we will need an Arduino, motors, transistors (circuit board and connectors). The connection diagram is given below, under the necessary text explanations. Connect the four PWM outputs (marked with ~ on the Arduino) to the transistor as shown in the pictures. After this, connect the connectors to the motors that are connected to the power source. The above quadcopter project used a 5V power supply, but a 3-5V battery should also work.

Make sure the transistors are grounded and the ground on the Arduino is connected to the ground from the power supply. Make sure the motor rotors are turning in the correct direction (they should be able to lift the quadcopter, not roll). If you switch the motor pin from 5V to the transistor, the motor rotor will start rotating in the opposite direction. Once configured, you will no longer have to change the direction of rotation of the motors. We will only change the speed.

After running the accelerometer and testing it, you need to install everything on the ProtoBoard (you can use a circuit board on which you solder rails for installation on the Arduino. You can go the more elegant route and buy a Proto Shield). You should not solder a transistor onto a prototyping board. It is better to use a socket with contacts for it so that you can replace it at any time.

In our case, we soldered the accelerometer to the board and only after that did the calibration. But practice shows that this is not entirely correct. To increase the accuracy of the gyroscope/accelerometer readings, it is better to first calibrate it on a flat surface and only then solder it.










You can read about PID control on Wiki if you are not familiar with these controllers. The PID class for Arduino uses three inputs: setpoint, measurement and output. The output depends on the current position and measurements. The PID controller tries to change the output so that the measurements correspond to the target position. The algorithm uses interesting mathematics. The PID control algorithm tries to work in such a way that the values ​​remain as stable as possible.

Our algorithm uses two PID controllers for stabilization: one for pitch and one for roll. The difference in the rotation speed of propellers 1 and 2 will be the same as the difference in the speed of propellers 3 and 4. Likewise for pairs 1,3 and 2,4. After this, the PID controller changes the difference in speed, bringing the pitch and roll to zero.

Don't forget to check which digital pins from the Arduino go to the motors and change the sketch accordingly.

Further modernization of the quadcopter using Arduino


The main problems with a small quadcopter are its cost and weight. You can look for larger and more powerful motors, but this will not particularly improve its performance. What will really help you (if you are willing to shell out more money) are brushless (valve) motors. In terms of characteristics, they are an order of magnitude better, but in addition to them you need to use speed controllers, which will make the quadcopter more expensive.

To reduce the weight of the design, it is best to use Arduino Uno, since for this controller model you can remove the “stitched” microprocessor chip and install it directly on your ProtoBoard. As a result, you will gain about 30 grams of weight, which is quite a lot on such a scale. Additionally, you will need to use several more capacitors, etc. Or how Alternative option, you can use Arduino Pro Mini.

The Arduino program, which was written and presented in the previous section, can be easily expanded and enriched with additional functionality. The most important thing is that at this stage the quadcopter can already automatically stabilize the flight. If you want to set up remote control, you can look towards transmitters/receivers or bluetooth modules. In general, you now have the basis, and there is even more room for further modernization.

Leave your comments, questions and share personal experience below. New ideas and projects are often born in discussions!

The quadcopter also has autonomous power supply. The total cost of such a homemade product is about $60.

If you have a more substantial amount, then it is more promising to equip your homemade product with motors without brushes with appropriate controllers.

A gyroscope and accelerometer are used to stabilize the flight. A gyroscope is needed to determine the angle of inclination of the quadcopter relative to earth's gravity. An accelerometer is needed to calculate acceleration.

Materials and tools:
- lithium batteries (3.7 V);
- wires;
- transistor ULN2003A Darlington Transistor (more powerful transistors can be used);
- engines type 0820 Coreless Motors;
- Arduino Uno microcontroller;
- MPU-6050 board (this is both a gyroscope and an accelerometer);
- availability of a 3D printer or access to it;
- necessary tools.

Manufacturing process:

Step one. Creating a quadcopter body
The body is made very quickly and simply. It is printed using a 3D printer. Creating a frame in this way is good because it comes out light, all this thanks to honeycomb printing. The design of parts took place in the Solidworks program. Using this program, you can edit the parameters of the case and make your own changes to it, if necessary.




After the quadcopter frame is printed, you can install the motors and solder wires to them.




Step two. Connecting Arduino
How to connect the MPU6050 board can be seen in the diagram below. It is important to understand that Arduino library implies connection through these contacts. If a circuit from another manufacturer is used, it is important to ensure that the contacts are arranged in the same sequence.


To power the board, only 3.3 V is used; if you power it with 5 V, it will be damaged. Some MPU6050 boards have a fuse that protects the system from high voltage, but it's better not to risk it. If the board has an AD0 pin, it must be connected to ground (GND). In this case, VIO is connected to the AD0 pin directly on the board, so there is no need to connect the AD0 pin.












In order for the Arduino to control the motors, transistors will be needed, thanks to them it will be possible to supply high voltage to the motors. You can see in more detail how all the elements are connected in the diagram.

Step three. Sketch for Arduino
Once the MPU-6050 is connected to the Arduino, you need to turn it on and download the I2C scanner code sketch. Next, you need to copy the program code and paste it into an empty sketch. After this, you need to open the Arduino IDE serial monitor (Tools->Serial Monitor) and make sure that the 9600 is connected.
If everything is done correctly, the I2C device will be detected, it will be assigned an address of 0x68 or 0x69, it needs to be written down.
Next, a sketch is loaded that processes information from the gyroscope and accelerometer. There are many of them on the Internet, but it is best to use.


At the final stage, you will need to calibrate the gyroscope and accelerometer values. To do this, you need to find a flat surface and place the MPU6050 on it. Next, the calibration sketch is launched, the resulting deviation data is recorded and then used in the MPU6050_DMP6 sketch.

Step four. Program for Arduino
Thanks to the program that is posted, the quadcopter stabilizes and hangs in a stable state. Next, using this program, the quadcopter is controlled.


Two PID controllers are used to stabilize the quadcopter. One is needed for pitch, and the second for roll. The controller measures the speed of rotation of the propellers and based on this the quadcopter is controlled.

Step five. Quadcopter modification

The main problem with a small and cheap quadcopter is its weight. To solve this problem, you need to install more powerful and lighter motors; brushless motors are best suited, they are also called valve motors. They are much better than brush ones, but you also need to buy speed controllers for them, so the cost of homemade products increases sharply.

In this article we will talk about quadcopters based on Arduino, their advantages and price categories.

Quadcopter with functions of a computer device.


IN modern world There are a wide variety of quadcopter models that are designed to be used for a variety of purposes. They are not just toys, which they were perceived to be from the very beginning of their appearance on the domestic market. They are now devices designed to perform many different types of tasks. They are used in many cases for professional creative activities. In order for the quadcopter to work properly, it must be equipped in production with additional parts in addition to the base. In order for the device to be controlled using the remote control remote control it is necessary to install a processor and many other electronics system parts on it. Today there are a large number of processors that are used to create high-quality and functional quadcopters. Among them, Arduino stands out. It is a fairly powerful processor that allows the quadcopter to have some of the functions of a computer device.

Today, a quadcopter using Arduino can be easily purchased at any online store that specializes in the production of such devices. Thanks to this processor, the largest and most powerful devices in terms of technical parameters are created. They are suitable for a wide variety of tasks. They are designed for both regular flights and professional filming. Thanks to powerful processor they easily perform all tasks assigned by users. The speed of data exchange with the remote control increases significantly. In addition, videos shot in real time are instantly transmitted to mobile devices, which are connected to the quadcopter on this moment. It should be noted that many users assemble devices based on the Arduino processor themselves. It is available in almost every specialty store.

Features of a quadcopter based on Adruino.


Arduino devices are very popular because they are very convenient to use. Quadcopters of this type quickly perform all tasks assigned by users. With this processor, it is possible to get a high-quality device that will allow you to enjoy limitless possibilities during a normal flight, and even during the filming process. Images are obtained with a high level of resolution. Devices based on Arduino work smoothly. Thanks to this processor, the device itself is able to carry out its work over a long period of time, and its system will not fail. Devices on latest versions Arduinos are not budget ones. However, this allows users to get professional-level quadcopters that will quickly and without interruption perform all the tasks assigned by consumers. It ensures reliable operation of the quadcopter.



Hello, Habro residents!
In this series of articles, we will open the lid of the quadcopter a little more than the hobby requires, and we will also write, configure and launch our own program for the flight controller, which will be a regular Arduino Mega 2560 board.

We have ahead:

  1. Basic concepts (for beginner copter pilots).
  2. PID controllers with an interactive web demonstration of operation on a virtual quadcopter.
  3. The actual program for Arduino and the configuration program for Qt.
  4. Dangerous tests of a quadcopter on a rope. First flights.
  5. Crash and loss in the field. Automatic search from the air using Qt and OpenCV.
  6. Final successful tests. Summarizing. Where to go?

The material is voluminous, but I will try to fit it into 2-3 articles.
Today we expect: a spoiler with a video of how our quadcopter flew; basic concepts; PID controllers and practice of selecting their coefficients.

What is this all for?

Academic interest, which, by the way, pursues not only me (, ,). And, of course, for the soul. I had great fun while working and felt real, indescribable happiness when “IT” flew with my program :-)

For whom?

This material may also be of interest to people who are far away, or who are just planning to get involved in multi-rotor systems. Now let's talk about the purpose of the main components of the quadcopter, how they interact with each other, about the basic concepts and principles of flight. Of course, all the knowledge we need can be found on the Internet, but we cannot be forced to look for it on the vast Internet.

Without compromising your understanding of basic concepts, feel free to skip everything you know until the next unfamiliar term, highlighted in bold, or to an incomprehensible illustration.

NO #1!

Don’t start writing your own program for a flight controller until you try ready-made solutions, of which there are quite a lot now (Ardupilot, MegapirateNG, MiltiWii, AeroQuad, etc.). First of all, it's dangerous! It takes practice to control a quadcopter without GPS and a barometer, and even more so when it glitches, flips over, or doesn’t fly exactly where it should - and this is almost unavoidable during the first tests. Secondly, it will be many times easier for you to program by understanding what needs to be programmed and how it should work in the end. Believe me: flight mathematics is only a small part of the program code.

NO #2!

Do not undertake to write your own program for a flight controller if you are not pursuing an academic interest and you only need what ready-made solutions have already been able to do for a long time (fly, take photographs, shoot videos, fly on assignment, etc.) While you write everything yourself, It will take a long time, even if you are not alone.

Basic Concepts


Quadcopters come in different varieties, but they are all united by four main rotors:
Despite the apparent symmetry, it is very important for the pilot to distinguish where the front of the quadcopter is (shown by the arrow). Here, like in radio-controlled car models: when the “forward” command is given, the quadcopter flies not where the pilot is looking, but where the imaginary nose of the quadcopter is pointing. This is fraught with danger: it can be difficult for beginners to return to themselves a device that has been caught by the wind, turned sideways somehow (we, of course, are not talking about flying in a first-person camera and about “smart” flight modes using a compass and GPS.) Solution This problem can be partially helped by front screws or beams of a different color, some kind of ball in front or different colored LEDs. But all this turns out to be useless when the pepelats rapidly turns into a dot above the horizon.

We will be flying on an “X” shaped quadcopter frame because I like the look of it better. Each design has its own advantages and purpose. In addition to quadcopters, there are other multicopters. Even if you don’t count exotic options, there are still a whole bunch of them!



Let's figure out how our quadcopter is structured inside, and what the flight controller that we plan to program should do.




Angles pitch, roll and yaw (pitch, roll, yaw)- angles by which it is customary to determine and set the orientation of the quadcopter in space.


Sometimes the word "angle" is omitted and they simply say: pitch, roll, yaw. But according to Wikipedia this is not entirely accurate. Flight of the quadcopter in the required direction is achieved by changing these three angles. For example, in order to fly forward, the quadcopter must tilt due to the fact that the rear motors spin a little stronger than the front ones:

Gas quadcopter- arithmetic average between the rotation speeds of all motors. The more gas, the greater the total thrust of the motors, the more they drag the quadcopter up(DO NOT FORWARD!!! “Slippers on the floor” here means the fastest ascent). Usually measured as a percentage: 0% - motors are stopped, 100% - rotating at maximum speed. Gas hovering- the minimum gas level that is necessary so that the quadcopter does not lose altitude.

Throttle, pitch, roll, yaw - if you can control these four parameters, then you can control the quadcopter. They are also sometimes called control channels. If you purchased a two-channel remote control, you will not be able to control the quadcopter. The three-channel one is more suitable for small helicopters: you can fly without roll control, but it’s not convenient on a quadcopter. If you want to change flight modes, you'll have to shell out for a five-channel remote control. If you want to control the tilt and pan of the camera on board, there are two more channels, although professionals use a separate remote control for this.

There are many flight modes. GPS, barometer, and rangefinder are used. But we want to implement the basic one - stabilization mode ( stab, stabilize, fly in a “stub”), in which the quadcopter maintains the angles that are given to it from the remote control, regardless of external factors. In this mode, in the absence of wind, the quadcopter can hang almost in place. The pilot will have to compensate for the wind.

The direction of rotation of the screws is not chosen randomly. If all the motors rotated in one direction, the quadcopter would rotate in the opposite direction due to the torques created. Therefore, one pair of opposing motors always rotates in one direction, and the other pair in the other. The effect of the occurrence of rotational torques is used to change the yaw angle: one pair of motors begins to rotate a little faster than the other, and now the quadcopter slowly turns to face us (what a horror):



  • LFW - left front clockwise rotation (left front, clockwise rotation)
  • RFC - right front counter clockwise rotation (right front, counterclockwise rotation)
  • LBC - left back counter clockwise rotation (left back, counterclockwise rotation)
  • RBW - right back clockwise rotation (right rear, clockwise rotation)

Controls the rotation speed of the motors flight controller (controller, brains). Usually this is a small board or box with many inputs and outputs. There are a huge number of different controllers with different sets of capabilities, different firmware, and different tasks. Here are just a few:




The general task of the flight controller is to perform a control cycle several tens of times per second, which includes: reading sensor readings, reading control channels, processing information and issuing control signals to the motors in order to carry out the pilot’s commands. This is what we are going to program.

There are many different types of sensors that can be used. We will use those that have already become almost mandatory in all quadcopters three-axis gyroscope and three-axis accelerometer. The accelerometer measures acceleration, the gyroscope measures angular velocity. Thanks to them, the flight controller knows the current pitch, roll and yaw angles. These sensors can be built into the flight controller or external. The process of calculating three angles based on sensor readings is a topic for a separate article. But we don’t need to know this here: the MPU-6050 will do everything for us. This is a small board that carries out the necessary calculations and filtering internally and produces almost ready-made angles using the i2c protocol. All we have to do is count them, process them with the rest of the data, and issue control signals to the motors.

Motors on multicopters consume large currents, so the flight controller controls them not directly, but through special hardware drivers called speed controllers (ESC, regulator, eska). These regulators are powered by the main on-board battery, the control signal is received from the controller, and at the output they have three wires (A, B, C), which go directly to the motors (each motor has its own regulator!)




The “protocol” of communication between the regulator and the motor is not as important to us as the “protocol” of communication between the flight controller and the regulator, because we have to programmatically control the regulator from the controller. There are regulators controlled via i2c, but the most common ones are controlled by a square wave signal with a minimum of 0 volts and a maximum of 3-5 volts (it is called PWM or PWM, and some argue that it is more correct - PPM. More details, for example).

“Protocol” is a strong word: to command the motor to rotate at maximum speed, the controller must send pulses lasting 2 milliseconds, interspersed with a logical zero lasting 10 - 20 milliseconds. A pulse duration of 1 millisecond corresponds to stopping the motor, 1.1 ms - 10% of maximum speed, 1.2 ms - 20%, etc. In practice, the duration of zero does not play any role; only the duration of the pulse itself is important.

Despite all the apparent simplicity, there is an ambush here: flight controllers come in different different settings, regulators are different, and the minimum (1 ms) and maximum (2 ms) are not universal. Depending on many factors, the 1-2 ms range may actually turn out to be 1.1 - 1.9 ms. In order for the regulator and controller to speak absolutely the same language, there is a procedure regulator calibration. During this procedure, the ranges of the controls are changed and become equal to the range of the controller. The procedure is hardwired into the program of each controller and includes several simple steps(steps may vary depending on manufacturer - read instructions!):

  • Turn off the power supply to the regulator.
  • Remove the propeller from the motor.
  • Apply a signal corresponding to the maximum rotation speed to the controller input.
  • Apply power to the regulator. The motor must remain motionless without outside help.
  • Apply a signal corresponding to the minimum rotation speed to the controller input.
  • Pause for 1-2 seconds, wait for the characteristic squeak.
  • Turn off the power supply to the regulator.

After this, the corresponding interval boundaries will be entered into the controller. When trying to take off with uncalibrated regulators, the consequences may be unexpected: from a sudden jerk of the quadcopter into the nearest tree to complete immobility of the motors at any throttle value.

PWM uses exactly the same principle onboard receiver. This small device, receiving radio control signals from the ground and transmitting them to the flight controller. Most often, the flight controller for each control channel (throttle, pitch, roll, etc.) has its own input to which PWM is supplied. The interaction logic is simple: a command, for example, “70% throttle,” continuously goes from the ground to the receiver, where it is converted into PWM and sent to the flight controller via a separate wire. Same with pitch, roll, yaw.

Since the receiver and the controller have their own friendly PWM relationship, they will also have to be calibrated: remote controls with receivers are different with their own operating ranges. The controller must be able to adapt. Procedure radio calibration, unlike calibration of regulators, we will have to create it ourselves as part of the flight program. The general calibration plan is as follows:

  • Remove the propellers from the motors just in case.
  • Somehow put the controller into radio calibration mode.
  • The controller starts radio calibration for several tens of seconds.
  • In the allotted time, move all the remote control sticks in all directions until they stop.
  • The controller remembers the maximums and minimums for all control channels during internal memory for centuries.
So: during radio calibration, the flight controller remembers the receiver ranges for all control channels; During ESC calibration, the flight controller range is entered into all ESCs.

In addition to the program for the flight controller, one more program is needed: flight controller setup interface. Most often, it is a PC program that connects to the flight controller via USB and allows the user to configure and check the flight program, for example: run radio calibration, configure stabilization parameters, check the operation of sensors, set a flight route on the map, determine the behavior of the multicopter when the signal is lost and much more. We will write our configuration interface in C++ and Qt in the form of a console utility. Here it is, if you look into the future:




No one is immune from accidents. Even ten-inch plastic propellers on small motors can leave bloody bruises on the skin that will hurt for another week (tested personally). It’s easy to give yourself a new make-up and hairstyle if you engage the gas stick on the remote control while you carry the quadcopter turned on. Therefore, the flight controller must provide at least some safety: the mechanism armed/disarmed. The quadcopter's "disarmed" state means that the motors are turned off and even a full throttle command from the remote control has no effect, although power is supplied. The “armed” state of the quadcopter means that commands from the remote control are carried out by the flight controller. In this state, quadcopters take off, fly and land. The quadcopter turns on and should immediately go into the disarmed state in case an inattentive pilot turns it on when the throttle stick on the remote control is not at zero. To put the copter into the “armed” state, the pilot needs to make some pre-agreed gesture with the remote control sticks. Often this gesture is to hold the left stick in the lower right corner (throttle = 0%, yaw = 100%) for a couple of seconds. After this, the flight controller performs at least a minimal self-test and if it passes successfully, " arming himself"(ready to fly!) With another gesture (throttle = 0%, yaw = 0%) quadcopter" becomes disarmed"Another good security measure is autodisarm, if the gas was at zero for 2-3 seconds.

About motors, batteries, regulators, propellers

The choice of components for a multicopter is a topic for a whole series of articles. If you are going to make your first quadcopter, formulate what you need it for and use the advice of experienced ones or take a list of components that someone else has compiled and successfully flies on it.

Still, for a general understanding, it is useful to know the main points.

Batteries


Among amateurs and professionals, multi-rotor systems are most common lithium polymer batteries, as the main power sources for on-board electronics and motors. They are distinguished by capacity, voltage and maximum current output. Capacity, as usual, is measured in ampere-hours or milliamp-hours. Voltage is measured in the number of “cells” of the battery. One “can” is an average of 3.7 volts. A fully charged “can” is 4.2 volts. The most common batteries are those with three to six cells. The maximum current output is measured in amperes, and is marked, for example, like this: 25C. C is the battery capacity, 25 is the multiplier. If the capacity is 5 amperes, then such a battery can deliver 25 * 5 = 125 amperes. Of course, it is better to take the current output parameter with a reserve, but, basically, the larger it is, the more expensive the battery. Labeling example: 25C 3S 4500mah.

Each bank is a separate battery. They are all soldered in series. In order to charge all banks evenly, a balancing connector is provided with access to each bank separately, and special charging device.

Motors, propellers, regulators


The main parameter of a brushless motor is its kv. This is the number of revolutions per minute for each volt of voltage applied. The most common motors are with kv from 300 to 1100. Kv closer to 1000 is usually chosen for small quadcopters (1-2 kilograms plus 500 grams of payload) and they are equipped with plastic propellers up to 12 inches in diameter. Large multicopters (for lifting good and heavy photo-video equipment) or long-flying aircraft (for flying time records) usually have motors with low kv (300-500) and huge carbon propellers (15 - 20 inches in diameter). Kv is not the only one important parameter motor: you can often find entire tables of the dependence of motor power and thrust on the supplied voltage and the type of propeller installed. In addition, each motor is designed for its own voltage range (number of battery cells) and its own maximum current. If the manufacturer writes 3-4S, you should not use it with 5S batteries. The same applies to regulators.

If the motor is designed for a current of up to 30A, then the regulator should be designed for a current of up to 30 + 10A to prevent overheating. Poor quality or unsuitable governors can cause so-called "synchronization slips" and stall the motor in flight, and you will recognize another multi-rotor term: " caught the planet." Another important point- thickness and quality of wires. An incorrectly sized wire or a bad connector can lead to a fire in the air.

As you can see, there are a lot of nuances. I haven’t even listed half of them, so it’s quite difficult to choose the components for your first multicopter yourself.

Stabilization mathematics, PID controllers (PID)


If you decide to get into multicopters, then sooner or later you will have to deal with setting the PID controller, since this mathematical apparatus is used in almost all stabilization tasks: stabilizing the angles of a quadcopter in the air, flying and holding a position using GPS, holding altitude using a barometer, brushless mechanisms stabilization of the video camera in flight (camera gimbal).

You buy a two-axis camera gimbal, put, for example, a GoPro there, turn it on, and instead of stabilization you get convulsions, vibrations and twitches, although all sensors are calibrated and mechanical problems are eliminated. The reason is incorrect parameters of the PID controllers.

You assemble a multicopter, calibrate sensors, regulators, radio, check everything, try to take off, and it is so dull in the air that even a light breeze turns it over. Or vice versa: he is so sharp that he suddenly takes off and does a triple somersault without permission. The reason is still the same: the parameters of PID controllers.

For many devices using PID controllers, there are instructions for setting up, and even several in addition to numerous video instructions from the users themselves. But to make it easier to navigate this diversity, it is useful to understand how these regulators work inside. In addition, we are going to write our own quadcopter stabilization system! I propose to “re-invent” and "on the fingers" to understand PID controller formula. For those who prefer dry mathematical language, I recommend Wikipedia, because... in Russian the material is not yet presented in such detail.

We will consider a quadcopter in two-dimensional space, where it has only one angle - the roll angle, and two motors: left and right.




The flight controller continuously receives commands from the ground: “roll 30 degrees”, “roll -10 degrees”, “roll 0 degrees (hold the horizon)”; its task is to perform them as quickly and accurately as possible using motors, taking into account: wind, uneven distribution of the weight of the quadcopter, uneven wear of the motors, inertia of the quadcopter, etc. Thus, the flight controller must continuously solve the problem of what rotation speed to apply to each motor, taking into account the current value of the bank angle and the required one. Continuously is, of course, a strong word. It all depends on the computing capabilities of the particular hardware. On Adruino, it is quite possible to fit one iteration of the processing and control cycle into 10 milliseconds. This means that once every 10 milliseconds the angles of the quadcopter will be read, and based on them, control signals will be sent to the motors. These 10 milliseconds are called regulation period. It is clear that the smaller it is, the more often and more accurately the regulation occurs.

The gas level flows from the receiver to the controller. Let's denote it . Let me remind you that this is the arithmetic average between the rotation speeds of all motors, expressed as a percentage of the maximum rotation speed. If and are the rotation speeds of the left and right motors, then:




where is the response of the quadcopter (force), which creates a torque due to the fact that the left motor rotates faster than the gas, and the right motor rotates just as much slower. can also take negative values, then the right motor will spin faster. If we learn to calculate this value at each iteration of the processing cycle, then we will be able to control the quadcopter. It is clear that at a minimum it should depend on the current roll angle () and the desired roll angle (), which comes from the control panel.

Let’s imagine a situation: the command “keep the horizon” ( = 0) is received, and the quadcopter has a roll to the left:




- the difference (error) between and , which the controller seeks to minimize.

The greater the difference between the desired roll angle and the current one, the stronger the reaction should be, the faster the left motor should spin relative to the right one. If we write this using our notation:



Here P is the proportionality coefficient. The larger it is, the stronger the reaction will be, the sharper the quadcopter will react to deviations from the required roll angle. This intuitive and simple formula describes the work proportional controller. The point is simple: the more the quadcopter deviates from the required position, the harder you need to try to return it. Unfortunately, this formula will have to be complicated. The main reason is overshoot.

In a few tens of milliseconds (several iterations of the processing cycle), under the influence of the proportional controller, the quadcopter will return to the required (in this case horizontal) position. All this time, the error and effort will have the same sign, although they will become less and less in magnitude. Having gained a certain turning speed (angular speed), the quadcopter will simply roll over to the other side, because no one will stop it in the required position. It’s like a spring that always wants to return to its original position, but if you pull it back and release it, it will oscillate until friction takes over. Of course, the quadcopter will also be affected by friction, but practice shows that it is not enough.

For this reason, one more term needs to be added to the proportional controller, which will slow down the rotation of the quadcopter and prevent overshooting (rolling in the opposite direction) - a kind of imitation of friction in a viscous medium: the faster the quadcopter turns, the harder you need to try to stop it, of course, in within reasonable limits. We denote the rotation speed (rate of error change) as , then:



where D is an adjustable coefficient: the larger it is, the stronger the stopping force. From a school physics course, vague memories emerge that the rate of change of any quantity is the derivative of this quantity with respect to time:

.

And now the proportional controller turns into a proportional-differential one (proportional term and differential):

.

The error is easy to calculate, because at each iteration we know and ; P and D are parameters that can be configured before launch. To calculate the derivative (rate of change), it is necessary to store the previous value, know the current value and know the time that has passed between measurements (control period). And here it is - sixth grade school physics (speed = distance / time):

.

- regulation period; - error value from the previous iteration of the regulation cycle. By the way, this formula is the simplest way numerical differentiation, and it is quite suitable for us here.

Now we have a proportional differential controller in a flat bicopter, but there is still one more problem. Let the left edge weigh a little more than the right, or, what is the same, the left motor works a little worse than the right. The quadcopter is slightly tilted to the left and does not turn back: the differential term is zero, and the proportional term, although it takes a positive value, is not enough to return the quadcopter to a horizontal position, because the left edge weighs slightly more than the right. As a result, the quadcopter will always pull to the left.

A mechanism is needed to monitor such deviations and correct them. A characteristic feature of such errors is that they correct themselves over time. The integral term comes to the rescue. It stores the sum of all errors across all iterations of the processing cycle. How will this help? If the proportional term is not enough to correct a small error, but it still exists, gradually, over time, the integral term gains strength, increasing the response and the quadcopter takes the required roll angle.

There is a nuance here. Let's assume it is 1 degree, the control cycle is 0.1s. Then in one second the sum of errors will take the value of 10 degrees. And if the processing cycle is 0.01s, then the amount will gain as much as 100 degrees. So that over the same time the integral term gains the same value for different periods of regulation, we will multiply the resulting amount by the regulation period itself. It is easy to calculate that in both cases the sum of 1 degree is obtained from the example. Here it is - the integral term (without an adjustable coefficient for now):

.

This formula is nothing more than a numerical integral over time of a function in the interval from zero to the current moment. That is why the term is called integral:

,

where T is the current moment in time.
It's time to write down the final formula for the proportional-integral-derivative controller:

,

where is one of the configurable parameters, of which there are now three: . This formula is convenient to use from program code, but here is the formula that is given in textbooks:

.

There are several variations of it, for example, you can limit the modulus of the integral term so that it does not exceed a certain permissible threshold (we will do this).

Practice

Well, now it’s time to practice selecting coefficients. Readers are offered a JavaScript page with a virtual quadcopter, which they have already seen in pictures: selection of PID controller parameters for a quadcopter(JSFiddle). At the first start, overshoot is immediately visible - oscillations around the required position. When the oscillations stop, you can observe the effect that the proportional coefficient cannot cope with the error due to the “asymmetrical” quadcopter (set by the “Asymmetry” checkbox). The parameters available for configuration are P, I, D. Now you know what to do with them. The “scroll” under the quadcopter can be controlled by the required roll value. “Interval (ms):” - regulation interval. Reducing it is cheating, but seeing how it affects the quality of stabilization is very useful.

For lovers of “pure” mathematics, we can offer configure abstract PID controller

The entered parameters are not automatically applied: you need to click “Apply”. A couple of small tips: if it seems to you that the quadcopter is too slow to respond to control, you can increase P, but too large a P value may lead to overshoot. Parameter D will help to cope with overshoot, but too large values ​​will lead to frequent oscillations, or again to overshoot. The I parameter is usually 10 - 100 times less than the P parameter because its strength lies in accumulation over time, not in rapid response.

Manually tuning PID parameters requires practice. There are analytical methods for calculating them, but they require good preparation and accurate knowledge of many parameters of the specific system being customized. As a middle ground between manual selection and analytical calculation, there are a wide range of empirical methods proposed by various researchers.

In our 2D quadcopter, only one angle changes - the roll angle. In tuning a 3D quadcopter, three independent PID controllers will be required for each of the angles, and control of a specific motor will represent the sum of the efforts across all controllers.

Conclusion of the first part

In this article, we got acquainted with the basic concepts: quadcopter and flight principle, pitch, roll, yaw, throttle, hover throttle, stabilize flight mode, flight controller, gyroscope, accelerometer, speed controller, PWM, controller calibration, radio calibration, on-board receiver, interface for setting up the flight controller, armed/disarmed states, autodisarm.

After that we reinvented the formula PID controller touching a little numerical differentiation and integration, and experienced the hard way how to configure the parameters P, I, D on virtual quadcopter .

Now, if you are proficient in lightsaber programming, you can begin your quadcopter stabilization program, or better yet, add fresh ideas to existing ones. open source projects. Well, in a week or two, when I have the strength and time to match the quality, I will continue the story of how it was all programmed, tested, crashed, cut my fingers and completely flew away in an unknown direction. If you really want to continue, you can contact me here or, for example, on Vkontakte: it gives a little incentive.

In conclusion of this part, I simply have to mention the person who helped me in choosing components and setting up the most complex (first!) quadcopter on the MegapirateNG firmware and patiently answered hundreds of questions on these very basic concepts: SovGVD, thank you! :-)

As a reward for those who were able to squander all this sheet, I’m posting the promised little video of how our quadcopter with our “invented” PID controllers flies on our program for Arduino Mega 2560:



Of course, it lacks GPS, as in commercial and mass-produced products, it lacks stability a little, but it is OUR, and we know it inside and out down to the last factor of the integral coefficient! And it’s really cool that such technologies are available to us today.