Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrote flight interface to receive commands #14

Merged
merged 17 commits into from
Jun 26, 2024
30 changes: 27 additions & 3 deletions modules/flight_interface/flight_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Creates flight controller and produces local drone odometry coupled with a timestamp.
"""

from . import conversions
from .. import drone_odometry_local

from modules import decision_command
from modules import drone_odometry_local
from ..common.mavlink.modules import drone_odometry
from ..common.mavlink.modules import flight_controller
from . import conversions


class FlightInterface:
Expand Down Expand Up @@ -66,3 +66,27 @@ def run(self) -> "tuple[bool, drone_odometry_local.DroneOdometryLocal | None]":
drone_orientation = odometry.orientation

return drone_odometry_local.DroneOdometryLocal.create(local_position, drone_orientation)

def run_decision_handler(self, command: decision_command.DecisionCommand) -> bool:
"""
Uploads decision commands to drone.
"""
if command.command == decision_command.DecisionCommand.CommandType.RESUME:
self.resume_handler()
return True
if command.command == decision_command.DecisionCommand.CommandType.STOP:
self.stop_handler()
return True
return False

def resume_handler(self) -> None:
"""
Resumes the AUTO mission.
"""
self.controller.set_flight_mode("AUTO")

def stop_handler(self) -> None:
"""
Stops the drone.
"""
self.controller.set_flight_mode("LOITER")
18 changes: 15 additions & 3 deletions modules/flight_interface/flight_interface_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
"""

import time
import queue

from modules import decision_command
from worker import queue_wrapper
from worker import worker_controller

from . import flight_interface


def flight_interface_worker(
address: str,
timeout: float,
period: float,
output_queue: queue_wrapper.QueueWrapper,
command_in_queue: queue_wrapper.QueueWrapper,
odometry_out_queue: queue_wrapper.QueueWrapper,
controller: worker_controller.WorkerController,
) -> None:
"""
Expand All @@ -25,6 +27,7 @@ def flight_interface_worker(
output_queue is the data queue.
controller is how the main process communicates to this worker process.
"""

result, interface = flight_interface.FlightInterface.create(address, timeout)
if not result:
return
Expand All @@ -38,4 +41,13 @@ def flight_interface_worker(
if not result:
Copy link
Contributor

Choose a reason for hiding this comment

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

pass instead of continue, if we don't get an printer
odometry but do get a command we do want to send the command

continue

output_queue.queue.put(value)
odometry_out_queue.queue.put(value)

try:
command: decision_command.DecisionCommand = command_in_queue.queue.get_nowait()
if command is None:
break
except queue.Empty:
continue

interface.run_decision_handler(command)
20 changes: 13 additions & 7 deletions tests/integration/test_flight_interface_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import queue
import time

from worker import worker_controller
from worker import queue_wrapper

from modules import drone_odometry_local
from modules.flight_interface import flight_interface_worker
from worker import worker_controller
from worker import queue_wrapper


# Constants
QUEUE_MAX_SIZE = 10
Expand All @@ -23,19 +24,20 @@ def main() -> int:
"""
Main function.
"""

controller = worker_controller.WorkerController()
manager = mp.Manager()
mp_manager = mp.Manager()

output_queue = queue_wrapper.QueueWrapper(manager, QUEUE_MAX_SIZE)
command_in_queue = queue_wrapper.QueueWrapper(mp_manager, QUEUE_MAX_SIZE)
odometry_out_queue = queue_wrapper.QueueWrapper(mp_manager, QUEUE_MAX_SIZE)

worker = mp.Process(
target=flight_interface_worker.flight_interface_worker,
args=(
FLIGHT_INTERFACE_ADDRESS,
FLIGHT_INTERFACE_TIMEOUT,
FLIGHT_INTERFACE_WORKER_PERIOD,
output_queue,
command_in_queue,
odometry_out_queue,
controller,
),
)
Expand All @@ -46,7 +48,9 @@ def main() -> int:

while True:
try:
input_data: drone_odometry_local.DroneOdometryLocal = output_queue.queue.get_nowait()
input_data: drone_odometry_local.DroneOdometryLocal = (
odometry_out_queue.queue.get_nowait()
)
assert (
str(type(input_data)) == "<class 'modules.drone_odometry_local.DroneOdometryLocal'>"
)
Expand All @@ -67,6 +71,8 @@ def main() -> int:

controller.request_exit()

command_in_queue.fill_and_drain_queue()

worker.join()

return 0
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_flight_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def create_flight_interface_instance(
address: str, timeout: float
) -> "tuple[bool, FlightInterface | None]":
) -> "tuple[bool, flight_interface.FlightInterface | None]":
"""
Construct a flight interface instance.
"""
Expand Down
Loading