Skip to content

Commit 9c1ba0d

Browse files
authored
Update drone_utils.py
Added few utility functions for reference.
1 parent 2bdeefd commit 9c1ba0d

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

drone_utils.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def telemetry():
129129
print("Is Armable?: {}".format(vehicle.is_armable))
130130
print("System status: {}".format(vehicle.system_status.state))
131131
print("Mode: {}".format(vehicle.mode.name))
132+
133+
# Call the mother of all.
134+
vehicle_state_json_builder()
135+
print_vehicle_state()
132136

133137

134138
# Check the battery percentage and land if less than 9.9
@@ -367,3 +371,115 @@ def velocity_flight():
367371

368372
while True:
369373
sleep(1)
374+
375+
376+
def print_vehicle_state(vehicle, configs):
377+
# Get all vehicle attributes (state)
378+
print "\nGet all vehicle attribute values:"
379+
print " Autopilot Firmware version: %s" % vehicle.version
380+
print " Major version number: %s" % vehicle.version.major
381+
print " Minor version number: %s" % vehicle.version.minor
382+
print " Patch version number: %s" % vehicle.version.patch
383+
print " Release type: %s" % vehicle.version.release_type()
384+
print " Release version: %s" % vehicle.version.release_version()
385+
print " Stable release?: %s" % vehicle.version.is_stable()
386+
print " Autopilot capabilities"
387+
# print " Supports MISSION_FLOAT message type: %s" % vehicle.capabilities.mission_float
388+
print " Supports PARAM_FLOAT message type: %s" % vehicle.capabilities.param_float
389+
print " Supports MISSION_INT message type: %s" % vehicle.capabilities.mission_int
390+
print " Supports COMMAND_INT message type: %s" % vehicle.capabilities.command_int
391+
print " Supports PARAM_UNION message type: %s" % vehicle.capabilities.param_union
392+
print " Supports ftp for file transfers: %s" % vehicle.capabilities.ftp
393+
print " Supports commanding attitude offboard: %s" % vehicle.capabilities.set_attitude_target
394+
print " Supports commanding position and velocity targets in local NED frame: %s" % vehicle.capabilities.set_attitude_target_local_ned
395+
print " Supports set position + velocity targets in global scaled integers: %s" % vehicle.capabilities.set_altitude_target_global_int
396+
print " Supports terrain protocol / data handling: %s" % vehicle.capabilities.terrain
397+
print " Supports direct actuator control: %s" % vehicle.capabilities.set_actuator_target
398+
print " Supports the flight termination command: %s" % vehicle.capabilities.flight_termination
399+
print " Supports mission_float message type: %s" % vehicle.capabilities.mission_float
400+
print " Supports onboard compass calibration: %s" % vehicle.capabilities.compass_calibration
401+
print " Global Location: %s" % vehicle.location.global_frame
402+
print " Global Location (relative altitude): %s" % vehicle.location.global_relative_frame
403+
print " Local Location: %s" % vehicle.location.local_frame
404+
print " Attitude: %s" % vehicle.attitude
405+
print " Velocity: %s" % vehicle.velocity
406+
print " GPS: %s" % vehicle.gps_0
407+
print " Gimbal status: %s" % vehicle.gimbal
408+
print " Battery: %s" % vehicle.battery
409+
print " EKF OK?: %s" % vehicle.ekf_ok
410+
print " Last Heartbeat: %s" % vehicle.last_heartbeat
411+
print " Rangefinder: %s" % vehicle.rangefinder
412+
print " Rangefinder distance: %s" % vehicle.rangefinder.distance
413+
print " Rangefinder voltage: %s" % vehicle.rangefinder.voltage
414+
print " Heading: %s" % vehicle.heading
415+
print " Is Armable?: %s" % vehicle.is_armable
416+
print " System status: %s" % vehicle.system_status.state
417+
print " Groundspeed: %s" % vehicle.groundspeed # settable
418+
print " Airspeed: %s" % vehicle.airspeed # settable
419+
print " Mode: %s" % vehicle.mode.name # settable
420+
print " Armed: %s" % vehicle.armed # settable
421+
422+
423+
def vehicle_state_json_builder(vehicle, configs):
424+
data = {
425+
'drone_id': configs.get('drone', 'drone_id'),
426+
'timestamp': vehicle._master.timestamp,
427+
'air_speed': vehicle.airspeed,
428+
'is_armable': vehicle.is_armable,
429+
'autopilot_type': vehicle._autopilot_type,
430+
'flightmode': vehicle._flightmode,
431+
'groundspeed': vehicle._groundspeed,
432+
'heading': vehicle.heading,
433+
'home_location': vehicle._home_location,
434+
'last_heartbeat': vehicle._last_heartbeat,
435+
'location': {
436+
'alt': vehicle._location._alt,
437+
'down': vehicle._location._down,
438+
'east': vehicle._location._east,
439+
'north': vehicle._location._north,
440+
'relative_alt': vehicle._location._relative_alt,
441+
'lat': vehicle._location._lat,
442+
'log': vehicle._location._lon
443+
},
444+
'location_global': {
445+
'alt': vehicle._location.global_frame.alt,
446+
'lat': vehicle._location.global_frame.lat,
447+
'log': vehicle._location.global_frame.lon
448+
},
449+
'location_global_relative': {
450+
'alt': vehicle._location.global_relative_frame.alt,
451+
'lat': vehicle._location.global_relative_frame.lat,
452+
'log': vehicle._location.global_relative_frame.lon
453+
},
454+
'location_local': {
455+
'down': vehicle._location.local_frame.down,
456+
'east': vehicle._location.local_frame.east,
457+
'north': vehicle._location.local_frame.north
458+
},
459+
'attitude': {
460+
'pitch': vehicle._pitch,
461+
'pitch_speed': vehicle._pitchspeed,
462+
'roll': vehicle._roll,
463+
'roll_speed': vehicle._rollspeed,
464+
'yaw': vehicle._yaw,
465+
'yaw_speed': vehicle._yawspeed
466+
},
467+
'battery': {
468+
'battery_current': vehicle.battery.current,
469+
'battery_level': vehicle.battery.level,
470+
'battery_voltage': vehicle.battery.voltage
471+
},
472+
'gps': {
473+
'eph': vehicle.gps_0.eph,
474+
'epv': vehicle.gps_0.epv,
475+
'fix_type': vehicle.gps_0.fix_type,
476+
'satellites_visible': vehicle.gps_0.satellites_visible
477+
},
478+
'groundspeed': vehicle._groundspeed,
479+
'last_heartbeat': vehicle.last_heartbeat,
480+
'velocity': vehicle.velocity,
481+
}
482+
483+
print data
484+
return data
485+

0 commit comments

Comments
 (0)