Skip to content

Commit

Permalink
Fix returns in main entry function (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xierumeng authored Nov 29, 2023
1 parent 12bd117 commit 69e878f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
# Python
__pycache__/
venv/

# Logging
*log*
8 changes: 5 additions & 3 deletions modules/qr_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ def qr_input(device: "int | str") -> "tuple[bool, str | None]":
is_qr_text_found, qr_text = scanner.get_qr_text(frame)

if is_qr_text_found:
# Exit and return decoded text if found
return True, qr_text
break

# Exit early on manual quit
if cv2.waitKey(1) == ord('q'):
break

return False, None
# Cleanup
cv2.destroyAllWindows()

return is_qr_text_found, qr_text
44 changes: 32 additions & 12 deletions path_2023.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,45 @@
from modules.common.kml.modules import waypoints_to_kml


WAYPOINT_FILE_PATH = pathlib.Path(".", "waypoints", "wrestrc_waypoints.csv")
WAYPOINT_FILE_PATH = pathlib.Path("waypoints", "wrestrc_waypoints.csv")
CAMERA = 0
ALTITUDE = 40
CONNECTION_ADDRESS = "tcp:localhost:14550"
KML_FILE_PARENT_DIRECTORY = pathlib.Path(".", "waypoints")
KML_FILE_PARENT_DIRECTORY = pathlib.Path("waypoints")
KML_FILE_PREFIX = "waypoints_log"
DELAY = 0.1 # seconds


# Required for checks
# pylint: disable-next=too-many-branches,too-many-return-statements
def run() -> int:
"""
Reads waypoints from QR code and sends drone commands.
"""
# Wait ready is false as the drone may be on the ground
drone = dronekit.connect(CONNECTION_ADDRESS, wait_ready = False)

result, waypoint_name_to_coordinates = load_waypoint_name_to_coordinates_map.load_waypoint_name_to_coordinates_map(WAYPOINT_FILE_PATH)
result, waypoint_name_to_coordinates = \
load_waypoint_name_to_coordinates_map.load_waypoint_name_to_coordinates_map(
WAYPOINT_FILE_PATH,
)
if not result:
print("ERROR: load_waypoint_name_to_coordinates_map")
return -1

result, waypoints_list = waypoints_dict_to_list.waypoints_dict_to_list(waypoint_name_to_coordinates)

result, waypoints_list = waypoints_dict_to_list.waypoints_dict_to_list(
waypoint_name_to_coordinates,
)
if not result:
print("ERROR: convert waypoints from dict to list")
return -1

# TODO: Remove tuple conversion when common repository's waypoint_to_kml() supports Waypoint class
waypoints_list_tuple = [(waypoint.latitude, waypoint.longitude) for waypoint in waypoints_list]
result, _ = waypoints_to_kml.waypoints_to_kml(waypoints_list_tuple, KML_FILE_PREFIX, KML_FILE_PARENT_DIRECTORY)
result, _ = waypoints_to_kml.waypoints_to_kml(
waypoints_list_tuple,
KML_FILE_PREFIX, KML_FILE_PARENT_DIRECTORY,
)
if not result:
print("ERROR: Unable to generate KML file")
return -1
Expand All @@ -58,21 +71,25 @@ def run() -> int:
print("ERROR: qr_to_waypoint_names")
return -1

result, waypoints = waypoint_names_to_coordinates.waypoint_names_to_coordinates(waypoint_names, waypoint_name_to_coordinates)
result, waypoints = waypoint_names_to_coordinates.waypoint_names_to_coordinates(
waypoint_names,
waypoint_name_to_coordinates,
)
if not result:
print("ERROR: waypoint_names_to_coordinates")
return -1

result, waypoint_commands = waypoints_to_commands.waypoints_to_commands(waypoints, ALTITUDE)
if not result:
print("Error: waypoints_to_commands")
return -1

result, takeoff_landing_commands = add_takeoff_and_landing_command.add_takeoff_and_landing_command(waypoint_commands, ALTITUDE)

result, takeoff_landing_commands = \
add_takeoff_and_landing_command.add_takeoff_and_landing_command(waypoint_commands, ALTITUDE)
if not result:
print("Error: add_takeoff_and_landing_command")
return -1

result = upload_commands.upload_commands(drone, takeoff_landing_commands)
if not result:
print("Error: upload_commands")
Expand All @@ -97,7 +114,10 @@ def run() -> int:


if __name__ == "__main__":
# Not a constant
# pylint: disable-next=invalid-name
result_run = run()
if result_run < 0:
print("ERROR")

print("Done")
35 changes: 24 additions & 11 deletions path_2024.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Task 1 path.
Path template.
"""
import pathlib
import time
Expand All @@ -15,14 +15,16 @@
from modules.common.kml.modules import waypoints_to_kml


WAYPOINT_FILE_PATH = pathlib.Path(".", "2024", "waypoints_task_2.csv")
WAYPOINT_FILE_PATH = pathlib.Path("2024", "waypoints", "wrestrc.csv")
ALTITUDE = 40
CONNECTION_ADDRESS = "tcp:localhost:14550"
KML_FILE_PARENT_DIRECTORY = pathlib.Path(".", "waypoints")
KML_FILE_PARENT_DIRECTORY = pathlib.Path("waypoints")
KML_FILE_PREFIX = "waypoints_log"
DELAY = 0.1 # seconds


# Required for checks
# pylint: disable-next=too-many-return-statements
def run() -> int:
"""
Reads in hardcoded waypoints from CSV file and sends drone commands.
Expand All @@ -39,30 +41,38 @@ def run() -> int:
if not result:
print("ERROR: load_waypoint_name_to_coordinates_map")
return -1

result, waypoints_list = waypoints_dict_to_list.waypoints_dict_to_list(waypoint_name_to_coordinates)

result, waypoints_list = waypoints_dict_to_list.waypoints_dict_to_list(
waypoint_name_to_coordinates
)
if not result:
print("ERROR: Unable to convert waypoints from dict to list")
return -1

# TODO: Remove tuple conversion when common repository's waypoint_to_kml() supports Waypoint class
waypoints_list_tuple = [(waypoint.latitude, waypoint.longitude) for waypoint in waypoints_list]
result, _ = waypoints_to_kml.waypoints_to_kml(waypoints_list_tuple, KML_FILE_PREFIX, KML_FILE_PARENT_DIRECTORY)
result, _ = waypoints_to_kml.waypoints_to_kml(
waypoints_list_tuple,
KML_FILE_PREFIX, KML_FILE_PARENT_DIRECTORY,
)
if not result:
print("ERROR: Unable to generate KML file")
return -1

waypoint_commands = waypoints_to_commands.waypoints_to_commands(waypoints_list, ALTITUDE)
if len(waypoint_commands) == 0:
result, waypoint_commands = waypoints_to_commands.waypoints_to_commands(
waypoints_list,
ALTITUDE,
)
if not result:
print("Error: waypoints_to_commands")
return -1

takeoff_landing_commands = \
result, takeoff_landing_commands = \
add_takeoff_and_landing_command.add_takeoff_and_landing_command(
waypoint_commands,
ALTITUDE,
)
if len(takeoff_landing_commands) == 0:
if not result:
print("Error: add_takeoff_and_landing_command")
return -1

Expand Down Expand Up @@ -90,7 +100,10 @@ def run() -> int:


if __name__ == "__main__":
# Not a constant
# pylint: disable-next=invalid-name
result_run = run()
if result_run < 0:
print("ERROR")

print("Done")

0 comments on commit 69e878f

Please sign in to comment.