-
Notifications
You must be signed in to change notification settings - Fork 63
/
multiwii.py
354 lines (295 loc) · 9.58 KB
/
multiwii.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#/!usr/bin/env python
import time
import logging
import serial
import threading
import struct # for decoding data strings
class drone:
def __init__(self, port):
self.started = True
self.ATT = 0 # Ask and save the attitude of the multicopter
self.ALT = 0 # Ask and save the altitude of the multicopter
self.RC = 1 # Ask and save the pilot commands of the multicopter
self.SET_RC = 1 # Set rc command
self.MOT = 0 # Ask and save the PWM of the motors that the MW is writing to the multicopter
self.RAW = 0 # Ask and save the raw imu data of the multicopter
self.CMD = 0 # Send commands to the MW to control it
self.UDP = 0 # Save or use UDP data (to be adjusted)
self.ASY = 0 # Use async communicacion
self.SCK = 0 # Use regular socket communication
self.SCKSRV = 0 # Use socketserver communication
self.PRINT = 0 # Print data to terminal, useful for debugging
###############################
# Communication via serial port
###############################
self.port = port
self.ser=serial.Serial()
self.ser.port=self.port
self.ser.baudrate=115200
self.ser.bytesize=serial.EIGHTBITS
self.ser.parity=serial.PARITY_NONE
self.ser.stopbits=serial.STOPBITS_ONE
self.ser.timeout=0
self.ser.xonxoff=False
self.ser.rtscts=False
self.ser.dsrdtr=False
self.ser.writeTimeout=2
self.timeMSP=0.02
try:
self.ser.open()
except Exception, e:
logging.error("Error while open serial port: " + str(e))
exit()
###############################
# Multiwii Serial Protocol
# Hex value for MSP request
##############################
self.BASIC="\x24\x4d\x3c\x00" #MSG Send Header (to MultiWii)
self.MSP_IDT=self.BASIC+"\x64\x64" #MSG ID: 100
self.MSP_STATUS=self.BASIC+"\x65\x65" #MSG ID: 101
self.MSP_RAW_IMU=self.BASIC+"\x66\x66" #MSG ID: 102
self.MSP_SERVO=self.BASIC+"\x67\x67" #MSG ID: 103
self.MSP_MOTOR=self.BASIC+"\x68\x68" #MSG ID: 104
self.MSP_RC=self.BASIC+"\x69\x69" #MSG ID: 105
self.MSP_RAW_GPS=self.BASIC+"\x6A\x6A" #MSG ID: 106
self.MSP_ATTITUDE=self.BASIC+"\x6C\x6C" #MSG ID: 108
self.MSP_ALTITUDE=self.BASIC+"\x6D\x6D" #MSG ID: 109
self.MSP_BAT = self.BASIC+"\x6E\x6E" #MSG ID: 110
self.MSP_COMP_GPS=self.BASIC+"\x71\x71" #MSG ID: 111
self.MSP_SET_RC=self.BASIC+"\xC8\xC8" #MSG ID: 200
self.CMD2CODE = {
# Getter
'MSP_IDENT':100,
'MSP_STATUS':101,
'MSP_RAW_IMU':102,
'MSP_SERVO':103,
'MSP_MOTOR':104,
'MSP_RC':105,
'MSP_RAW_GPS':106,
'MSP_COMP_GPS':107,
'MSP_ATTITUDE':108,
'MSP_ALTITUDE':109,
'MSP_ANALOG':110,
'MSP_RC_TUNING':111,
'MSP_PID':112,
'MSP_BOX':113,
'MSP_MISC':114,
'MSP_MOTOR_PINS':115,
'MSP_BOXNAMES':116,
'MSP_PIDNAMES':117,
'MSP_WP':118,
'MSP_BOXIDS':119,
# Setter
'MSP_SET_RAW_RC':200,
'MSP_SET_RAW_GPS':201,
'MSP_SET_PID':202,
'MSP_SET_BOX':203,
'MSP_SET_RC_TUNING':204,
'MSP_ACC_CALIBRATION':205,
'MSP_MAG_CALIBRATION':206,
'MSP_SET_MISC':207,
'MSP_RESET_CONF':208,
'MSP_SET_WP':209,
'MSP_SWITCH_RC_SERIAL':210,
'MSP_IS_SERIAL':211,
'MSP_DEBUG':254,
}
###############################
# Initialize Global Variables
###############################
self.latitude = 0.0
self.longitude = 0.0
self.altitude = -0
self.heading = -0
self.timestamp = -0
self.gpsString = -0
self.numSats = -0
self.accuracy = -1
self.beginFlag = 0
self.roll = 0
self.pitch = 0
self.yaw = 0
self.throttle = 0
self.angx = 0.0
self.angy = 0.0
self.m1 = 0
self.m2 = 0
self.m3 = 0
self.m4 = 0
self.message = ""
self.ax = 0
selfay = 0
self.az = 0
self.gx = 0
self.gy = 0
self.gz = 0
self.magx = 0
self.magy = 0
self.magz = 0
self.elapsed = 0
self.flytime = 0
self.numOfValues = 0
self.precision = 3
self.rcData = [1500, 1500, 1500, 1500] #order -> roll, pitch, yaw, throttle
self.loopThread = threading.Thread(target=self.loop)
if self.ser.isOpen():
print("Wait 5 sec for calibrate Multiwii")
time.sleep(5)
self.loopThread.start()
def stop(self):
self.started = False
#############################################################
# littleEndian(value)
# receives: a parsed, hex data piece
# outputs: the decimal value of that data
# function: swaps byte by byte to convert little
# endian to big endian
# function: calls 2's compliment to convert to decimal
# returns: The integer value
#############################################################
def littleEndian(self, value):
length = len(value) # gets the length of the data piece
actual = ""
for x in range(0, length/2): #go till you've reach the halway point
actual += value[length-2-(2*x):length-(2*x)] #flips all of the bytes (the last shall be first)
x += 1
intVal = self.twosComp(actual) # sends the data to be converted from 2's compliment to int
return intVal # returns the integer value
###################################################################
# twosComp(hexValue)
# receives: the big endian hex value (correct format)
# outputs: the decimal value of that data
# function: if the value is negative, swaps all bits
# up to but not including the rightmost 1.
# Else, just converts straight to decimal.
# (Flip all the bits left of the rightmost 1)
# returns: the integer value
###################################################################
def twosComp(self, hexValue):
firstVal = int(hexValue[:1], 16)
if firstVal >= 8: # if first bit is 1
bValue = bin(int(hexValue, 16))
bValue = bValue[2:] # removes 0b header
newBinary = []
length = len(bValue)
index = bValue.rfind('1') # find the rightmost 1
for x in range(0, index+1): # swap bits up to rightmost 1
if x == index: #if at rightmost one, just append remaining bits
newBinary.append(bValue[index:])
elif bValue[x:x+1] == '1':
newBinary.append('0')
elif bValue[x:x+1] == '0':
newBinary.append('1')
x += 1
newBinary = ''.join(newBinary) # converts char array to string
finalVal = -int(newBinary, 2) # converts to decimal
return finalVal
else: # if not a negative number, simply convert to decimal
return int(hexValue, 16)
def sendData(self, data_length, code, data):
checksum = 0
total_data = ['$', 'M', '<', data_length, code] + data
for i in struct.pack('<2B%dh' % len(data), *total_data[3:len(total_data)]):
checksum = checksum ^ ord(i)
total_data.append(checksum)
try:
b = None
b = self.ser.write(struct.pack('<3c2B%dhB' % len(data), *total_data))
except Exception, ex:
print 'send data error'
print(ex)
return b
#############################################################
# askRC()
# receives: nothing
# outputs: nothing
# function: Do everything to ask the MW for data and save it on globals
# returns: nothing
#############################################################
def askRC(self):
self.ser.flushInput() # cleans out the serial port
self.ser.flushOutput()
self.ser.write(self.MSP_RC) # gets RC information
time.sleep(self.timeMSP)
response = self.ser.readline()
if str(response) == "":
#print(msp_hex)
#print("Header: " + msp_hex[:6])
#payload = int(msp_hex[6:8])
#print("Payload: " + msp_hex[6:8])
#print("Code: " + msp_hex[8:10])
#print("RC data unavailable")
return
else:
msp_hex = response.encode("hex")
if msp_hex[10:14] == "":
print("roll unavailable")
else:
self.roll = float(self.littleEndian(msp_hex[10:14]))
if msp_hex[14:18] == "":
print("pitch unavailable")
else:
self.pitch = float(self.littleEndian(msp_hex[14:18]))
if msp_hex[18:22] == "":
print("yaw unavailable")
else:
self.yaw = float(self.littleEndian(msp_hex[18:22]))
if msp_hex[22:26] == "":
print("throttle unavailable")
else:
self.throttle = float(self.littleEndian(msp_hex[22:26]))
#print(str(self.roll) + " " + str(self.pitch) + " " + str(self.yaw) + " " + str(self.throttle))
def setRC(self):
self.sendData(8, self.CMD2CODE["MSP_SET_RAW_RC"], self.rcData)
time.sleep(self.timeMSP)
#print self.rcData
def loop(self):
print('success')
try:
#########################################################################
# Loop
#########################################################################
while self.started:
if self.SET_RC:
self.setRC()
if self.ATT:
askATT()
if self.ALT:
askALT()
if self.RC:
self.askRC()
if self.MOT:
askMOTOR()
if self.RAW:
askRAW()
if self.SCK:
getUDP()
"""
if beginFlag != 1: # Won't send any data until both altitude and heading are valid data
if self.TIME:
message = str(round(diff,precision))
if self.FLYT:
message = message+" "+str(round(elapsed,precision))
if self.ATT:
message = message+" "+str(angx)+" "+str(angy)+" "+str(heading)
if self.RC:
message = message+" "+str(roll)+" "+str(pitch)+" "+str(yaw)+" "+str(throttle)
if self.ALT:
message = message+" "+str(altitude)
if self.MOT:
message = message+" "+str(m1)+" "+str(m2)+" "+str(m3)+" "+str(m4)
if self.RAW:
message = message+" "+str(ax)+" "+str(ay)+" "+str(az)+" "+str(gx)+" "+str(gy)+" "+str(gz)+" "+str(magx)+" "+str(magy)+" "+str(magz)
if self.UDP:
if udp_mess == "":
message = message+" "+udp_mess2
udp_mess2=""
message = message+" "+udp_mess
if self.PRINT:
print(message)
else: # If invalid, continue looping
beginFlag = 0 # resets the flag """
self.ser.close()
file.close()
except Exception,e1: # Catches any errors in the serial communication
print("Error on main: "+str(e1))