There is one common pitfall associated with switching from distance to rate/speed as the process variable.
Consider a position control loop as implemented by WPIlib:
Code:
Motor Voltage = Kp*error + Ki*error_sum + Kd*(error-error_last)
Error is in units of distance; the output is in units of percentage of motor voltage (roughly, speed). When you have zero error (and zero error_sum and zero error_last...e.g. you are exactly at your goal) in a position control loop, you command a zero motor speed. This make sense; stopping will leave you at your goal.
Now consider what would happen if you implemented the same loop, but switch the "error" units from distance to speed. When you have zero error in a velocity control loop, you do NOT want to command a zero motor speed. That would slow the motor and result in MORE error. Your best bet is probably to command the motor to keep going whatever speed it was at last!
There are two ways you can get your velocity controller behaving in this situation:
(1) You need to treat P and I as if they were actually D and P, respectively! This makes sense; you are basically taking the time-derivative of position, which is speed, but are still commanding the same quantity (voltage). Hence the meaning of your gains changes to reflect the derivative of the input.
(2) You can make a slight tweak to the way the PID output is used. Instead of
you can simply accumulate the output:
Code:
Output = Output + ...
Using the second method, you can think of your output not being "Voltage", per se, but rather "Change in Voltage". In other words, you are taking the derivative of the left side of the equation as well as the right.