Running a command group during autonomous

Hi i created a command group to use during autonomous and i wanted to know how do i use the command group that i created during autonomous , ( we are using command based programming).
Thanks!

Use CommandScheduler.getInstance().schedule(command), where the command is your command or command group.

1 Like

If you are using this year’s new Command Based robot project setup, simply return an instance of the command group from RobotContainer’s getAutonomousCommand() method. As such:

public Command getAutonomousCommand(){
return new YourCommandGroup();
}

Then, if you have not edited the Robot class, when you run Autonomous mode on the Driver station, your code will run

1 Like

thanks! does it work with the old command base template and also we are pretty new to java do you have a picture of what is should look like?

The code for the old command framework is Scheduler.getInstance().add(command), which is very similar. However, the old framework is not recommended anymore.

What do you mean by a picture? This is a single line of code. If you are following the template, you could use the method described by @Arparas. Otherwise, you could simply call CommandScheduler.getInstance().schedule(command) inside the autonomousInit() function.

1 Like

Untitled
like that?

I’ve never used the old command framework on the robot, so if you insist on using that version, I can’t be of much help. However, it looks like you’ve already scheduled your command with Scheduler.getInstance().add(m_autonomousCommand);, so you don’t need section with the if statement

We cannot get a Command Group to run anything on our robot. We tried running the command group with a button while held and by setting it up in robot container to run the command. Can someone offer us guidance as to what we are doing wrong? This is only our 2nd year on java, so it may be something fairly obvious.

This is what we have in our SequentialCommandGroup:

package frc.robot.commands;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandGroupBase;
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import frc.robot.commands.Auto_Rotate;
import frc.robot.commands.Autodrive_forward;

public class ComplexAuto extends SequentialCommandGroup {

public void ComplexAuto() {
addCommands(new Auto_Rotate(5, 45));
addCommands(new Autodrive_forward(5, 5));

}
}

Our entire code is here: TigerTrons222-2020/Tigertrons222-TestCode at master · DJthefirst/TigerTrons222-2020 · GitHub

Thanks!

The if section is default, he added the other line.

@fr3sh, you don’t need to do that line. The m_autonomousCommand.start() is already doing that for you. All you need to do is make sure m_autonomousCommand is an instance of your command group. Right now, it’s set to whatever the m_chooser returns (look at the docs for how SendableChooser works it you don’t know). If you don’t want to use the chooser, then just replace that line you added with m_autonomousCommand = new YourCommandGroupClass()

1 Like

Untitled
like that?

If autonomous is your CommandGroup class name, then yes

1 Like

Thank you!

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