Recently I developed an autopilot system that was capable of taking off and flying toward a target using a relatively complex funky tree. I wanted to provide a breakdown of how it works so others can make their own autopilot builds.
Ok tbh nobodys actually gonna read all of this so im just doing it for fun :)
First, I'll show a breakdown of the FT commands using proper indentation to make them more clear.
Elevator
smooth(
for manual mode
Pitch
Activate1 turns on loop, equivalent of Activate1 ? * : 0
+clamp01(Activate1)*(
1
0-abs(RollAngle / 90) * 0.5
extends elevators while turning
all other functions have set endpoints via a clamp, can be changed
+clamp(
2
(Altitude - 3000 - VTOL * 2000) / 100
Sets approximate cruising altitude (VTOL for further adjustment) full extension during takeoff
3
, -0.2,0.2)
+clamp(
(PitchAngle)/100
smaller adjustments to help moderate PitchAngle during flight
4
, -0.2,0.2)
+ 0.3*clamp01(PitchAngle-15)
One-way clamp, larger adjustment to moderate PitchAngle during takeoff
)
, 1)
Lets try to understand all of that. The comments provide a short summary of each part in the code. However, what is important is how they all function together.
First of all there are a couple of functions to stabilize the aircraft during takeoff and flight. Let's analyse each of these functions in each of their roles.
This plane is designed so it can be launched from midair as well as from the ground. That means there is not be a desperate code for the takeoff, so the same functions are used for both takeoff and flight, making the system much more versatile. (refer to the each labeled function in the comments)
Takeoff
1. The aircraft will have a rollAngle of 0 during takeoff. This function is negligible.
2. This is the main function used during takeoff. When lifting off, this function is the only one with a noticeable role, and it will raise the elevators to bring the aircraft off the runway and into the air. Once the aircraft reaches cruising altitude, it will bring the plane out of the climb and into the cruise.
3. Although this function moves a good bit during takeoff, it still does not effect the flight pattern much. Its only role during takeoff is to help function 4 and smoothen the flight once it reaches the set 15-degree limit.
4. Function 4 is important as it is the only function that is meant solely for takeoff, and it puts a 15 degree fly-by-wire limit on the pitchAngle so that the plane does not try and fly straight up. Unlike the roll technology, function 2 does not set angle endpoints, only elevator endpoints; therefore, without the function 4 limiter, function 2 would increase the pitchAngle too far.
Cruising/Turning
1. Function 1 only works in conjunction with the aileron, similar to the rudder. Both this function and the rudder's only function help assist the turn. Evidently neither of these functions are very effective as the plane turns quite slowly, but increasing their endpoints will make the plane increase altitude while turning, which is not what we want.
2. Function 2 works to maintain a set altitude during the cruise and adjust if needed. This function works amazingly well and can keep the aircraft set at an altitude even in heavy winds. It resembles an autopilot function ALT used in normal planes.
3. Function 3 resembles the PITCH autopilot function and assists function 2 in keeping the plane flying straight.
4. Function 4 is set to 0 during the cruise.
phew
On to the ailerons, I guess...
Roll
sets endpoints for one HUGE funtion
+ clamp01(Activate1)*
clamp(
(RollAngle- Function within a function, whatever results past this = the angle of the turn
Takeoff algorithm, very annoyed I had to put this in because it requires you to start the flight immediately after spawning in
smooth(
Time < 45 ?
atan Longitude/Latitude allows you to calculate target heading using trig
0 :
-clamp(
atan(
Contains presets for stuff, to add preset add -clamp01(ActivateX)LongitudeOfLocation
(Longitude-clamp01(Activate2)*4275-clamp01(Activate3)*10128)
same idea except latitude
/(Latitude-clamp01(Activate2)*131224-clamp01(Activate3)*(8257+8*Time))
Subtract current so that function reduces this to 0
)-Heading
45 means it turns 45 degrees, i recommend changing this for other aircraft
,-1,1)
,0.5)
*45)/90
Endpoints for aileron throw``
,-0.3,0.3)
As you can see instead of four main functions like the elevators, these have only one main function with an embedded function inside of it. This makes it a bit simpler than the elevators. The main function here compares the embedded function to rollAngle
. Therefore, the main function simply sets the output of the embedded function to the rollAngle of the aircraft, allowing it to turn in the proper direction.
The meat of this tree is in the embedded function. In its simplest form this function is just -clamp((Input heading)-Heading)
(ignoring endpoints for simplicity) and it basically functions again to allow Input heading to approach Heading. Most of the flight tests were done by inputting a random heading in place of Input heading and seeing if it goes that way. Let's take this a step further.
We now have expanded the function to -clamp(atan(Longitude/Latitude)-Heading)
. Using this function, we use the trig rule of -atan(y/x) to find the angle away from the origin, and then invert it once again to find the angle towards the origin. Using Longitude and Latitude means that it will find the location of the plane in the normal coordinate plane, with coords 0,0 as the center of this plane (you'll see when this changes in a minute)
Lets expand this further by allowing us to shift the coordinate plane using -clamp(atan((Longitude-a)/(Latitude/b))-Heading)
. In summary this will shift the coordinate plane by (a,b) and instead of having coords (0,0) as the origin, the origin will be at coords (a,b). Since the plane always flies toward the origin of its coordinate plane, it will now fly to (a,b). In order to adjust a and b during flight, I tied them to activation groups; therefore, to create your own preset, you can put -clamp01(ActivateX)*a
after Longitude and -clamp01(ActivateX)*b
after Latitude, assuming a and b are the longitude and latitude respectively of your target and X is the group you tie it to.
I know that is probably really confusing but I hope you get the gist. There are some small tweaks I didn't really cover in this tutorial because they are pretty simple and self explanatory.
Enjoy simpleplanesing!
-Ultimate
I will have to try this! I recently got the rudder to turn the plane towards a point on the map and fly their but this is next level!
are these supposed to be applied on wings or rotator?
@ProjectVideoGame i'm not sure what you mean, but if you want the source code to use in your own build these are the same inputs used in project aurora so you can download that and copy them from there.
@UltimatePlayer123, can you simplify? like remove the description of that bit of code into a single line of code
You know, this is really damn useful. Thank you!
@Sleet01 yeah really would be great
@rexzion It would be nice if there were a link to the markdown guide.
@UltimatePlayer123 gotta say. I need this. Thank you very much for making this. I'll try my best building one. Thank you again and have a nice day sir :D
you should add chonkier titles and separate things
so it's easier to read
@PoyuToyu