View Single Post
  #9   Spotlight this post!  
Unread 27-01-2017, 00:02
wlogeais wlogeais is offline
Registered User
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2016
Rookie Year: 2011
Location: Minnesota
Posts: 18
wlogeais is an unknown quantity at this point
Re: Building Commands at Runtime

Quote:
Originally Posted by dmelcer9 View Post
Last year, our team tried to have a modular autonomous command. The way this worked was something like this:

...

Then, in a CommandGroup created during AutonomousInit:

Code:
addSequential((Command)Robot.autoDefenseChooser.getSelected());
However, this runs into a few problems. The biggest problem was that the robot would crash if you enter Autonomous mode twice without power cycling the robot...
Here are two solutions to this: the boring way and the fun(ctional) way.
In my opion the root of this issue is from missuse of CommandGroup itself rather than alternatives which can be written for special situations like this.

Call option 3, and either boring or exciting if you like.
I'm maining use of your same 2 chooservars for this year as they are.

I'd just change the autonomousInit() to a new commandgroup-like alternative.

Code:
autonomousCommand = new MultiAutonomousStarter();
     // REPLACES ... =  chooser.getSelected();

// unchanged from wpilib default...
if (autonomousCommand != null)
	autonomousCommand.start();
And then depending on the situation any 'command' can behave like a CommandGroup with special powers too.

(such as here in my MultiAutonomousStarter example to handle your 2-choosers)

Code:
	private Command step1cmd;
	private Command step2cmd;
	
	public MultiAutonomousStarter() {
		// FOLLOWING is NOT needed because this command will work like a commandgroup.
		// requires(Robot.exampleSubsystem);
	}

	// here the chooser-setup  is locked in.
	protected void initialize() {
		step1cmd = Robot.autoStep1.getSelected();
		step2cmd = Robot.autoStep2.getSelected();
		
		step1cmd.start();
	}

	// Here a sequential-like handoff or other special control is maintained.
	protected void execute() {
		if ( step1cmd!=null && !step1cmd.isRunning())
		{
			step1cmd = null;
			step2cmd.start();
		}
			
	}

	// isFinished() { return false; } is required revised it could add its own flexibility...

	// end() {} is required but not relevant this time.

	// in this case Called when autonomous times out.
	protected void interrupted() {

		if ( step1cmd!=null && step1cmd.isRunning())
			step1cmd.cancel();

		if ( step2cmd!=null && !step2cmd.isRunning())
			step2cmd.cancel();
	}
Reply With Quote