Skip to content

Commit

Permalink
new cluster data struct and converted lidar detection to have x, y co… (
Browse files Browse the repository at this point in the history
#35)

* 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
  • Loading branch information
ashum68 authored Aug 20, 2024
1 parent 5636ee0 commit 84a2b6e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
39 changes: 39 additions & 0 deletions modules/detection_cluster.py
Original file line number Diff line number Diff line change
@@ -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}. "
18 changes: 6 additions & 12 deletions modules/lidar_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}. "

0 comments on commit 84a2b6e

Please sign in to comment.