![]() |
How to change command associated with button
We're doing some night-of-regionals programming changes, and one idea we were investigating was swapping the command associated with a button by calling WhenPressed() to replace the command bound to the button. (Our goal was to make one button that advances through a couple different states).
Looking at the WPILib code, it looks like WhenPressed() calls are additive, and it's not possible to replace the command associated with a button. Is that correct? Our alternate approach is to maintain a state variable and vary the behavior of the associated command based on the state, but it seems less clean than replacing the command. |
Re: How to change command associated with button
Another option might be to have the command call other "subcommands" based on the state variable. That way you can keep all the commands separate, with the only messiness being in the main command that does nothing except dispatch to other commands.
|
Re: How to change command associated with button
Alan -- good idea. Our programmer Ryan came up with a similar solution where a command has a Boolean state that indicates which of the two desired command actions should be executed. It then schedules the desired command, and slaves IsFinished() to the same state of the scheduled command. I'll see if I can get him to post it here so that the there's a record for the community in the future.
|
Re: How to change command associated with button
Quote:
|
Re: How to change command associated with button
@jwakeman: your issue appears to be that your alternate condition was causing a new command to be created every iteration. This would cause a new command to be created, assigned to the button, and interrupt the old each iteration.
I haven't tried this code, but the pattern I used was like this: Code:
<in class definition>I put this code in the OI class, and added an UpdateOI function, which is called from the teleop periodic loop. |
Re: How to change command associated with button
Quote:
Code:
if(throwSwitch1.get() && oi.current_interface == oi.CONFIG_FIELD_INTERFACE) |
Re: How to change command associated with button
This is what Ryan from 3081 put together. We did a bit of testing of it at Wisconsin, will be bashing it more in the next few days.
Code:
|
Re: How to change command associated with button
Here is a updated version of the swap command. It waits a few cycles before checking to see if the command has finished, to allow the command to be scheduled and then ran by the scheduler.
Header Code:
#ifndef SWAPCOMMANDCOMMAND_HCode:
#include "SwapCommandCommand.h" |
| All times are GMT -5. The time now is 12:09. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi