Profile image

Need help with my PID code

1,600 SlowStall35  one month ago

So I wrote a PID code to stabilize the pitch of my craft that works wonders under normal circumstances like normal flying and so on. However, it has a problem, I researched it and I think it's integral windup.
Here's the code:

clamp((smooth((-PID(PitchRate, Pitch* 28, ((IAS>230)? (0.004/(IAS/230)) : 0.012), 0.02, ((IAS>230)? (0.075* (IAS/230)) : 0.0225))), 3.5)), -1, 1)

Basically, the PID controller doesn't have a limit and just keeps summing my input up as long as it can't do anything to match its goal with its output, for example: when I'm stationary on the ground and try to pitch up, the PID controller controls the horizontal stabilizers BUT it cannot match its output value to my plane's PitchRate (which is the PID controller's goal) since it's stationary. That goes on and on and the value that the controller outputs just goes up. When I apply a negative pitch input, I'd have to wait for it to get down to 1 from whatever value it's at for my horizontal stabilizer to even move, causes a lot of delay.

I've tried clamping it between -1 and 1, that does stop its actual output from going past -1 or 1 BUT the PID value itself still goes well past those limits and I get an output between those limits just because it's clamped. Stops the rotator from spinning around but doesn't prevent that delay I mentioned earlier.

I'd be glad if someone can help me figure that out, thanks in advance.

  • Log in to leave a comment
  • Profile image
    1,600 SlowStall35

    @PlaneFlightX I guess I've found somewhat of a solution:
    clamp((smooth((-PID(PitchRate, LimGoal, (KpPitch), (KiPitch), (KdPitch))), 3.5)), -1, 1)
    .
    KpPitch is ((IAS>230)? (0.004/(IAS/230)) : 0.012)
    .
    KiPitch is 0.02 (or any fixed value that works the best)
    .
    KdPitch is ((IAS>230)? (0.075* (IAS/230)) : 0.0225)
    .
    I kind of had to create a variable (named it PID) with the expression:
    -PID(PitchRate, Pitch* 28, KpPitch, KiPitch, KdPitch)
    .
    I used that new PID variable to control yet another variable that I named "LimGoal" (which is used in my main code), LimGoal's expression is:
    .
    (PID>0.99) ? clamp((Pitch* 28), -28, 0) : ((PID<(-0.99)) ? clamp((Pitch* 28), 0, 28) : Pitch* 28)
    that seems to have solved that windup problem. It causes some minor oscillations every now and then but I think I can solve that too

    EDIT: that seemed to solve the windup problem but it still has other problems, needs a lot more factors to work properly

    one month ago
  • Profile image
    1,600 SlowStall35

    @PlaneFlightX I could use some help on how to do that. I've tried clamping the target value even though it's already limited. Also I'd need a negative target value in order to decrease its output since it acts more like sum(), so yeah. I'd appreciate it if you could advise me on how to do that

    one month ago
  • Profile image
    46.6k PlaneFlightX

    Stop the target in the PID from going up that high. (Make the PID think it's reaching its target so it stops trying harder)

    +2 one month ago