Command-Based Logic and Commands After Startup?

Our team is new to the WPILib command-based libraries and have been struggling to implement logic to choose paths for Galactic Search. Here is our code relevant to the problem:

public class RobotContainer {

   //command groups
   private final Command pathABlue = new PathABlue(m_robotDrive, m_intakeSubsystem);
   private final Command pathARed = new PathARed(m_robotDrive, m_intakeSubsystem);
    
   private Command GalacticAChooser() {
       if (powercell is in A path location) {
            return pathARed;
       }
       return pathABlue;
  }
  
  //later in program
  m_chooser.addOption("Galactic Search Path A", GalacticAChooser());
}

This sort of works, but the logic is only run on startup. If we deploy code or turn on the robot in the correct place, the path is chosen correctly, but if we do not, then the program fails to correctly locate the powercell.

There is a related issue where command groups do not reset after the robot is disabled (because they are defined as finals in the robot container), which makes testing a pain. We have looked for solutions, but have not found much. Are we missing something and is there a better way to do this? Any help would be appreciated!

Here is our code.

You need to consider when things are being evaluated. You want the path loads to be done on startup, but the actual choice part you want to be done on AutoInit.

First off you are adding path A to an auto chooser which means you are also adding path B. It is illegal to have the user select the path.

You could either break that command into its own file so you have direct control on when each part is run or if doing it inline you can use a lambda to ensure that the search part isn’t run until execution rather than on command creation.

1 Like

Thanks!

I suppose the way to choose paths during/after Init in our case would likely be to create a command that both chooses and schedules one of the path commands, though this will have to wait until tomorrow when I’m less tired and have our other programmers and the robot to work with.

Wow, how did we miss this??? Thanks so much for pointing that out and thank goodness it doesn’t require a major change.

I’m still not seeing a clear way to rerun SequentialCommandGroups without restarting, though that is a smaller issue and this might be enough for us to complete the challenge legally. Thanks again!