Hi,
How do you cancel a commandgroup (either sequential or parallel) that is currently running, from coding logic inside one of the commands that is in the group?
eg a command group that moves, intakes balls, looks for a target, aligns to the target, spins up shooters, moves again, intakes more balls, looks for target, aligns to the target, spins up shooters, etc, etc. If something goes wrong somewhere in this process it would be nice to cancel the whole commandgroup.
To use the .cancel() method on the commandgroup you would need to pass a pointer to commandgroup into the commands that are inside the group and this pointer doesn’t exist at the time of creating the group. Do you have to create a pointer to a pointer that holds a command and then pass this value into the sub commands and then double de-reference it to call the .cancel() method on command group?
Another way I can see how to do this is to pass the address of some global boolean variable into ever command in the group and then have this boolean checked in the isfinished method of every command inside the group. Problem with this approach is that the initialize method of every subsequent command will still run once unless the initialize method also checks this boolean.
Another option would be to make some fake subsystem and pass the address of this subsystem into every sub command that has the ability to cancel the group and then use the frc2::scheduleCommand to branch out to another new command that requires the fake subsystem. This would interrupt the command group (i think)
All of these methods seem to be an ugly workaround to what should be an easy problem, there must be an easier way.
Hi, Thanks for your insight. I can sort of see how this would work, but it still doesn’t really do what we need it to do in our situation. I’d like the command to be able to end naturally if it finishes everything without issue ( A Parallel Race Group instead of a deadline group could fix this)
But in our situation we want to bail out on a multi stage auto run if at any shooting point in the run the target detection isn’t valid at the time it should be shooting or if a collision it detected during a move. If this was implemented as a deadline or race group it could finish the whole command group at any time if the target wasn’t valid for a spilt second. (which could easily happen when following a complex trajectory that faces the limelight away from the target)
But I can now see a fairly simple work around for this. You would need to pass the address of the specially created race command to the other commands in the group that need the ability to cancel the whole group. Then you could cancel this race command and I assume this would causes it to end which would in turn cause the command group to end. Might have to try this out.
It’s the same thing, .raceWith() creates a ParallelRaceGroup under the hood and if indeed it is Java-only, it is to reduce verbosity that exists only in Java.