Rate-based PID feedback using PIDController and Encoder

Hi, our team is working on a PID feedback loop for our motors. Every example of code I could find was for distance-based PID feedback and we want to go off the rate of our encoders to modify the motor speed.

Here’s an example of the code I’ve been trying. We use IterativeRobot.

In the constructor:

m_testEnc = new Encoder(ENC_A_CHANNEL, ENC_B_CHANNEL);
m_testJag = new Jaguar(MOTOR_CHANNEL);
m_testPIDController = new PIDController(0.1, 0.1, 0.1, m_testEnc, m_testJag);

EDIT: All variables are declared previously, all-caps variables are #defined.

In AutonomousPeriodic:


SetDistancePerPulse is a dummy number, this is a motor on a piece of wood. We put it in there because it looked like the encoder rate needed it for some reason. The desired function of the code was to approach 0.5 motor speed and use PID feedback to stick there.

Here’s what we’ve done so far:

Our initial assumption was that the PID loop would go off the rate of the encoder, and we were pleased to find that it was actually sending a motor value. However, the motor value went up straight past 0.5, and the error on the PIDController was only changed by the encoder rate changing. I tried setting the Setpoint in terms of the rate but that didn’t work out at all. I’m confused as to what we’re actually suposed to set the setpoint in terms of. I switched it back to 0.5 (half motor speed) and thought that the reason the value surpassed the value and kept going up was from one of the P, I, or D constants. I set I and D to zero, and then the value didn’t change at all from loop to loop. I set only I to zero and the value still didn’t change at all.

Any input, CD? At this point the only problem I can think of is from the SetDistancePerPulse dummy, even though we are using rate and not distance.


We fixed our problem. We figured out that we needed GetRate to be -1.0 to 1.0 based on our desired max speed, so the DistancePerPulse was set to a number that made GetRate equal to 1.0 at our desired maximum motor speed.