Experiments in Airplane AutoPilots

This is 3 graphs of experimental Pitch/Airspeed data created as part of an autopilot build. I created a set of controllers for an airplane autopilot, but was having a terrible time with transient response; every time I reset the altitude I was flying at, the airplane would drop it's nose, and it took time for the integral term to correct it. What I realized was that it was relearning how to fly every single time; I'm using the PID controller to set the desired pitch directly, not a gradual change in pitch, and the proportional term is not able to get anywhere close to the expected pitch for the response that I want.

So, I did an experiment! The autopilot worked fine in steady state with zero error, so I commanded it to fly at set test airspeeds, and once SHIP:AIRSPEED and SHIP:VERTICAL SPEED and SHIP:ALTITUDE were all steady to within a small margin, I logged the Speed and Pitch.

The graph shows 0.1 m/s resolution, (though the first test sequence, I did every 5 m/s). The above graph shows a 3rd order polynomial model, created using a least squares regression. Error went down significantly with the 3rd order, and further additions did not have significant reduction to the error. To put this back into KerboScript, I copied calculated coefficients into a new function, and when I input my plane's airspeed, I can rather accurately predict the pitch the plane needs to maintain altitude. The autopilot now pitches to that level, increasing or decreasing from there with a normal PID controller.

There's a weird error with the data itself at about 49 m/s; the airplane suddenly needs far less pitch in order to maintain altitude, and it's most apparent with the red error signal in the 3rd graph of the 3rd order system. Maybe creating a piece-wise predictive function is the best solution.

Increasing the test resolution from 5 M/S to 0.1 M/S absolutely increased the quality of the data, though the logging script crashes after ~200 values. A simple straight line predicts pitch to a maximum error of ~0.35 degrees, which looks large on the graph I uploaded, but honestly is pretty good compared to when my autopilot would drop in 50+ meters in altitude. A 2nd order fit had a maximum error of 0.046 degrees, which is negligible. The 3rd order had a high maximum error at 0.087, but lower error overall.

I guess the conclusion would be, it's rather helpful to estimate steady state pitch for autopilots; a 2nd order fit is perfectly fine; it's pretty cool to use KOS data in external programs. Next, I'm curious what pitch is required to maintain a planned vertical velocity at different speeds. When I pass in SHIP:AIRSPEED, and a desired vertical velocity, I should be able to predict the starting pitch.

Posted May 18, 2018, 6:30 PM UTC · edited

124
oblivion4•

So you got an initial pitch value via regression and the pid controller was able to figure it out from there. Neat!

What do you think the factors are that cause this kind of pitch error?

2
WinglessFluttersOP•

It ended up being a straight up face-palm moment, the error was due to fuel weight. My logging script had been crashing every so often, I think due to GoogleDrive interactions, so I had split the speeds into different flights. So speed X was at fuel capacity 90%, and then I'd revert and test speed X+1 with fuel capacity 99%. But thanks to that error, it's overall more accurate, now.

At least at relatively low pitch values, pitch was directly proportional to mass, so now the equation predicts pitch, corrects for mass, and then takes corrections from a PID. This was my final graph when I called it complete. I went through speed sequence forward starting at 100% fuel, reached top speed at 90%fuel, and the a decelerating sequence, completing at about 80% fuel. Not perfect, but accurate enough that it doesn't affect flying any more!


function PitchEstimator {

//calculated a polynomial through experimental data to estimate pitch based on aircraft speed.

//future upgrades: expand to Speed/VSI/Pitch

//returns estimated pitch, by pulling SHIP:Airspeed

set alpha5 to -1.62545956208591e-12.

set alpha4 to 3.04426727532867e-09.

set alpha3 to -2.20127445900764e-06.

set alpha2 to 0.000764213300451092.

set alpha1 to -0.129171332907072.

set alpha0 to 9.56140194962889.

return (SHIP:MASS/SHIP:WETMASS)MAX(alpha5SHIP:Airspeed^5 + alpha4SHIP:Airspeed^4 + alpha3SHIP:Airspeed^3 + alpha2SHIP:Airspeed^2 + alpha1SHIP:Airspeed^1 + alpha0,0.25).//CORRECTS PITCH FOR FUEL-MASS

}

1
oblivion4•

I meant why is the pid failing in the first place, but that's interesting too. I see you have plans to update it; do you think it would work for crafts that don't share the same properties as your test craft?

1
WinglessFluttersOP•

Oh, got it. It was using a linear Kp term to try to compensate for a non-linear relationship between Pitch and VSI. When I sent a VSI to maintain, the pitch it originally chose was wildly off, until the integral term caught up. For instance, leveling off, with a commanded 0 VSI initially gave a 0 desired pitch, which resulted in large altitude losses.

I have put two different craft into this autopilot, (without updating the predicted pitch), and they functioned, not perfectly, but better than they would have without it. A more general solution would be to feed the current pitch back into the system, so we're adjusting from our current state; outputting a required change in pitch, rather than an absolute pitch. However, that loses using the MaxOutput and MinOutput features.

1