Need help, having trouble with defining the correct type of argument for Conditional Command

Our team is trying to use Conditional Command to select which game piece (cube or cone) our arm is trying to pick up, and we used this example in wpilib documentations:

frc2::ConditionalCommand(commandOnTrue, commandOnFalse, [&m_limitSwitch] { return m_limitSwitch.Get(); })

we structured our code based on the code above and this is what we have:

frc2::CommandPtr ArmSubsystem::floorPickupPosition(){
    frc2::ConditionalCommand(moveArmCommand(60,0) , moveArmCommand(30,0) , [this] { return this->isConeMode;} );

moveArmCommand is a command pointer and isConeMode is a boolean.

Intellisense is throwing this error on frc2:

no instance of constructor "frc2::ConditionalCommand::ConditionalCommand" matches the argument list"

When we tried to compile it, the compiler threw an error of:

no known conversion for argument 1 from 'frc2::CommandPtr' to 'std::unique_ptr<frc2::Command>&&' 57 | ConditionalCommand(std::unique_ptr<Command>&& onTrue

We don’t know how to convert CommandPtr to std::unique_ptr.

We have two days left before our competition please help. Thanks!!!

You can use Unwrap() to get a std::unique_ptr<CommandBase> from a CommandPtr. So assuming that e.g. moveArmCommand() returns a CommandPtr, you want to do moveArmComand(60,0).Unwrap().

It also looks like you’re missing a return statement from the code you pasted.


that worked for us!! Thank you very much!

frc2::CommandPtr ArmSubsystem::floorPickupPosition(){ return frc2::ConditionalCommand(moveArmCommand(0.0, 75.0).Unwrap(), moveArmCommand(10.0, 111.5).Unwrap(), [this] {return this->isConeMode;} ).ToPtr(); };

this is the code that worked for us and thanks to Peter Johnson, the .Unwrap is what we needed. The .ToPtr() converts the ConditionalCommand to a CommandPtr.

As a side note, frc2::cmd::Either (from frc2/Commands.h) should make this easier, as it accepts and returns CommandPtrs.

That header also has some more useful factory functions, I suggest taking a look!

1 Like

thanks!!! We’ll try that out.

1 Like

Hi Starlight,
does the frc2::cmd::Either command still exist in wpilib 2024, We having trouble implementing it.

We are trying to make an “either” command that is returned from a subsystem as a CommandPtr. The condition is to be based on alliance color at run time and the true/false commandPtrs to choose from are two methods from inside the same subsystem that both return CommandPtrs.
eg in subsystem .h

CommandPtr TurnIntakeToRight(); 
CommandPtr TurnIntakeToLeft();
CommandPtr TurnIntakeToRightForBlueAutos();  // This command is used in pathplanner autos that get mirrored when running on the Red alliance.  If we are red, our intake direction need to be flipped

What should we include, commands.h doesn’t appear to be at frc2/commands.h

edit: Found the correct header, it looks like the include needs to be frc2/command/commands.h

Should it look something like this

CommandPtr TurnIntakeToRightForBlueAutos(){  
return frc2::cmd::Either(TurnIntakeToLeftt(),TurnIntakeToRight(), 
    [] { return (frc::DriverStation::GetAlliance().value() == frc::DriverStation::Alliance::kRed);}

Do we have to capture [this] in the bool lambda, I’m never sure how to use these?
Also do we have to check for the non existence of a driver station colour, how can there be no driver station colour?

Thanks for any help

Your code looks good!

You only need to capture this if you’re accessing member variables and not capturing them directly.

If the driver station hasn’t connected yet, an empty optional is returned. Since this is inside a command that necessarily runs when enabled (which means that a driver station is connected), I’d say it’s fair to assume that there won’t be an empty color.
This is better than the previous behavior, which returned red by default.

1 Like