+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 18

Thread: PID controller - what is it and how to?

  1. #1
    Wire Sofaking

    Fizyk will become famous soon enough Fizyk will become famous soon enough Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    729
    Blog Entries
    1

    Default PID controller - what is it and how to?

    I see many people still don't know this excellent and pretty simple method of stabilizing things, so I decided to write a tutorial about it.

    What is it?

    PID controller is a thing that lets you stabilize many different things. The most basic example is a prop hovering in place - you take its position, feed it to a PID controller, and the controller outputs a force that can be used to hold the prop in place.

    How does it work?

    A PID controller is used to make a quantity (like position) reach a target value (a target position). The first thing a PID controller does is to calculate the error. The error is just the difference between the target value and the current value, for example:

    Code:
    Error = TargetPos - E:pos()
    
    Having the error, PID generates three values based on it, that the letters "P", "I" and "D" stand for:
    • P - proportional term - it's just Number*Error, which means the greater the error, the greater the P term.
    • I - integral term - this is more complicated. It's a value that comes from summing errors over time. Basically, the greater the error, the faster this value grows. For those of you familiar with calculus, it's just Error integrated over time.
    • D - derivative term - value that measures the change of Error over time. In GMod it will usually be just delta of Error, or $Error in E2 - that is, if at one moment the error is Error1, and a while later it's Error2, then the D term will equal Error2-Error1. Sometimes it's good to divide it by change (or delta) of time. For those familiar with calculus, it's just derivative of Error over time.

    P, I and D terms can be generated in E2 like that:
    Code:
    @inputs TargetPos:vector
    @persist T [ITerm Error]:vector
    
    runOnTick(1)
    
    T = curtime()
    Error = TargetPos - entity():pos() #calculate error
    
    #initialize gains
    GainP = SomeNumber
    GainI = SomeNumber
    GainD = SomeNumber
    
    PTerm = GainP * Error
    ITerm += GainI * Error * $T
    DTerm = GainD * $Error / $T
    
    Gains describe influence of the terms on the output value. The greater the gain, the more influence on output will a term have. Gain equal to 0 makes a term equal to 0 too, so it will have no influence on the output at all then.

    Now, when we have P, I and D terms, we can generate output - it's just sum of P, I and D terms:
    Code:
    Out = PTerm + ITerm + DTerm
    
    More detailed description of the P, I, D terms

    Ok, we know what are the P, I, D terms and how to calculate them, but... what do they do?

    P term can be thought of like that: when you use the PID controller to hold a prop in position, P term makes the force grow with distance from target. The further away from target the prop is, the stronger it is pushed towards the target. When it is exactly in place, the P term is 0, so it isn't pushed at all. Great!

    But... When the prop was far away, it was pushed towards the target all the time along its way to the target. So, when it reaches the desired position, the force is 0, but it has a nonzero velocity... Simply put, it is still flying quite fast, so it will miss the target, turn around at some place and head to the target again... and again miss it. Here the D term comes to help!

    The D term causes the prop to brake harder when it goes faster. This way, when the prop is going towards the target, the P term is pushing it to go faster, and the D term is pulling it away. At the target, when P term is 0, if the prop is still going fast, the D term is nonzero and causes it to brake. This way the prop will eventually stop at the target. The greater the D gain, the sooner it will happen. But be careful, too great D gain will cause the prop to go towards the target very slowly.

    So, it seems like P and D terms can bring the prop to the target very well. Why do we need an I term then? Well, when we have gravity, the prop won't actually stop at the target, but a bit below it. Why? When the prop is floating without movement at the target, both P and D terms are 0, and yet the gravity is still pulling the prop down. The gravity force isn't countered by anything, so the prop will fall down... to the point where P term and gravity are in balance. You can make this effect smaller by increasing the P gain, but you will never get it exactly to the target this way.

    The I term can fix this situation. When the prop floats below it's target, the error is nonzero, so the I term grows. It grows, and grows, and grows, to the point where error reaches 0. But this is exactly what we wanted, error 0 means we are exactly at the target

    I term cutoff

    The growing property of I term can be a trouble sometimes. If we start very far from our destination, the error is big and the I term grows very fast. It can become so big, that the prop will fly off into space before it can stabilize. Usually I term is used only to correct small errors that P and D alone can't fix - that's why I term is often calculated only when P term or D term are low enough, that is, it is "cut off" when P and D are too big.

    It can be done for example like this:
    Code:
    PTerm = GainP * Error
    DTerm = GainD * $Error / $T
    
    if(PTerm:length() < 10 & DTerm:length() < 10) {
        ITerm += GainI * Error * $T
    } else {
        ITerm = vec() #make I term equal to 0 when P and D terms are too big
    }
    
    Summary - perfect position stabilizing E2

    Time to put all the knowledge from this tutorial together and make a perfect stabilizing E2.

    Code:
    @name Position Stabilizing
    @inputs TargetPos:vector E:entity
    @persist T [ITerm Error]:vector
    @persist GainP GainI GainD
    
    #TargetPos is the position we want to hold prop E at
    
    runOnTick(1)
    
    if(first()) {
        ITerm = vec()
        #those are the values that worked for me - adjust them, if they don't work for you
        GainP = 5
        GainD = 1
        GainI = 4
    }
    
    #this will let us calculate DeltaT ($T)
    T = curtime()
    
    #calculate error
    Error = TargetPos - E:pos()
    
    #calculate P and D terms
    PTerm = GainP*Error
    DTerm = GainD*$Error/$T
    
    #use I cutoff
    if(PTerm:length()<50 & DTerm:length()<10) {
        ITerm += GainI * Error * $T
    } else {
        ITerm = vec()
    }
    
    #calculate PID output
    Out = PTerm + ITerm + DTerm
    
    #apply force
    E:applyForce(Out*E:mass())
    
    I hope that now you understand how PID works and how to make your own self-stabilizing contraptions
    Last edited by Fizyk; 03-03-2010 at 10:25 AM. Reason: Changed highlight boxes to code boxes

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  2. #2
    Wirezard

    Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte's Avatar
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    3,151

    Default Re: PID controller - what is it and how to?

    Good job, you really explained this well.
    "If anybody says he can think about quantum physics without getting giddy, that only shows he has not understood the first thing about them."
    -- Niels Bohr


    Wire FPGA

  3. #3
    Lurker Whodunnit will become famous soon enough Whodunnit's Avatar
    Join Date
    Jan 2008
    Location
    New Zealand, Ackl
    Posts
    655

    Default Re: PID controller - what is it and how to?

    Nice work, hopefully you will inspire more people to become physicists and engineers in the future

    to read more about this stuff try control systems engineering textbooks.
    ЗАГРУЗКА...................

  4. #4
    Wirererer Dskodk is an unknown quantity at this point Dskodk's Avatar
    Join Date
    Sep 2009
    Location
    NZ
    Posts
    117

    Default Re: PID controller - what is it and how to?

    ooh, so this is physics... i like it... IMA BECOMING A PHYSICIST~!
    Working on:
    O-Serious (OS)
    O-Serious (GPU GUI Edition)

  5. #5
    Lurker Maxaxle is on a distinguished road Maxaxle's Avatar
    Join Date
    Sep 2009
    Posts
    189

    Default Re: PID controller - what is it and how to?

    Quote Originally Posted by Dskodk View Post
    ooh, so this is physics... i like it... IMA BECOMING A PHYSICIST~!
    Quick. Get a crowbar.
    Still very clueless about code in general.
    92% of teens have moved onto rap. If you are part of the 8% that still listen to real music, copy and paste this into your signature.

  6. #6
    billywitchdoctor.com Whosdr has a spectacular aura about Whosdr has a spectacular aura about Whosdr has a spectacular aura about Whosdr's Avatar
    Join Date
    Dec 2008
    Posts
    2,120

    Default Re: PID controller - what is it and how to?

    Lol.
    .siht daer ot gniyrt emit detsaw ev'uoY

  7. #7
    Wire Sofaking

    Fizyk will become famous soon enough Fizyk will become famous soon enough Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    729
    Blog Entries
    1

    Default Re: PID controller - what is it and how to?

    Matte, Whodunnit: thanks

    Dskodk: yeah, this is one of the things you do when you are learning physics. Or rather, physics tells you more about why this works, it's more like engineering to learn how to make things like this

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  8. #8
    Wire Sofaking Ehmmett will become famous soon enough Ehmmett's Avatar
    Join Date
    Sep 2009
    Location
    owner():pos()
    Posts
    531

    Default Re: PID controller - what is it and how to?

    Alright so I tried using this. It doesn't stabilize angles and when I set things on it they just fall off
    Am I doing it correctly?
    I'm back. :3
    Thanks to Black Phoenix<3

  9. #9
    Wire Sofaking

    Fizyk will become famous soon enough Fizyk will become famous soon enough Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    729
    Blog Entries
    1

    Default Re: PID controller - what is it and how to?

    Yeah, the example I presented here is only meant to stabilize the prop in position, which means it can still rotate freely. To stabilize angles too you need to add other PIDs, using quaternions, direction vectors or angles, whichever one you prefer.

    For quaternions and direction vectors, your error will usually be the rotationVector() (or equivalent, like cross product, for direction vectors). For angles, the error will be the difference in angles.

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  10. #10
    Wire Amateur krazyfool has a little shameless behaviour in the past krazyfool's Avatar
    Join Date
    Jul 2009
    Posts
    49

    Default Re: PID controller - what is it and how to?

    Thanks man have a in fact have a whole batch.






+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. [Cam Controller] ColorMod on Cam Controller
    By emspike in forum Ideas & Suggestions
    Replies: 8
    Last Post: 10-08-2009, 02:08 PM
  2. adv. pod controller bug
    By frog in forum Bug Reports
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  3. Turret with Adv. Pod Controller and Cam Controller?
    By Jaded Misanthrope in forum Technical Support
    Replies: 6
    Last Post: 07-21-2008, 10:47 AM
  4. Pod Controller
    By uoykcuf in forum Bug Reports Archive
    Replies: 7
    Last Post: 12-17-2007, 08:22 AM
  5. How do I use the pod controller?
    By Tom in forum Technical Support
    Replies: 2
    Last Post: 07-20-2007, 09:29 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots