| 663 | |
| 664 | |
| 665 | class LaneInvasionSensor(object): |
| 666 | def __init__(self, parent_actor, hud): |
| 667 | self.sensor = None |
| 668 | self._parent = parent_actor |
| 669 | self.hud = hud |
| 670 | world = self._parent.get_world() |
| 671 | bp = world.get_blueprint_library().find('sensor.other.lane_invasion') |
| 672 | self.sensor = world.spawn_actor(bp, carla.Transform(), attach_to=self._parent) |
| 673 | # We need to pass the lambda a weak reference to self to avoid circular |
| 674 | # reference. |
| 675 | weak_self = weakref.ref(self) |
| 676 | self.sensor.listen(lambda event: LaneInvasionSensor._on_invasion(weak_self, event)) |
| 677 | |
| 678 | @staticmethod |
| 679 | def _on_invasion(weak_self, event): |
| 680 | self = weak_self() |
| 681 | if not self: |
| 682 | return |
| 683 | lane_types = set(x.type for x in event.crossed_lane_markings) |
| 684 | text = ['%r' % str(x).split()[-1] for x in lane_types] |
| 685 | self.hud.notification('Crossed line %s' % ' and '.join(text)) |
| 686 | |
| 687 | # ============================================================================== |
| 688 | # -- GnssSensor -------------------------------------------------------- |