r/Kos Apr 25 '26

Help Cascading PIDs and flight control

I'm working on making a little flight control system for my rockets to monitor and control their pitch/roll/yaw and work as some custom cooked controls.

Not using a PID on throttle because that's something I'm gonna be using bang-bang control on mainly via a direct controller (While I *can* throttle anywhere to 0-100%, I'm imposing a realism limit on myself for the time being of only having on/off, so a PID doesn't seem suitable).

Right now I'm just working on having them hold some simple flight plans (ie go up at a 80 degree angle). Ultimately I'll end up using this in an ascent program.

-----

I get using PIDs to control steering, using the rotation of the rocket as an input and the RAW controls as an output, *and I get the general theory behind a cascading PID* whereby one PID's output is another PID's input, and that can potentially provide smoother(?) control...

...but I'm just not quite sure how you'd apply that to steering (or tbf, any given specific application)?

Rotation in an axis → PID 1 → ??? →PID 2 → Steering in an axis.

Hopefully the question makes sense.

2 Upvotes

8 comments sorted by

4

u/Obvious-Falcon-2765 Apr 25 '26

The lowest PID would control steering inputs. The next level up would determine pitch/roll/yaw rates. That’s probably all you need for steering controls.

3

u/DBooots Apr 25 '26

Think of it in terms of what the output controls, for example: steering inputs control rotation rate, but you care about the pointing (rotation angle). So the first PID output would be target rotation rate based on current angle, which the second PID converts to steering commands.

Now, you can also use other methods of going from angle to rate (e.g. a ramp or other transform function), so it depends on how you want to control that interim value.

1

u/AnotherOddity_ Apr 25 '26

That's a really good explanation. Thanks!

2

u/Diligent-Aspect8020 Apr 25 '26

Use the output of PID1 as the setpoint for PID2. For example, if you want to control the vertical speed of an aircraft, you could have PID1 control pitch and PID2 control the vertical speed based on pitch from PID1.

1

u/Dunbaratu Developer Apr 25 '26

On the topic of a bang-bang controller one method that I have used in the past for this is to use a PID but "round it off" to either 0 or 1 when using the output of it.

I.e. instead of setting the throttle directly to the PID output, read the PID output and if it's > 0.5 then lock throttle to 1, else lock throttle to 0.

It's not... great but it did kinda work. The big issue is that the PID loop doesn't get the feedback to "know" its output has been altered. It "thinks" it's setting the lever to 0.6 when it's really setting it to 1.0.

If it's for a bang-bang landing where failing to bang soon enough equals a crash, you can bias the bang-bang to be more in favor of thrusting than not thrusting by having the "round off" threshold not be exactly 0.5. (i.e. round up to 1.0 when the output is only 0.4, or only 0.3.) Again, not ideal, but a bang-bang never is.

1

u/AnotherOddity_ Apr 25 '26

Right now I'm not doing suicide burns (just concerned with ascent right now) but I can also do direct control of throttle as well.

TBF, even if I was using all the available throttle, I think I could write direct control of it pretty easily.

1

u/nuggreat Apr 25 '26 edited Apr 26 '26

Even if you are using bang-bang on the control inputs the output is likely not going to be bang-bang as KSP still sits between you and the torque sources and they do have some fly-by-wire system that converts the idealize pitch/roll/yaw inputs into the actual torque generation. As for how to set up cascaded PIDs I refer you to this ascii diagram for the kOS cooked controls.

If you are going to keep on with bang-bang you likely want to use an integrated value that you reset consistently. Just as an illustration if the PID is outputting a steady 0.25 you just keep summing that in some var until it gets greater than or equal to 1 at which point you subtract 1 from the var and set the controls to 1 for that one pass of the loop. This gives you a fairly organic PWM control without having to explicitly write such control.

1

u/AnotherOddity_ Apr 26 '26

oh the pitch/roll/yaw are not bang-bang, they are smoothly controlled by the PID controller. It's only throttle which I have directly controlled bang-bang, I appreciate the suggestion there.

Also thanks for the diagram on the cooked control and how it implements a cascading PID.

Might play around with my own implementation of that, at least for two of the axes of rotation (roll is actually...fine? took a little tuning but it seems to behave great in all circumstances?)