PID Tuning Question!

Hi everyone. I’ve been working on a PID for my team but I’m running into some problems. I am trying to write a PID for the auton so that it will travel accurate distances. I have been using shaft encoders to achieve this. The issue that I have been having is that it travels about two inches less each time I run it. This happens most of the time, but other times it is just randomy inconsistent. I was wondering if I just need to keep adjusting my values for Kp, Ki, and Kd, or if something else is causing a problem. I would appreciate any help and ideas!!!

1 Like

These inconsistencies could be due to a variety of reasons. Regarding tuning, you should always start with isolating P to tune while having I and D temporary set to 0. You can tell if kp is tuned well if the bot gets to your desired position with at most one or two small oscillations before coming to a stop. After that, you can move on to tuning Kd to hopefully remove any oscillation, and if you see fit, tune Ki last. Although I is rarely necessary so many people run PD loops or even just P loops.

Regarding your code, I notice that it’s exiting the while loop when the robot position becomes greater than the desired position. Instead, I would set it to exit once it’s within a threshold of the desired position. This way, if the bot overshoots the target, it can go back to the target and only exit the loop once it’s within the threshold you set of the target. I’d also add a 20 msec wait in the loop so the brain isn’t constantly working so hard.

If none of these work, it could be a hardware problem. If you’re using encoders on powered shafts, there could be a bit of wheel slippage for every move the bot makes which can add up over time. If your bot has the room, it could be beneficial to put the encoder on an unpowered free spinning wheel that’s tensioned to the ground so that it never loses contact with the ground.


Thank you so so much for this!!! Changing the while loop like you suggested was a great idea!!!