-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDroneController.py
332 lines (291 loc) · 13 KB
/
DroneController.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
To run this:
Must instantiate DroneController with a System object and call the methods as needed.
See test cases of inidividual methods in 1_and_3_dean_new_connect_receive_data.py and 2_1_test_both_control.py.
For future reference, a means to calculate alpha and beta is found in 1-1_dean_alpha_beta.py.
"""
import numpy as np
import asyncio
from mavsdk import System
from mavsdk.offboard import Offboard, OffboardError, ActuatorControl, ActuatorControlGroup, PositionNedYaw
from mavsdk.action import Action
from mavsdk.telemetry import Telemetry
CURRENT_CONNECTION = "udp://:14540"
class DroneController:
def __init__(self, drone: System):
self.drone = drone
@staticmethod
def clamp(value, min_value, max_value):
"""Clamps the value between min_value and max_value."""
return max(min_value, min(value, max_value))
async def set_actuator_control_homemade(self, elevator, aileron, rudder, starboard_front_motor,
rear_motor, front_motor, starboard_rear_motor, forward_propulsion_motor):
"""Sets the actuator control with two control groups and specific control modifications."""
group1_controls = [
float('nan'), # NaN for RC ROLL
float('nan'), # NaN for RC PITCH
float('nan'), # NaN for RC YAW
float('nan'), # NaN for RC Throttle
self.clamp(elevator, -1, 1),
self.clamp(aileron, -1, 1), # Left aileron
self.clamp(rudder, -1, 1),
self.clamp(-aileron, -1, 1) # Reversed aileron for right
]
group2_controls = [
float('nan'), # Unused
float('nan'), # Unused
float('nan'), # Unused
self.clamp(starboard_front_motor, 0, 1),
self.clamp(rear_motor, 0, 1),
self.clamp(front_motor, 0, 1),
self.clamp(starboard_rear_motor, 0, 1),
self.clamp(forward_propulsion_motor, 0, 1)
]
control_group1 = ActuatorControlGroup(controls=group1_controls)
control_group2 = ActuatorControlGroup(controls=group2_controls)
try:
await self.drone.offboard.set_actuator_control(ActuatorControl(groups=[control_group1, control_group2]))
except OffboardError as e:
print(f"Setting actuator control failed with error: {e}")
@staticmethod
def print_pretty_dict(d, indent=4):
"""Prints the contents of a dictionary (and nested dictionaries) in a pretty format."""
def print_dict(d, level=0):
for key, value in d.items():
if isinstance(value, dict):
print(' ' * (level * indent) + f"{key}:")
print_dict(value, level + 1)
else:
print(' ' * (level * indent) + f"{key}: {value}")
print_dict(d)
async def setup_mavlink_offboard(self, curr_conn=CURRENT_CONNECTION):
"""Sets up the MAVSDK MAVLink protocols."""
await self.drone.connect(system_address=curr_conn)
print("Waiting for drone to connect...")
async for state in self.drone.core.connection_state():
if state.is_connected:
print("-- Connected to drone!")
break
print("Waiting for drone to have a global position estimate...")
async for health in self.drone.telemetry.health():
if health.is_global_position_ok and health.is_home_position_ok:
print("-- Global position estimate OK")
break
print("-- Arming")
await self.drone.action.arm()
print("-- Setting initial setpoint")
await self.drone.offboard.set_position_ned(PositionNedYaw(0.0, 0.0, 0.0, 0.0))
print("-- Starting offboard")
try:
await self.drone.offboard.start()
except OffboardError as error:
print(f"Starting offboard mode failed with error code: {error._result.result}")
print("-- Disarming")
await self.drone.action.disarm()
return False
return True
@staticmethod
def euler_to_rotation_matrix(roll, pitch, yaw):
"""Convert Euler angles to a rotation matrix."""
roll_rad = np.radians(roll)
pitch_rad = np.radians(pitch)
yaw_rad = np.radians(yaw)
R_z = np.array([
[np.cos(yaw_rad), -np.sin(yaw_rad), 0],
[np.sin(yaw_rad), np.cos(yaw_rad), 0],
[0, 0, 1]
])
R_y = np.array([
[np.cos(pitch_rad), 0, np.sin(pitch_rad)],
[0, 1, 0],
[-np.sin(pitch_rad), 0, np.cos(pitch_rad)]
])
R_x = np.array([
[1, 0, 0],
[0, np.cos(roll_rad), -np.sin(roll_rad)],
[0, np.sin(roll_rad), np.cos(roll_rad)]
])
R = R_z @ R_y @ R_x
return R
@staticmethod
def calculate_velocity_body(velocity_ned, euler_angles):
"""Calculate body frame velocities from NED frame velocities and Euler angles."""
R = DroneController.euler_to_rotation_matrix(
euler_angles['roll_deg'],
euler_angles['pitch_deg'],
euler_angles['yaw_deg']
)
ned_velocity = np.array([
velocity_ned['north_m_s'],
velocity_ned['east_m_s'],
velocity_ned['down_m_s']
])
body_velocity = R @ ned_velocity
return {
'body_x_m_s': body_velocity[0],
'body_y_m_s': body_velocity[1],
'body_z_m_s': body_velocity[2]
}
async def collect_telemetry_data(self):
"""Collect telemetry data from the drone.
TODO: See if it is worth it to hold old data values from last run so that, if a value is not grabbed, it will hold the previous value.
"""
data = {}
async def request_rc_status():
try:
if not hasattr(self.drone.telemetry, 'rc_status'):
data['rc_status_error'] = "RC Status method not available."
return
rc_status_iter = self.drone.telemetry.rc_status()
try:
rc_status = await asyncio.wait_for(rc_status_iter.__anext__(), timeout=0.1)
data['rc_status'] = {
'is_available': rc_status.is_available,
'was_available_once': rc_status.was_available_once,
'signal_strength_percent': rc_status.signal_strength_percent
}
except asyncio.TimeoutError:
data['rc_status'] = {
'is_available': 0,
'was_available_once': 0,
'signal_strength_percent': 0
}
except AttributeError as e:
data['rc_status_error'] = str(e)
async def request_raw_imu():
try:
if not hasattr(self.drone.telemetry, 'raw_imu'):
data['raw_imu_error'] = "Raw IMU method not available."
return
raw_imu_iter = self.drone.telemetry.raw_imu()
try:
imu = await asyncio.wait_for(raw_imu_iter.__anext__(), timeout=0.1)
data['raw_imu'] = {
'acceleration_frd': imu.acceleration_frd,
'gyroscope_frd': imu.gyroscope_frd,
'magnetometer_frd': imu.magnetometer_frd
}
except asyncio.TimeoutError:
data['raw_imu'] = {
'acceleration_frd': 0,
'gyroscope_frd': 0,
'magnetometer_frd': 0
}
except AttributeError as e:
data['raw_imu_error'] = str(e)
async def request_euler_angles():
try:
euler_angles_iter = self.drone.telemetry.attitude_euler()
try:
euler_angle = await asyncio.wait_for(euler_angles_iter.__anext__(), timeout=0.1)
data['euler_angles'] = {
'roll_deg': euler_angle.roll_deg,
'pitch_deg': euler_angle.pitch_deg,
'yaw_deg': euler_angle.yaw_deg,
'timestamp_us': euler_angle.timestamp_us
}
except asyncio.TimeoutError:
data['euler_angles'] = {
'roll_deg': 0,
'pitch_deg': 0,
'yaw_deg': 0,
'timestamp_us': 0
}
except AttributeError:
data['euler_angles_error'] = "Euler Angle method not available."
async def request_angular_velocity():
try:
if not hasattr(self.drone.telemetry, 'attitude_angular_velocity_body'):
data['angular_velocity_error'] = "Attitude Angular Velocity Body method not available."
return
angular_velocity_iter = self.drone.telemetry.attitude_angular_velocity_body()
try:
angular_velocity = await asyncio.wait_for(angular_velocity_iter.__anext__(), timeout=0.1)
data['angular_velocity'] = {
'roll_rad_s': angular_velocity.roll_rad_s,
'pitch_rad_s': angular_velocity.pitch_rad_s,
'yaw_rad_s': angular_velocity.yaw_rad_s
}
except asyncio.TimeoutError:
data['angular_velocity'] = {
'roll_rad_s': 0,
'pitch_rad_s': 0,
'yaw_rad_s': 0
}
except AttributeError as e:
data['angular_velocity_error'] = str(e)
async def request_airspeed():
try:
airspeed_iter = self.drone.telemetry.fixedwing_metrics()
try:
metrics = await asyncio.wait_for(airspeed_iter.__anext__(), timeout=0.1)
data['airspeed'] = {
'airspeed_m_s': metrics.airspeed_m_s,
'throttle_percentage': metrics.throttle_percentage,
'climb_rate_m_s': metrics.climb_rate_m_s
}
except asyncio.TimeoutError:
data['airspeed'] = {
'airspeed_m_s': 0,
'throttle_percentage': 0,
'climb_rate_m_s': 0
}
except AttributeError:
data['airspeed_error'] = "FixedwingMetrics method not available."
async def request_position():
try:
position_iter = self.drone.telemetry.position()
try:
position = await asyncio.wait_for(position_iter.__anext__(), timeout=0.1)
data['position'] = {
'latitude_deg': position.latitude_deg,
'longitude_deg': position.longitude_deg,
'absolute_altitude_m': position.absolute_altitude_m,
'relative_altitude_m': position.relative_altitude_m
}
except asyncio.TimeoutError:
data['position'] = {
'latitude_deg': 0,
'longitude_deg': 0,
'absolute_altitude_m': 0,
'relative_altitude_m': 0
}
except AttributeError:
data['position_error'] = "Position method not available."
async def request_velocity_ned():
try:
velocity_ned_iter = self.drone.telemetry.velocity_ned()
try:
velocity = await asyncio.wait_for(velocity_ned_iter.__anext__(), timeout=0.1)
data['velocity_ned'] = {
'north_m_s': velocity.north_m_s,
'east_m_s': velocity.east_m_s,
'down_m_s': velocity.down_m_s
}
except asyncio.TimeoutError:
data['velocity_ned'] = {
'north_m_s': 0,
'east_m_s': 0,
'down_m_s': 0
}
except AttributeError:
data['velocity_ned_error'] = "Velocity NED method not available."
# Run all the requests concurrently
await asyncio.gather(
request_rc_status(),
request_raw_imu(),
request_euler_angles(),
request_angular_velocity(),
request_airspeed(),
request_position(),
request_velocity_ned()
)
if 'velocity_ned' in data and 'euler_angles' in data:
data['velocity_body'] = self.calculate_velocity_body(data['velocity_ned'], data['euler_angles'])
else:
data['velocity_body'] = {
'body_x_m_s': 0,
'body_y_m_s': 0,
'body_z_m_s': 0
}
return data