@@ -19,7 +19,7 @@ def __init__(self, estop: EStop | None, **kwargs) -> None:
1919 """a bumper was triggered (argument: the bumper name)"""
2020 self .BUMPER_RELEASED = Event [str ]()
2121 """a bumper was released (argument: the bumper name)"""
22- self .active_bumpers : list [str ] = []
22+ self .active_bumpers : set [str ] = set ()
2323
2424
2525class BumperHardware (Bumper , ModuleHardware ):
@@ -50,13 +50,14 @@ def __init__(self, robot_brain: RobotBrain, *,
5050 estop = estop )
5151
5252 def handle_core_output (self , time : float , words : list [str ]) -> None :
53- active_bumpers = list ( self .active_bumpers )
53+ previous_active_bumpers = self .active_bumpers . copy ( )
5454 states : dict [str , bool ] = {name : words .pop (0 ) == 'true' for name in self .pins }
55- self .active_bumpers [:] = [name for name , active in states .items () if active ]
55+ self .active_bumpers .clear ()
56+ self .active_bumpers .update (name for name , active in states .items () if active )
5657 if self .estop and self .estop .active :
5758 return
5859 for name , active in states .items ():
59- was_active = name in active_bumpers
60+ was_active = name in previous_active_bumpers
6061 if active and not was_active :
6162 self .BUMPER_TRIGGERED .emit (name )
6263 elif not active and was_active :
@@ -68,7 +69,8 @@ class BumperSimulation(Bumper, ModuleSimulation):
6869
6970 def set_active (self , pin : str , active : bool ) -> None :
7071 if active and pin not in self .active_bumpers :
72+ self .active_bumpers .add (pin )
7173 self .BUMPER_TRIGGERED .emit (pin )
72- self .active_bumpers .append (pin )
7374 if not active and pin in self .active_bumpers :
74- self .active_bumpers .remove (pin )
75+ self .active_bumpers .discard (pin )
76+ self .BUMPER_RELEASED .emit (pin )
0 commit comments