Skip to content

Commit 3b2ba61

Browse files
turn active collections into sets
1 parent 22e097f commit 3b2ba61

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

rosys/hardware/bumper.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2525
class 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)

rosys/hardware/estop.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, **kwargs) -> None:
2222
"""the e-stop was triggered (argument: the e-stop name)"""
2323
self.ESTOP_RELEASED = Event[str]()
2424
"""the e-stop was released (argument: the e-stop name)"""
25-
self.active_estops: list[str] = []
25+
self.active_estops: set[str] = set()
2626

2727
@property
2828
def active(self) -> bool:
@@ -37,10 +37,10 @@ def is_soft_estop_active(self) -> bool:
3737
async def set_soft_estop(self, active: bool) -> None:
3838
"""Set the soft e-stop to the given state."""
3939
if active and not self.is_soft_estop_active:
40-
self.active_estops.append('soft')
40+
self.active_estops.add('soft')
4141
self.ESTOP_TRIGGERED.emit('soft')
4242
elif not active and self.is_soft_estop_active:
43-
self.active_estops.remove('soft')
43+
self.active_estops.discard('soft')
4444
self.ESTOP_RELEASED.emit('soft')
4545

4646

@@ -65,11 +65,12 @@ async def set_soft_estop(self, active: bool) -> None:
6565
await self.robot_brain.send(f'en3.level({"false" if active else "true"})')
6666

6767
def handle_core_output(self, time: float, words: list[str]) -> None:
68-
active_estops = list(self.active_estops)
68+
previous_active_estops = self.active_estops.copy()
6969
states = {name: words.pop(0) == 'true' for name in self.pins}
70-
self.active_estops[:] = [name for name, active in states.items() if active]
70+
self.active_estops.clear()
71+
self.active_estops.update(name for name, active in states.items() if active)
7172
for name, active in states.items():
72-
was_active = name in active_estops
73+
was_active = name in previous_active_estops
7374
if active and not was_active:
7475
self.ESTOP_TRIGGERED.emit(name)
7576
elif not active and was_active:

0 commit comments

Comments
 (0)