From 3c70a0e5fd669b9a2ad83d2c1c5f71c7676cfc2c Mon Sep 17 00:00:00 2001 From: RexBerry <59031902+RexBerry@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:02:32 -0600 Subject: [PATCH] Add Mapping state and impl #29 --- state_machine/states/__init__.py | 10 +++++- state_machine/states/impl/__init__.py | 1 + state_machine/states/impl/mapping_impl.py | 44 +++++++++++++++++++++++ state_machine/states/mapping.py | 30 ++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 state_machine/states/impl/mapping_impl.py create mode 100644 state_machine/states/mapping.py diff --git a/state_machine/states/__init__.py b/state_machine/states/__init__.py index 1dc0bf68..50dc37f4 100644 --- a/state_machine/states/__init__.py +++ b/state_machine/states/__init__.py @@ -1,4 +1,12 @@ """Defines the state classes.""" -from state_machine.states.impl import Airdrop, Land, ODLC, Start, Takeoff, Waypoint +from state_machine.states.impl import ( + Airdrop, + Land, + Mapping, + ODLC, + Start, + Takeoff, + Waypoint, +) from state_machine.states.state import State diff --git a/state_machine/states/impl/__init__.py b/state_machine/states/impl/__init__.py index a43e65ef..707d602c 100644 --- a/state_machine/states/impl/__init__.py +++ b/state_machine/states/impl/__init__.py @@ -2,6 +2,7 @@ from state_machine.states.impl.airdrop_impl import Airdrop from state_machine.states.impl.land_impl import Land +from state_machine.states.impl.mapping_impl import Mapping from state_machine.states.impl.odlc_impl import ODLC from state_machine.states.impl.start_impl import Start from state_machine.states.impl.takeoff_impl import Takeoff diff --git a/state_machine/states/impl/mapping_impl.py b/state_machine/states/impl/mapping_impl.py new file mode 100644 index 00000000..4d107f8e --- /dev/null +++ b/state_machine/states/impl/mapping_impl.py @@ -0,0 +1,44 @@ +"""Implements the behavior of the Mapping state.""" + +import asyncio +import logging + +import dronekit + +from state_machine.state_tracker import ( + update_state, + update_drone, + update_flight_settings, +) +from state_machine.states.mapping import Mapping + + +async def run(self: Mapping) -> None: + """ + Implements the run method for the Mapping state. + + This method captures photos of the mapping area and then transitions to the ODLC state. + + Returns + ------- + ODLC : State + The next state after the drone has successfully landed. + """ + try: + update_state("Mapping") + update_drone(self.drone) + update_flight_settings(self.flight_settings) + + logging.info("Mapping") + + # TODO: Implement the mapping state + raise NotImplementedError("Mapping state not implemented yet") + + logging.info("Mapping state complete.") + except asyncio.CancelledError as ex: + logging.error("Mapping state canceled") + raise ex + + +# Setting the run_callable attribute of the Mapping class to the run function +Mapping.run_callable = run diff --git a/state_machine/states/mapping.py b/state_machine/states/mapping.py new file mode 100644 index 00000000..b0966693 --- /dev/null +++ b/state_machine/states/mapping.py @@ -0,0 +1,30 @@ +"""Declares the Mapping state class.""" + +from typing import Awaitable, Callable, ClassVar + +from state_machine.states.state import State + + +class Mapping(State): + """ + The Mapping state of the state machine. + + This state represents the phase in which the drone is capturing photos of the mapping area. + + Attributes + ---------- + run_callable : ClassVar[Callable[["Mapping"], Awaitable[State]]] + The callable object to call when this state is run. This object is + shared between all instances of this class. + + Methods + ------- + run(self) -> Awaitable[State]: + Execute the logic associated with this state and return the next state + to transition to. + """ + + run_callable: ClassVar[Callable[["Mapping"], Awaitable[State]]] + + def run(self) -> Awaitable[State]: + return self.run_callable()