How to Write your own Flight Controller Software — Part 1

Why would You?

It is a reasonable question. There are a number of Open Source and proprietary quadcopter flight controllers with firmware available. The problem is we want to use an Arduino board, to fit in with our other training programs. As soon as this becomes a constraint your options are very limited. In fact, your only real option is to write your own. So here we are.

The Hardware

Figure 1. Prototype Drone Hardware.
Figure 2. Drone Block Diagram.

Flight Controller Theory

When talking about quadcopters there are two common layouts, “X” and “+”. The “+” layout is simpler to model mathematically, but this configuration gets in the way of a camera. Our design (Figure 1), utilises the X layout.

Figure 3. Drone Free Body Diagram.
Equation 1. PID Loop Response
Figure 4. Drone PID Control Loop

ESC PWM Motor Control

To achieve stable flight, it is recommended that our PID loop be running at a minimum frequency of 100 Hz. We want our PWM control signal to be as fast as our ESC can handle, so standard out of the box PWM wont cut it.

Figure 5. Arduino Nano 33 IoT Header Pins (credit).
  • M3 — D5 (PA05): TCC0 CH 1;
  • M2 — D6 (PA04): TCC0 CH 0; and
  • M1 — D7 (PA06): T1C0;
PWM frequency = (frequency GCLK) / (N * PER + 1)
  • N = timer prescaler (can be 1, 2, 4, 8, 16, 64, 256 & 1024)
  • PER = value of the timer’s period register
Timer resolution = log(PER + 1) / log(2)
log = log to the base 10

The Nano 33 BLE and Portenta H7

The advantage we have with these two boards is that the Arduino Core sits on top of the Mbed OS. ARM Mbed OS is a real time open-source embedded operating system designed specifically for IoT.

Figure 6. Nano 33 BLE Pinout (credit).

Thrust Calculations

The completed Quad is shown in Figure 1. It weighs around 360g. From Newton’s 2nd law, we know that the effect of gravity on the drone is:

F = ma = 0.360 x 9.8 (acceleration due to gravity) = 3.528 N
Figure 7. Racerstar BR2205 Motor Specifications.

Correlating PWM Speed and Thrust

To correlate PWM duty cycle input to the ESC vs thrust, we have to measure it. The usual approach is to build a rig which holds the drone upside down on a weight scale. You can zero the scale and then as you adjust the PWM duty cycle, measure the grams of thrust on the scale.

Reefwing Software · iOS & Android Development · Robotics · #followback #iOS #Android #developer #indiedev #robotics #startup #arduino #raspberrypi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store