I recently got a package of CIMCoders for our Robot’s Drivetrain. The problem is that I’ve never actually programmed encoders before; Especially, CIMCoders. So, I used the example code provided from AndyMark, but I could never get any values from the CIMCoder. I knew the CIMCoder worked because both Channel A and B flashed when I moved the motor. Please help I’m very confused! (Code I used is listed below)
package frc.robot;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
- The VM is configured to automatically run this class, and to call the functions corresponding to
- each mode, as described in the TimedRobot documentation. If you change the name of this class or
- the package after creating this project, you must also update the build.gradle file in the
- project.
/
public class Robot extends TimedRobot {
/*- This function is run when the robot is first started up and should be used for any
- initialization code.
*/
Encoder encoder;
private static final double cpr = 64;
private static final double whd = 6;
@Override
public void robotInit() {
encoder = new Encoder(0,1);
encoder.setDistancePerPulse(Math.PIwhd/cpr); //distance per pulse is pi (wheel diameter / counts per revolution)
}
@Override
public void robotPeriodic() {
double dist = encoder.getDistance();
SmartDashboard.putNumber(“Encoder”, dist);
}