@@ -200,7 +200,6 @@ def __init__(self, tabWidget, helper, *args):
200
200
201
201
self .flying_enabled = False
202
202
self .switch_flight_mode (FlightModeStates .DISCONNECTED )
203
- self .cf_ready_to_fly = False
204
203
self .path_pos_threshold = 0.2
205
204
self .circle_pos_threshold = 0.1
206
205
self .circle_radius = 1.5
@@ -211,37 +210,37 @@ def __init__(self, tabWidget, helper, *args):
211
210
self .new_path = []
212
211
self .recording = False
213
212
self .land_for_recording = False
214
- self .default_flight_paths = [[
215
- "Path 1: Sandbox" , [ 0.0 , - 1.0 , 1.0 , 0.0 ], [ 0.0 , 1.0 , 1.0 , 0.0 ]
216
- ] ,
217
- [
218
- "Path 2: Height Test" ,
219
- [ 0.0 , 0.0 , 0.5 , 0.0 ],
220
- [ 0.0 , 0.0 , 1.0 , 0.0 ] ,
221
- [0.0 , 0.0 , 1 .5 , 0.0 ],
222
- [0.0 , 0.0 , 2 .0 , 0.0 ],
223
- [0.0 , 0.0 , 2.3 , 0.0 ],
224
- [0.0 , 0.0 , 1.8 , 0.0 ],
225
- [0.0 , 0.0 , 0.5 , 0.0 ],
226
- [0.0 , 0.0 , 0.3 , 0.0 ],
227
- [0.0 , 0.0 , 0.15 , 0.0 ]
228
- ],
229
- [
230
- "Path 3: 'Spiral'" ,
231
- [ 0.0 , 0.0 , 1.0 , 0.0 ] ,
232
- [0.5 , 0.5 , 1.0 , 0.0 ],
233
- [0.0 , 1.0 , 1.0 , 0.0 ],
234
- [ - 0.5 , 0.5 , 1.0 , 0.0 ],
235
- [ 0.0 , 0.0 , 1.0 , 0.0 ],
236
- [0.5 , 0.5 , 1.2 , 0.0 ],
237
- [0.0 , 1.0 , 1.4 , 0.0 ],
238
- [ - 0.5 , 0.5 , 1.6 , 0.0 ],
239
- [ 0.0 , 0.0 , 1.8 , 0.0 ],
240
- [0.5 , 0.5 , 1.5 , 0.0 ],
241
- [0.0 , 1.0 , 1.0 , 0.0 ],
242
- [ - 0.5 , 0.5 , 0.5 , 0.0 ],
243
- [ 0.0 , 0.0 , 0.25 , 0.0 ]
244
- ]]
213
+ self .default_flight_paths = [
214
+ [
215
+ "Path 1: Sandbox" ,
216
+ [ 0.0 , - 1.0 , 1.0 , 0.0 ],
217
+ [ 0.0 , 1.0 , 1.0 , 0.0 ]] ,
218
+ [
219
+ "Path 2: Height Test" ,
220
+ [0.0 , 0.0 , 0 .5 , 0.0 ],
221
+ [0.0 , 0.0 , 1 .0 , 0.0 ],
222
+ [0.0 , 0.0 , 1.5 , 0.0 ],
223
+ [0.0 , 0.0 , 2.0 , 0.0 ],
224
+ [0.0 , 0.0 , 2.3 , 0.0 ],
225
+ [0.0 , 0.0 , 1.8 , 0.0 ],
226
+ [0.0 , 0.0 , 0.5 , 0.0 ],
227
+ [ 0.0 , 0.0 , 0.3 , 0.0 ],
228
+ [ 0.0 , 0.0 , 0.15 , 0.0 ]],
229
+ [
230
+ "Path 3: 'Spiral'" ,
231
+ [0.0 , 0.0 , 1.0 , 0.0 ],
232
+ [0.5 , 0.5 , 1.0 , 0.0 ],
233
+ [ 0.0 , 1.0 , 1.0 , 0.0 ],
234
+ [ - 0.5 , 0.5 , 1.0 , 0.0 ],
235
+ [0.0 , 0.0 , 1.0 , 0.0 ],
236
+ [0.5 , 0.5 , 1.2 , 0.0 ],
237
+ [ 0.0 , 1.0 , 1.4 , 0.0 ],
238
+ [ - 0.5 , 0.5 , 1.6 , 0.0 ],
239
+ [0.0 , 0.0 , 1.8 , 0.0 ],
240
+ [0.5 , 0.5 , 1.5 , 0.0 ],
241
+ [ 0.0 , 1.0 , 1.0 , 0.0 ],
242
+ [ - 0.5 , 0.5 , 0.5 , 0.0 ],
243
+ [ 0.0 , 0.0 , 0.25 , 0.0 ] ]]
245
244
246
245
# The position and rotation of the cf and wand obtained by the
247
246
# camera tracking, if it cant be tracked the position becomes Nan
@@ -481,6 +480,19 @@ def add_transition(mode, child_state, parent):
481
480
self ._machine .setInitialState (parent_state )
482
481
self ._machine .start ()
483
482
483
+ def _update_flight_status (self ):
484
+ prev_flying_enabled = self .flying_enabled
485
+ self .flying_enabled = self ._cf is not None and \
486
+ self ._qtm_connection is not None
487
+
488
+ if not prev_flying_enabled and self .flying_enabled :
489
+ self .switch_flight_mode (FlightModeStates .GROUNDED )
490
+ t = threading .Thread (target = self .flight_controller )
491
+ t .start ()
492
+
493
+ if prev_flying_enabled and not self .flying_enabled :
494
+ self .switch_flight_mode (FlightModeStates .DISCONNECTED )
495
+
484
496
def _is_discovering (self , discovering ):
485
497
if discovering :
486
498
self .qtmIpBox .clear ()
@@ -815,8 +827,7 @@ async def setup_qtm_connection(self):
815
827
self .discoverQTM .setEnabled (False )
816
828
self .qtmIpBox .setEnabled (False )
817
829
818
- if self .cf_ready_to_fly :
819
- self .switch_flight_mode (FlightModeStates .GROUNDED )
830
+ self ._update_flight_status ()
820
831
821
832
self ._ui_update_timer .start (200 )
822
833
@@ -832,6 +843,7 @@ async def on_qtm_disconnect(self, reason):
832
843
"""Callback when QTM has been disconnected"""
833
844
834
845
self ._ui_update_timer .stop ()
846
+ self ._update_flight_status ()
835
847
836
848
self ._qtm_connection = None
837
849
logger .info (reason )
@@ -844,10 +856,7 @@ async def on_qtm_disconnect(self, reason):
844
856
self .connectQtmButton .setEnabled (True )
845
857
self .connectQtmButton .setText ('Connect QTM' )
846
858
self .qtmStatus = ': not connected : {}' .format (
847
- reason if reason is not None else ''
848
- )
849
-
850
- self .switch_flight_mode (FlightModeStates .DISCONNECTED )
859
+ reason if reason is not None else '' )
851
860
852
861
def on_qtm_event (self , event ):
853
862
logger .info (event )
@@ -938,8 +947,7 @@ def _flight_mode_land_entered(self):
938
947
def _flight_mode_path_entered (self ):
939
948
self .path_index = 1
940
949
941
- current = self .flight_paths [self .pathSelector .
942
- currentIndex ()]
950
+ current = self .flight_paths [self .pathSelector .currentIndex ()]
943
951
self .current_goal_pos = Position (
944
952
current [self .path_index ][0 ],
945
953
current [self .path_index ][1 ],
@@ -991,6 +999,7 @@ def _flight_mode_disconnected_entered(self):
991
999
992
1000
def flight_controller (self ):
993
1001
try :
1002
+ logger .info ('Starting flight controller thread' )
994
1003
self ._cf .param .set_value ('stabilizer.estimator' , '2' )
995
1004
self .reset_estimator (self ._cf )
996
1005
@@ -1048,8 +1057,8 @@ def flight_controller(self):
1048
1057
if self .valid_cf_pos .distance_to (
1049
1058
Position (self .current_goal_pos .x ,
1050
1059
self .current_goal_pos .y ,
1051
- ( self .current_goal_pos .z / self .land_rate
1052
- ) )) < self .path_pos_threshold :
1060
+ self .current_goal_pos .z / self .land_rate
1061
+ )) < self .path_pos_threshold :
1053
1062
self .land_rate *= 1.1
1054
1063
1055
1064
if self .land_rate > 1000 :
@@ -1074,8 +1083,8 @@ def flight_controller(self):
1074
1083
1075
1084
if position_hold_timer > self .position_hold_timelimit :
1076
1085
1077
- current = self .flight_paths [self . pathSelector .
1078
- currentIndex ()]
1086
+ current = self .flight_paths [
1087
+ self . pathSelector . currentIndex ()]
1079
1088
1080
1089
self .path_index += 1
1081
1090
if self .path_index == len (current ):
@@ -1271,6 +1280,8 @@ def flight_controller(self):
1271
1280
logger .error (err )
1272
1281
self .cfStatus = str (err )
1273
1282
1283
+ logger .info ('Terminating flight controller thread' )
1284
+
1274
1285
def save_current_position (self ):
1275
1286
if self .recording :
1276
1287
# Restart the timer
@@ -1285,15 +1296,9 @@ def _connected(self, link_uri):
1285
1296
"""Callback when the Crazyflie has been connected"""
1286
1297
1287
1298
self ._cf = self ._helper .cf
1299
+ self ._update_flight_status ()
1288
1300
1289
- if not self .flying_enabled :
1290
- self .flying_enabled = True
1291
- self .cfStatus = ": connecting..."
1292
- t = threading .Thread (target = self .flight_controller )
1293
- t .start ()
1294
-
1295
- self .uri = link_uri
1296
- logger .debug ("Crazyflie connected to {}" .format (self .uri ))
1301
+ logger .debug ("Crazyflie connected to {}" .format (link_uri ))
1297
1302
1298
1303
# Gui
1299
1304
self .cfStatus = ': connected'
@@ -1303,9 +1308,8 @@ def _disconnected(self, link_uri):
1303
1308
1304
1309
logger .info ("Crazyflie disconnected from {}" .format (link_uri ))
1305
1310
self .cfStatus = ': not connected'
1306
- self .flying_enabled = False
1307
- self .cf_ready_to_fly = False
1308
1311
self ._cf = None
1312
+ self ._update_flight_status ()
1309
1313
1310
1314
def _param_updated (self , name , value ):
1311
1315
"""Callback when the registered parameter get's updated"""
@@ -1366,15 +1370,15 @@ def wait_for_position_estimator(self, cf):
1366
1370
1367
1371
if (max_x - min_x ) < threshold and (
1368
1372
max_y - min_y ) < threshold and (
1369
- max_z - min_z ) < threshold :
1370
- logger .info (
1371
- "Position found with error in, x: {}, y: {}, z: {}" .
1372
- format (max_x - min_x , max_y - min_y , max_z - min_z ))
1373
+ max_z - min_z ) < threshold :
1374
+ logger .info ("Position found with error in, x: {}, y: {}, "
1375
+ "z: {}" .format (max_x - min_x ,
1376
+ max_y - min_y ,
1377
+ max_z - min_z ))
1373
1378
1374
1379
self .cfStatus = ": connected"
1375
1380
1376
1381
self .switch_flight_mode (FlightModeStates .GROUNDED )
1377
- self .cf_ready_to_fly = True
1378
1382
1379
1383
break
1380
1384
@@ -1389,14 +1393,14 @@ def reset_estimator(self, cf):
1389
1393
1390
1394
def switch_flight_mode (self , mode ):
1391
1395
# Handles the behaviour of switching between flight modes
1392
-
1393
1396
self .flight_mode = mode
1394
1397
1395
1398
# Handle client input control.
1396
1399
# Disable gamepad input if we are not grounded
1397
1400
if self .flight_mode in [
1398
- FlightModeStates .GROUNDED , FlightModeStates .DISCONNECTED ,
1399
- FlightModeStates .RECORD
1401
+ FlightModeStates .GROUNDED ,
1402
+ FlightModeStates .DISCONNECTED ,
1403
+ FlightModeStates .RECORD
1400
1404
]:
1401
1405
self ._helper .mainUI .disable_input (False )
1402
1406
else :
0 commit comments