Coding Your Own Flight Controller?

by nickrehm | February 11, 2021 | (5) Posted in Projects
What if I told you that you can code your own flight controller? dRehmFlight VTOL is a bare-bones, DIY flight controller intended for weird hovering VTOL aircraft. Other flight controllers work well, but are intended for specific vehicle configurations, like quadcopters. They're also filled with "fluff" features that many of us don't necessarily care about, which can clutter the code and make it seem nearly impossible to make any modifications for our unique projects. dRehmFlight VTOL takes care of the hard bits of getting a flight controller working, allowing you to make some simple code modifications that will let you fly nearly any type of RC aircraft.




What does a Flight Controller do, Exactly?

A flight controller comes in between your receiver and the motors or servos on your aircraft and does some computations to stabilize the vehicle. The physical flight controller is just a really fast microcontroller running some code, with an Inertial Measurement Unit (IMU) that tells the flight controller it's orientation and rotation rates in space. The receiver provides the flight controller with "setpoints," or desired angles or rotation rates, and the flight controller decides how to move the servos or throttle the motors to get the vehicle to do what you requested it to do. Do you need a flight controller? Not necessarily. Some vehicle types like airplanes are inherently stable and direct control from the receiver will let you fly just fine. A flight controller only becomes necessary when you try to hover, for example on a quadcopter or VTOL airplane.

The two parts of the flight controller we primarily care about are the PID controller, and the control mixer. The PID controller is the actual chunk of code that calculates values to stabilize the vehicle about each axis--roll, pitch, and yaw. It has tunable parameters known as "control gains" that let you tweak its behavior to get it running smoothly on different sized vehicles. The control mixer is where the stabilized values from the PID controller are "mixed" to the motors and servos. What does that mean? It means that we basically tell each actuator how it should respond to a roll, pitch, or yaw disturbance. For example, on a tricopter, the rear motor only needs to react to pitch disturbances in order for it to do it's job in stabilizing the vehicle.


Introducing dRehmFlight VTOL

dRehmFlight VTOL is coded within the user-friendly Arduino environment, and runs on the extremely fast and cost-effective Teensy 4.0/4.1 microcontroller with an MPU6050 IMU:

Teensy 4.0: https://amzn.to/3aZ5TrB

MPU6050: https://amzn.to/3rCcYFs

All of the difficult coding bits like interfacing with the receiver, getting the IMU data, and the PID controller implementation are all taken care of. You just need to plug in your receiver, tell the motors and servos what to do in the control mixer, and get to flying. There's plenty of freedom in configuring the outputs within the code, but even with no coding experience, you can be making meaningful changes for your unique project in no time. You'll need to solder up the physical flight controller (you can customize it to your needs, but this is the suggested default setup for maximum flexibility): 


The default code supports PWM, PPM, and SBUS type receivers, and has outputs for 6 motors using OneShot125 protocol (faster than PWM for better motor control) and 7 servos or motors using conventional PWM. The number of outputs can be increased for even larger projects with minimal changes to the default flight controller code. Here is a tutorial that shows you how to get your receiver hooked up to the flight controller:


The IMU is the physical device used to measure the orientation of the flight controller. This is used by the internal PID controller that calculates a stabilized output for each axis based on the error between the desired angles (or angular rates) and the measurements from the IMU. This video will walk you through the first-time IMU setup with dRehmFlight VTOL to make sure you have good measurements:


The cool thing about the bare-bones nature of dRehmFlight VTOL is that you won't have to sort through tons of unused features to understand the core of what's going on in the code. This will allow you to make changes and add features you want, giving a lot more creative freedom. This video walks you through the code to help familiarize you with the processes required within a flight controller to stabilize an aircraft, and explains a bit about how you can modify the code for your application:



Download the Code and Get Started!

The code and complete documentation .pdf is available on GitHub here: 

https://github.com/nickrehm/dRehmFlight

Need help or have questions? FliteTest Forum Support Thread: 

https://forum.flitetest.com/index.php?threads/drehmflight-vtol-teensy-arduino-flight-controller-and-stabilization.65261/ 

So, what are you waiting for? Go get started making a cool VTOL and modifying the flight controller code!


Make sure to check out these other articles of RC vehicles that use this same flight controller:

https://www.flitetest.com/articles/what-is-a-cyclocopter

https://www.flitetest.com/articles/radio-controller-starhopper

https://www.flitetest.com/articles/small-scale-spacex-starship-that-does-it-all


COMMENTS

DokCosmac on February 20, 2021
Thanks, this may come in handy on my next project!
Log In to reply
NYCBobby on February 27, 2021
Thank you so much for this great article. I'm in the process of replicating the Quad Rotor/FT Explorer that Ran D St. Clair posted some years ago. He used Open Aero Vtol. I'm close to finishing the project but you are definitely correct that the firm ware is like a black box. Randy understands it and has tried to fill in some of the gaps but it's not satisfying not knowing what it's really doing. I hope to try your suggestions on my next project. It would be nice to get GPS capability. Great work you are doing...keep it up!!!
Log In to reply

You need to log-in to comment on articles.


Coding Your Own Flight Controller?