Commands don't stop with whiletrue()

Here is some simple code in my robotcontainer class:

m_Controller.x().whileTrue(
     CommandUtililty.holdWrist()
);

my interpretation of the documentation is that when I hold the X button on the xbox controller, the command will run, and when I release the button, the command will stop and cancel.

the problem is, this doesn’t happen.

when I press the button, the command starts, and when I release the button, it continues to run.

Am I doing something wrong?

the same thing happens when I use the following:

m_Controller.x().whileTrue(
            CommandUtililty.holdWrist()
        ).whileFalse(
            new InstantCommand(() -> CommandUtililty.holdWrist().cancel())
        );

Another problem that is happening, is that even after disabling and reenabling the robot, the command still runs after reenabling the robot.

Anyone else encountered these problems before?

Link to full code: Files · Wrist · Elliot Scher / 2815 Robot 2023 · GitLab

Thanks!!

I just glanced through the code, but the first thought I had was: does anything tell the wrist control loop to stop holding position?

Your command is to set the wrist setpoint of your PID controller to match the current position. When that command stops running, the setpoint isn’t overwritten and the controller doesn’t stop running, right? The wrist controller is still active and still trying to reach the setpoint it is set to. Something else needs to happen to stop that.

1 Like

I will say this now I’m not entirely sure on how your code is structured.

But from experience of forgetting to set motor values back to 0 after a command is done and having things go haywire even through disables, it sounds like that may be the culprit.

1 Like

Your latest commit seems to have solved your issue, just a quick note that since CommandUtililty.holdWrist() returns a new command, doing CommandUtililty.holdWrist().cancel() will not cancel the existing command

You description is not accurate, it will not continuously restart the command, Trigger (WPILib API 2023.3.2). An easy way to do this is to add an End method, Command (WPILib API 2023.3.2), to the command so when the button is released and the Trigger cancels the command it runs that method to stop whatever u want.

3 Likes

Good information! I would not have expected that behavior based on how that method is named.