Skip to content

Instantly share code, notes, and snippets.

@smoser
Created January 31, 2022 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoser/6ed10bb6b71b8f3067a77ee88a17383b to your computer and use it in GitHub Desktop.
Save smoser/6ed10bb6b71b8f3067a77ee88a17383b to your computer and use it in GitHub Desktop.
Byting Irish Programming Assignment 2.

Byting Irish Programming Assignment number 2

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.

Step 1

Get the current source with git from https://github.com/FRC7660/Robot

Step 2

  • 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);    
     }

Step 3 Creaet set methods for Left and Right speed.

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);              
}

Step 4

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);
    }

Step 5

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));

Buidl the code

press F5 to build

Turn on display for motor controllers

Do this by pressing the Hardware button on the ribbon and then selecting "PWM Outputs"

Toggle between auton and disable

Using the GUI toggle between auton and disable--you should see the PWM Outputs flipping on/off at 0.5 speed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment