Issues Executing a Command More than Once

Hoping someone on here can help out. Our team, 6808, is a second year team, and I am a first year mentor for software. We are stumped. For our command based robot code, we have a command to drive a given distance using our encoders when a button is pressed. The command executes fine once. After that, it seems that the command does not create a new command and call the constructor, so isFinished immediately returns true and won’t execute again unless the roboRIO is reset, or the code is redeployed. Any suggestions would be greatly appreciated.

Here is the code:

OI.java that creates the command on the button press, the command in question is the button press that creates the DriveToDistance2 command

OI.java (5.0 KB)

DriveTrain Code:
DriveTrain.java (3.1 KB)

Command DriveToDistance2 Code:
DriveToDistance2.java (2.5 KB)

Nevermind - I see the bug now. Your end() call sets distance to 0.

Shouldn’t distance be reset back to 12 the next time the command is created on a new button press?

driverX_Start.whenPressed(new DriveToDistance2(12.0, 0.5));

No - your command is only ever created once. After that, further button presses will only call initialize()

(followed by execute() and so forth)

That expains it. I didn’t realize the constructor would only be called once even with the “new” command. I’m new to java and FRC, is this behavior due to java or wpilib? I’m used to C++, “new” will allocate a new object and always call the constructor. Thanks for the help, it’s been driving me crazy for nearly a week!

Never mind. I see it now. The when pressed is in the OI constructor so only executed once. Dumb mistake on my part. Thanks again for the fix!

You’re ahead of me at that point. It took a few years to realize why there aren’t memory leaks all around the command framework.

The whenPressed is setup in a constructor, but is a listener and will fire every time your button is pressed.

Constructors are executed when an object is instantiated. That’s why the initialize and execute methods are so useful in commands. You wouldn’t try to call a constructor over and over again. You could rely on the initialize/execute methods, or write your own method that gets called from within the initialize or execute methods.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.