Help with Command Based Programming

I am using command based for the first time and having trouble know where to place certain code. I keep getting errors that I cannot resolve.

Here is the code that I am trying to place in the Command Base structure. Can someone help as to where it should go? And if there is additional steps with it?

//drive train in motor init

m_leftMainMotor = new CANSparkMax(leftMainID, MotorType.kBrushless);
m_leftFollowMotor1 = new CANSparkMax(leftFollow1ID, MotorType.kBrushless);
m_leftFollowMotor2 = new CANSparkMax(leftFollow2ID, MotorType.kBrushless);
m_rightMainMotor = new CANSparkMax(rightMainID, MotorType.kBrushless);

m_rightFollowMotor1 = new CANSparkMax(rightFollow1ID, MotorType.kBrushless);
m_rightFollowMotor2 = new CANSparkMax(rightFollow2ID, MotorType.kBrushless);

//factory default


//current limit


//follow method & drive


//differential drive
m_myRobot = new DifferentialDrive(m_rightMainMotor, m_leftMainMotor);

Thank you, I feel like I am hitting a dead end.

Tim Lemke

I would highly recommend reading over the Command-Based Documentation on WPILIB Command-Based Programming — FIRST Robotics Competition documentation and maybe also a video series on it, like team 6841 Cerberus 2.0’s playlist First Robotics Competition - Command Base System 2020 - VS Code - Java - YouTube or the relevant videos from team 3847 Spectrum’s Curriculums Spectrum FRC Training Curriculum

As for your specific question, Command Based is split up into 4 main pieces. With two being explicit classes in your code, and two being concepts. One of these concepts is the Subsystem, which defines the interface to your physical robot hardware. So anything relating to the motors, relays, pneumatics, sensors, ect. directly should go in a subsystem, in this case a Drivetrain subsystem or similarly named.

The other concept is Commands, which is where you tell your subsystems to actually DO things. The two “explicit” ones are the Constants and the RobotContainer. Constants is where you put your “hard” numbers, like port numbers, can IDs, setpoints, ect. It can be very beneficial to have all of these in one place and named so that looking at code later, your numbers all have names instead of wondering what the random 137.2 means in your PID output adjustment code or something. The RobotContainer on the other hand is where everything comes together. Its where you instantiate your subsystems, controllers, and buttons/triggers on those controllers. Then you assign those buttons to commands, which in turn manipulate subsystems.

@ExploitSage has a very good explanation

There are 3 main places to put code (although you will put code in other places, this is where most of your editing will be)

  1. subsystems
  2. commands
  3. robotContainer

Subsystems are physical hardware/subsystems of your robot. Think drivetrain, climber, shooter, elevator, etc

Commands tell a subsystem or a group of subsystems what to do. For example “set shooter speed” or “run intake”

RobotContainer is where you map your controller buttons to your commands. For example, when you press A, run the intake

You will also put code in

Our code is very basic but got the job done and worked really well. Take a look if you get stuck

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.