Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
119 changes: 118 additions & 1 deletion bootcamp_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import multiprocessing as mp
import queue
import time

from pymavlink import mavutil
Expand All @@ -30,10 +29,24 @@
# ↓ BOOTCAMPERS MODIFY BELOW THIS COMMENT ↓
# =================================================================================================
# Set queue max sizes (<= 0 for infinity)
TELEM_TO_COMMAND_QUEUE_MAX = 32
HEARTBEAT_RECV_TO_MAIN_QUEUE_MAX = 64
COMMAND_TO_MAIN_QUEUE_MAX = 64

# Set worker counts
HEARTBEAT_SENDER_COUNT = 1
HEARTBEAT_RECEIVER_COUNT = 1
TELEMETRY_COUNT = 1
COMMAND_COUNT = 1

# Any other constants
HEARTBEAT_PERIOD_S = 1.0
DISCONNECT_THRESHOLD = 5
TELEMETRY_PERIOD_S = 1.0
Z_SPEED_M_S = 1.0
ANGLE_TOLERANCE_DEG = 5.0
HEIGHT_TOLERANCE_M = 0.5
TARGET_POSITION = command.Position(10, 20, 30)

# =================================================================================================
# ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
Expand Down Expand Up @@ -74,43 +87,147 @@ def main() -> int:
# ↓ BOOTCAMPERS MODIFY BELOW THIS COMMENT ↓
# =============================================================================================
# Create a worker controller
controller = worker_controller.WorkerController()

# Create a multiprocess manager for synchronized queues
mp_manager = mp.Manager()

# Create queues
telem_to_command_queue = queue_proxy_wrapper.QueueProxyWrapper(
mp_manager, TELEM_TO_COMMAND_QUEUE_MAX
)
hb_recv_to_main_queue = queue_proxy_wrapper.QueueProxyWrapper(
mp_manager, HEARTBEAT_RECV_TO_MAIN_QUEUE_MAX
)
command_to_main_queue = queue_proxy_wrapper.QueueProxyWrapper(
mp_manager, COMMAND_TO_MAIN_QUEUE_MAX
)

# Create worker properties for each worker type (what inputs it takes, how many workers)
# Heartbeat sender
hb_sender_result, hb_sender_props = worker_manager.WorkerProperties.create(
count=HEARTBEAT_SENDER_COUNT,
target=heartbeat_sender_worker.heartbeat_sender_worker,
work_arguments=(connection, HEARTBEAT_PERIOD_S),
input_queues=[],
output_queues=[],
controller=controller,
local_logger=main_logger,
)
if not hb_sender_result:
return -1

# Heartbeat receiver
hb_recv_result, hb_recv_props = worker_manager.WorkerProperties.create(
count=HEARTBEAT_RECEIVER_COUNT,
target=heartbeat_receiver_worker.heartbeat_receiver_worker,
work_arguments=(
connection,
HEARTBEAT_PERIOD_S,
DISCONNECT_THRESHOLD,
),
input_queues=[],
output_queues=[hb_recv_to_main_queue],
controller=controller,
local_logger=main_logger,
)
if not hb_recv_result:
return -1

# Telemetry
telemetry_result, telemetry_props = worker_manager.WorkerProperties.create(
count=TELEMETRY_COUNT,
target=telemetry_worker.telemetry_worker,
work_arguments=(connection, TELEMETRY_PERIOD_S),
input_queues=[],
output_queues=[telem_to_command_queue],
controller=controller,
local_logger=main_logger,
)
if not telemetry_result:
return -1

# Command
command_result, command_props = worker_manager.WorkerProperties.create(
count=COMMAND_COUNT,
target=command_worker.command_worker,
work_arguments=(
connection,
TARGET_POSITION,
TELEMETRY_PERIOD_S,
Z_SPEED_M_S,
ANGLE_TOLERANCE_DEG,
HEIGHT_TOLERANCE_M,
),
input_queues=[telem_to_command_queue],
output_queues=[command_to_main_queue],
controller=controller,
local_logger=main_logger,
)
if not command_result:
return -1

# Create the workers (processes) and obtain their managers
worker_managers: list[worker_manager.WorkerManager] = []
for props in (hb_sender_props, hb_recv_props, telemetry_props, command_props):
# Get Pylance to stop complaining
assert props is not None
ok, mgr = worker_manager.WorkerManager.create(
worker_properties=props, local_logger=main_logger
)
if not ok:
return -1
assert mgr is not None
worker_managers.append(mgr)

# Start worker processes
for mgr in worker_managers:
mgr.start_workers()

main_logger.info("Started")

# Main's work: read from all queues that output to main, and log any commands that we make
# Continue running for 100 seconds or until the drone disconnects
end_time = time.time() + 100
current_state = "Unknown"
while time.time() < end_time:
try:
# Heartbeat state
state = hb_recv_to_main_queue.queue.get(timeout=HEARTBEAT_PERIOD_S * 2)
current_state = str(state)
main_logger.info(f"Heartbeat state: {current_state}")
if current_state == "Disconnected":
break
# Drain any command outputs without blocking
while True:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the extra while True, it can still be in the same overall while loop

cmd_out = command_to_main_queue.queue.get_nowait()
if cmd_out is None:
continue
main_logger.info(f"Command: {cmd_out}")
except: # pylint: disable=bare-except
pass

# Stop the processes
controller.request_exit()

main_logger.info("Requested exit")

# Fill and drain queues from END TO START
telem_to_command_queue.fill_and_drain_queue()
hb_recv_to_main_queue.fill_and_drain_queue()
command_to_main_queue.fill_and_drain_queue()

main_logger.info("Queues cleared")

# Clean up worker processes
for mgr in worker_managers:
mgr.join_workers()

main_logger.info("Stopped")

# We can reset controller in case we want to reuse it
# Alternatively, create a new WorkerController instance
controller.clear_exit()

# =============================================================================================
# ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
Expand Down
28 changes: 28 additions & 0 deletions logs/command/command_drone_48796.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
23:51:57: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 45] Logger initialized
23:51:57: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:51:58: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:01: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:01: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:02: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:02: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:03: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:03: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:04: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:04: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:05: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:05: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:06: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:06: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:07: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:07: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:08: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:08: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:09: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:09: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:10: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:10: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:11: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:11: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:12: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:12: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 76] Received a valid command
23:52:16: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/command_drone.py | main | 83] Passed!
58 changes: 58 additions & 0 deletions logs/command/command_worker_48793.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
23:51:57: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command_worker.py | command_worker | 52] Logger initialized
23:51:57: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 4.000)
23:51:57: [INFO] CHANGE ALTITUDE: 1.0
23:51:58: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 1.000)
23:51:58: [INFO] CHANGE ALTITUDE: -1.0
23:51:58: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 0.667)
23:51:59: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 0.500)
23:51:59: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 0.400)
23:52:00: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 0.333)
23:52:00: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 0.286)
23:52:01: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 2.500, 0.250)
23:52:01: [INFO] CHANGE YAW: 63.43494882292201
23:52:01: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 4.444, 0.222)
23:52:01: [INFO] CHANGE YAW: 90.0
23:52:02: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 6.000, 0.200)
23:52:02: [INFO] CHANGE YAW: 116.56505117707799
23:52:02: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (1.818, 5.455, 0.182)
23:52:02: [INFO] CHANGE YAW: 45.0
23:52:03: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (3.333, 5.000, 0.167)
23:52:03: [INFO] CHANGE YAW: 63.43494882292201
23:52:03: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (4.615, 4.615, 0.154)
23:52:03: [INFO] CHANGE YAW: 90.0
23:52:04: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (4.286, 2.857, 0.143)
23:52:04: [INFO] CHANGE YAW: 26.565051177077994
23:52:04: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (4.000, 1.333, 0.133)
23:52:04: [INFO] CHANGE YAW: 45.0
23:52:05: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (3.750, 0.000, 0.125)
23:52:05: [INFO] CHANGE YAW: 90.0
23:52:05: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (2.353, 0.000, 0.118)
23:52:05: [INFO] CHANGE YAW: 45.0
23:52:06: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (1.111, 0.000, 0.111)
23:52:06: [INFO] CHANGE YAW: 90.0
23:52:06: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 0.105)
23:52:06: [INFO] CHANGE YAW: 135.0
23:52:07: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 1.000, 0.100)
23:52:07: [INFO] CHANGE YAW: -26.565051177077994
23:52:07: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 1.905, 0.095)
23:52:07: [INFO] CHANGE YAW: -45.0
23:52:08: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 2.727, 0.091)
23:52:08: [INFO] CHANGE YAW: -90.0
23:52:08: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.870, 2.609, 0.087)
23:52:08: [INFO] CHANGE YAW: -45.0
23:52:09: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (1.667, 2.500, 0.083)
23:52:09: [INFO] CHANGE YAW: -90.0
23:52:09: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (2.400, 2.400, 0.080)
23:52:09: [INFO] CHANGE YAW: -135.0
23:52:10: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (2.308, 1.538, 0.077)
23:52:10: [INFO] CHANGE YAW: -63.43494882292201
23:52:10: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (2.222, 0.741, 0.074)
23:52:10: [INFO] CHANGE YAW: -90.0
23:52:11: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (2.143, 0.000, 0.071)
23:52:11: [INFO] CHANGE YAW: -116.56505117707799
23:52:11: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (1.379, 0.000, 0.069)
23:52:11: [INFO] CHANGE YAW: -45.0
23:52:12: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.667, 0.000, 0.067)
23:52:12: [INFO] CHANGE YAW: -63.43494882292201
23:52:12: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/command/command.py | run | 100] Average velocity: (0.000, 0.000, 0.065)
23:52:12: [INFO] CHANGE YAW: -90.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be in your main log

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this still needs to be fixed

4 changes: 4 additions & 0 deletions logs/command/main.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
23:51:56: [INFO] [/Users/tomalmog/Clubs/WARG/p2/modules/common/modules/logger/logger_main_setup.py | setup_main_logger | 62] main logger initialized
23:51:57: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/test_command.py | main | 144] Connected!
23:51:57: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/test_command.py | read_queue | 81] CHANGE ALTITUDE: 1.0
23:51:58: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/test_command.py | read_queue | 81] CHANGE ALTITUDE: -1.0
13 changes: 13 additions & 0 deletions logs/heartbeat_receiver/heartbeat_receiver_drone_48745.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
23:51:18: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | main | 42] Logger initialized
23:51:18: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:19: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:20: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:21: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:22: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:31: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:32: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:33: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:34: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:35: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:37: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | send_heartbeats | 59] Drone: Sent a heartbeat
23:51:38: [INFO] [/Users/tomalmog/Clubs/WARG/p2/tests/integration/mock_drones/heartbeat_receiver_drone.py | main | 79] Passesd!
Loading