Unit 7: Advanced FRC Coding
Timed movements are a good start, but for competition-level precision and reliability, we use sensor-based movements. This means we command the robot to act until a sensor tells it to stop, creating a "closed-loop" system that is accurate and repeatable.
The philosophy is to make decisions based on real-world feedback, not blind guesses about timing. This makes our autonomous routines resilient to the unpredictable conditions of a real match.
Using sensors makes our commands intelligent. They automatically adapt to real-world conditions.
This commitment to feedback-driven control is a core reason for our team's consistent success.
Let's build one of the most common and important autonomous commands: a `DriveDistanceCommand`. This command will drive the robot a specified distance and then stop, using encoder feedback to know when it's done.
public class DriveDistanceCommand extends Command {
private final DriveSubsystem m_drive;
private final double m_distance;
private final double m_speed;
public DriveDistanceCommand(DriveSubsystem drive, double distance, double speed) {
m_drive = drive;
m_distance = distance;
m_speed = speed;
addRequirements(m_drive);
}
@Override
public void initialize() {
// Reset the encoders to 0 at the start of the command
m_drive.resetEncoders();
m_drive.setSpeed(m_speed); // Start driving
}
// This is the key: the command is finished when the encoder reading
// is greater than or equal to the target distance.
@Override
public boolean isFinished() {
return m_drive.getDistance() >= m_distance;
}
@Override
public void end(boolean interrupted) {
// Stop the motors when the command ends
m_drive.stop();
}
}
Question: In a sensor-based `DriveDistanceCommand`, which command lifecycle method is responsible for checking the encoder to see if the robot has reached its target?