Log in

View Full Version : Run Command Once When Trigger is Pressed


inkspell4
09-02-2013, 16:40
How do i make it so that a command runs once when the trigger is pressed?

Ether
09-02-2013, 16:47
How do i make it so that a command runs once when the trigger is pressed?

Set a boolean TRUE on rising edge of trigger, and

Run the command when the boolean is TRUE and set the boolean OFF after running.

Arhowk
09-02-2013, 16:49
yourTrigger.whileActive(yourCommand);

krieck
09-02-2013, 16:50
Try:

Joystick stick = new Joystick(1);
JoystickButton trigger = new JoystickButton(stick, 1);
trigger.whenPressed(new MyCommand));

inkspell4
09-02-2013, 16:58
Set a boolean TRUE on rising edge of trigger, and

Run the command when the boolean is TRUE and set the boolean OFF after running.




Could you give me an example of how to do this

RufflesRidge
09-02-2013, 17:02
Could you give me an example of how to do this

If you're using the Command Based templates I second what Kriek is suggesting.

Ether's suggestion is the generic description of exactly what Kriek
suggestion does for you.

inkspell4
09-02-2013, 17:12
If you're using the Command Based templates I second what Kriek is suggesting.

Ether's suggestion is the generic description of exactly what Kriek
suggestion does for you.

It doesn't work because the execute command keeps going on. Is there a way to stop the execute command and make the subsystem revert to the default command.

Arhowk
09-02-2013, 17:14
It doesn't work because the execute command keeps going on. Is there a way to stop the execute command and make the subsystem revert to the default command.
refer back to my first post. :(

inkspell4
09-02-2013, 17:15
refer back to my first post. :(

Will that work even if the command takes more time to execute than the trigger is held for?

Arhowk
09-02-2013, 17:20
Will that work even if the command takes more time to execute than the trigger is held for?

No.

if you use whenPressed (or whenActive), it will activate when the button is pressed and wont stop untill isFinished returns true

if you use whileHeld (or whileActive), it will activate when the button is pressed and wont stop untill isFinished returns true OR the button is released (if the button is released, the command is cancelled but the "end" method will not run and the "interrupted" method will. Keep that in mind when setting things like shooter jags)

BradAMiller
09-02-2013, 17:21
Here is an example of running commands whenever a joystick button is pressed. You can also associate commands with buttons from the Cypress module or any arbitrary polled trigger (see Trigger class).

http://Wpilib.screenstepslive.com/s/3120/m/7952/l/97457

Besides using whenPressed() you can also call whenReleased() to get a command to run when the button is released or whileHeld() to get a command to repeatedly run while the button is pressed.

Brad

RufflesRidge
09-02-2013, 17:21
It doesn't work because the execute command keeps going on. Is there a way to stop the execute command and make the subsystem revert to the default command.

That's what isFinished() is for.

inkspell4
09-02-2013, 17:22
That's what isFinished() is for.

Forgot about that, Thanks

Peragore
10-02-2013, 19:54
Probably already solved, but what I would do is assign a flag to whether the code is required to run. so, when the trigger is pressed (>= .01, maybe), you set the flag to true. the method runs when the method is true, and at the end of the method, it sets the flag to false. Granted this is using SimpleRobot template.

NotInControl
12-02-2013, 20:35
Simply set the isFinsihed() method to return true;


The execute method will run once, isfinsih() will be true and then the command will call the end() method and die.