-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDroneController_test.py
71 lines (56 loc) · 2.44 KB
/
DroneController_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
README
To run this test code:
1. Run JMAVSim from the px4 library (make px4_sitl_default jmavsim from the PX4-Autopilot directory)
2. Run this script
"""
import asyncio
from mavsdk import System
from mavsdk.offboard import OffboardError, PositionNedYaw
import numpy as np
from DroneController import DroneController # Replace 'your_module' with the actual module name
# Test the DroneController class
async def test_drone_controller():
drone = System()
controller = DroneController(drone)
# Set up MAVLink and offboard mode
success = await controller.setup_mavlink_offboard()
if not success:
print("Failed to set up MAVLink offboard mode.")
return
# Perform movements
print("-- Go 0m North, 0m East, -5m Down within local coordinate system")
await drone.offboard.set_position_ned(PositionNedYaw(0.0, 0.0, -5.0, 0.0))
await asyncio.sleep(10)
# Collect telemetry data
data = await controller.collect_telemetry_data()
print("Collected telemetry data:")
controller.print_pretty_dict(data)
# Check velocity body calculation
if 'velocity_ned' in data and 'euler_angles' in data:
velocity_body = controller.calculate_velocity_body(data['velocity_ned'], data['euler_angles'])
print("Calculated velocity body:")
controller.print_pretty_dict(velocity_body)
# Perform more movements
print("-- Go 5m North, 0m East, -5m Down within local coordinate system, turn to face East")
await drone.offboard.set_position_ned(PositionNedYaw(5.0, 0.0, -5.0, 90.0))
await asyncio.sleep(10)
print("-- Go 5m North, 10m East, -5m Down within local coordinate system")
await drone.offboard.set_position_ned(PositionNedYaw(5.0, 10.0, -5.0, 90.0))
await asyncio.sleep(15)
print("-- Go 0m North, 10m East, 0m Down within local coordinate system, turn to face South")
await drone.offboard.set_position_ned(PositionNedYaw(0.0, 10.0, 0.0, 180.0))
await asyncio.sleep(10)
# Stop offboard mode
print("-- Stopping offboard")
try:
await drone.offboard.stop()
except OffboardError as error:
print(f"Stopping offboard mode failed with error code: {error._result.result}")
# Collect telemetry data again
data = await controller.collect_telemetry_data()
print("Collected telemetry data after stopping offboard:")
controller.print_pretty_dict(data)
# Run the test
if __name__ == "__main__":
asyncio.run(test_drone_controller())