geel9
12-02-2013, 19:53
Hey,
We're trying to get our PID loops to control the motors' rate, as opposed to their distance.
Here is our code:
//Initializes the motors.
private final SpeedController left = new Jaguar(1);
private final SpeedController right = new Jaguar(2);
//Initializes the Encoders.
private final Encoder leftEncoder = new Encoder(1, 2);
private final Encoder rightEncoder = new Encoder(3, 4);
//Proportional, Integral, and Dervative constants.
//These values will need to be tuned for your robot.
private final double Kp = 0.1;
private final double Ki = 0.001;
private final double Kd = 0;
//This must be fully initialized in the constructor, after the settings
//for the encoders have been done.
private final PIDController leftPID;
private final PIDController rightPID;
public PIDDrive() {
//Sets the distance per pulse in inches.
leftEncoder.setDistancePerPulse(-1);
rightEncoder.setDistancePerPulse(-1);
//Starts the encoders.
leftEncoder.start();
rightEncoder.start();
//Sets the encoders to use distance for PID.
//If this is not done, the robot may not go anywhere.
//It is also possible to use rate, by changing kDistance to kRate.
leftEncoder.setPIDSourceParameter(Encoder.PIDSourc eParameter.kRate);
rightEncoder.setPIDSourceParameter(Encoder.PIDSour ceParameter.kRate);
//Initializes the PID Controllers
leftPID = new PIDController(Kp, Ki, Kd, leftEncoder, left);
rightPID = new PIDController(Kp, Ki, Kd, rightEncoder, right);
leftPID.setPercentTolerance(25);
rightPID.setPercentTolerance(25);
leftPID.setContinuous();
rightPID.setContinuous();
//Enables the PID Controllers.
leftPID.enable();
rightPID.enable();
}
public static void WriteDriverStationLine(DriverStationLCD.Line line, String text){
DriverStationLCD.getInstance().println(line, 1, text);
DriverStationLCD.getInstance().updateLCD();
}
/**
* This function is called once each time the robot enters operator control.
* Teleop commands are put in here
*/
public void operatorControl() {
leftPID.setSetpoint(1000);
rightPID.setSetpoint(1000);
while(isOperatorControl()){
WriteDriverStationLine(DriverStationLCD.Line.kUser 1, "E: " + leftPID.getError());
WriteDriverStationLine(DriverStationLCD.Line.kUser 2, "E2: " + rightPID.getError());
WriteDriverStationLine(DriverStationLCD.Line.kUser 3, "D: " + leftEncoder.getRate());
WriteDriverStationLine(DriverStationLCD.Line.kUser 4, "D2: " + rightEncoder.getRate());
}
The wheels spin forward, then rapidly switch directions.
We're trying to get our PID loops to control the motors' rate, as opposed to their distance.
Here is our code:
//Initializes the motors.
private final SpeedController left = new Jaguar(1);
private final SpeedController right = new Jaguar(2);
//Initializes the Encoders.
private final Encoder leftEncoder = new Encoder(1, 2);
private final Encoder rightEncoder = new Encoder(3, 4);
//Proportional, Integral, and Dervative constants.
//These values will need to be tuned for your robot.
private final double Kp = 0.1;
private final double Ki = 0.001;
private final double Kd = 0;
//This must be fully initialized in the constructor, after the settings
//for the encoders have been done.
private final PIDController leftPID;
private final PIDController rightPID;
public PIDDrive() {
//Sets the distance per pulse in inches.
leftEncoder.setDistancePerPulse(-1);
rightEncoder.setDistancePerPulse(-1);
//Starts the encoders.
leftEncoder.start();
rightEncoder.start();
//Sets the encoders to use distance for PID.
//If this is not done, the robot may not go anywhere.
//It is also possible to use rate, by changing kDistance to kRate.
leftEncoder.setPIDSourceParameter(Encoder.PIDSourc eParameter.kRate);
rightEncoder.setPIDSourceParameter(Encoder.PIDSour ceParameter.kRate);
//Initializes the PID Controllers
leftPID = new PIDController(Kp, Ki, Kd, leftEncoder, left);
rightPID = new PIDController(Kp, Ki, Kd, rightEncoder, right);
leftPID.setPercentTolerance(25);
rightPID.setPercentTolerance(25);
leftPID.setContinuous();
rightPID.setContinuous();
//Enables the PID Controllers.
leftPID.enable();
rightPID.enable();
}
public static void WriteDriverStationLine(DriverStationLCD.Line line, String text){
DriverStationLCD.getInstance().println(line, 1, text);
DriverStationLCD.getInstance().updateLCD();
}
/**
* This function is called once each time the robot enters operator control.
* Teleop commands are put in here
*/
public void operatorControl() {
leftPID.setSetpoint(1000);
rightPID.setSetpoint(1000);
while(isOperatorControl()){
WriteDriverStationLine(DriverStationLCD.Line.kUser 1, "E: " + leftPID.getError());
WriteDriverStationLine(DriverStationLCD.Line.kUser 2, "E2: " + rightPID.getError());
WriteDriverStationLine(DriverStationLCD.Line.kUser 3, "D: " + leftEncoder.getRate());
WriteDriverStationLine(DriverStationLCD.Line.kUser 4, "D2: " + rightEncoder.getRate());
}
The wheels spin forward, then rapidly switch directions.