Commands not being interrupted

Team 1726 is programming their robot using C++ for the first time this year. We are having trouble getting commands to interrupt each other.

In our current test setup we have two commands: DriveForward and DriveBackwards, set to Joystick buttons. Each command requires the drivetrain subsystem, and has had SetInterruptable(true) called. The relevant snippets of code are:

Constructor from DriveForward.cpp:

DrivingForward::DrivingForward() : CommandBase("Foward") {
	// Use Requires() here to declare subsystem dependencies
	// eg. Requires(chassis.get());
	Requires(drivetrain.get());
	SetInterruptible(true);
}

Constructor from DriveBackwards.cpp:

DrivingBackward::DrivingBackward() : CommandBase("Backwards") {
	// Use Requires() here to declare subsystem dependencies
	// eg. Requires(chassis.get());
	Requires(drivetrain.get());
	SetInterruptible(true);
}

Class declaration from CommandBase.h:

class CommandBase: public frc::Command {
public:
	CommandBase(const std::string& name);
	CommandBase() = default;

	// Create a single static instance of all of your subsystems
	static std::unique_ptr<OI> oi;
	static std::unique_ptr<DriveTrain> drivetrain;
};

Class definition from CommandBase.cpp:

std::unique_ptr<OI> CommandBase::oi = std::make_unique<OI>();
std::unique_ptr<DriveTrain> CommandBase::drivetrain = std::make_unique<DriveTrain>();

CommandBase::CommandBase(const std::string &name) :
		frc::Command(name) {

}

Constructor from OI.cpp:

OI::OI() :
	driver(JOY_DRIVER),
	driverB(&driver, BUTTON_B),
	driverA(&driver, BUTTON_A),
	driverX(&driver, BUTTON_X)
{
	// Process operator interface input here.
	driverA.WhenPressed(new DrivingForward);
	driverX.WhenPressed(new DrivingBackward);
}

The commands will execute successfully, but when a second command is started both commands will be trying to run at the same time.

What you are doing looks right. We never call SetInterruptable(true) and our commands interrupt fine but that could just be because interruptable is the default.

EDIT: Ignore this line. I was thinking “WhilePressed”. I wonder if it has to do with how WhenPressed works. It could be that the WhenPressed functionality keeps calling your command over and over.

One thing that we found helped to track down what’s actually going on is to put a “std::cout << “Starting <fill in command name here> command” << std::endl;” in the initialize for every command. Do the same for ends and interrupted methods as necessary. Then in the driver’s station, click the gear in the right pane and select Prints. You should see the messages there while you’re driving. You might be surprised at when commands actually run.