Replies: 2 comments 3 replies
-
DeferredCommand can always work for this stuff, just be careful to list requirements properly. For the case of choosing between commands at schedule time, |
Beta Was this translation helpful? Give feedback.
-
So at this point I ended up creating a base class that stores a The code I'm using is : class ConstructOnDemandCommand {
public:
ConstructOnDemandCommand() = default;
virtual void Schedule() final {
cmd = CommandFactory().Unwrap();
cmd->Schedule();
}
private:
virtual frc2::CommandPtr CommandFactory() = 0;
std::unique_ptr<frc2::Command> cmd;
}; This class is derived from and the If a class called joystick.Button(1).OnTrue( frc2::cmd::RunOnce([this] { timer.Schedule(); } ) ); This cleans up my |
Beta Was this translation helpful? Give feedback.
-
I have been having trouble with Commands that need to know the current state of the robot when they are triggered. This came about because we wanted to create a command that would determine which of the three stage climbing locations was closest and move to the correct position, climb, and insert the note in the trap. This was a fairly complex set of sequential commands. They way our team made this work was to subclass SequentialCommandGroup and then allocate an instance of it (with new) and schedule it each time the button was pressed. This worked.
I have recently been trying to move to factory methods for my commands and so I moved the complex sequential commands to a factory function returning a CommandPtr. I'm not sure the best way to get the same behavior as in the case where the command is a subclass of SequentialCommandGroup or if that is even possible.
The simplest case I could come up with is:
with the button binding being:
joystick.Button(1).OnTrue( autos::StateCommand( &m_subsystem) );
When this is simply bound to a Trigger it prints the start time of when the code initialized and the correct ending time. Is there an elegant way to make this code work correctly? I can't use the same allocation trick since the factory method is not a class. Am I missing something?
The SequentialCommandGroup code that does work correctly is:
with the button binding code as:
where
cptr
is aCommand *
initialized to nullptr as a member of the RobotContainer.I could leave the commands that need state information as SequentialCommandGroup subclasses but I can't figure out how to add a
CommandPtr
type to theAddCommands()
function for the other Commands that I have converted to factory methods. It is mentioned at the bottom of https://docs.wpilib.org/en/stable/docs/software/commandbased/cpp-command-discussion.html that Unwrap() is used for command compositions but I have not been able to figure out how to make that work.Beta Was this translation helpful? Give feedback.
All reactions