How to Use a PID Controller to Make Encoder Turns More Accurate

Hello All,

I am looking to make my tuns and straight-line driving more accurate and have learned that PID Controllers are the best way to achieve this. I have looked around the forum and have read the documentation for PID Controllers, however, I was unable to understand how these are used to make turns more accurate. Additionally, I fail to understand how the Integral and Derivative components are actually used to make the motion more accurate.

I would really appreciate if someone could shed light on this system.

Thank You
Ani

There’s a lot that could be disseminated on this topic. A couple of key aspects are response and time. While the P is the response to how far off you are to your target (error), the integral is an accumulation of the the errors over a period of time and is supposed to help add to the response. The derivative is a damper (or counter force) to the effects of the integral since the integral is an accumulator. The derivative takes away from the integral values as you get closer to your target (based on the PID formula). So over time as you get closer to your target, the D should remove the affects of the I which has accumulated over time.

Key here is properly tuning your mechanism so you get the right K values for your PID.

As they are typically used, Proportion is the primary component of the equation, providing more power the further you are from the target. Integral helps to arrive at the target quickly, and will be the dominant component in the very last moments before the target is reached. Derivative looks to dampen oscillation when P and I overshoot the target.

For drive base motion, P and I work to get you to the target, with I taking over at the very end, D tends to act as a brake once the target is met.

So, P gets you going (and I helps), P approaches zero as the target nears, if the motion is short of the target, I builds up until the motion gets to the target, and D leads the way back if the target is overshot.

3 Likes

Do you if a ‘P’ loop will suffice for a chassis. Additionally can you share some example code for integral and derivative. Pseudocode will suffice.

Thank you very much, I appriciate it :slight_smile:

Yes, P control is often all you need for a drive base… momentum is a substitute for I and (with V5) braketype(hold) can stand in for D.


has more than you want to know, but also some pseudocode at the bottom.

This forum has a multitude of threads on PID, A PID controller in RobotC by jPearman would be a good place to start (just don’t post to that 9 year old thread)!

2 Likes

I understand, based on my experience with breakType::hold it stops pretty quickly. I will reference the resource you included above.

Thank You!

Check out this guide. It explains how to program a PID, and through that you can learn how it works. Btw, I almost never use I, as it can wind up and cause too many oscillations.

2 Likes