We all love drones, and we love to just buy one and go outside and make it fly by itself, this is great. But what is actually going on inside the drone?

In this post I'm going to explain a bit how a loiter controller works, with the difference is that I'll show my controller, share the python code and that I'm using a motion capture system inside my lab. The great MAST Lab.

First things first, you can check the code here.  And secondly, I need to explain our setup. We are using Altax Flight Stack which is a tuple of computers connected with each other and sharing information. The flight controller is a very cheap naze32, running baseflight (but cleanflight will work as well), and the companion computer is a Raspberry Pi (any version will do the work...). The entire script does not consume too much CPU.


The connection diagram is showed above, the motion capture system is connected to a desktop computer and this computer sends the mocap data and the joystick information via a common wireless network (UDP), this information is used by the raspberry pi to know the position and attitude of the vehicle, then the rpi calculates a set of commands (roll angle, pitch angle, yaw rate and throttle) using simplistic PID controllers and then it sends the information to the flight controller.

This outer loop control is working at 100hz, but it can be configured to go slower.

Important to notice that we have crashed lots of times when starting to test/debug this system. Most of the crashes are due to the desktop computer "hanging out"... then the vehicle stops receiving the information and will keep the last command. A auto-landing feature is needed, this feature will be added on version 2.

In the part of the control, we are using angle mode on the inner loop (naze32) and then we calculate the appropriate angle commands (pitch and roll) from desired accelerations (outputs of the controllers) to make the vehicle hold the commanded position in the space. The most important part of the code is when we calculate desired angle commands from the desired accelerations coming from the PID controllers:


And the proper math:


The rest of the code is just to deal with data, vehicle and make everything work on threads. One thread for the control and another for receiving the data. The code is extremely easy to understand and to tweak (I hope...). With this setup, the joystick is the one that activates the automatic behavior, if the proper switch is on manual, then you will be able to fly the vehicle using the joystick. This is by no means the same technique used by Pixhawk in loiter mode. But perhaps is a nice way to start learning about flight modes (and controlling aerial vehicles) so that then you can learn how advanced flight modes developed by the team of PX4 and ArduPilot work.

With a couple of changes, this same script and controllers should work with dronekit+pixhawk... I'll try that soon.

You can see the post here: http://altax.net/blog/hover-controller/

Views: 3373

Comment by Hugues on February 20, 2016 at 1:14pm

Cool, thx for explaining.

Comment by John Dennings on February 20, 2016 at 1:40pm


You mention 18 cameras used for motion capture, apparently Optitracks?  What model to you use?

Comment by Aldo Vargas on February 20, 2016 at 1:50pm


Sorry for not give too much detail about the motion capture system... we have an 5 year old optitrack system, with 18 Flex-3 cameras. 

Comment by Hector Garcia de Marina on February 22, 2016 at 5:46am

Sorry for the off-topic, but when I saw you in the video I recalled this scene from Simpsons


If I were you, I would not "disturb" the quadcopter in that way.

Comment by Aldo Vargas on February 22, 2016 at 5:53am

haha good one!

Actually the disturbance is a good measurement that will help then to re-adjust the gains for the controllers, I have done this (approaching to the vehicle) lots of times before doing this video, and believe me, I wouldn't do it I do not trust the controller, but in this case I trust my stuff ;)

That being said, I have been cut and sliced several time on failures (therefore googles...) 

Comment by Hector Garcia de Marina on February 22, 2016 at 6:07am

Hi Aldo,

I am not saying that your system and controllers are bad/good :P. In fact I like to see people broadcasting knowledge in the way you do!

On the other hand, after seeing many accidents in more than ten years flying. I would never approach myself to working propellers without any kind of safety. Think that one ESC or motor can die for whatever reasons, then the other three will flip the quadcopter to you, and an eye (googles :P) or finger can be seriously cut off.

Comment by Patrick Poirier on February 22, 2016 at 6:09am

Hello Aldo,

Very good tutorial, just like your blogs, thanks for sharing.

Unfortunately I cannot see your pictures here (diagram - desiredd angle -math) be on your web yes.


Comment by Aldo Vargas on February 22, 2016 at 9:04am

Thanks Patrick!! :D

Comment by MUTHU AERO on March 2, 2016 at 4:28am


Comment by Phi Vu on September 2, 2016 at 11:54pm

Can I know the "filter_bandwidth" type that you use in your code?


You need to be a member of DIY Drones to add comments!

Join DIY Drones

© 2020   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service