Skip to content

Instantly share code, notes, and snippets.

@skypanther
Created February 11, 2017 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skypanther/bbd627a8584400857ed856db91274091 to your computer and use it in GitHub Desktop.
Save skypanther/bbd627a8584400857ed856db91274091 to your computer and use it in GitHub Desktop.
scratch code for using the Talon SRX encoders
int circumferenceInInches = 123;
int pulsesPerRotation = 1000;
protected void function auto1() {
RobotDrive drive = RobotMap.driveSystemdrive;
CANTalon talon = RobotMap.driveSystemCANTalon1;
//Change control mode of talon, other options are Follower Mode or Voltage Compensation
talon.changeControlMode(ControlMode.Position);
//Select either QuadEncoder or Pulse Width / Analog, we want Quad
talon.setFeedbackDevice(FeedbackDevice.QuadEncoder);
//Set the PID constants (p, i, d)
talon.setPID(0.5, 0.001, 0.0);
//Set the voltage ramp rate, will go from 0V to 6V in 1sec
talon.setVoltageRampRate(6);
//Enable PID control on the talon
talon.enableControl();
//What position does the encoder think it's in now?
System.out.println(talon.getEncPosition());
//Reset the encoder to zero as its current position
talong.setPosition();
int distanceInInches = 48;
//Tells the talon to go to XXX encoder counts, using the preset PID parameters.
talon.set(distanceInInches * circumferenceInInches * pulsesPerRotation);
finished = true;
}
// another option, call this repeatedly and when it returns true you turn off the motors
protected void function hasDrivenFarEnough(int distance) {
int currentPosition = talon.getEncPosition();
int targetPulseCount = distance * circumferenceInInches * pulsesPerRotation;
System.out.println("Current position: " + String.valueOf(currentPosition));
System.out.println("Target position: " + String.valueOf(targetPulseCount));
if (currentPosition >= targetPulseCount) {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment