Skip to content

Commit c0ffaa5

Browse files
authored
Add positon callback instead of passing in flightcontroller to HITL (#84)
* Add positon callback instead of passing in flightcontroller * Fix typo in position_callback parameter name * Change position set callback to dronekit * Adds position and HITL enable options
1 parent 3235fb9 commit c0ffaa5

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

modules/hitl/hitl_base.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Setup for HITL modules.
33
"""
44

5-
from modules.mavlink.flight_controller import FlightController
5+
from mavlink import dronekit
66

77
from modules.hitl.position_emulator import PositionEmulator
88
from modules.hitl.camera_emulator import CameraEmulator
@@ -19,42 +19,55 @@ class HITL:
1919

2020
@classmethod
2121
def create(
22-
cls, drone: FlightController, camera_module: bool, images_path: str | None = None
22+
cls,
23+
drone: dronekit.Vehicle,
24+
hitl_enabled: bool,
25+
position_module: bool,
26+
camera_module: bool,
27+
images_path: str | None = None,
2328
) -> "tuple[True, HITL] | tuple[False, None]":
2429
"""
2530
Factory method to create a HITL instance.
2631
2732
Args:
28-
drone: The FlightController instance for the drone.
33+
drone: The dronekit instance to use for sending MAVLink messages.
34+
hitl_enabled: Boolean indicating if HITL is enabled.
35+
position_module: Boolean indicating if the position module is enabled.
2936
camera_module: Boolean indicating if the camera module is enabled.
3037
images_path: Optional path to the images directory for the camera emulator.
3138
3239
Returns:
3340
Success, HITL instance | None.
3441
"""
35-
if not isinstance(drone, FlightController):
42+
if not isinstance(drone, dronekit.Vehicle):
3643
return False, None
3744

38-
result, position_emulator = PositionEmulator.create(drone)
39-
if not result:
40-
return False, None
45+
if not hitl_enabled:
46+
return True, HITL(cls.__create_key, drone, None, None)
47+
48+
if position_module:
49+
result, position_emulator = PositionEmulator.create(drone)
50+
if not result:
51+
return False, None
4152

4253
if camera_module:
4354
result, camera_emulator = CameraEmulator.create(images_path)
4455
if not result:
4556
return False, None
4657

4758
hitl = HITL(
48-
cls.__create_key, drone, position_emulator, camera_emulator if camera_module else None
59+
cls.__create_key,
60+
position_emulator if position_emulator else None,
61+
camera_emulator if camera_module else None,
4962
)
5063

5164
return True, hitl
5265

5366
def __init__(
5467
self,
5568
class_private_create_key: object,
56-
drone: FlightController,
57-
position_emulator: "PositionEmulator",
69+
drone: dronekit.Vehicle,
70+
position_emulator: "PositionEmulator | None" = None,
5871
camera_emulator: "CameraEmulator | None" = None,
5972
) -> None:
6073
"""
@@ -63,6 +76,5 @@ def __init__(
6376
assert class_private_create_key is HITL.__create_key, "Use create() method"
6477

6578
self.drone = drone
66-
6779
self.position_emulator = position_emulator
6880
self.camera_emulator = camera_emulator

modules/hitl/position_emulator.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Emulates position and attitude to Pixhawk.
33
"""
44

5-
from modules.mavlink.flight_controller import FlightController
5+
from mavlink import dronekit
66

77

88
class PositionEmulator:
@@ -14,7 +14,7 @@ class PositionEmulator:
1414

1515
@classmethod
1616
def create(
17-
cls, drone: FlightController
17+
cls, drone: dronekit.Vehicle
1818
) -> "tuple[True, PositionEmulator] | tuple[False, None]":
1919
"""
2020
Setup position emulator.
@@ -23,15 +23,12 @@ def create(
2323
Success, PositionEmulator instance.
2424
"""
2525

26-
if not isinstance(drone, FlightController):
27-
return False, None
28-
2926
return True, PositionEmulator(cls.__create_key, drone)
3027

31-
def __init__(self, class_private_create_key: object, drone: FlightController) -> None:
28+
def __init__(self, class_private_create_key: object, drone: dronekit.Vehicle) -> None:
3229
"""
3330
Private constructor, use create() method.
3431
"""
3532
assert class_private_create_key is PositionEmulator.__create_key, "Use create() method"
3633

37-
self._drone = drone
34+
self.drone = drone

0 commit comments

Comments
 (0)