The idea behind this is that students understand the interplay between commands and subsystems. Once the subsystems are well defined with RobotBuilder (i.e., we have all our parts), the primary work will be:
- creating public methods that control the private subsystem components (e.g., ControllerGroup or PWMSparkMax) and
- creating commands that (a) identify the subsystem(s) that they need (via a call to addRequirements) and (b) use the accessor methods in the subsystem class to make the robot do things. By creating lots of small, simple commands as "modules," we can then build up to more complex sequential or parallel commands for complex actions
Assignment number 1 has some information on setting up wpilib.
Get the current source with git from https://github.com/FRC7660/Robot
-
Create a new command class
DriveForward.java
. Students can copy the AutonomousCommand.java file already in our repository but all command classes are simple to create because they have the same structure of initialize(), execute(), isFinished(), and end()). -
in DriveForward.java import the Drive system with:
import frc.robot.subsystems.Drive;
-
Create a new member called m_subsystem in
DriveForward.java
. The member's type is "Drive" i.e., the subsystem:Drive m_subsystem;
-
Create a constructor that adds the
Drive
subsystem requirements to the command scheduler (the scheduler will thus check if all the requirements for this command are available (e.g., motorcontrollers)):public DriveForward(Drive subsystem) { m_subsystem = subsystem; addRequirements(m_subsystem); }
In the drive subsystem (Drive.java) create two "set" methods--effectively these are accessor methods that give commands access to the parts (which are private members) of the Drive subsystem (e.g., the motor controllers)
// Put methods for controlling this subsystem
// here. Call these from Commands.
public void setLeftControllerGroup(double speed){
leftControllerGroup.set(speed);
}
public void setRightControllerGroup(double speed){
rightControllerGroup.set(speed);
}
Now go back to DriveForward.java and program the execute() method using the two "set" methods just created in Drive.java. Here is an example:
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
double speed = 0.5;
m_subsystem.setLeftControllerGroup(speed);
m_subsystem.setRightControllerGroup(speed);
}
In RobotContainer.java
comment the creation of the autonomous command 'Autonomous command' so it instead uses the newly created DriveForward command.
You can remove the original 'Autonmous Command' or comment it out.
//m_chooser.setDefaultOption("Autonomous Command", new AutonomousCommand());
m_chooser.setDefaultOption("DriveForward Command", new DriveForward(m_drive));
press F5
to build
Do this by pressing the Hardware button on the ribbon and then selecting "PWM Outputs"
Using the GUI toggle between auton and disable--you should see the PWM Outputs flipping on/off at 0.5 speed.