Our team has been using RobotPy for already 2 years with the MagicBot framework, and we’ve always questioned the way we currently implemented pressed actions - i.e actions that happen only once when a joystick button is pressed.
Due to the fact that both components and the robot itself run in a loop, we have to check whether the joystick state was changed from the previous iteration, and so we use a boolean variable for every action. The code gets a bit messy, and it seems like it can be implemented a lot better.
Currently, the structure of such an action inside teleopPeriodic looks similar to:
if button.get() and not self.did_action: self.component.do_action() self.did_action = True else: self.did_action = False
From our perspective, it looks weird that the handling of button states is done in the robot class, especially not creating a boolean variable for every such action. It is possible to do the same thing inside the specific component creating 2 functions like:
def do_action(self): if not self.did_action: self.action_flag = self.did_action = True def reset_action(self): self.did_action = False
And then in teleopPeriodic:
if button.get(): self.component.do_action() else: self.component.reset_action()
Both ways seem pretty messy and require creating functions and variables for each and every such action - which is bad practice and creates a ton load of code repetition which cannot be generalized because the components are not related to each other. Have any of you or your team found a better solution to this problem, or perhaps something can be done within the library to make such actions easier?
Thanks in advance,
~ Nick S. - Phoenix 5654