There are a few things that my team would like to have happen automatically and all in order during teleop and we figured a CommandGroup is best for that. It seems to be running, I have it print to the console that the CommandGroup and following Commands are run. However nothing happens. When we run the Commands that the CommandGroup are running one by one and not in the group, they individually work fine. This leads me to believe it is an issue with CommandGroups and teleop.
To start the CommandGroup, we run a Command based off of a button press. In that Command’s execute(), a new instance of the CommandGroup that we want to run is instantiated. The isFinished() for that Command returns true so it only runs once.
For example:
public void execute(){
ExampleCommandGroup name = new ExampleCommandGroup();
name.start();
}
public boolean isFinished(){
return true;
}
After typing this out, I had an idea. Would starting the ExampleCommandGroup and then finishing the command you started the ExampleCommandGroup in end the ExampleCommandGroup?
EDIT: This is our first year using Java. Would starting/stopping commands like this potentially cause memory issues? Or, when a command is interrupted, does it 100% without fail stop existing? As in the object that was that class no longer exists?
You don’t need to run the CommandGroup within a new Command, it is a subclass of Command.
So when you want the button to run the command group, just do:
“myButton.whenPressed(new MyCommandGroup());”
Is it possible that a default command for a Subsystem or another command being issued automatically is Causing your command group to be interrupted?
When running command groups, any other command that requires any of the subsystems inside of the command group will interrupt the command group, unless the commands are set to not be interruptible.
also, an error happens when you try to call
“.start()” on a command group.
You should never really be calling “.start()”, instead do “Scheduler.getInstance().add(new MyCommand());”
Oh wow, thanks for that. And to address your previous comment, you’re right that I should change it out of the command. I was running something else before running the CommandGroup but that can just become a Command run in the CommandGroup. Thanks!
I’ve logic-ed everything out (I think) to where any interrupts do what they need to do and have made sure I know when they’re being run. I’ve even added prints to the console in each interrupt. When I run the CommandGroup I have, on the console I see all the print outs that I’ve put in the CommandGroup but it’s as if the Commands that are being run don’t run. So I’m not really sure what the problem is.
the command will instantly be canceled because isFinished is already true. What you really need to do is this:
boolean isDone = false;
public void execute(){
ExampleCommandGroup name = new ExampleCommandGroup();
name.start();
isDone = true;
}
public boolean isFinished(){
return isDone;
}
But an even more complete method of doing this is to do this:
public class yourCommandGroupClassName extends CommandGroup {
public yourCommandGroupClassName() {
addSequential(new differentCommand());
addParallel(new driveWithOI());
}
}
public class differentCommand extends Command {
boolean isDone = false;
public differentCommand() {
}
protected void execute() {
//put what your command should do here
isDone = true;
}
protected boolean isFinished() {
return isDone;
}
protected void end() {
//put what your command should do when isFinished = true when polled.
//ie. stopping your motors if you started them in the command
}
}
public class driveWithOI extends Command {
public driveWithOI() {
requires(Robot.drivetrain);
}
protected void execute() {
drivetrain.drive(oi.lJoy.getRawAxis(RobotMap.JOYSTICK_Y_AXIS), oi.rJoy.getRawAxis(RobotMap.JOYSTICK_Y_AXIS);
}
}
Use the second method; Its better. The way that command groups should work is either a series of individual commands going off one after the other(addSequential()), or individual commands all going off at the same time(addParallel()). If you use parallel commands, be warned however that two commands that use the same subsystem cannot be run at the same time(This is what requires(aSubsystem) does).