Skip to content

Commit

Permalink
Finished task 2, 3, 4
Browse files Browse the repository at this point in the history
  • Loading branch information
krish1905 committed Feb 1, 2025
1 parent 7e19ede commit 6b5c530
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 14 deletions.
Binary file added .DS_Store
Binary file not shown.
29 changes: 23 additions & 6 deletions modules/bootcamp/decision_simple_waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ def __init__(self, waypoint: location.Location, acceptance_radius: float) -> Non
Initialize all persistent variables here with self.
"""
self.waypoint = waypoint
print(f"Waypoint: {waypoint}")

self.acceptance_radius = acceptance_radius

# ============
# ↓ BOOTCAMPERS MODIFY BELOW THIS COMMENT ↓
# ============

# Add your own

# ============
# ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
# ============
Expand Down Expand Up @@ -68,10 +65,30 @@ def run(
# ↓ BOOTCAMPERS MODIFY BELOW THIS COMMENT ↓
# ============

# Do something based on the report and the state of this class...

status = report.status
position = report.position

distance_from_waypoint = (self.waypoint.location_x - position.location_x) ** 2 + (
self.waypoint.location_y - position.location_y
) ** 2

# check if status is HALTED; if it is, set the relative destination
if status == drone_status.DroneStatus.HALTED:
# if the drone is in the acceptance radius, we have reached
if distance_from_waypoint < (self.acceptance_radius**2):
return commands.Command.create_land_command()
relative_destination = location.Location(
self.waypoint.location_x - position.location_x,
self.waypoint.location_y - position.location_y,
)
return commands.Command.create_set_relative_destination_command(
relative_destination.location_x, relative_destination.location_y
)
# if the drone is in the acceptance radius, we have reached
if distance_from_waypoint < (self.acceptance_radius**2):
return commands.Command.create_halt_command()
# ============
# ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
# ============

return command
return command
74 changes: 71 additions & 3 deletions modules/bootcamp/decision_waypoint_landing_pads.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,27 @@
from .. import location
from ..private.decision import base_decision


# Disable for bootcamp use
# No enable
# pylint: disable=duplicate-code,unused-argument


def calculate_distance(position: location.Location, landing_pad: location.Location) -> float:
"""
Calculate the Euclidean distance between a given position and a landing pad.
Args:
position (object): An object with attributes `location_x` and `location_y` representing the coordinates of the position.
landing_pad (object): An object with attributes `location_x` and `location_y` representing the coordinates of the landing pad.
Returns:
float: The Euclidean distance between the position and the landing pad.
"""
return (landing_pad.location_x - position.location_x) ** 2 + (
landing_pad.location_y - position.location_y
) ** 2


class DecisionWaypointLandingPads(base_decision.BaseDecision):
"""
Travel to the designed waypoint and then land at the nearest landing pad.
Expand All @@ -39,6 +54,11 @@ def __init__(self, waypoint: location.Location, acceptance_radius: float) -> Non

# Add your own

# a boolean to see if the drone has reached the waypoint
self.reached = False

self.nearest_landing_pad = None

# ============
# ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
# ============
Expand Down Expand Up @@ -68,10 +88,58 @@ def run(
# ↓ BOOTCAMPERS MODIFY BELOW THIS COMMENT ↓
# ============

# Do something based on the report and the state of this class...
status = report.status
position = report.position

if not self.reached:
distance_from_waypoint = (self.waypoint.location_x - position.location_x) ** 2 + (
self.waypoint.location_y - position.location_y
) ** 2

# if the drone is in the acceptance radius, we have reached
if distance_from_waypoint < (self.acceptance_radius**2):
self.reached = True
self.nearest_landing_pad = min(
landing_pad_locations,
key=lambda landing_pad: calculate_distance(position, landing_pad),
)
relative_destination = location.Location(
self.nearest_landing_pad.location_x - position.location_x,
self.nearest_landing_pad.location_y - position.location_y,
)
return commands.Command.create_set_relative_destination_command(
relative_destination.location_x, relative_destination.location_y
)
# return commands.Command.create_halt_command()
# otherwise, the drone has not reached in the acceptance radius
# If the drone is not at the waypoint yet
if status == drone_status.DroneStatus.HALTED:
relative_destination = location.Location(
self.waypoint.location_x - position.location_x,
self.waypoint.location_y - position.location_y,
)
return commands.Command.create_set_relative_destination_command(
relative_destination.location_x, relative_destination.location_y
)
return command
distance_to_pad = calculate_distance(position, self.nearest_landing_pad)
# if we are within the acceptance radius of the landing pad, we land
if distance_to_pad < (self.acceptance_radius**2):
if status == drone_status.DroneStatus.HALTED:
return commands.Command.create_land_command()
return commands.Command.create_halt_command()
# if we are not within the acceptance radius of the landing pad, we head towards it
if status == drone_status.DroneStatus.HALTED:
relative_destination = location.Location(
self.nearest_landing_pad.location_x - position.location_x,
self.nearest_landing_pad.location_y - position.location_y,
)
return commands.Command.create_set_relative_destination_command(
relative_destination.location_x, relative_destination.location_y
)

# ============
# ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
# ============

return command
return command
4 changes: 2 additions & 2 deletions modules/bootcamp/tests/run_decision_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# to reach the 1st command
# Increase the step size if your computer is lagging
# Larger step size is smaller FPS
TIME_STEP_SIZE = 0.1 # seconds
TIME_STEP_SIZE = 0.2 # seconds

# OpenCV ignores your display settings,
# so if the window is too small or too large,
Expand Down Expand Up @@ -51,4 +51,4 @@ def main() -> int:
if result_main != 0:
print(f"ERROR: Status code: {result_main}")

print("Done!")
print("Done!")
4 changes: 2 additions & 2 deletions modules/bootcamp/tests/run_decision_simple_waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# to reach the 1st command
# Increase the step size if your computer is lagging
# Larger step size is smaller FPS
TIME_STEP_SIZE = 0.1 # seconds
TIME_STEP_SIZE = 0.8 # seconds

# OpenCV ignores your display settings,
# so if the window is too small or too large,
Expand Down Expand Up @@ -53,4 +53,4 @@ def main() -> int:
if result_main != 0:
print(f"ERROR: Status code: {result_main}")

print("Done!")
print("Done!")
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pytest
# If you are on MacOS, don't have a CUDA capable GPU,
# or if things don't work and you just want to use the CPU,
# comment out the "--extra-index-url" option
--extra-index-url https://download.pytorch.org/whl/cu124
#--extra-index-url https://download.pytorch.org/whl/cu124

# ============
# ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
Expand Down

0 comments on commit 6b5c530

Please sign in to comment.