diff --git a/modules/detection_cluster.py b/modules/detection_cluster.py new file mode 100644 index 0000000..4135c51 --- /dev/null +++ b/modules/detection_cluster.py @@ -0,0 +1,39 @@ +""" +LiDAR detection cluster data structure. +""" + +from . import lidar_detection + + +class DetectionCluster: + """ + Cluster of LiDAR detections. + """ + + __create_key = object() + + @classmethod + def create( + cls, detections: "list[lidar_detection.LidarDetection]" + ) -> "tuple[bool, DetectionCluster]": + """ + Combines lidar readings in close proximity. + """ + return True, DetectionCluster(cls.__create_key, detections) + + def __init__( + self, create_key: object, detections: "list[lidar_detection.LidarDetection]" + ) -> None: + """ + Private constructor, use create() method. + """ + assert create_key is DetectionCluster.__create_key, "Use create() method" + + self.detections = detections + + def __str__(self) -> str: + """ + String representation. + """ + detections_str = ", ".join(str(detection) for detection in self.detections) + return f"{self.__class__.__name__} (Count: {len(self.detections)}): {detections_str}. " diff --git a/modules/lidar_detection.py b/modules/lidar_detection.py index 3912052..88ea91a 100644 --- a/modules/lidar_detection.py +++ b/modules/lidar_detection.py @@ -5,26 +5,20 @@ class LidarDetection: """ - Lidar scan + Lidar scan. """ __create_key = object() - __DISTANCE_LIMIT = 50 - __ANGLE_LIMIT = 170 - @classmethod def create(cls, distance: float, angle: float) -> "tuple[bool, LidarDetection | None]": """ - Distance is in meters. + Distance is in metres. Angle is in degrees. """ - if distance < 0 or distance > cls.__DISTANCE_LIMIT: - return False, None - - if abs(angle) > cls.__ANGLE_LIMIT: + # lidar_driver.py returns -1 for an invalid LiDAR reading. + if distance == -1: return False, None - return True, LidarDetection(cls.__create_key, distance, angle) def __init__(self, create_key: object, distance: float, angle: float) -> None: @@ -38,6 +32,6 @@ def __init__(self, create_key: object, distance: float, angle: float) -> None: def __str__(self) -> str: """ - String representation + String representation. """ - return f"{self.__class__.__name__}: Distance: {self.distance}, Angle: {self.angle}. " + return f"{self.__class__.__name__}: distance: {self.distance}, angle: {self.angle}. "