Hello. So my team has been trying to write code to get the robot to drive in a straight line for a certain distance, which we managed to do using encoders and gyro input; however, once the encoders register that we have traveled the ideal distance and the motor shuts off, our robot drifts a considerable amount. We’re trying to come up with a solution using PIDs, as shown below.
Any help with deciding on what the Kp, Ki and Kd values should be or with
any potential logic flaws we have would be appreciated. Also, if anyone has any better ideas than PIDs to reduce drifting, feel free to comment them. Ask for more code if you need to see anything else.Thanks!
//Initialization code for the gyrodrive command that is called to drive
the robot forward along a straight line until it reaches the desired dist.
protected void initialize() {
log("Initialized GyroDriveCommand"); // prints to show that
CommandBasedRobot.resetEncoders(); // resets encoders
drivesub.enable(); //
gyroCalc(); //
}
//isFinished method for gyroDrive to try to stop without passing the desired
position
protected boolean isFinished() {
return(drivesub.getPosition() - setpoint) < 1; // finishes when pos - setpoint < 1
}
Declarations for the Kp, Ki and Kd values as well as the PIDInput and
PID output methods:
private static final double Kp = 3;
private static final double Ki = 0.2;
private static final double Kd = 0.0;
protected double returnPIDInput() {
return EncoderInfo.distanceL;//returns the current encoder distance
}
protected void usePIDOutput(double d) {
System.out.println(d);
start(d, 0);//gives the speed value to arcade drive
}
public DriveTrainSubsystem(){
super("DriveTrainSubsystem", Kp, Ki, Kd);
//OI code for calling the gyrodrivecommand
//dist, speed, setpoint
button1.whenPressed(new GyroDriveCommand(24, 0.70, 24));
}