Tim Lapinskas
← Back to All Posts

PID Control

December 5, 2017 · Read time: 3 mins

Yesterday evening, I went through the PID Control lesson taught by Sebastian Thrun. In relation to the previous lessons of Term 2, I found this one to be much more consumable. Most likely due to its simplicity. Hearing Sebastian say that a version of PID Control is built in to many of the Google self-driving cars was very interesting. I’m assuming this course was put together a few years ago, so Waymo may not be using it anymore. Here are a few takeaways from my consumption of the lesson.

Check out this code:

def run(robot, tau_p, tau_d, tau_i, n=100, speed=1.0):
  x_trajectory = []
  y_trajectory = []
  prev_cte = robot.y
  int_cte = 0
  for i in range(n):
    cte = robot.y
    diff_cte = cte - prev_cte
    prev_cte = cte
    int_cte += cte
    steer = -tau_p * cte - tau_d * diff_cte - tau_i * int_cte
    robot.move(steer, speed)
    x_trajectory.append(robot.x)
    y_trajectory.append(robot.y)
  return x_trajectory, y_trajectory

This snippet of code makes up the majority of the PID Controller implementation. It is obviously missing some classes (e.g. robot), but let me walk you through the code.

Given a parameter (tau), speed, and target position (robot.x, robot.y), it is possible to calculate an error (cte above). This is done in three separate parts as shown below:

Proportional = -tau_p * cte

Integral = -tau_i * int_cte

Derivative = -tau_d * diff_cte

PID (steer) = -tau_p * cte – tau_d * diff_cte – tau_i * int_cte

You can see that we calculate the diff_cte and int_cte by moving a time step forward or backward. In other implementations, the derivative is hard coded into the PID Controller. Here is a wiki picture that provides a great diagram:

PID_en.svg

Credit to Wikipedia for this chart

The sum of Proportional, Integral, and Derivative gives us a steering angle that directs us towards the final target. As a note, it is possible to implement each step individually without summing them together to gather a steering angle. Check out the chart below – I think it helps clarify how each one performs (individually and together).

pid

Credit to Udacity for this graph

You can see the the P controller has trouble reaching its target (0) and overshoots the target each time. The PD controller smoothly moves towards 0 and PID has the ability to course correct if it overshoots. The PD controller does look like the better choice according to this graph, but this is with a simple target to hit.

When implementing the PID Controller in the project, the target will require course correction. I may try out the PD or PI version first to see how it performs, but most likely will end up with the full PID controller when it’s all said and done.

This most likely isn’t the best explanation of how the PID Controller truly works, so check out this YouTube video as well. It really helped clarify a few of the key concepts that almost anyone with at least Calculus I/II can understand. Feel free to shoot over any questions at tim.lapinskas@gmail.com!

Shout out to Udacity for providing a great set of lectures and YouTube for being my go-to place for clarification on concepts I have trouble grasping.

Originally published on tlapinsk.wordpress.com.