From 84a2b6e8c837af204acf62593096ddd69cd02c03 Mon Sep 17 00:00:00 2001 From: Andrew Shum <84476051+ashum68@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:48:11 -0400 Subject: [PATCH] =?UTF-8?q?new=20cluster=20data=20struct=20and=20converted?= =?UTF-8?q?=20lidar=20detection=20to=20have=20x,=20y=20co=E2=80=A6=20(#35)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * new cluster data struct and converted lidar detection to have x, y coords instead of dist, angle * formatting * added dist and angle back to struct and added better desc * removed coordinates from lidar_detection * edited docstring * added better comments --- modules/detection_cluster.py | 39 ++++++++++++++++++++++++++++++++++++ modules/lidar_detection.py | 18 ++++++----------- 2 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 modules/detection_cluster.py 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}. "