284 points · by TheRealPomax · 2y ago · 64 comments
Note that this is a complete do-over of a previous page that used a combination of Python and JavaScript, and only covered basic autopilot functionality. This rewrite threw all of that out, and goes much, much further:
you just want to have the plane take off without having to do anything? Check, this tutorial covers auto-takeoff.
Flying with waypoint-based navigation, not by "programming airports and radio beacons one letter at a time with a jog-dial" but by just placing markers on a map and dragging those around as you please, mid-flight, Google Maps style? Check. After all, why would we not, putting a map on a webpage is super easy (barely an inconvenience).
In fact, let's throw in flight plan saving and loading, so others can fly the same route!
And maybe you want the plane to fly a fixed distance above the ground, instead of at a fixed altitude... well: check. You'll learn about terrain-follow using free Digital Elevation Model datasets for planet Earth.
And of course the big one, for the ultimate "click button, JS flies the plane start to finish" experience: ever wished the in-game autopilot could just land the plane for you? Well, it's a video game, and we know programming, so: why not? We're also implementing auto-landing and it's going to be amazing.
If you play MSFS, and you're a developer/programmer, and you've always thought how cool it would be if you could just do some scripting for it... you can, there's a lot of cool stuff you can do!
And of course if you don't want to follow along and just want to have everything set up and ready to fly your plane, just clone https://github.com/Pomax/are-we-flying and have fun.
Honestly would never see something like this writeup from MSFS add-on developers. They are a protective bunch who share nothing with each other. This is in contrast to X-Plane add-on developers, who are friendly and collaborative.
Today MSFS is constantly updated with new functionality, so you can just focus on your project, but old attitudes remain.
We're going to get things wrong, planes are going to crash in myriad ways, and we'll discover how and why things go wrong, to hopefully figure out how we can navigate those problems. And we're going learn from our mistakes.
Including learning to throw away all the work we did because that's the right thing to do =)
https://austinsnerdythings.com/2021/10/15/creating-an-autopi...
Anyways, I chose to go with PID control for my control systems, which worked really well right "out of the box" with some somewhat generic values for P, I, and D.
I read/skimmed all the way through your post. It looks like your control system is more or less a P & D controller? Any reason you didn't just go with PID loops? Is this part of needing things to be stateless to work for JS (I am no JS expert, just saw a few mentions of that in the conclusion)?
Someone could, if they wanted (as one of the "what's left" options =D) replace all of this with PD code but they'll probably need to run at a much higher data rate for things to actually behave, and optimize the SimConnect calls to get the dataframes necessary without tying up a thread.
Last month it was radar [1], this month navigation!
Can you extend the tutorial to cover positioning from the elevation data? :-)
I only skimmed over it now, so maybe I missed it: How do all these developed methods compare to existing real autopilot systems? Are there even existing autopilot systems which work similarly? Did the author come up with these methods just by themselves, or were they adapted from existing systems? Would those developed methods make sense in a real autopilot system?
Also, it seems that this involved a lot of trial-and-error, and the solutions come with a number of heuristics and hyper parameters to tune. I wonder if you can do better and avoid that as much as possible.
First of all, you would need to have an actual metric which covers all the things you would care about. Obviously no fatal crash, but then also maybe somewhat smooth flight (you can measure that) and how close you want to get to each waypoint, while maybe taking not too much fuel. And given those, let it find the best possible path. I guess this is optimal control theory then. When you have an actual metric which you can measure, then you can quantitatively compare all the methods, also automatically optimize any parameters.
Autopilots depend a lot on which kind of plane it is in, but for the most part, autopilots aren't really "auto pilots". They consist of various fairly basic primitives rather than more complex decision making.
As a rule of thumb, the complexity of the autopilot is proportional to the size of the plane.
Basic autopilots can maintain headings, maintain (barometric) altitudes (i.e. above sea level) and similar very basic operations. For these autopilots it is the pilot's job to ensure the plane is operating within the envelope and does not depart from controlled flight and the throttle is controlled by the pilot.
As an example, the autopilot in an F-16 fighter jet has four things it can do: maintain attitude, maintain altitude, fly toward nav point and fly a compass heading. That's it.
Autopilots in airliner jets are more complex and can handle a set of nav point, ascent/descent, arrival and, depending on the airfield, approach and landing. They also feature autothrottles. Their implementation is still fairly simple, since it's a matter of matching throttle, attitude and heading with the position and altitude of each nav point. The autopilot doesn't have to do any pathfinding or more complex decision making, in order to keep it simple, predictable and bug free.
Then, aren't regional cultural ethics necessary to solve a trolley problem; when there are multiple simultaneous predictable accidents and you can only prevent one.
Does "autopilot" solve the trolley problem for the liable pilot?
Military drones are a different matter: you will never find yourself in the same air space, because you will never be told they're there. They are ghosts, much like airplanes that run without a transponder "because it's legal". You don't see another aircraft unless you know exactly where to look, or you're right on top of them.
GPS is usually not used for altitude.
The most important thing is that everyone in an area reference the same altitude/altimeter setting for traffic avoidance. Whether or not that is off from the actual altitude by a few hundred feet or not is secondary, since ground avoidance typically uses a lot bigger safety margins. Airliners and military planes have radar altimeters to measure height above ground when flying in IMC (low vis), while smaller planes typically fly VFR (based on visual rather than instruments).
You can! However, that's basically another 100 pages of "and now let's throw all this code away again, because it's time to switch to self-tuning PD controllers with a drastically decreased polling interval".
Really an autopilot is a combination of as "super dumb, but effective" control processes (the dumber the better, because less code equal less bug equal more alive) that are ideal for PD control, but that does require running the control loop as close to "real time" as you can, whereas we started with "let's not bog down the system, and run twice a second" (because MSFS is programmed in a, let's call it "special" way, and it's basically always CPU bottlenecked)
So... exercise for the reader right now =D
[1]https://www.flightgear.org/ [2] https://wiki.flightgear.org/Portal:Developer
"wasn't that the guy where I got my baguette recipe from?"
goes searching
"it WAS! beautiful. Then this guide here surely will also be fun"
(I need a better nav menu solution for mobile, at the very least)
Could I ask you to file an issue about that over on https://github.com/Pomax/are-we-flying/issues ?
SpaceX: Hold my beer (https://os-system.com/blog/javascript-in-space-spacex-devs-h...)
Heck, for a long time - decades, even - the principle 'interface' to the rocket systems being used to regularly launch humans to space was, literally, a stick.
Javascript seems like a huge upgrade from a stick. ;)
_Fantastic_ tutorial!
You need to get aaaall the way of my back about why flying planes with JS sounds like a bad idea :-)