I have created a command, DriveFor, which takes feet and speed and moves using our encoder that is linked to the PIDSubsystem of the DriveTrain. When cancelling that command or disabling while running that command results in our robot shaking itself forward slowly. I have linked to two videos of the shaking and posted the code below.
DriveFor Command:
package org.usfirst.frc.team5546.robot.commands.driveTrain;
import org.usfirst.frc.team5546.robot.Robot;
import edu.wpi.first.wpilibj.command.Command;
/**
*
*/
public class DriveFor extends Command {
boolean finished = false;
double distance = 0;
double speed;
public DriveFor(double feet, double _speed) {
// Use requires() here to declare subsystem dependencies
requires(Robot.driveTrain);
this.distance = feet * Robot.driveTrain.DISTANCE_PER_FOOT;
speed = _speed;
System.out.println(feet);
}
// Called just before this Command runs the first time
protected void initialize() {
Robot.driveTrain.rotate = false;
Robot.driveTrain.squaredInputs = false;
Robot.driveTrain.encoderLeft.reset();
Robot.driveTrain.imu.reset();
Robot.driveTrain.getPIDController().setPID(4, 0.1, 0);
Robot.driveTrain.setSetpointRelative(distance);
Robot.driveTrain.spd = speed;
System.out.println("Setpoint: " + Robot.driveTrain.getSetpoint());
System.out.println("Position: " + Robot.driveTrain.getPosition());
System.out.println("Position - Setpoint: " + (Robot.driveTrain.getPosition() - Robot.driveTrain.getSetpoint()));
Robot.driveTrain.setAbsoluteTolerance(100);
Robot.driveTrain.enable();
//System.out.println(Robot.driveTrain.getSetpoint());
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
//System.out.println("Executing DriveFor.");
//Robot.driveTrain.enable();
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
//return finished;
return Robot.driveTrain.onTarget();// || Robot.oi.cancelGearBtn.get();
}
// Called once after isFinished returns true
protected void end() {
System.out.println("Finished DriveFor");
Robot.driveTrain.disable();
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
}
}
As a rule, a violent shaking like that is often two different things sending opposing commands to the motors. In some cases, the proper use of requires() helps with this.
This was my first initial thought too. But I think the PID loop is still running on the subsystem when the command is interrupted. Try adding the disable call on the subsystem in the interrupted method too.
When canceling a command (and possibly disabling, too, not sure), the end() method is not called; the interrupted() method is. Adding a call to end() to interrupted() should resolve this.