PID Positional Control (Java)

Hello all.

Recently I’ve been attempting to write some code to control a motor to travel to setpoint (like spin half a revolution) and then stop. I’ve been using a quadrature encoder to obtain my distance traveled data. I’ve had little to no success with either CAN Positional control or using an adapted methods of PID-DEMO and ArmDemo. So my question is are there any teams using Java that would be willing to share their code from past robot arms that had positional control. That would help me a lot I believe.

Hi, PID is a great tool, I understand you are using jaguars, you may want to hook the encoders directly into the DSC, When in java use the encoder as a PID source and then start tuning your variables. We have had issues in the past with jaguars we no longer use them.

Above robot init


Victor motor;
Encoder encoder;
PIDController pid;

In your robot init


motor = new Victor(1); # or jaguar
encoder = new Encoder(1,2); # A channel B channel
enncoder.setPIDSourceParameter(Encoder.PIDSourceParameter.kDistance);
PID = new PIDController(0.01,0,0.01,encoder,motor);

In teleop ECT


PID.enable();
PID.setSetpoint(10.0);

Hope this helps, Please don’t hesitate to PM me if you need anything,
-Matt

Does the setpoint have to be a distance, or can we set the setpoint to a certain number of counts?

“Distance” is just a descriptor. You can use whatever you want. If you want a certain number of counts just set your “distance per count” to 1.

Basically, distance per count allows you to put units into your PID instead of relying just on counts. You could translate counts into inches, feet, etc.

Edit: Please note this is using WPILib Java. If you’re not using WPILib, the lines of code mentioned above won’t work out of the box.