forked from PiStuffing/Quadcopter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRC.py
337 lines (269 loc) · 13.1 KB
/
RC.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
#!/usr/bin/python
from __future__ import division
from smbus2 import SMBusWrapper, i2c_msg
import math
import socket
import struct
import sys
import select
import time
def client():
################################################################################################
# MAKE CONNECTION TO SERVER
################################################################################################
poll = select.poll()
go_go_go = False
pack_format = "=ffffb?"
pack_size = struct.calcsize(pack_format)
unpack_format = "=?"
unpack_size = struct.calcsize(unpack_format)
client = socket.socket()
addr = "192.168.42.1"
port = 31415
while True:
try:
client.connect((addr, port))
except:
time.sleep(0.1)
else:
break
continue
client_fd = client.fileno()
poll.register(client_fd, select.POLLIN | select.POLLPRI)
################################################################################################
# SET UP THE JOYSTICKS FSM
################################################################################################
PASSIVE = 0
TAKEOFF = 1
FLYING = 2
LANDING = 3
POWEROFF = 4
state = TAKEOFF
status_quo = [0.0, 0.0, 0.0, 0.0]
#----------------------------------------------------------------------------------------------
# Acquire contact with the piDrone; only continue below once acquired.
#----------------------------------------------------------------------------------------------
with SMBusWrapper(1) as bus:
# bus.write_byte_data(0x40, 0x76, 2)
# bus.write_byte_data(0x41, 0x76, 2)
while state != POWEROFF:
########################################################################################
# HANDLE MESSAGES FROM THE SERVER
########################################################################################
results = poll.poll(500) # milliseconds
#---------------------------------------------------------------------------------------
# Check whether there's I/O from RC
#---------------------------------------------------------------------------------------
for fd, event in results:
assert fd == client_fd, "WTF HAPPENED HERE"
#-----------------------------------------------------------------------------------
# Has piDrone told piRC to start?
#-----------------------------------------------------------------------------------
raw = client.recv(unpack_size)
assert (len(raw) == unpack_size), "Invalid data"
#-----------------------------------------------------------------------------------
# React on the action
#-----------------------------------------------------------------------------------
formatted = struct.unpack(unpack_format, raw)
if formatted[0]: # is True
go_go_go = True
state = TAKEOFF
takeoff_time = time.time()
else:
go_go_go = False
state = POWEROFF
if not go_go_go:
continue
########################################################################################
# HANDLE MESSAGES FROM THE JOYSTICKS
########################################################################################
#---------------------------------------------------------------------------------------
# UD = Up / Down - upwards is positive
# YR = Yaw Rate - anticlockwise is positive
# LR = Left / Right - leftwards is positive
# FB = Forwards / Backwards - forwards is positive
#---------------------------------------------------------------------------------------
msg = i2c_msg.read(0x40, 2)
bus.i2c_rdwr(msg)
data = list(msg)
assert (len(data) == 2), "Joystick 0 data len: %d" % len(data)
if (data[0] > 127):
UD = data[0] - 256
else:
UD = data[0]
if (data[1] > 127):
YR = data[1] - 256
else:
YR = data[1]
msg = i2c_msg.read(0x41, 2)
bus.i2c_rdwr(msg)
data = list(msg)
assert (len(data) == 2), "Joystick 1 data len: %d" % len(data)
if (data[0] > 127):
LR = data[0] - 256
else:
LR = data[0]
if (data[1] > 127):
FB = -(data[1] - 256)
else:
FB = -data[1]
#=======================================================================================
# FSM INPUT, STATES, OUTPUT
#=======================================================================================
beep = False
#---------------------------------------------------------------------------------------
# Special cases for takeoff and landing. Only between these states do we tell the
# piDrone what to do.
#---------------------------------------------------------------------------------------
if abs(UD) < 20 and YR > 20 and abs(FB) < 20 and LR < -20:
#-------------------------------------------------------------------------------
# Take-off - we send fixed takeoff param regardless of joystick for next 3 seconds.
#-------------------------------------------------------------------------------
if state == PASSIVE:
print "takeoff-takeoff-takeoff"
state = TAKEOFF
beep = True
takeoff_time = time.time()
if abs(UD) < 20 and YR < -20 and abs(FB) < 20 and LR > 20:
#-----------------------------------------------------------------------------------
# Send a shut-down to the piDrone, and shut our selves down once we get a confirmation
# from the piDrone.
#-----------------------------------------------------------------------------------
if state == FLYING:
print "landing-landing-landing"
state = LANDING
beep = True
landing_time = time.time()
elif state == PASSIVE:
print "poweroff-poweroff-poweroff"
state = POWEROFF
beep = True
#=======================================================================================
# FSM INPUTS
#=======================================================================================
if state == TAKEOFF:
if time.time() - takeoff_time < 3.0: # seconds
UD = 0.33
YR = 0.0
LR = 0.0
FB = 0.0
else:
UD, YR, FB, LR = status_quo
state = FLYING
beep = True
#-------------------------------------------------------------------------------
# Take the timestamp of flight, and initiate initial height
#-------------------------------------------------------------------------------
before = time.time()
height = 1.0
elif state == FLYING:
#-------------------------------------------------------------------------------
# Joysticks are +/- 80, convert these to +/- 1m/s. The exception is the yaw rate
# where +/-80 maps to +/- 90 degrees (pi/2) per second
#-------------------------------------------------------------------------------
UD /= 80
YR /= (80 * 2 / math.pi)
FB /= 80
LR /= 80
#-------------------------------------------------------------------------------
# Integrate the height increment and mark the landing timestamo
#-------------------------------------------------------------------------------
now = time.time()
dt = now - before
before = now
height += UD * dt #RC! Flawed: future velocity x historic dt
landing_period = height / 0.33 + 1 #RC! OK?
elif state == LANDING:
if time.time() - landing_time < landing_period: # seconds
UD = -0.33
YR = 0.0
FB = 0.0
LR = 0.0
else:
UD, YR, FB, LR = status_quo
state = PASSIVE
beep = True
passive_time = time.time()
elif state == PASSIVE:
UD, YR, FB, LR = status_quo
if time.time() - passive_time > 60.0:
state = POWEROFF
beep = True
else:
assert state == POWEROFF, "Should be on poweroff state here!"
UD, YR, FB, LR = status_quo
output = struct.pack(pack_format, UD, YR, FB, LR, state, beep)
client.send(output)
print "SENT: UD = %f | YR = %f | FB = %f | LR = %f | status = %d | beep = %d" % (UD, YR, FB, LR, state, beep)
else:
#---------------------------------------------------------------------------------------
# We get here when the server sends us the "running = False"
#---------------------------------------------------------------------------------------
client.close()
client()
'''
def server():
poll = select.poll()
unpack_format = "=ffffb?"
unpack_size = struct.calcsize(unpack_format)
pack_format = "=?"
server = socket.socket()
addr = "192.168.42.1"
port = 31415
server.bind((addr, port))
server.listen(5)
try:
connection, addr = server.accept()
connection_fd = connection.fileno()
poll.register(connection_fd, select.POLLIN | select.POLLPRI)
#-------------------------------------------------------------------------------------------
# Tell the client to go-go-go!
#-------------------------------------------------------------------------------------------
output = struct.pack(pack_format, True)
connection.send(output)
#-------------------------------------------------------------------------------------------
# Listen to the client and do what it says.
#-------------------------------------------------------------------------------------------
while True:
results = poll.poll(500)
#---------------------------------------------------------------------------------------
# Check whether there's I/O from RC
#---------------------------------------------------------------------------------------
for fd, event in results:
assert fd == connection_fd, "WHOSE FD IS THIS?"
#-----------------------------------------------------------------------------------
# Unpack the data received
#-----------------------------------------------------------------------------------
raw = connection.recv(unpack_size)
assert (len(raw) == unpack_size), "Invalid data"
#-----------------------------------------------------------------------------------
# React on the action
#-----------------------------------------------------------------------------------
formatted = struct.unpack(unpack_format, raw)
assert (len(formatted) == 6), "Bad formatted size"
UD = formatted[0]
YR = formatted[1]
FB = formatted[2]
LR = formatted[3]
state = formatted[4]
beep = formatted[5]
print "RECEIVED: UD = %f | YR = %f | FB = %f | LR = %f | status = %d | beep = %d" % (UD, YR, FB, LR, state, beep)
except KeyboardInterrupt:
#-------------------------------------------------------------------------------------------
# Tell the client to stop-stop-stop!
#-------------------------------------------------------------------------------------------
output = struct.pack(pack_format, False)
connection.send(output)
except Exception, err:
print err
finally:
connection.close()
if len(sys.argv) != 2:
print "Select DRONE or RC"
elif sys.argv[1] == "RC":
client()
elif sys.argv[1] == "DRONE":
server()
else:
print "Select RC or DRONE"
'''