Skip to content

Commit bcef5ab

Browse files
committed
Adds basic hitl module
1 parent b66262e commit bcef5ab

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

modules/hitl/__init__.py

Whitespace-only changes.

modules/hitl/camera_emulator.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Emulates camera input to PI.
3+
"""
4+
5+
6+
class CameraEmulator:
7+
"""
8+
Setup for camera emulator.
9+
"""
10+
11+
def create(self) -> "tuple[True, CameraEmulator] | tuple[False, None]":
12+
"""
13+
Setup camera emulator.
14+
15+
Returns:
16+
Success, CameraEmulator instance.
17+
"""
18+
return True, self

modules/hitl/hitl_base.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Setup for HITL modules.
3+
"""
4+
5+
from modules.mavlink.flight_controller import FlightController
6+
7+
from modules.hitl.position_emulator import PositionEmulator
8+
from modules.hitl.camera_emulator import CameraEmulator
9+
10+
11+
class HITL:
12+
"""
13+
Hardware In The Loop (HITL) setup for emulating drone hardware.
14+
Provides a way to emulate the drone's position and camera input
15+
for testing purposes without needing actual hardware.
16+
"""
17+
18+
__create_key = object()
19+
20+
@classmethod
21+
def create(
22+
cls, drone: FlightController, camera_module: bool
23+
) -> "tuple[True, HITL] | tuple[False, None]":
24+
"""
25+
Setup workers for.
26+
27+
Args:
28+
drone: The FlightController instance for the drone.
29+
camera_module: Boolean indicating if the camera module is enabled.
30+
31+
Returns:
32+
Success, HITL instance.
33+
"""
34+
if not isinstance(drone, FlightController):
35+
return False, None
36+
37+
position_emulator = PositionEmulator()
38+
result, position_emulator = position_emulator.create(drone)
39+
if not result:
40+
return False, None
41+
42+
if camera_module:
43+
camera_emulator = CameraEmulator()
44+
result, camera_emulator = camera_emulator.create()
45+
if not result:
46+
return False, None
47+
48+
hitl = HITL(
49+
cls.__create_key, drone, position_emulator, camera_emulator if camera_module else None
50+
)
51+
52+
return True, hitl
53+
54+
def __init__(
55+
self,
56+
class_private_create_key: object,
57+
drone: FlightController,
58+
position_emulator: "PositionEmulator",
59+
camera_emulator: "CameraEmulator | None" = None,
60+
) -> None:
61+
"""
62+
Private constructor, use create() method.
63+
"""
64+
assert class_private_create_key is HITL.__create_key, "Use create() method"
65+
66+
self.drone = drone
67+
68+
self.position_emulator = position_emulator
69+
self.camera_emulator = camera_emulator

modules/hitl/position_emulator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Emulates position and attitude to Pixhawk.
3+
"""
4+
5+
from modules.mavlink.flight_controller import FlightController
6+
7+
8+
class PositionEmulator:
9+
"""
10+
Setup for position emulator.
11+
"""
12+
13+
def create(
14+
self, drone: FlightController
15+
) -> "tuple[True, PositionEmulator] | tuple[False, None]":
16+
"""
17+
Setup position emulator.
18+
19+
Returns:
20+
Success, PositionEmulator instance.
21+
"""
22+
return True, self

0 commit comments

Comments
 (0)