-
Notifications
You must be signed in to change notification settings - Fork 8
/
ARDroneNavdata.py
177 lines (157 loc) · 6.73 KB
/
ARDroneNavdata.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
# -*- coding:Utf-8 -*-
# ARDrone Lib Package
prog_name = "AR.Drone NavData"
# version:
version = 4
# By Vianney Tran, Romain Fihue, Giulia Guidi, Julien Lagarde
# License: Creative Commons Attribution-ShareAlike 3.0 (CC BY-SA 3.0)
# (http://creativecommons.org/licenses/by-sa/3.0/)
##############
### IMPORT ###
##############
import struct
###############
### GLOBALS ###
###############
###############
### CLASSES ###
###############
###################
### DEFINITIONS ###
###################
def _drone_status_decode(packet):
"Decode the block which contains Drone Status"
# NavData packet has been splited let's go decode it
drone_state=dict() #It's in block[0]'s second 32bits int = block[0][1]
drone_state['flying'] = packet & 1
drone_state['video_on'] = packet>>1 & 1
drone_state['vision_on'] = packet>>2 & 1
drone_state['angle_algo'] = packet>>3 & 1
drone_state['altitude_algo'] = packet>>4 & 1
drone_state['user_feedback'] = packet>>5 & 1
drone_state['command_ack'] = packet>>6 & 1
drone_state['fw_ok'] = packet>>7 & 1
drone_state['fw_new'] = packet>>8 & 1
drone_state['fw_update'] = packet>>9 & 1
drone_state['navdata_demo'] = packet>>10 & 1
drone_state['navdata_bootstrap'] = packet>>11 & 1
drone_state['motor_status'] = packet>>12 & 1
drone_state['com_lost'] = packet>>13 & 1
drone_state['vbat_low'] = packet>>15 & 1
drone_state['user_emergency'] = packet>>16 & 1
drone_state['timer_elapsed'] = packet>>17 & 1
drone_state['too_much_angle'] = packet>>19 & 1
drone_state['ultrasound_ok'] = packet>>21 & 1
drone_state['cutout'] = packet>>22 & 1
drone_state['pic_version_ok'] = packet>>23 & 1
drone_state['atcodec_thread_on'] = packet>>24 & 1
drone_state['navdata_thread_on'] = packet>>25 & 1
drone_state['video_thread_on'] = packet>>26 & 1
drone_state['acq_thread_on'] = packet>>27 & 1
drone_state['ctrl_watchdog'] = packet>>28 & 1
drone_state['adc_watchdog'] = packet>>29 & 1
drone_state['com_watchdog'] = packet>>30 & 1
drone_state['emergency'] = packet>>31 & 1
return drone_state
def _navdata_demo_decode(packet):
"Decode the navdata_demo which is data about the flight"
navdata_demo=dict()
if packet[0]!=0:
raise IOError("Packet isn't navdata-demo packet")
navdata_demo["ctrl_state"]= struct.unpack_from("=I",packet[2],0)[0]
navdata_demo["battery_percentage"]= struct.unpack_from("=I",packet[2],4)[0]
navdata_demo["theta"]= int(struct.unpack_from("=f",packet[2],8)[0]/1000)
navdata_demo["phi"]= int(struct.unpack_from("=f",packet[2],12)[0]/1000)
navdata_demo["psi"]= struct.unpack_from("=f",packet[2],16)[0]
navdata_demo["altitude"]=struct.unpack_from("=i",packet[2],20)[0]
navdata_demo["vx"]=int(struct.unpack_from("=f",packet[2],24)[0])
navdata_demo["vy"]=int(struct.unpack_from("=f",packet[2],28)[0])
navdata_demo["vz"]=int(struct.unpack_from("=f",packet[2],32)[0])
return navdata_demo
def _vision_detect_decode(packet):
"Decode the vision detection packet, packet is (id=16, size, data)"
if packet[0] != 16:
raise IOError("Packet is not vision-detect packet")
# Have to check if there isn't tag and size first
vision_detect = dict()
vision_detect["nb_detected"] = struct.unpack_from("=I",packet[2][0:20])[0]
vision_detect["xc"] = struct.unpack_from("=I",packet[2][20:36])[0]
vision_detect["yc"] = struct.unpack_from("=I",packet[2][36:52])[0]
vision_detect["width"] = struct.unpack_from("=I",packet[2][52:68])[0]
vision_detect["height"] = struct.unpack_from("=I",packet[2][68:84])[0]
vision_detect["distance"] = struct.unpack_from("=I",packet[2][84:100])[0]
return vision_detect
def _gps_decode(packet):
"Decode data about the GPS"
gps_info = dict()
if packet[0]!=27:
raise IOError("Packet isn't navdata-demo packet")
unpacked = struct.unpack_from("=ddddBBB",packet[2],0)
gps_info["latitude"]= unpacked[0]
gps_info["longitude"]= unpacked[1]
gps_info["elevation"]= unpacked[2]
gps_info["hdop"]= unpacked[3]
gps_info["data_available"]= unpacked[4]
gps_info["zero_validated"]= unpacked[5]
gps_info["wpt_validated"]= unpacked[6]
return gps_info
def navdata_decode(packet):
"Split then decodes the navdata packet gathered from UDP 5554"
position=0
offset = 0
block=[]
block.append(struct.unpack_from("=IIII",packet,position))
offset += struct.calcsize("=IIII")
i=1
while 1:
try:
block.append([])
block[i]=list(struct.unpack_from("=HH",packet,offset)) #Separate Option ID & Size of option int
offset += struct.calcsize("=HH")
except struct.error:
break
block[i].append(packet[offset:offset-struct.calcsize("=HH")+int(block[i][1])])
offset += block[i][1] - struct.calcsize("=HH")
i=i+1
# block[0] is (header, drone_state, sequence_number, vision_flag)
# block[1:-1] is Option0, Option1 ...
# block[-1] is checksum
# Decode the drone state
drone_state = _drone_status_decode(block[0][1])
vision_detect = None
navdata_demo = None
gps_info = None
# For each option
unsupported_option = []
for i in range(1,len(block)):
if block[i] != []:
# Vision detection option
if block[i][0]==16:
vision_detect = _vision_detect_decode(block[i])
# Option0: Navdata_demo(useful data)
elif block[i][0]==0:
navdata_demo = _navdata_demo_decode(block[i])
# GPS Info
elif block[i][0]==27:
gps_info = _gps_decode(block[i])
# Checksum option
elif block[i][0] == 65535:
pass
# Else we don't know
else:
unsupported_option.append((block[i][0],block[i][1]))
navdata=dict()
navdata['drone_state']=drone_state
navdata['vision_detect']=vision_detect
navdata['navdata_demo'] = navdata_demo
navdata['gps_info'] = gps_info
navdata['unsupported_option'] = unsupported_option
return navdata
##################
### __MAIN__ ###
##################
if __name__ == "__main__":
print "> Welcome to " + str(prog_name) + " (r" + str(version) + ")"
print "> By Vianney Tran, Romain Fihue, Giulia Guidi, Julien Lagarde (under CC BY-SA 3.0 license)"
print "> Loading program ..."
print "> This is a library only, please use the test instead"