• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FS2004 Autopilot PID Variable Calculation

Messages
260
Country
us-kentucky
Hello all,

In my pursuit of the perfect 747, I have decided to tackle the incredibly difficult challenge of making my own autopilot.

With the flight dynamics I have created, the default autopilot features do not work. The default autopilot is expecting roll damping, pitch damping, etc. values that are in excess of 2 - 10 times what they are in real life, and thus I get horrific oscillations with the default autopilot.

As such, I need to make my own. However, FS9 does not provide variables for acceleration, only velocity. I am not sure how, within the constraints of an XML gauge, I can accurately calculate acceleration. Once I can get that, I can attempt to set out creating the PIDs for the autopilot modes.

Chris
 
I think you should be able to adjust the FS2004 autopilot through the parameters available in aircraft.cfg. I never did it but I think it can be done. If you still want to develop your own autopilot, FS9 provides variables for acceleration, but they are only accessible in C/C++.
 
You have everything needed to get acceleration (physics wise), velocity delta (new velocity - old velocity) & time delta. (time delta = 1 / 18, 1 gauge read when gauge is at 18hz)
This should work..
Not sure exactly which var to use, this is just an example & not tested -
XML:
<!-- GVar1 holds the old velocity value from previous gauge read -->
(A:Velocity body X, m/s) s1 (G:Var1) - 0.055555555555 / (>L:My_Accel_X, number)
l1 (>G:Var1)

Ofc, as Eric mentions, using the aircraft.cfg & .air file to dial it in would work too.
 
Last edited:
You have everything needed to get acceleration (physics wise), velocity delta (new velocity - old velocity) & time delta. (time delta = 1 / 18, 1 gauge read when gauge is at 18hz)
This should work..
Not sure exactly which var to use, this is just an example & not tested -
XML:
<!-- GVar1 holds the old velocity value from previous gauge read -->
(A:Velocity body X, m/s) s1 (G:Var1) - 0.055555555555 / (>L:My_Accel_X, number)
l1 (>G:Var1)

Ofc, as Eric mentions, using the aircraft.cfg & .air file to dial it in would work too.

What would be the formula for this in Fps?

I have tried changing the cfg values to no real avail. I can get rid of the oscillation but only by using very small values that cause the autopilot to respond very slowly.
 
Just change the A:Var's units suffix from m/s to feet per second or feet/second.
(A:Velocity body X, m/s) to (A:Velocity body X, feet per second) or (A:Velocity body X, feet/second)
The rest of the code remains the same.
 
Just change the A:Var's units suffix from m/s to feet per second or feet/second.
(A:Velocity body X, m/s) to (A:Velocity body X, feet per second) or (A:Velocity body X, feet/second)
The rest of the code remains the same.

I get values that fluctuate quite a bit using this formula. They do seem to be responding to the correct velocity though. I am using velocity y to determine the pitch acceleration. The values do seem to correlate, but fluctuate quite greatly and go to zero when paused.
 
This "may" work to make it less extreme. Use the Update attribute "Frequency" ( an integer, 1 thru 18 ) to adjust sample rates. A pause check is also included.
It should be in it's own gauge and the code must remain in an Update section.
Acceleration will always fluctuate. If you pull up and keep a constant acceleration the end result will be a loop.
XML:
<Gauge Name="My_Acceleration_Vars" Version="1.0">
    <!-- adjust the frequency attribute below as needed -->
    <Update Frequency="1" Hidden="Yes">

        <!-- G:Var4 has 2 purposes, 1, as a 'find if paused' register and 2, as part of the delta time for acceleration formula -->
        <!-- find out if paused -->
        (E:Absolute time,seconds) (G:Var4) != if{
            <!-- GVar1, 2 and 3 holds the old velocity value from previous gauge read, s4/l4 carries the time delta throughout -->
            (A:Velocity body Y, feet/second) s1 (G:Var1) - (E:Absolute time, seconds) (G:Var4) - sp4 / (>L:My_Accel_Y, number)
            l1 (>G:Var1)
            (A:Velocity body X, feet/second) s2 (G:Var2) - l4 / (>L:My_Accel_X, number)
            l2 (>G:Var2)
            (A:Velocity body Z, feet/second) s3 (G:Var2) - l4 / (>L:My_Accel_Z, number)
            l3 (>G:Var3)
        }
        <!-- update the 'find if paused' register (delta time too) -->
        (E:Absolute time,seconds) (>G:Var4)

    </Update>
</Gauge>
 
Last edited:

Should work in FS9 as well. Note that you're extremely limited by the 18 Hz gauge refresh rate.
Also note my posts below regarding writing to trim.
 
This "may" work to make it less extreme. Use the Update attribute "Frequency" ( an integer, 1 thru 18 ) to adjust sample rates. A pause check is also included.
It should be in it's own gauge and the code must remain in an Update section.
Acceleration will always fluctuate. If you pull up and keep a constant acceleration the end result will be a loop.
XML:
<Gauge Name="My_Acceleration_Vars" Version="1.0">
    <!-- adjust the frequency attribute below as needed -->
    <Update Frequency="1" Hidden="Yes">

        <!-- G:Var4 has 2 purposes, 1, as a 'find if paused' register and 2, as part of the delta time for acceleration formula -->
        <!-- find out if paused -->
        (E:Absolute time,seconds) (G:Var4) != if{
            <!-- GVar1, 2 and 3 holds the old velocity value from previous gauge read, s4/l4 carries the time delta throughout -->
            (A:Velocity body Y, feet/second) s1 (G:Var1) - (E:Absolute time, seconds) (G:Var4) - sp4 / (>L:My_Accel_Y, number)
            l1 (>G:Var1)
            (A:Velocity body X, feet/second) s2 (G:Var2) - l4 / (>L:My_Accel_X, number)
            l2 (>G:Var2)
            (A:Velocity body Z, feet/second) s3 (G:Var2) - l4 / (>L:My_Accel_Z, number)
            l3 (>G:Var3)
        }
        <!-- update the 'find if paused' register (delta time too) -->
        (E:Absolute time,seconds) (>G:Var4)

    </Update>
</Gauge>

It looks as though you accidentally typed Gvar2 in a place where Gvar3 should be. In any case, with that corrected, this seems to work really well for X and Z, but returns extremely tiny values for Y. Works very well with an update frequency of 6.

I changed it to look like this. This code returns what looks to be expected values, but more research is needed. :stirthepo

XML:
<Gauge Name="My_Acceleration_Vars" Version="1.0">
    <!-- adjust the frequency attribute below as needed -->
    <Update Frequency="6" Hidden="Yes">

        <!-- G:Var4 has 2 purposes, 1, as a 'find if paused' register and 2, as part of the delta time for acceleration formula -->
        <!-- find out if paused -->
        (E:Absolute time,seconds) (G:Var4) != if{
            (E:Absolute time, seconds) (G:Var4) - sp4
            <!-- GVar1, 2 and 3 holds the old velocity value from previous gauge read, s4/l4 carries the time delta throughout -->
            (A:Velocity body Y, feet per second) s1 (G:Var1) - l4 / (>L:My_Accel_Y, number)
            l1 (>G:Var1)
            (A:Velocity body X, feet per second) s2 (G:Var2) - l4 / (>L:My_Accel_X, number)
            l2 (>G:Var2)
            (A:Velocity body Z, feet per second) s3 (G:Var3) - l4 / (>L:My_Accel_Z, number)
            l3 (>G:Var3)
        }
        <!-- update the 'find if paused' register (delta time too) -->
        (E:Absolute time,seconds) (>G:Var4)
    </Update>
</Gauge>


Should work in FS9 as well. Note that you're extremely limited by the 18 Hz gauge refresh rate.
Also note my posts below regarding writing to trim.

As much as I would love to use this, FS9 does not have variables for Acceleration Body X/Y/Z, only velocity. Unless I've missed something, of course... But my intention is to use a PID much like this as soon as I can calculate the acceleration, which is what I'm trying to do here.

EDIT: I have confirmed that Roman's code as modified does work for acceleration, so I am now on to implementing it. I have the controller working to change elevator in response to speed differences. Now to figure out how to limit the pitch, get rid of oscillations, and adjust pitch trim.
 
Last edited:
So some preliminary testing has yeilded...

Oscillations. Lots of really bad, huge oscillations. The airplane doesn't outright lose control, but it is pitching way too high to find the speed target for Bjoern's speed mode script, then pitching way down as the speed goes well below the target.

I am 100% confident in my flight dynamics, so I am not adjusting any numbers there.

I am not even going to pretend to understand PID controllers, and I will do some more research into them for sure, but some guidance on where to begin with these in relation to Flight Sim would be most helpful.

I do have this document: https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19730001300.pdf

On page 567, it lists the different autopilot pitch modes and gives values for the gain on each one and some integrator values. I have no idea if this is relevant to the discussion at hand, but once again a nudge in the right direction would be very appreciated.
 
Well..
Sir Isaac Newton is rolling in his grave.
Flight simulator has broken the rules of physics and has created time travel. o_O :rolleyes:
For validation I created a gauge, in fsx, to compare the computed acceleration values versus what the sim gives. :stirthepo
It is close.. :banghead: I believe it still "may" be of use to you. I think it is the timing of when the sample of either is taken.
The code:
XML:
<Gauge Name="My_Acceleration_Vars" Version="1.0">

    <Image Name="bkgd.bmp"/>

    <!-- adjust the frequency attribute below as needed -->
    <Update Frequency="18" Hidden="Yes">

        <!-- G:Var4 has 2 purposes, 1, as a 'find if paused' register and 2, as part of the delta time for acceleration formula -->
        <!-- find out if paused -->
        (E:Absolute time,seconds) (G:Var4) != if{
            (E:Absolute time, seconds) (G:Var4) - sp4
            <!-- GVar1, 2 and 3 holds the old velocity value from previous gauge read, s4/l4 carries the time delta throughout -->
            (A:Velocity body Y, feet per second) s1 (G:Var1) - l4 / (>L:My_Accel_Y, number)
            l1 (>G:Var1)
            (A:Velocity body X, feet per second) s2 (G:Var2) - l4 / (>L:My_Accel_X, number)
            l2 (>G:Var2)
            (A:Velocity body Z, feet per second) s3 (G:Var3) - l4 / (>L:My_Accel_Z, number)
            l3 (>G:Var3)
        }
        <!-- update the 'find if paused' register (delta time too) -->
        (E:Absolute time,seconds) (>G:Var4)
         
        (A:ACCELERATION BODY X, Feet per second squared) (>G:Var5)
        (A:ACCELERATION BODY Y, Feet per second squared) (>G:Var6)
        (A:ACCELERATION BODY Z, Feet per second squared) (>G:Var7)
     
    </Update>
   
   
    <Element Name="DISPLAY">
        <Position X="15" Y="80"/>
        <FormattedText X="446" Y="180" Font="Arial" FontSize="34" LineSpacing="50" VerticalAdjust="Center" Color="Red" Bright="Yes">
            <String>
              %\{tabs=0L,1L,291L}\t%((G:Var5))%!7.5f!%\t%((L:My_Accel_X, number))%!7.5f!%\n
              %\{tabs=0L,1L,291L}\t%((G:Var6))%!7.5f!%\t%((L:My_Accel_Y, number))%!7.5f!%\n
              %\{tabs=0L,1L,291L}\t%((G:Var7))%!7.5f!%\t%((L:My_Accel_Z, number))%!7.5f!%
            </String>
        </FormattedText>
    </Element>
       
</Gauge>
I even tried using the rotational velocity vars, it is worse.
The gauge was running at 18 hz (to match to internals versus calc'd) but the video was slowed down to 5.5% of original speed to see the differences.
You may see the video here.

For FS9 another way you may be able to get it is thru fsuipc --> lua --> lvar --> your gauges.
 

Attachments

Something to be aware when using VELOCITY BODY, VELOCITY WORLD A:Vars -

Acceleration determined by the cycle-to-cycle change in A:VELOCITY BODY will yield some large oscillations itself.

Consider the following LOGGER recording (P3D v4 was used, but the points related to VELOCITY BODY A:Vars in FS9 are the same). These data were all recorded at 18 Hz.

- A shows A:VELOCITY BODY X and Y. The charts' Y axis is Velocity and the X axis is Time. Smooth looking curves.

- B shows the change in velocity from the current gauge update cycle to the previous divided by absolute time difference cycle-to-cycle. It's the same acceleration calc as in the XML above. There are very significant oscillations.

- C shows A:ACCELERATION BODY X and Y. These vars are not available in FS9, but I include it just for comparison to the change in velocity chart above it.


1596139038175.png


You might consider an average of three samples or use of a cycle counter to take a VELOCITY BODY sample every ~3 to 4 update cycles. Not sure about the latter, but the former should smooth things out.

Bob

[edit] - a little more detail... here is a blow up of the calculated Acceleration Body Y. The oscillations have a period of 3 update cycles (the numerator drives the big variation, not the denom). I didn't try varying the update frequency, e.g. <Update Frequency="12"> but it would be interesting to see if such a change settles things down. Hope so. I ran into a related issue some years ago when using GeoCalcDistance differences cycle-on-cycle @ 18 Hz. At the time, Tom Aguilo postulated that the aircraft had not moved sufficiently far from one cycle to the next to get stable distance subtractions. A sampling issue, as Roman mentions, above.

1596144030181.png


Chris, if you want to make similar recordings as you tweak PID constants (you appear to be a fellow excel user), download LOGGER here. Although it is bundled with XMLTOOLS, XMLTOOLS doesn't work with FS9. The original version of LOGGER does, however.

Even if you get these acceleration curves to settle down, tweaking PID constants will still be tedious.
 
Last edited:
As much as I would love to use this, FS9 does not have variables for Acceleration Body X/Y/Z, only velocity. Unless I've missed something, of course... But my intention is to use a PID much like this as soon as I can calculate the acceleration, which is what I'm trying to do here.

Ah, I did not know that.



Oscillations. Lots of really bad, huge oscillations. The airplane doesn't outright lose control, but it is pitching way too high to find the speed target for Bjoern's speed mode script, then pitching way down as the speed goes well below the target.

That's where the long and painful tuning phase begins.

But I've written all of that down in the post I've linked to.
 
The oscillations have a period of 3 update cycles (the numerator drives the big variation, not the denom)
Bob.. Good catch - Thank You..
If one ever looks at the stock, unaltered GPS, the update frequency is 6. Perhaps there is a solid reason for this. :scratchch
Ran 2 more tests (both @ 6 hz), 1 with the delta time calculation & 1 with a fixed delta time @ 0.1666666667 - the latter proved to be less "spikey".
Much, much better! I believe with a finely tuned PID these outputs will work fine.
 
Last edited:
The tuning part is something I need to look into more for sure.

I did discover one problem though, the outputs for acceleration that I was using needed to be multiplied by -1 to work with Bjoern's formula. Doing so evened out the oscillations considerably. So I am on the right track! Maybe one day I'll actually have a completely working autopilot. :eek:
 
The more I look into and think about this, the more I am losing hope of being able to do this well with XML. Mostly because of trim. Unless I really am missing something big here.

I have the oscillations under control to a great degree using Bjoern's code (thank you!) and it almost behaves well now. But the more I get into this autopilot development, the more I'm realizing "I can't do that with XML, etc. etc."

To try to keep things short with a simple discussion, the 747 has pitch damping, roll damping, and yaw damping values of -22, -0.4, and -0.24 respectively at low angles of attack. These numbers do not change very much with mach, and only roll damping changes very much with angle of attack. Source: https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19730001300.pdf

The default flight simulator autopilot functions are built around airplanes that have extremely fudged numbers for these values, and as a result using the default autopilot with realistic numbers results in some heavy oscillations, especially in roll and yaw. For example, the default Boeing 737 in FS2004 has pitch damping, roll damping, and yaw damping values of -214, -4, and approx -0.83 (corrected for a bug in the FDE of FS2004). These values are 10.3, 10.0, and 2.9 times what my realistic values are for the 747, and they aren't going to change that much between one Boeing jet transport and the other. I suspect that it is these extremely fudged numbers that cause oscillations with the default autopilot if realistic numbers are used: the default autopilot is expecting these values to be much, much higher than they realistically are.

I have tested this by equipping my 747 with the default 737-400 autopilot, and adjusting the following scalars:

pitch_stability= 1.0
roll_stability= 1.0
yaw_stability= 1.0

If I set the roll and yaw scalars to as little as 3 - 5, the results are astonishing: the default autopilot almost functions without roll oscillations! The major problem then is pitch oscillation, but only at high altitude. Get this, I had to set this scalar to a whopping 9, that's right, 9! And as such, I had to adjust the aileron, elevator, and rudder effectiveness scalars to maintain controllability, but then I realized that this was also throwing off all of my other carefully tuned stability and control derivatives responsible for dihedral effect, stall and spin effects, etc. The whole thing became like flying on rails just to make the autopilot work. Unacceptable!

I tried setting the pitch and roll velocity and acceleration limits in the aircraft.cfg to many different combinations, some of which were entirely unrealistically slow to respond, but nothing I tried would make the oscillations stop, especially pitch at high altitude.

This is the reason I cannot use the default autopilot.

However, it would seem more and more that an XML autopilot will not suffice either. Just the idea of coming up with a way of setting the trim of the airplane accurately using XML and Key events sounds like a nightmare. With all of the different CGs, flap setting effects, mach effects, I just can't imagine how to do this properly, not to mention imposing the proper limits and schedules on each separate mode of the autopilot.

How well does C++ handle these sorts of things? Maybe it's time I picked up a new language...
 
I've been away from the world of flight simulation in recent years, and I might have misunderstood your aims here - so forgive me if I'm about to spout nonsense - but, for what it's worth, I can say a multi-mode XML autopilot, using a proper PID routine that directly commands the trim / main surfaces, is indeed do-able and not all that complex in FS. Not sure if the added complexity / idiosyncrasies of FS C++ will aid your quest meaningfully here, at least when it comes to learning enough to make an encouraging start (assuming that PIDs and C++ are new to you).

Perhaps interestingly (for this discussion) I used the David Allerton 747 autopilot pseudocode as a base - which is really just the raw PID algorithm - with his specified algorithms for attitude / altitude hold + varied nav modes (he even covers autolanding). IIRC, none of them used acceleration data (mine certainly doesn't), only properly tuned gain values. You can find Allerton's description in this book, which is well worth finding a library to acquire https://arc.aiaa.org/doi/book/10.2514/4.867033 (Do note, Allerton's model was for a professional non-FS installation, but the principles are exactly the same).

Ultimately, in my implementation, only a single line of code actually calculates the PID loop and commands the trim along the varied axes, while a mode 'flow-chart' specifies error sources, and any gain adjustments, if needed. My (limited) understanding is that real autopilots also employ different gain values where necessary, but for FS you could well need only one (although gentle compromise might be needed for general good behavior vs. edge of the envelope conditions).

When testing the system, I found the basic AP routine could be remarkably robust when dealing with different airframe configurations / speeds / flight conditions, so you might not need as many intricate submodes as you might imagine. Obviously, at a fixed 18hz update rate, an XML autopilot will need to be tuned rather differently vs. a real AP, but I did find it responsive / effective for my purposes - enough to perform decent terrain recovery manoeuvres...

You can use the 'controller calibration' key command to knock the user's flight controls offline when the AP is running - it's terribly hack-y, but might be good enough for you. I've used this for temporary (emergency) surface override, but for normal 'trim only' routines I simply 'command to 0' when a certain level of user control input is detected. Not ideal, but it does work with a sensible pilot!

Apologies again if I've utterly misunderstood what you're after here, but an XML system might be enough, I reckon, for your purposes - at least enough to learn about basic AP behavior.
 
Last edited:

This sounds like exactly what I need! I went ahead and picked up the book! Only problem is, I'm having some trouble reading the programming examples in the book there, but I am dedicated enough to keep reading and looking over it. This will require much more reading, I think...
 
Back
Top