-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.py
95 lines (75 loc) · 2.14 KB
/
lib.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
import bluetooth
action_list = {
'forward':'Move forward',
'back':'Move back',
'left':'Move Leftward',
'right':'Move Rightward',
'rpunch':'Right punch',
'lpunch':'Left punch',
'tleft':'Turn Left',
'tright':'Turn Right',
'default':'',
'getup':'get up',
}
def discover():
print("searching ...")
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("found %d devices" % len(nearby_devices))
print(nearby_devices)
for addr, name in nearby_devices:
if name == "Alpha1_E896":
return addr
def get_address():
bd_addr = None
counter = 1
while (bd_addr is None):
print('attempt',counter)
counter+=1
bd_addr = discover()
port = 6
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
return bd_addr
def create_message(command,parameters):
header = b'\xFB\xBF'
end = b'\xED'
parameter = b''.join(parameters)
length = bytes([len(parameters)+5])
data = [command, length]
data.extend(parameters)
print(data)
# total = [sum((ord(x)) for x in data)]
total = 0
for x in data:
total += ord(x)
total %= 256
print (total)
check = bytes([total])
return header+length+command+parameter+check+end
def single_servo_command(servo_id,angle,runtime,frame_interval=0):
if servo_id < 1 or servo_id>16:
print("the servo id doesn't exist")
return None
param = [servo_id,angle,runtime,frame_interval]
param = [(x).to_bytes(1, byteorder='big') for x in param]
return param
def send_single_servo(data,sock):
msg = create_message(b'\x22', data)
sock.send(msg)
rres = sock.recv(1024)
return rres
def send_multiple_servo(data,sock):
msg = create_message(b'\x23', data)
sock.send(msg)
rres = sock.recv(1024)
return rres
def send_action(action,sock):
data = action_list.get(action)
print(data)
data = bytes(data,'ascii')
data = [(x).to_bytes(1,'big') for x in data]
print(data)
msg = create_message(b'\x03',data)
print(msg)
sock.send(msg)
rres = sock.recv(1024)
return rres