From 97f669e9147476b367c55388835b320c023323f3 Mon Sep 17 00:00:00 2001 From: Vyomm Khanna Date: Thu, 12 Sep 2024 00:35:14 -0400 Subject: [PATCH] made changes indicated in review by ashum68 --- .../bootcamp/decision_waypoint_landing_pads.py | 18 +++++++++++++----- modules/bootcamp/detect_landing_pad.py | 12 +++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/modules/bootcamp/decision_waypoint_landing_pads.py b/modules/bootcamp/decision_waypoint_landing_pads.py index b1a3abac..60f396a3 100644 --- a/modules/bootcamp/decision_waypoint_landing_pads.py +++ b/modules/bootcamp/decision_waypoint_landing_pads.py @@ -7,7 +7,6 @@ # Disable for bootcamp use # pylint: disable=unused-import -import sys from .. import commands from .. import drone_report from .. import drone_status @@ -58,13 +57,18 @@ def find_closest_landing_pad( self, current_position: location.Location, landing_pad_locations: "list[location.Location]", - ) -> location.Location: + ) -> location.Location | None: """ Find the closest landing pad """ + + # Handle case when no landing pads present + if len(landing_pad_locations) == 0: + return None + closest_pad = None # Setting default to maximum integer value for easy troubleshooting - min_distance = sys.maxsize + min_distance = float("inf") # Trivial sorter to determine the closest landing pad for lander in landing_pad_locations: @@ -118,9 +122,9 @@ def run( # Do something based on the report and the state of this class... + # Removed redundant variable current_position if not self.arrived: - current_position = report.position - squared_distance_to_waypoint = self.squared_distance(current_position, self.waypoint) + squared_distance_to_waypoint = self.squared_distance(report.position, self.waypoint) # Check acceptance radius if squared_distance_to_waypoint <= self.acceptance_radius**2: @@ -136,6 +140,10 @@ def run( if self.arrived: closest_pad = self.find_closest_landing_pad(report.position, landing_pad_locations) + # Keep simulation running if no landing pad found + if closest_pad is None: + return commands.Command.create_null_command() + # Calculate squared distance to the closest landing pad squared_distance_to_pad = self.squared_distance(report.position, closest_pad) diff --git a/modules/bootcamp/detect_landing_pad.py b/modules/bootcamp/detect_landing_pad.py index 5018041a..541294eb 100644 --- a/modules/bootcamp/detect_landing_pad.py +++ b/modules/bootcamp/detect_landing_pad.py @@ -38,7 +38,7 @@ class DetectLandingPad: __MODEL_NAME = "best-2n.pt" @classmethod - def create(cls, model_directory: pathlib.Path) -> tuple: + def create(cls, model_directory: pathlib.Path) -> "tuple[bool, DetectLandingPad | None]": """ model_directory: Directory to models. """ @@ -97,7 +97,7 @@ def run(self, image: np.ndarray) -> "tuple[list[bounding_box.BoundingBox], np.nd # Plot the annotated image from the Result object # Include the confidence value - image_annotated = prediction.plot() + image_annotated = prediction.plot(conf=True) # Get the xyxy-boxes list from the Boxes object in the Result object boxes_xyxy = prediction.boxes.xyxy @@ -105,15 +105,13 @@ def run(self, image: np.ndarray) -> "tuple[list[bounding_box.BoundingBox], np.nd # Detach the xyxy boxes to make a copy, # move the copy into CPU space, # and convert to a numpy array - boxes_cpu = boxes_xyxy.cpu().numpy() + boxes_cpu = boxes_xyxy.detach().cpu().numpy() # Loop over the boxes list and create a list of bounding boxes bounding_boxes = [] - # Use a range-based for-loop based on number of elements - for i in range(boxes_cpu.shape[0]): - box = boxes_cpu[i, :] - result, box = bounding_box.BoundingBox.create(boxes_cpu[i]) + for box in boxes_cpu: + result, box = bounding_box.BoundingBox.create(box) if result: bounding_boxes.append(box) # Hint: .shape gets the dimensions of the numpy array