Building the Drone
In Part 1 of this article series, Raul gave an overview of a drone design using open-source autopilot platforms. Here in Part 2, he goes step by step and describes how to build, configure and test a DIY quadrotor drone with the PX4 platform.
In Part 1 of this article series last month, I discussed the general architecture of a DIY multirotor drone and its main hardware and software components. I also gave a general introduction to the Ardupilot and PX4 platforms, discussing some examples of supported flight controller hardware and vehicle types, as well as available ground control software from both platforms. Additionally, I provided a general introduction to the MAVLink protocol used to communicate vehicles with ground control stations in both platforms. In this second part, I will discuss the main steps involved in the building and configuration of a DIY quadrotor with the PX4 platform. It will not be a complete tutorial, but a review of the most important steps involved. I will also give some tips that address potential problematic situations in the build process that are not always immediately obvious for beginners.
There are a lot of well documented tutorials out on the Internet. Consider this article a summary of steps and important tips from my own experience, for beginners who want to build their first quadrotor. At the end, I will also share additional tips on how to get started with autonomous flight by using the MAVSDK library and MAVROS package. Let’s see if, with this article, I can encourage you to build your first quadcopter, and begin to experiment with autonomous flight in the near future.
Figure 1 shows an amazing wiring chart made by Jethro Hazelhurst for the Ardupilot website . To see greater detail, you can check the original version by using the link in the figure caption. Analyze the picture in detail. It will be instructive if you have never built a quadcopter before.
The build process for a typical quadrotor—like the one I will show as an example in this article—is practically the same for both the PX4 and Ardupilot platforms. For this summary, I will use the PX4 platform to explain the process. So, what steps are generally involved in building, configuring and flying a quadcopter? I divide the process in four sections: 1) the hardware build; 2) flight controller firmware programming and configuration; 3) Electronic Speed Controller (ESC) calibration and pre-testing; and 4) flight test(s). Within each of those sections are several steps. The following development assumes you are already familiar with the main parts used to build a multirotor. Please see Part 1 of this article series (Circuit Cellar 357, April 2020) for more details.
So, here is one of the first tips I would give to anyone doing their first quadrotor build.
Tip #1: Never rush with any steps involved in the process, always double-check everything you do, and when in doubt, it is always better to do further research or ask anyone who’s more knowledgeable before proceeding.
THE HARDWARE BUILD
Step 1: The Frame. You’ll generally begin by building the frame. Frames come with a build instructions chart, or you can easily find one on the Internet. Just don’t tighten the screws hard the first time. I highly recommend that you build the frame first, just to “present” it.
Tip #2: For the first builds, it is better not to tighten the screws tightly at first, or to use threadlocker glue. Leave that for the final steps, after you are sure everything is correct and in place.
Step 2: ESCs and brushless motors. For connecting brushless motors, ESCs and battery power supply lines, “banana” type connectors are generally used. Although the S500 frame I used for my example build (Figure 2) comes with a power distribution board “embedded” in the bottom plate of the frame (which is just a double-sided PCB used also as part of the frame structure), for beginners I would recommend using a separate power distribution board (PDB).
The reason for that is because it’s always possible to mess up with the soldering needed in the PDB. If this happens, it is cheaper and easier to replace the separate PDB than to replace the frame’s bottom plate. A typical quadrotor of this size will consume around 15A in total while hovering in soft wind and without payload. But the total current can easily peak up to the double (if not more), when carrying a payload, doing aggressive maneuvers or flying in severe wind conditions.
Tip #3: All soldering related to the propulsion system must be done carefully, tidily and robustly. Those connection points will carry high currents, so they must have good electrical continuity. There must also be the best isolation possible between the positive and negative terminals.
As a safety measure, always check the absence of electrical continuity between every pair of “+” and “-” pads on the PDB. You want to be sure there isn’t a short circuit in it, due to the soldering process. Some ESCs, brushless motors and power distribution boards come with bullet connectors already soldered. If not, you must solder them by yourself. Figure 2 shows the ESCs, motors and PDB embedded in my S500 frame, with all the necessary connections. ESCs and cables will later be secured to the frame by using plastic zip ties.
Step 3: Flight controller, RC receiver and GPS module. The spinning motors and propellers in the quadrotor generate a certain amount of mechanical vibration that is detected by the accelerometer and gyroscope sensors in the flight controller. This vibration is noise to the sensors, and can potentially affect the quadcopter’s stability. To minimize it, the flight controller can be installed on top of an anti-vibration mount, as shown in Figure 3. I initially used rubber bands to fix the flight controller to the anti-vibration mount. In the final steps, it will be fixed with double-sided tape. Figure 4 shows the motor layout for a “Quad X” configuration. Each motor has a given number, position and rotation direction, so it is critical to connect their corresponding ESC signal cables to the flight controller’s outputs in the given order.
Tip #4: The correct layout of all motors in the frame and connection of ESC input signal cables to correct output pins in the flight controller are of utmost importance. Failing to do this can make the quadcopter behave erratically and ultimately crash.
For this build, I used a conventional 6-channel remote control (RC) system with parallel analog PWM outputs. The Pixhawk controller, however, just accepts serial PPM-Sum or S.Bus input signals, which provide all RC channel signals via a single serial digital output. You can purchase an RC system with a serial output receiver suitable for connecting directly to the Pixhawk. Nevertheless, if you have a system with parallel analog PWM channels, low-cost parallel analog PWM-to-digital serial converters, such as the one I used for this build, are available.
The connection of the GPS and external compass module is straightforward. The module has two connectors. The wider one is for the GPS receiver, and the narrow one for the digital compass. The digital compass is highly sensitive to magnetic interference. With that in mind, a stand for the GPS module is generally recommended to avoid interference from the rest of the electronics, especially the power system. Figure 3 shows the flight controller, RC receiver and GPS module with its stand.
Step 4. Power module, battery and alarm. The power module connected to the flight controller and the PDB, as well as the LiPo battery in place with its low voltage alarm, are shown in Figure 5. The connection between the flight controller, power module, battery and propulsion system are illustrated clearly in Figure 1, so go back to this at any time to get a more detailed view of other connections.
To fly this quadcopter, I’m using a 5,000mAhour, 3S, 8C battery.
Tip #5: Improper use of LiPo batteries can be extremely dangerous. Before charging, connecting and using them, take the time to thoroughly research their use and care. Many resources on the Internet cover this topic in detail.
Step 5. Telemetry module, gimbal and FPV (first person view) transmitter. The telemetry module must be connected to the TELEM1 port in the Pixhawk (Figure 1). The gimbal receives power from the power distribution board. Mine can work with LiPo batteries between 2S and 6S. Gimbals generally have a signal input to manually control the camera pitch angle. This input can be connected to any unused output in the RC receiver, preferably one from a channel associated with a knob in the RC transmitter.
Once the gimbal is powered on, the camera should be automatically stabilized, and the pitch angle controlled with the associated knob in the RC transmitter. Drone cameras have a video signal output to be connected to the FPV video transmitter (powered also from the PDB). Usually, the FPV system is separated from the drone system itself (Figure 1).
Step 6. Finishing the hardware build. I usually connect the battery once at the end of the hardware build, to be sure everything is okay and nothing is getting hot due to a short circuit, especially in the propulsion system. Then, I proceed to tighten all screws with threadlocker glue. I take them out one by one from the frame and motors, apply threadlocker and tighten them well in place again. Threadlocker needs at least 24 hours to properly cure. I use Loctite Threadlocker Blue, which is recommended for multirotor builds.
Tip #6: After your first flight and before flying again, always check that the motor and frame screws are still tight. Sometimes they can loosen, because parts tend to settle further with mechanical vibration, and the screws would need to be tightened again. A loose motor or frame part can make the quadcopter vibrate too much and make it crash.
Although I’m using PX4 firmware for this build example, the same quadrotor setup can also be flashed with Ardupilot firmware by following a similar set of steps, such as the ones outlined as follows:
Step 1. Firmware flashing. It is really easy to flash the firmware to the flight controller by using ground control software. With QGroundControl, you can do that by going to the “Firmware” submenu located in the “Vehicle Setup” menu (Figure 6) and following the instructions given there. After flashing the PX4 firmware, the frame type must be configured in the “Airframe” submenu, under the “Quadrotor x” option. I chose the “S500” for this build (Figure 7).
Step 2. Sensor calibration. Next, from the “Sensors” submenu, we calibrate the compass, gyroscope and accelerometer, and also level the horizon (Figure 8). Plenty of guidelines are given in each step by QGroundControl, itself.
Step 3. Radio setup. The radio calibration, done in the “Radio” submenu (Figure 9), ensures that the minimum and maximum values for each RC channel are correctly set in the flight controller. These values generally vary from one RC transmitter to another.
Step 4. Flight modes configuration. In the “Flight Modes” submenu (Figure 10), we can configure up to six different flight modes. With a 6-channel RC, I generally assign channel 5 for controlling the flight modes and channel 6 for the “Kill switch” function. The three most useful flight modes I can recommend for beginners are Altitude, Position and Return.
Altitude automatically stabilizes the quadcopter’s altitude by using the barometric pressure sensor (altimeter), but it doesn’t stabilize position in the horizontal plane. This is because this flight mode does not use the GPS receiver. It requires you to stabilize the quadcopter in the horizontal plane manually, which can be tricky for beginners, especially with strong winds. In this mode, the quadcopter can be armed without the need for GPS fix (useful for indoor motor testing).
Tip #7: Never fly a quadcopter indoors if you are a beginner, or if there are people around. For your flight tests, always use big, open fields without people, animals or obstacles around.
Position is the preferred flight mode I recommend for beginners, especially for flying in windy conditions, because it uses the barometric pressure sensor and also the GPS receiver for stabilizing the drone vertically and horizontally. But it doesn’t allow arming the motors and drone take-off without a good GPS fix, which you won’t get unless the GPS receiver has good sky visibility.
Return flight mode automatically returns the drone to the home position, and is useful if, for some reason, you lose control, and just want the flight controller to take over and land the drone by itself at the original take-off spot.
Finally, it is also important to configure the “Kill switch” function to turn off the motors, especially when something goes awfully wrong. For example, if the drone falls down to the ground and the motors are still spinning, even with the throttle stick at zero—or when the drone is out of control and it is better to switch it off (and crash it) before anything worse happens.
Step 5. Power setup. In the “Power” submenu, the flight controller’s battery power monitoring function can be configured, so it can accurately estimate the remaining power and flight time. I generally just configure the “Number of cells” parameter (“3S” for a 3S battery), and leave the rest at their default values. At first, it’s better not to change the remaining parameters, until you understand well how these parameters affect the way the flight controller does the aforementioned estimations.
Step 6. Failsafe actions. A “failsafe action” is a predefined safety measure the quadcopter will take when some types of failures occur. Examples of failures include when the battery is at its minimum, when the RC signal is lost, or when the data link (telemetry) is lost. Available options for safety measure configuration range from doing nothing or giving a warning, to landing the drone, returning it to home or hovering it in place. Failsafe actions can be configured in the “Safety” submenu. The default options are also a good starting point.
ESC CALIBRATION & PRE-TESTING
One last important step to take before the first flight is ESC calibration (available from the “Power” submenu). ESC calibration ensures that all motors respond equally to the available throttle range from the RC transmitter. This is accomplished by teaching the ESCs to recognize the minimum and maximum PWM values for zero and full throttle.
Tip #8: Before attempting to take off for the first time, check the rotation direction of all motors according to what’s specified in the layout for the selected configuration. Also check the correct propeller mounting. Motors spinning in the wrong direction, or propellers incorrectly mounted will cause erratic behavior, ranging from not being able to take off at all, to taking off and spinning uncontrollably in the horizontal plane, or taking off and turning upside down and ultimately crash.
If a motor spins in the wrong direction, swap two of the three motor cables connected to the ESC (any two of the three available) to change the rotation direction. Also verify the proper propeller mounting, with the upper and lower cambers correctly positioned, and the leading edge “cutting” right into the air, in the right rotation direction.
Until this point, for safety reasons, always do all motor pre-tests without propellers, and put the propellers on the motors just before the first flight. Pick an open field with no people, animals or obstacles around. If there are natural or man-made obstacles (such as tall trees, hills, buildings or towers), the GPS receiver will take longer to get a fix, or worse yet, it will lose it intermittently during flight.
Tip #9: Always try to do your flight tests in the morning, when the wind is generally calmer than in the afternoon. Avoid flying in relatively strong winds until you develop good piloting skills. You can carry a pocket anemometer to measure wind speed before taking off.
The maximum wind speed in which a quadrotor can be safely flown depends on the quadrotor’s maximum speed. A good rule of thumb is to set the maximum wind speed at two-thirds or less of the quadrotor’s maximum speed. For example, if the quadrotor’s top speed is 80mph, two-thirds of that is about 53mph. This, according to the rule, will be the maximum recommended wind speed to fly the quadcopter safely. Last but not least, don’t forget to comply with all regulations in place for flying drones in your town, to avoid legal problems.
From a functional standpoint, PX4 and Ardupilot are not very different. The processes of flashing firmware and configuring parameters are almost the same in both platforms. Besides, both are MAVLink-compatible and generally work well with the same quadcopter setup, as long as the flight controller is compatible with both platforms.
After you have flashed and configured your quadcopter with, say, PX4, and flown your drone for some time, you can try Ardupilot if you wish. Every step described in the Firmware Flashing and Configuration section of this article will also apply, with minimum differences. You can even try to use Mission Planner or APM Planner this time. These are two ground-control software applications from the Ardupilot ecosystem, and are similar to QGroundControl.
MAVSDK AND MAVROS
In contrast, the MAVROS package (which is an ROS-to-MAVLink bridge, also from the PX4 ecosystem), allows the use of MAVLink communication, to make it possible for any computer running the Robot Operating System (ROS) middleware to communicate with any vehicle or ground-control station software that uses the MAVLink protocol.
In summary, MAVSDK will allow you to program a robotic drone, and MAVROS will allow you to do it straight away in an ROS environment, for a more modular, distributed, scalable and professional approach. Ardupilot has also its own MAVLink library, called Dronekit, with available Python and Android APIs. But apparently, not much work has been done lately to provide support for other programming languages.
If you want to get started with autonomous flight application development within the PX4 ecosystem, I suggest you try MAVSDK and/or MAVROS in three stages.
First, generally the easiest way to try MAVSDK is to install the Python wrapper and try some included examples with a “Software-in-the-Loop” (SITL) simulator. The C++ version installation can be a little more involved, in some cases requiring building the library from source. MAVROS is useful only if you already know how to use ROS. If you are new to ROS, perhaps you should first learn ROS, before trying MAVROS. But you still can make your drone fly autonomously by using MAVSDK. Both MAVSDK and MAVROS can connect to vehicles via serial port, UDP and TCP connections. When working with a simulation, TCP/UDP connections will usually be the way to connect our software application with the simulator.
Second, once your code runs well on simulation, you can try it on a real drone by opening the telemetry module serial port from the application running on your development computer or mobile device. In this case, MAVSDK and MAVROS will communicate with your drone by using the MAVLink connection established via the telemetry modules.
Third and last, to make your drone truly autonomous, you can upgrade your system by adding a companion computer (Raspberry Pi, Odroid, Jetson Nano or others) to your quadcopter setup. The companion computer can be connected directly to the TELEM2 serial port of the flight controller to run the MAVSDK/MAVROS code that controls the vehicle, along with some other code interfacing with additional sensors and actuators especially suited for your application.
I hope the information presented in this two-part article series gave you a general perspective about the PX4 and Ardupilot platforms, and what is involved in building a quadcopter for aerial photography, or perhaps more interesting, for experimenting with autonomous flight. If you have never built a quadcopter before, I really encourage you to do it and experiment.
I’m not going to lie, you’ll crash many times while learning to pilot it and experimenting with it, but that’s the normal learning process for anyone. Still, it is a lot of fun! Most excitingly, once you get a good grasp of what’s involved in building and configuring a quadcopter properly, you can begin to experiment with autonomous drone applications.
In a future follow-up to this article, I will cover in more detail—and with a concrete example—how to use the MAVSDK library to develop code for autonomous drones using SITL simulation. Until the next time!
PX4 Basic Configuration
PX4 ESC Calibration
How to care for your lipo batteries
PX4 SITL simulation
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • MAY 2020 #358 – Get a PDF of the issueSponsor this Article
Raul Alvarez Torrico has a B.E. in electronics engineering and is the founder of TecBolivia, a company offering services in physical computing and educational robotics in Bolivia. In his spare time, he likes to experiment with wireless sensor networks, robotics and artificial intelligence. He also publishes articles and video tutorials about embedded systems and programming in his native language (Spanish), at his company’s web site www.TecBolivia.com. You may contact him at email@example.com