In this section we will be going over
1) Create a new command called DriveDistance
2) Before the constructor create a double called distance
3) In the DriveDistance constructor add a double parameter called inches
4) Inside type:
distance = inches; 5) In initialize add our resetDriveEncoder method
6) In execute add our arcadeDrive method and change the moveSpeed parameter to a RobotPreference named driveDistanceSpeed and rotateSpeed to 0.0
7) In isFinished type:
return Robot.m_drivetrain.getDriveEncoderDistance() == distance; 8) In end stop the Drivetrain and call end in interrupted
Your full DriveDistance.java should look like this
package frc.robot.commands;
import edu.wpi.first.wpilibj.command.Command;
import frc.robot.Robot;
import frc.robot.RobotPreferences;
public class DriveDistance extends Command {
private double distance;
public DriveDistance(double inches) {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
requires(Robot.m_drivetrain);
distance = inches;
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
Robot.m_drivetrain.resetDriveEncoder();
}
// Called repeatedly when this Command is scheduled to run
@Override
protected void execute() {
Robot.m_drivetrain.arcadeDrive(RobotPreferences.driveDistanceSpeed(), 0.0);
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return Robot.m_drivetrain.getDriveEncoderDistance() == distance;
}
// Called once after isFinished returns true
@Override
protected void end() {
Robot.m_drivetrain.arcadeDrive(0.0, 0.0);
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
@Override
protected void interrupted() {
end();
}
} The code you typed in RobotPreferences.java should be this
public static final double driveDistanceSpeed() {
return Preferences.getInstance().getDouble("driveDistanceSpeed", 0.5);
} 1) Create a new Command Group named Autonomous
2) In the constructor type
addSequential(new DriveDistance(RobotPreferences.autoDriveDistance()));
addSequential(new ShooterUp()); 1) Create a new command called DoDelay
2) Before the constructor add two private doubles called expireTime and timeout
3) In the constructor add a double called seconds in the parameter
4) Inside the constructor set timeout equal to seconds
5) Create a protected void method called startTimer
6) Inside set expireTime equal to timeSinceInitialized + timeout
7) In initialized add our startTimer method
8) In isFinished return timeSinceInitialized is greater or equal to expireTime
Your full DoDelay.java should look like this
package frc.robot.commands;
import edu.wpi.first.wpilibj.command.Command;
public class DoDelay extends Command {
private double expireTime;
private double timeout;
public DoDelay(double seconds) {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
timeout = seconds;
}
protected void startTimer() {
expireTime = timeSinceInitialized() + timeout;
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
startTimer();
}
// Called repeatedly when this Command is scheduled to run
@Override
protected void execute() {
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return (timeSinceInitialized() >= expireTime);
}
// Called once after isFinished returns true
@Override
protected void end() {
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
@Override
protected void interrupted() {
}
} Your full Autonomous.java should look like this
package frc.robot.commands;
import edu.wpi.first.wpilibj.command.CommandGroup;
import frc.robot.RobotPreferences;
public class Autonomous extends CommandGroup {
/**
* Add your docs here.
*/
public Autonomous() {
addSequential(new DriveDistance(RobotPreferences.autoDriveDistance()));
addSequential(new DoDelay(RobotPreferences.autoDelay()));
addSequential(new ShooterUp());
}
} The code you typed in RobotPreferences.java should look like this
public static double autoDelay() {
return Preferences.getInstance().getDouble("autoDelay", 5.0);
}
public static double autoDriveDistance() {
return Preferences.getInstance().getDouble("autoDriveDistance", 12.0);
} In order to run our Autonomous command in autonomous we will have to put it in Robot.java so that it will run as soon as the robot enters the autonomous mode
In Robot.java under autonomousInit find m_autonomousCommand = m_chooser.getSelected(); and change it to
public void autonomousInit() {
m_autonomousCommand = new Autonomous();
...