In this section we will be going over
1) For this tutorial we are going to add a switch to a shooter subsystem to automatically change the pitch of the shooter
The code you typed should be this
DigitalInput shooterSwitch = null; In the constructor
shooterSwitch = new DigitalInput(Constants.SHOOTER_SWITCH); In Constants.Java
// Digital Inputs
public static final int SHOOTER_SWITCH = 0; 1) Create a public boolean method called isShooterSwitchClosed
2) Inside type:
return shooterSwitch.get(); Switches have 2 states: open and closed.
Make sure you know which is true or false or you may have to invert the switch by rewiring or using the ! operator
Your isShooterSwitchClosed() should look like this
public boolean isShooterSwitchClosed() {
return shooterSwitch.get();
} package frc.robot.subsystems;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
public class Shooter extends SubsystemBase {
/**
* Creates a new Shooter.
*/
DigitalInput shooterSwitch = null;
public Shooter() {
shooterSwitch = new DigitalInput(Constants.SHOOTER_SWITCH);
}
public boolean isShooterSwitchClosed() {
return shooterSwitch.get();
}
@Override
public void periodic() {
// This method will be called once per scheduler run
}
} 1) For this tutorial we will use the switch to create a button that automatically pitches the shooter up after the switch is pressed
2) Create a new command called ShooterUpAuto
3) In the constructor add requires(Robot.m_Shooter)
4) In isFinished return our isShooterSwitchClosed method
5) In end add our pitchUp method
Your full ShooterUpAuto.java should look like this
package frc.robot.commands;
import edu.wpi.first.wpilibj.command.Command;
import frc.robot.Robot;
public class ShooterUpAuto extends Command {
public ShooterUpAuto() {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
requires(Robot.m_shooter);
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
}
// 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 Robot.m_shooter.isShooterSwitchClosed();
}
// Called once after isFinished returns true
@Override
protected void end() {
Robot.m_shooter.pitchUp();
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
@Override
protected void interrupted() {
}
} The code you typed should be this
D3.whenPressed(new ShooterUpAuto()); Or this
D3.whileHeld(new ShooterUpAuto()); 1) For this tutorial we are going to add a encoder to the Drivetrain subsystem to keep track of the distance the robot has driven
2) Inside the Drivetrain subsystem we are going to create an encoder called driveEncoder
The code you typed outside the constructor should be this
Encoder driveEncoder = null; Inside the constructor
driveEncoder = new Encoder(Constants.DRIVETRAIN_ENCODER_A, Constants.DRIVETRAIN_ENCODER_B); 1) Create a public double method called getDriveEncoderCount
2) Inside type:
return driveEncoder.get(); 3) Create a public method called resetDriveEncoderCount
4) Inside type:
driveEncoder.reset(); The code you typed should be this
public double getDriveEncoderCount() {
return driveEncoder.get();
}
public void resetDriveEncoder() {
driveEncoder.reset();
} 1) Create a new InstantCommand called DriveResetEncoder
2) In the constructor add requires(Robot.m_drivetrain)
3) In initialize() add our resetDriveEncoder method
Your full DriveResetEncoder command should look like this
package frc.robot.commands;
import edu.wpi.first.wpilibj.command.InstantCommand;
import frc.robot.Robot;
/**
* Add your docs here.
*/
public class DriveResetEncoder extends InstantCommand {
/**
* Add your docs here.
*/
public DriveResetEncoder() {
super();
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
requires(Robot.m_drivetrain);
}
// Called once when the command executes
@Override
protected void initialize() {
Robot.m_drivetrain.resetDriveEncoder();
}
}