How does one initialize code?

Hi. Our team is new to command-based programming. We are wondering where does the code initialize all of our commands. We have all of our commands defined but do not know where the commands begin running. We have code for the drive train and the lift mechanism.

Greetings. Have you looked through this yet?


This documentation seems very old. The updated documentation can be found here.
OP, are you using the old or new Command-based framework?


A specific command can start running in a few different ways:

  1. Pass an instance into the setDefaultCommand(Command) method of a subsystem
  2. Add it to the SmartDashboard via the SmartDashboard.putData(Sendable) method. This creates a UI button on the dashboard you can click to start the command. (Typically not practical for actual game play, but good for prototyping and testing.)
  3. Call the Command.schedule() method on it in version 2 of the Command Based API, or the Command.start() method in version 1 of the Command Based API . This “adds” it to the scheduler so on the next loop it runs (loops are by default every 20ms). This is typically how you start an autonomous command. In autonomousInit() you call schedule() (or start() in v1) on the command you want to run.
  4. Pass it into a Joystick (or gamepad) button method. Then perform the action on the joystick (such as pressing, holding, or releasing a button). See the example below.

Example of using commands with a Joystick:

Joystick joystick = new Joystick(1);
JoystickButton button = new JoystickButton(joystick, 5);
button.whenPressed(new StartMotorCommand());
button.whenReleased(new StopMotorCommand());

It’s important that you read through this section a few times, writing down the nuances you find. This is the hardest thing for new FRC programmers to understand when using the Command-Based framework. However, once they get it, it massively simplifies things mentally.

The most important thing you can glean from that is how does the Scheduler work, and how does it manage what is interacting with your subsystems.

The Default Command is the command running on the Subsystem when no other command is scheduled to run that requires that particular subsystem. So, a good example would be a DriveTrain subsystem, where by default you use joysticks to control the robot. Other commands that may require the drivetrain would be something like “DriveDistance” or “TurnToAngle”, when those are scheduled, they preempt the Default Command to get their stuff done.

So now the question is how does one schedule others? You basically just need some subclass of Trigger and Bob’s your uncle.

More specifically, the common case in FRC is to assign commands to buttons on joysticks, but Triggers can be activated from the SmartDashboard/Shuffleboard via a mouse click, over the network, or a few other ways. Here’s how you bind those commands to triggers.

TLDR version of this answer:
Instantiate your Commands and bind them to triggers in the file AFTER you have instantiated your subsystems so you can pass them all in as dependencies to the commands.

1 Like

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