-
Notifications
You must be signed in to change notification settings - Fork 12
/
base_ctrl.py
286 lines (228 loc) · 7.59 KB
/
base_ctrl.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
import serial
import json
import queue
import threading
import yaml
import os
import time
import glob
import numpy as np
curpath = os.path.realpath(__file__)
thisPath = os.path.dirname(curpath)
with open(thisPath + '/config.yaml', 'r') as yaml_file:
f = yaml.safe_load(yaml_file)
class ReadLine:
def __init__(self, s):
self.buf = bytearray()
self.s = s
self.sensor_data = []
self.sensor_list = []
try:
self.sensor_data_ser = serial.Serial(glob.glob('/dev/ttyUSB*')[0], 115200)
print("/dev/ttyUSB* connected succeed")
except:
self.sensor_data_ser = None
self.sensor_data_max_len = 51
try:
self.lidar_ser = serial.Serial(glob.glob('/dev/ttyACM*')[0], 230400, timeout=1)
print("/dev/ttyACM* connected succeed")
except:
self.lidar_ser = None
self.ANGLE_PER_FRAME = 12
self.HEADER = 0x54
self.lidar_angles = []
self.lidar_distances = []
self.lidar_angles_show = []
self.lidar_distances_show = []
self.last_start_angle = 0
def readline(self):
i = self.buf.find(b"\n")
if i >= 0:
r = self.buf[:i+1]
self.buf = self.buf[i+1:]
return r
while True:
i = max(1, min(512, self.s.in_waiting))
data = self.s.read(i)
i = data.find(b"\n")
if i >= 0:
r = self.buf + data[:i+1]
self.buf[0:] = data[i+1:]
return r
else:
self.buf.extend(data)
def clear_buffer(self):
self.s.reset_input_buffer()
def read_sensor_data(self):
if self.sensor_data_ser == None:
return
try:
buffer_clear = False
while self.sensor_data_ser.in_waiting > 0:
buffer_clear = True
sensor_readline = self.sensor_data_ser.readline()
if len(sensor_readline) <= self.sensor_data_max_len:
self.sensor_list.append(sensor_readline.decode('utf-8')[:-2])
else:
self.sensor_list.append(sensor_readline.decode('utf-8')[:self.sensor_data_max_len])
self.sensor_list.append(sensor_readline.decode('utf-8')[self.sensor_data_max_len:-2])
if buffer_clear:
self.sensor_data = self.sensor_list.copy()
self.sensor_list.clear()
self.sensor_data_ser.reset_input_buffer()
except Exception as e:
print(f"[base_ctrl.read_sensor_data] error: {e}")
def parse_lidar_frame(self, data):
# header = data[0]
# verlen = data[1]
# speed = data[3] << 8 | data[2]
start_angle = (data[5] << 8 | data[4]) * 0.01
# print(start)
# end_angle = (data[43] << 8 | data[42]) * 0.01
for i in range(0, self.ANGLE_PER_FRAME):
offset = 6 + i * 3
distance = data[offset+1] << 8 | data[offset]
confidence = data[offset+2]
# lidar_angles.append(np.radians(start_angle + i * 0.167))
self.lidar_angles.append(np.radians(start_angle + i * 0.83333 + 180))
# lidar_angles.append(np.radians(start_angle + end_angle))
self.lidar_distances.append(distance)
# end_angle = (data[43] << 8 | data[42]) * 0.01
# timestamp = data[45] << 8 | data[44]
# crc = data[46]
return start_angle
def lidar_data_recv(self):
if self.lidar_ser == None:
return
try:
while True:
self.header = self.lidar_ser.read(1)
if self.header == b'\x54':
# Read the rest of the data
data = self.header + self.lidar_ser.read(46)
hex_data = [int(hex(byte), 16) for byte in data]
start_angle = self.parse_lidar_frame(hex_data)
if self.last_start_angle > start_angle:
break
self.last_start_angle = start_angle
else:
self.lidar_ser.flushInput()
self.last_start_angle = start_angle
self.lidar_angles_show = self.lidar_angles.copy()
self.lidar_distances_show = self.lidar_distances.copy()
self.lidar_angles.clear()
self.lidar_distances.clear()
except Exception as e:
print(f"[base_ctrl.lidar_data_recv] error: {e}")
self.lidar_ser = serial.Serial(glob.glob('/dev/ttyACM*')[0], 230400, timeout=1)
class BaseController:
def __init__(self, uart_dev_set, buad_set):
self.ser = serial.Serial(uart_dev_set, buad_set, timeout=1)
self.rl = ReadLine(self.ser)
self.command_queue = queue.Queue()
self.command_thread = threading.Thread(target=self.process_commands, daemon=True)
self.command_thread.start()
self.base_light_status = 0
self.head_light_status = 0
self.data_buffer = None
self.base_data = None
self.use_lidar = f['base_config']['use_lidar']
self.extra_sensor = f['base_config']['extra_sensor']
def feedback_data(self):
try:
while self.rl.s.in_waiting > 0:
self.data_buffer = json.loads(self.rl.readline().decode('utf-8'))
if 'T' in self.data_buffer:
self.base_data = self.data_buffer
self.data_buffer = None
if self.base_data["T"] == 1003:
print(self.base_data)
return self.base_data
self.rl.clear_buffer()
self.data_buffer = json.loads(self.rl.readline().decode('utf-8'))
self.base_data = self.data_buffer
return self.base_data
except Exception as e:
self.rl.clear_buffer()
print(f"[base_ctrl.feedback_data] error: {e}")
def on_data_received(self):
self.ser.reset_input_buffer()
data_read = json.loads(self.rl.readline().decode('utf-8'))
return data_read
def send_command(self, data):
self.command_queue.put(data)
def process_commands(self):
while True:
data = self.command_queue.get()
self.ser.write((json.dumps(data) + '\n').encode("utf-8"))
def base_json_ctrl(self, input_json):
self.send_command(input_json)
def gimbal_emergency_stop(self):
data = {"T":0}
self.send_command(data)
def base_speed_ctrl(self, input_left, input_right):
data = {"T":1,"L":input_left,"R":input_right}
self.send_command(data)
def gimbal_ctrl(self, input_x, input_y, input_speed, input_acceleration):
data = {"T":133,"X":input_x,"Y":input_y,"SPD":input_speed,"ACC":input_acceleration}
self.send_command(data)
def gimbal_base_ctrl(self, input_x, input_y, input_speed):
data = {"T":141,"X":input_x,"Y":input_y,"SPD":input_speed}
self.send_command(data)
def base_oled(self, input_line, input_text):
data = {"T":3,"lineNum":input_line,"Text":input_text}
self.send_command(data)
def base_default_oled(self):
data = {"T":-3}
self.send_command(data)
def bus_servo_id_set(self, old_id, new_id):
# data = {"T":54,"old":old_id,"new":new_id}
data = {"T":f['cmd_config']['cmd_set_servo_id'],"raw":old_id,"new":new_id}
self.send_command(data)
def bus_servo_torque_lock(self, input_id, input_status):
# data = {"T":55,"id":input_id,"status":input_status}
data = {"T":f['cmd_config']['cmd_servo_torque'],"id":input_id,"cmd":input_status}
self.send_command(data)
def bus_servo_mid_set(self, input_id):
# data = {"T":58,"id":input_id}
data = {"T":f['cmd_config']['cmd_set_servo_mid'],"id":input_id}
self.send_command(data)
def lights_ctrl(self, pwmA, pwmB):
data = {"T":132,"IO4":pwmA,"IO5":pwmB}
self.send_command(data)
self.base_light_status = pwmA
self.head_light_status = pwmB
def base_lights_ctrl(self):
if self.base_light_status != 0:
self.base_light_status = 0
else:
self.base_light_status = 255
self.lights_ctrl(self.base_light_status, self.head_light_status)
def gimbal_dev_close(self):
self.ser.close()
def breath_light(self, input_time):
breath_start_time = time.time()
while time.time() - breath_start_time < input_time:
for i in range(0, 128, 10):
self.lights_ctrl(i, 128-i)
time.sleep(0.1)
for i in range(0, 128, 10):
self.lights_ctrl(128-i, i)
time.sleep(0.1)
self.lights_ctrl(0, 0)
if __name__ == '__main__':
# RPi5
base = BaseController('/dev/ttyAMA0', 115200)
# RPi4B
# base = BaseController('/dev/serial0', 115200)
# breath light for 15s
base.breath_light(15)
# gimble ctrl, look forward
# x y spd acc
base.gimbal_ctrl(0, 0, 10, 0)
# x(-180 ~ 180)
# x- look left
# x+ look right
# y(-30 ~ 90)
# y- look down
# y+ look up