-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPCServer.py
More file actions
442 lines (384 loc) · 12.1 KB
/
RPCServer.py
File metadata and controls
442 lines (384 loc) · 12.1 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/python3
# coding=utf8
import os
import sys
sys.path.append('/home/pi/TurboPi/')
import time
import logging
import threading
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
from jsonrpc import JSONRPCResponseManager, dispatcher
import HiwonderSDK.mecanum as mecanum
import Functions.Running as Running
import Functions.lab_adjust as lab_adjust
import Functions.ColorDetect as ColorDetect_
import Functions.ColorTracking as ColorTracking_
import Functions.VisualPatrol as VisualPatrol_
import Functions.Avoidance as Avoidance_
import Functions.ColorWarning as ColorWarning_
import Functions.FaceTracking as FaceTracking_
import Functions.GestureRecognition as GestureRecognition_
import Functions.LineFollower as LineFollower_
import Functions.QuickMark as QuickMark_
if sys.version_info.major == 2:
print('Please run this program with python3!')
sys.exit(0)
__RPC_E01 = "E01 - Invalid number of parameter!"
__RPC_E02 = "E02 - Invalid parameter!"
__RPC_E03 = "E03 - Operation failed!"
__RPC_E04 = "E04 - Operation timeout!"
__RPC_E05 = "E05 - Not callable"
HWSONAR = None
QUEUE = None
board = None
car = mecanum.MecanumChassis()
def set_board():
Avoidance_.board = board
VisualPatrol_.board = board
ColorDetect_.board = board
ColorTracking_.board = board
ColorWarning_.board = board
FaceTracking_.board = board
GestureRecognition_.board = board
LineFollower_.board = board
QuickMark_.board = board
ColorDetect_.initMove()
board.set_buzzer(1900, 0.3, 0.7, 1)
@dispatcher.add_method
def map(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
@dispatcher.add_method
def SetPWMServo(*args, **kwargs):
ret = (True, (), 'SetPWMServo')
print("SetPWMServo:",args)
arglen = len(args)
try:
servos = args[2:arglen:2]
pulses = args[3:arglen:2]
use_times = args[0]
dat = zip(servos, pulses)
data = []
for (s, p) in dat:
pulses = int(map(p,90,-90,500,2500))
data.extend([[s, pulses]])
board.pwm_servo_set_position(use_times/1000.0, data)
except Exception as e:
print('error3:', e)
ret = (False, __RPC_E03, 'SetPWMServo')
return ret
@dispatcher.add_method
def SetMovementAngle(angle):
try:
if angle == -1:
car.set_velocity(0,90,0)
else:
car.set_velocity(70,angle,0)
except:
ret = (False, __RPC_E03, 'SetMovementAngle')
return ret
# 电机控制
@dispatcher.add_method
def SetBrushMotor(*args, **kwargs):
ret = (True, (), 'SetBrushMotor')
arglen = len(args)
# print(args)
if 0 != (arglen % 2):
return (False, __RPC_E01, 'SetBrushMotor')
try:
motors = args[0:arglen:2]
speeds = args[1:arglen:2]
for m in motors:
if m < 1 or m > 4:
return (False, __RPC_E02, 'SetBrushMotor')
data = []
dat = zip(motors, speeds)
for m, s in dat:
data.extend([[m, s]])
board.set_motor_duty(data)
except:
ret = (False, __RPC_E03, 'SetBrushMotor')
return ret
# 获取超声波测距
@dispatcher.add_method
def GetSonarDistance():
global HWSONAR
ret = (True, 0, 'GetSonarDistance')
try:
ret = (True, HWSONAR.getDistance(), 'GetSonarDistance')
except:
ret = (False, __RPC_E03, 'GetSonarDistance')
return ret
# 获取当前电池电压
@dispatcher.add_method
def GetBatteryVoltage():
ret = (True, 0, 'GetBatteryVoltage')
try:
ret = (True, board.get_battery(), 'GetBatteryVoltage')
except Exception as e:
print(e)
ret = (False, __RPC_E03, 'GetBatteryVoltage')
return ret
# 设置超声波rgb灯模式
@dispatcher.add_method
def SetSonarRGBMode(mode = 0):
global HWSONAR
HWSONAR.setRGBMode(mode)
return (True, (mode,), 'SetSonarRGBMode')
# 设置超声波rgb灯颜色
@dispatcher.add_method
def SetSonarRGB(index, r, g, b):
global HWSONAR
# print((r,g,b))
if index == 0:
HWSONAR.setPixelColor(0, (r, g, b))
HWSONAR.setPixelColor(1, (r, g, b))
else:
HWSONAR.setPixelColor(index, (r, g, b))
return (True, (r, g, b), 'SetSonarRGB')
# 设置超声波闪烁的颜色和周期
@dispatcher.add_method
def SetSonarRGBBreathCycle(index, color, cycle):
global HWSONAR
HWSONAR.setBreathCycle(index, color, cycle)
return (True, (index, color, cycle), 'SetSonarRGBBreathCycle')
# 设置超声波开始闪烁
@dispatcher.add_method
def SetSonarRGBStartSymphony():
global HWSONAR
HWSONAR.startSymphony()
return (True, (), 'SetSonarRGBStartSymphony')
# 设置避障速度
@dispatcher.add_method
def SetAvoidanceSpeed(speed=50):
# print(speed)
return runbymainth(Avoidance_.setSpeed, (speed,))
# 设置避障阈值
@dispatcher.add_method
def SetSonarDistanceThreshold(new_threshold=30):
# print(new_threshold)
return runbymainth(Avoidance_.setThreshold, (new_threshold,))
# 获取当前避障阈值
@dispatcher.add_method
def GetSonarDistanceThreshold():
return runbymainth(Avoidance_.getThreshold, ())
def runbymainth(req, pas):
if callable(req):
event = threading.Event()
ret = [event, pas, None]
QUEUE.put((req, ret))
count = 0
while ret[2] is None:
time.sleep(0.01)
count += 1
if count > 200:
break
if ret[2] is not None:
if ret[2][0]:
return ret[2]
else:
return (False, __RPC_E03 + " " + ret[2][1])
else:
return (False, __RPC_E04)
else:
return (False, __RPC_E05)
@dispatcher.add_method
def SetBusServoPulse(*args, **kwargs):
ret = (True, (), 'SetBusServoPulse')
arglen = len(args)
if (args[1] * 2 + 2) != arglen or arglen < 4:
return (False, __RPC_E01, 'SetBusServoPulse')
try:
servos = args[2:arglen:2]
pulses = args[3:arglen:2]
use_times = args[0]
for s in servos:
if s < 1 or s > 6:
return (False, __RPC_E02)
data = []
dat = zip(servos, pulses)
for (s, p) in dat:
data.extend([[s, p]])
board.bus_servo_set_position(use_times/1000.0, data)
except Exception as e:
print(e)
ret = (False, __RPC_E03, 'SetBusServoPulse')
return ret
@dispatcher.add_method
def SetBusServoDeviation(*args):
ret = (True, (), 'SetBusServoDeviation')
arglen = len(args)
if arglen != 2:
return (False, __RPC_E01, 'SetBusServoDeviation')
try:
servo = args[0]
deviation = args[1]
board.bus_servo_set_offset(servo, deviation)
except Exception as e:
print(e)
ret = (False, __RPC_E03, 'SetBusServoDeviation')
@dispatcher.add_method
def GetBusServosDeviation(args):
ret = (True, (), 'GetBusServosDeviation')
data = []
if args != "readDeviation":
return (False, __RPC_E01, 'GetBusServosDeviation')
try:
for i in range(1, 7):
dev = board.bus_servo_read_offset(i)
if dev is None:
dev = 999
data.append(dev)
ret = (True, data, 'GetBusServosDeviation')
except Exception as e:
print(e)
ret = (False, __RPC_E03, 'GetBusServosDeviation')
return ret
@dispatcher.add_method
def SaveBusServosDeviation(args):
ret = (True, (), 'SaveBusServosDeviation')
if args != "downloadDeviation":
return (False, __RPC_E01, 'SaveBusServosDeviation')
try:
for i in range(1, 7):
dev = board.bus_servo_save_offset(i)
except Exception as e:
print(e)
ret = (False, __RPC_E03, 'SaveBusServosDeviation')
return ret
@dispatcher.add_method
def UnloadBusServo(args):
ret = (True, (), 'UnloadBusServo')
if args != 'servoPowerDown':
return (False, __RPC_E01, 'UnloadBusServo')
try:
for i in range(1, 7):
board.bus_servo_enable_torque(i, 1)
except Exception as e:
print(e)
ret = (False, __RPC_E03, 'UnloadBusServo')
@dispatcher.add_method
def GetBusServosPulse(args):
ret = (True, (), 'GetBusServosPulse')
data = []
if args != 'angularReadback':
return (False, __RPC_E01, 'GetBusServosPulse')
try:
for i in range(1, 7):
pulse = board.bus_servo_read_position(i)
if pulse is None:
ret = (False, __RPC_E04, 'GetBusServosPulse')
return ret
else:
data.append(pulse)
ret = (True, data, 'GetBusServosPulse')
except Exception as e:
print(e)
ret = (False, __RPC_E03, 'GetBusServosPulse')
return ret
@dispatcher.add_method
def StopBusServo(args):
ret = (True, (), 'StopBusServo')
if args != 'stopAction':
return (False, __RPC_E01, 'StopBusServo')
try:
AGC.stop_action_group()
except Exception as e:
print(e)
ret = (False, __RPC_E03, 'StopBusServo')
@dispatcher.add_method
def GetSonarDistance():
global HWSONAR
ret = (True, 0, 'GetSonarDistance')
try:
ret = (True, HWSONAR.getDistance(), 'GetSonarDistance')
except:
ret = (False, __RPC_E03, 'GetSonarDistance')
return ret
def runbymainth(req, pas):
if callable(req):
event = threading.Event()
ret = [event, pas, None]
QUEUE.put((req, ret))
count = 0
while ret[2] is None:
time.sleep(0.01)
count += 1
if count > 200:
break
if ret[2] is not None:
if ret[2][0]:
return ret[2]
else:
return (False, __RPC_E03 + " " + ret[2][1])
else:
return (False, __RPC_E04)
else:
return (False, __RPC_E05)
@dispatcher.add_method
def LoadFunc(new_func = 0):
return runbymainth(Running.loadFunc, (new_func, ))
@dispatcher.add_method
def UnloadFunc():
return runbymainth(Running.unloadFunc, ())
@dispatcher.add_method
def StartFunc():
return runbymainth(Running.startFunc, ())
@dispatcher.add_method
def StopFunc():
return runbymainth(Running.stopFunc, ())
@dispatcher.add_method
def FinishFunc():
return runbymainth(Running.finishFunc, ())
@dispatcher.add_method
def Heartbeat():
return runbymainth(Running.doHeartbeat, ())
@dispatcher.add_method
def GetRunningFunc():
return runbymainth("GetRunningFunc", ())
return (True, (0,))
@dispatcher.add_method
def ColorTracking(*target_color):
# print('target_color',target_color)
return runbymainth(ColorTracking_.setTargetColor, target_color)
@dispatcher.add_method
def ColorTrackingWheel(state = 0):
# print('Wheel',state)
return runbymainth(ColorTracking_.setVehicleFollowing, state)
@dispatcher.add_method
def VisualPatrol(*target_color):
# print('target_color',target_color)
return runbymainth(VisualPatrol_.setTargetColor, target_color)
@dispatcher.add_method
def ColorDetect(*target_color):
# print('target_color',target_color)
return runbymainth(ColorDetect_.setTargetColor, target_color)
# 设置颜色阈值
# 参数:颜色lab
# 例如:[{'red': ((0, 0, 0), (255, 255, 255))}]
@dispatcher.add_method
def SetLABValue(*lab_value):
return runbymainth(lab_adjust.setLABValue, lab_value)
# 保存颜色阈值
@dispatcher.add_method
def GetLABValue():
return (True, lab_adjust.getLABValue()[1], 'GetLABValue')
# 保存颜色阈值
@dispatcher.add_method
def SaveLABValue(color=''):
return runbymainth(lab_adjust.saveLABValue, (color, ))
@dispatcher.add_method
def HaveLABAdjust():
return (True, True, 'HaveLABAdjust')
@Request.application
def application(request):
dispatcher["echo"] = lambda s: s
dispatcher["add"] = lambda a, b: a + b
response = JSONRPCResponseManager.handle(request.data, dispatcher)
return Response(response.json, mimetype='application/json')
def startRPCServer():
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
run_simple('', 9030, application)
if __name__ == '__main__':
startRPCServer()