-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduino_connector.py
152 lines (127 loc) · 3.04 KB
/
arduino_connector.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
import serial
from config import serial_port
# init serial
ser = serial.Serial(serial_port, 57600)
ser.close()
speedtext = ""
def writeArduino(s):
"""
Talk to arduino and wait the echo.
Parameters
----------
s: str
The brief string send to arduino
Raises
------
ValueError
If the echo did not match the input, which
indicate the connectaion may fail.
"""
if not ser.is_open:
ser.open()
# print("Send: ", s)
ser.write(s.encode())
while True:
# out = ser.readline().decode().strip()
out = ser.read_until(';'.encode()).decode().strip()
# print(out)
if "[speed]" in out:
global speedtext
speedtext = out
if "[echo]" not in out:
continue
if out == "[echo] " + s:
break
else:
ser.close()
raise ValueError(out)
ser.close()
return True
def writeArduinoEasy(device, value=0):
""" A simple wrap function """
writeArduino(f"{device},{value};")
def setArduinoArm(device, value=0):
"""
Set Arduino Arm movement
Parameters
----------
device: Integer
The index of arm you want to move
value: Integer
The angle of arm you want to set
Raises
------
ValueError
See writeArduino
"""
writeArduino(f"a{device},{value};")
def setArduinoCar(mode, speed, distance=0):
"""
Set Arduino Arm movement
Parameters
----------
mode: Char
A single character to indicate mode
`F` for forward,
`B` for backward,
`R` for right,
`L` for left,
speed: Integer
The rotation speed(mm/s)
distance: Integer
The distance you want to go(mm)
Raises
------
ValueError
See writeArduino
"""
writeArduino(f"c{mode[0]}{speed},{distance};")
def setArduinoSpeed(direction, speed, wheel):
"""
Set Arduino Wheel speed
Parameters
----------
Direction: Char
A single character to indicate direction
`F` for forward,
`B` for backward,
`R` for right,
`L` for left,
speed: Integer
The rotation speed(mm/s)
wheel: char
A or B wheel you want to set
Raises
------
ValueError
See writeArduino
"""
writeArduino(f"w{direction[0]}{wheel[0]},{speed};")
def getArduinoSpeed():
"""
Get Arduino Wheel speed
Returns
------
Speed: (Integer, Interger)
The speed in mm of A and B motor
Raises
------
ValueError
See writeArduino
"""
writeArduino("s;")
global speedtext
print(speedtext)
if speedtext:
speedA, speedB = speedtext.split(']')[1].split(',')
return int(speedA), int(speedB)
speedtext = ""
if __name__ == "__main__":
# python3 arduino_connector.py a0 135
# setArduinoArm(2, 0)
# setArduinoCar('R', 200, 100)
# setArduinoSpeed('F', 100, 'A')
# print(getArduinoSpeed())
import sys
s = sys.argv[1:]
writeArduinoEasy(*s)