Command Based binding buttons

When using the command based framework, is it valid to schedule commands within robot.py in teleopPeriodic, instead of configuring buttons in robotcontainer.py?

Ex:

def teleopPeriodic(self) -> None:
    if self.container.driverController.getAButton():
        MyCommand(self.container.drive).schedule()
1 Like

It will work as far as I can tell, but why would you want to do that?

That looks like it would keep creating and scheduling new instances of MyCommand as long as the A button is held, so after holding A down for 1 second you’d have 50 copies of MyCommand running.
I think it needs to be more like this:

// Somewhere else like in  some *init() code create
    self.my_command = MyCommand(self.container.drive)

def teleopPeriodic(self) -> None:
    if self.container.driverController.getAButton():
        self.my_command.schedule() 

It’s OK to keep calling schedule(). First time around it actually starts the command. If the command is already running, schedule() doesn’t do anything.

I would recommend reading the documentation for command-based programming for good patterns to emulate. In particular, the section “Binding Commands to Triggers” seems like what you’re interested in.

Edit: I suspect I didn’t read your question carefully enough, so you probably already knew that. FWIW, I’m not a commands-based expert, but it does seem to me that it’s an anti-pattern to even have a command robot with a teleopPeriodic that does anything other than running the scheduler.

You can, but to make it do anything useful you’d have to re-engineer the internals of the Trigger class (in particular, the edgefinding logic) and you’d miss out on all of the convenient features of the existing implementation. This seems like a poor use of resources.

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