Implementing a (rudimentary) autopilot

Hey all, I’m currently using simconnect to show where my plane is and what it’s doing on a webpage, but figured why not try my hand at implementing a rudimentary autopilot?

Implementing a LVL button was basically a one liner (just set the aileron trim to a sllightly dampened opposite of whatever our current bank angle is) but that’s where things stop being easy =D

I figured I’d try HDG mode first, but I tried both a DFA approach and a PID approach, and the former sort of works, but not great, and the second works so poorly that no amount of parameter tweaking is going to save what it does. Are there any tutorials or posts that cover this topic concretely rather than abstractly? (I can find lots of articles that explain “how an autopilot works” but none actually about implementing that. What polling rate you need, what parameters control what other parameters, etc).

As DFA I had a four state machine,

  1. banking
  2. coasting
  3. correcting
  4. level flight

where any time a new heading gets set, we set the aileron trim to 0.03 (with the sign based on whether we need to bank left or right), and then once we hit a bank angle of 20, setting it to 0 so that we can coast until we get near the target heading, then switch to correcting which sets the trim to 0.03 in the opposite direction to decrease the bank, until we get to the desired heading at which point we switch to level flight.

(With some shortcuts to go from banking to correcting if the diffrence between current/target is too low, and from banking to level flight if our rate of turn is low and we’re almost at the target)

That works (sort of… well enough to pretend it works at least) but only for large heading changes.

I also tried a PID with two controllers: an outer controller based on getting the difference between current and target heading to zero, used to control the rate of turn. That, in turn, uses an inner controller that sets trim values. The problem with this is that the PIDs have no idea how to “slow down before we get there”, they just overshoot by a wild amount, and no amount of parameter tweaking yields a curve that doesn’t first grossly overshoots the target (way more than the DFA does) and then oscillates around the target heading in a way that would make both passengers and pilot quite sick.

Any tips for someone getting into the more serious parts flightsim coding?

Do you know how often your PID is run? Is it every frame? As I recall, the sim manager is periodically tasked. It assigns a priority and skips a number of frames between calls. It might explain the overshoot. I assume you already knew this, though.

The only additional suggestion I might have would be to add more states to your state machine. This might help the “slow down before we get there” issue. This might be used, for example, to make the PID work in the opposite direction when within a certain distance.


It was running every half second, because it’s not running as an addon but using the python simconnect bridge. I spent some more time trying to tune things but ultimate abandoned PID, as well as a state machine, for stepwise “correct away from the wrong direction” instead. The naive version of which still leads to moderate overshoot, but with code that is easily extended to get rid of them.

I did a hopefully fun enough write-up over on Flying Planes with Python and JavaScript | are-we-flying that ends with the naive code, and suspect that’ll get at least one followup where the code gets updated to have zero overshoot, as well as some ridiculous but highly fun things like having the autopilot still work even when the plane is inverted… some tests with the Gee Bee R3 Special are absolutely hilarious.

(Although for an acrobatic plane, the half-second update interval does make the plane correct in a “clockwork” fashion, since the changes kick in so fast. It looks silly and would feel pretty uncomfortable, but then sticking an autopilot on an acrobatic plane is basically “we’re not going for realism here, just video game fun” territory =)

What an incredibly fun and well written site! Going to study it in more depth!