I’m trying to make a command that drives forward a specified distance based on the drive wheel encoders. It uses very simple bang-bang controlling as I am just trying to get it functional, I don’t need it precise to the millimeter. This command can be found below:
public class DriveDistanceCommand extends CommandBase {
/** Creates a new DriveDistanceCommand. */
private DriveSubsystem driveSubsystem;
private Translation2d translation;
private double meters;
private double cutoffTimeSeconds;
private Timer timer;
private double averageEncoderCount;
public DriveDistanceCommand(DriveSubsystem driveSubsystem, Translation2d translation, double meters, double cutoffTimeSeconds) {
this.driveSubsystem = driveSubsystem;
this.translation = translation;
this.meters = meters;
this.cutoffTimeSeconds = cutoffTimeSeconds;
timer = new Timer();
addRequirements(driveSubsystem);
// Use addRequirements() here to declare subsystem dependencies.
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
// resets encoders
for(SwerveModule mod : driveSubsystem.mSwerveMods){
mod.mDriveMotor.setSelectedSensorPosition(0);
}
timer.reset();
timer.start();
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
driveSubsystem.drive(translation, 0, false, false);
for(SwerveModule mod : driveSubsystem.mSwerveMods){
averageEncoderCount += mod.getDriveMotorEncoder();
}
averageEncoderCount /= 4;
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
driveSubsystem.drive(new Translation2d(0, 0), 0, false, false);
timer.stop();
timer.reset();
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
if(timer.get() > 0){
return Conversions.falconToMeters(averageEncoderCount) >= meters || timer.get() >= cutoffTimeSeconds;
}
return Conversions.falconToMeters(averageEncoderCount) >= meters;
}
}
This logic flow works, at least in my head. The problem is that my encoders have a certain number of counts, and after that, I believe that they reset to 0. I can not confirm this as the build team is having problems with can wires and I haven’t been able to test anything for a while, but I certainly want to be prepared for it. Is there a simple way to get around this, or am I just going to have to make a variable that counts how many times the encoder has reset?