Home > Reflections | โฎ๏ธ โญ๏ธ

2024-05-18 | ๐Ÿ•น๏ธ PID ๐Ÿซ› Pod ๐Ÿ Racer

โŒจ๏ธ๐Ÿ•น๏ธ CodinGame: Mad Pod Racing

  • ๐ŸŽ‰ Exciting!
  • ๐Ÿ’ก I was very interested when I learned about PID controllers in college, but Iโ€™ve never gotten around to implementing one.
  • ๐Ÿš€ Iโ€™ve finally found an application thatโ€™s literally asking for it!
  • ๐Ÿค– I wrote a simple, intuitive control algorithm to drive my bot last night.
  • ๐Ÿ‘ Iโ€™m impressed with the performance of such a similar algorithm!
  • ๐Ÿง  But Iโ€™m excited to apply some rigor and see how much juice we can squeeze from theory.
  • ๐Ÿ“บ I revisit one of my favorites YouTube channels for a refresher on PID application
  • โš™๏ธ PID controllers are nice because they are simple and effective.
  • ๐Ÿ“ We just need to determine a couple of modeling assumptions to get started.
  • ๐ŸŽฏ A PID controller aims to minimize an error signal by manipulating a control variable.
  • ๐Ÿ’ช For this application, a good initial choice for the control variable is obvious: thrust!
  • ๐Ÿค” Deciding on an error signal to minimize is more nuanced.
  • ๐Ÿ“ In my intuitive control algorithm, I used the angle between my popโ€™s aim and the next checkpoint as a sort of error signal.
  • ๐Ÿ“‰ I reduce the thrust proportionally to this angle.
  • โžก๏ธ If the angle is zero, weโ€™re pointed at the target, so itโ€™s full speed ahead!
  • ๐Ÿข If thereโ€™s a big angle between our podโ€™s aim and the checkpoint, weโ€™re not headed in the right direction, so we may want to slow down.
  • ๐Ÿ’ญ That was my thinking anyway.
  • ๐Ÿ I suspect there may be better choices for error signals, as the point of a race isnโ€™t just to point in the right direction.
  • โœ… But I think this will serve as a good enough starting point while we implement our first PID controller!
  • ๐Ÿ”„ We can always iterate and tune when we have the infrastructure in place.
  • โžก๏ธ Also, I suspect that my intuitive algorithm is basically a propotional controller (the P in PID) and it works pretty well already.
  • ๐Ÿš€ Letโ€™s see how much it improves with the addition of integral and derivative signals (I and D, respectively).

๐Ÿ” A Google search led me to a nice, simple reference implementation to get started.

def PID(Kp, Ki, Kd, setpoint, measurement):  
    global time, integral, time_prev, e_prev  
  
    # Value of offset - when the error is equal zero  
    offset = 320  
      
    # PID calculations  
    e = setpoint - measurement  
          
    P = Kp*e  
    integral = integral + Ki*e*(time - time_prev)  
    D = Kd*(e - e_prev)/(time - time_prev)  
  
    # calculate manipulated variable - MV   
    MV = offset + P + integral + D  
      
    # update stored data for next iteration  
    e_prev = e  
    time_prev = time  
    return MV  
  • ๐Ÿš€ An easy read at 11 lines of Python!
  • โฑ๏ธ This should only take a few minutes to translate, and maybe a โณ a half hour or so to get up and running in our ๐ŸŽ๏ธ pod racer.