Quote:
Originally Posted by mark219
Essentially, all I know about PID is that it is a way to minimize error, but the question I am asking is how does it work? Can you show an example of code? Is its only use for autonomous mode?
|
There are uses for PID in teleop; it's just not an auton thing. In this game I could see mashing a button to put the robot into a crawler mode where you could put a speed limit on a tank tread when crawling over some of the obstacles. Going over the rumble wall you may want to reduce power to one side when it's entirely in the air so that when it does make contact it doesn't spin the robot to one side, but you would also want to it to come back up to speed quickly once it's under load.
The code is incredibly simple and so is the math. Tuning your PID values is not. Here's the crux of simple PID calculation code:
Code:
double error = target-current;
double errorChange = error-lastError;
double correction = Kp*error + Ki*errorSum + Kd*errorChange;
lastError = error;
return correction;
Figuring out what the values for Kp, Ki, and Kd are is the tricky bit. Let's try an example. Say you're driving a car and you only open your eyes every second or so and make a rash decision about how to jerk the wheel about. This is your Kp value. Assume your steering wheel works at a 1:1 ratio to your tires here. If you open your eyes and find you're driving 5 degrees left of the line you want you'd jerk the wheel 10 degrees right to get you back on track. When you open your eyes again you might be just slightly right of the line, say 3 degrees, so you jerk 6 left, and you kinda keep doing this until you oscillate around the line. That would be how a robot would respond if you give it a Kp value of 2.0. It would double the response to the immediate error. I'm not saying Kp of 2.0 is ever a good idea it's just an example. But first you have to find a Kp value that gives you that subtle and sane oscillation around your target. Once that's done you work on Ki which, as an integral, represents your general inability to keep on track. Once dialed in that dampens the oscillation in that it reduces the effect Kp has on the correction. If you keep correcting solely on Kp but your'e 10% off each time you dampen it down. Finally you define Kd to fix what you can't compensate for with Kp and Ki. Kd is generally going to be 0 unless you want to get really freaking precise about something.
Hope that helps.