-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathline_following.py
More file actions
452 lines (416 loc) · 21.2 KB
/
line_following.py
File metadata and controls
452 lines (416 loc) · 21.2 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
443
444
445
446
447
448
449
450
#!/usr/bin/env python3
# encoding: utf-8
# line following
import os
import cv2
import math
import rclpy
import queue
import threading
import numpy as np
import sdk.pid as pid
import sdk.common as common
try:
from scenario_pkg.roi_config import get_rois
except Exception:
def get_rois(camera_type):
roi_table = {
"ascamera": ((0.9, 0.95, 0, 1, 0.7), (0.8, 0.85, 0, 1, 0.2), (0.7, 0.75, 0, 1, 0.1)),
"aurora": ((0.81, 0.83, 0, 1, 0.7), (0.69, 0.71, 0, 1, 0.2), (0.57, 0.59, 0, 1, 0.1)),
"usb_cam": ((0.79, 0.81, 0, 1, 0.7), (0.67, 0.69, 0, 1, 0.2), (0.55, 0.57, 0, 1, 0.1)),
}
return roi_table.get(camera_type, roi_table["aurora"])
from rclpy.node import Node
from app.common import Heart
from cv_bridge import CvBridge
from app.common import ColorPicker
from geometry_msgs.msg import Twist
from std_srvs.srv import SetBool, Trigger
from sensor_msgs.msg import Image, LaserScan
from interfaces.srv import SetPoint, SetFloat64, SetString
from rclpy.qos import QoSProfile, QoSReliabilityPolicy
from ros_robot_controller_msgs.msg import MotorsState, SetPWMServoState, PWMServoState
from servo_controller_msgs.msg import ServosPosition
from servo_controller.bus_servo_control import set_servo_position
MAX_SCAN_ANGLE = 240 # degree(the scanning angle of lidar. The covered part is always eliminated)
def _get_camera_type(default: str = "aurora") -> str:
"""Fetch DEPTH_CAMERA_TYPE with a sane default so missing envs don't crash."""
camera_type = os.environ.get("DEPTH_CAMERA_TYPE") or os.environ.get("CAMERA_TYPE")
if not camera_type:
camera_type = default
os.environ["DEPTH_CAMERA_TYPE"] = camera_type
return camera_type
def _load_lab_config():
"""Resolve LAB config from env override or user home; return empty dict on failure."""
candidates = []
env_path = os.environ.get("LAB_CONFIG_PATH")
if env_path:
candidates.append(os.path.expanduser(env_path))
candidates.append(os.path.expanduser("~/software/lab_tool/lab_config.yaml"))
for path in candidates:
if os.path.exists(path):
try:
return common.get_yaml_data(path)
except Exception:
break
return {}
class LineFollower:
def __init__(self, color, node):
self.node = node
self.target_lab, self.target_rgb = color
self.depth_camera_type = _get_camera_type()
# Keep ROI selection in sync with scenario_runner via shared helper.
self.rois = get_rois(self.depth_camera_type)
self.weight_sum = sum(roi[-1] for roi in self.rois)
@staticmethod
def get_area_max_contour(contours, threshold=100):
'''
获取最大面积对应的轮廓(get the contour of the largest area)
:param contours:
:param threshold:
:return:
'''
contour_area = zip(contours, tuple(map(lambda c: math.fabs(cv2.contourArea(c)), contours)))
contour_area = tuple(filter(lambda c_a: c_a[1] > threshold, contour_area))
if len(contour_area) > 0:
max_c_a = max(contour_area, key=lambda c_a: c_a[1])
return max_c_a
return None
def __call__(self, image, result_image, threshold, color=None, use_color_picker=True):
centroid_sum = 0
h, w = image.shape[:2]
if self.depth_camera_type == 'ascamera':
w = w + 200
if use_color_picker:
min_color = [int(self.target_lab[0] - 50 * threshold * 2),
int(self.target_lab[1] - 50 * threshold),
int(self.target_lab[2] - 50 * threshold)]
max_color = [int(self.target_lab[0] + 50 * threshold * 2),
int(self.target_lab[1] + 50 * threshold),
int(self.target_lab[2] + 50 * threshold)]
target_color = self.target_lab, min_color, max_color
lowerb = tuple(target_color[1])
upperb = tuple(target_color[2])
else:
lowerb = tuple(color['min'])
upperb = tuple(color['max'])
for roi in self.rois:
blob = image[int(roi[0]*h):int(roi[1]*h), int(roi[2]*w):int(roi[3]*w)] # roi(intercept roi)
img_lab = cv2.cvtColor(blob, cv2.COLOR_RGB2LAB) # rgblab(convert rgb into lab)
img_blur = cv2.GaussianBlur(img_lab, (3, 3), 3) # (perform Gaussian filtering to reduce noise)
# mask = cv2.inRange(img_blur, tuple(target_color[1]), tuple(target_color[2])) # (image binarization)
mask = cv2.inRange(img_blur, lowerb, upperb) # (image binarization)
eroded = cv2.erode(mask, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))) # (corrode)
dilated = cv2.dilate(eroded, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))) # (dilate)
# cv2.imshow('section:{}:{}'.format(roi[0], roi[1]), cv2.cvtColor(dilated, cv2.COLOR_GRAY2BGR))
contours = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)[-2] # (find the contour)
max_contour_area = self.get_area_max_contour(contours, 30) # (get the contour corresponding to the largest contour)
if max_contour_area is not None:
rect = cv2.minAreaRect(max_contour_area[0]) # (minimum circumscribed rectangle)
box = np.intp(cv2.boxPoints(rect)) # (four corners)
for j in range(4):
box[j, 1] = box[j, 1] + int(roi[0]*h)
cv2.drawContours(result_image, [box], -1, (0, 255, 255), 2) # (draw the rectangle composed of four points)
# (acquire the diagonal points of the rectangle)
pt1_x, pt1_y = box[0, 0], box[0, 1]
pt3_x, pt3_y = box[2, 0], box[2, 1]
# (center point of the line)
line_center_x, line_center_y = (pt1_x + pt3_x) / 2, (pt1_y + pt3_y) / 2
cv2.circle(result_image, (int(line_center_x), int(line_center_y)), 5, (0, 0, 255), -1) # (draw the center point)
centroid_sum += line_center_x * roi[-1]
if centroid_sum == 0:
return result_image, None
center_pos = centroid_sum / self.weight_sum # (calculate the center point according to the ratio)
deflection_angle = -math.atan((center_pos - (w / 2.0)) / (h / 2.0)) # (calculate the line angle)
return result_image, deflection_angle
class LineFollowingNode(Node):
def __init__(self, name):
rclpy.init()
super().__init__(name, allow_undeclared_parameters=True, automatically_declare_parameters_from_overrides=True)
self.name = name
self.color = ''
# self.camera_type = 'Stereo'
self.set_callback = False
self.is_running = False
self.color_picker = None
self.follower = None
self.scan_angle = math.radians(45)
self.scan_angle = math.radians(45)
self.pid = pid.PID(0.030, 0.003, 0.0) # Edit this line to change PID values original values (0.005, 0.001, 0.)
self.empty = 0
self.count = 0
self.stop = False
self.threshold = 0.5
self.stop_threshold = 0.4
self.lock = threading.RLock()
self.image_sub = None
self.lidar_sub = None
self.image_height = None
self.image_width = None
self.bridge = CvBridge()
self.use_color_picker = True
self.lab_data = _load_lab_config()
self.image_queue = queue.Queue(2)
self.camera_type = _get_camera_type()
self.lidar_type = os.environ.get('LIDAR_TYPE')
self.machine_type = os.environ.get('MACHINE_TYPE')
self.pwm_pub = self.create_publisher(SetPWMServoState,'ros_robot_controller/pwm_servo/set_state',10)
self.mecanum_pub = self.create_publisher(Twist, '/controller/cmd_vel', 1) # (chassis control)
self.result_publisher = self.create_publisher(Image, '~/image_result', 1) # (publish the image processing result)
self.create_service(Trigger, '~/enter', self.enter_srv_callback) # (enter the game)
self.create_service(Trigger, '~/exit', self.exit_srv_callback) # (exit the game)
self.create_service(SetBool, '~/set_running', self.set_running_srv_callback) # (start the game)
self.create_service(SetString, '~/set_color', self.set_color_srv_callback)
self.create_service(SetPoint, '~/set_target_color', self.set_target_color_srv_callback) # (set the color)
self.create_service(Trigger, '~/get_target_color', self.get_target_color_srv_callback) # (get the color)
self.create_service(SetFloat64, '~/set_threshold', self.set_threshold_srv_callback) # (set the threshold)
self.joints_pub = self.create_publisher(ServosPosition, 'servo_controller', 1)
Heart(self, self.name + '/heartbeat', 5, lambda _: self.exit_srv_callback(request=Trigger.Request(), response=Trigger.Response())) # (heartbeat package)
# Default debug to True so the UI window opens when not set via parameters
self.declare_parameter('debug', True)
self.debug = self.get_parameter('debug').value
if self.debug:
threading.Thread(target=self.main, daemon=True).start()
self.create_service(Trigger, '~/init_finish', self.get_node_state)
self.get_logger().info('\033[1;32m%s\033[0m' % 'start')
def pwm_controller(self,position_data):
pwm_list = []
msg = SetPWMServoState()
msg.duration = 0.2
for i in range(len(position_data)):
pos = PWMServoState()
pos.id = [i+1]
pos.position = [int(position_data[i])]
pwm_list.append(pos)
msg.state = pwm_list
self.pwm_pub.publish(msg)
def get_node_state(self, request, response):
response.success = True
return response
def main(self):
while True:
try:
image = self.image_queue.get(block=True, timeout=1)
except queue.Empty:
continue
result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow("result", result)
if self.debug and not self.set_callback:
self.set_callback = True
# (set a callback function for mouse click event)
cv2.setMouseCallback("result", self.mouse_callback)
k = cv2.waitKey(1)
if k != -1:
break
self.mecanum_pub.publish(Twist())
rclpy.shutdown()
def mouse_callback(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
self.get_logger().info("x:{} y{}".format(x, y))
msg = SetPoint.Request()
if self.image_height is not None and self.image_width is not None:
msg.data.x = x / self.image_width
msg.data.y = y / self.image_height
self.set_target_color_srv_callback(msg, SetPoint.Response())
def enter_srv_callback(self, request, response):
self.get_logger().info('\033[1;32m%s\033[0m' % "line following enter")
if self.camera_type != 'ascamera':
a self.pwm_controller([1850,1500]) ## Pan / tilt originally [1850, 1500]
with self.lock:
self.stop = False
self.is_running = False
self.color_picker = None
self.pid = pid.PID(1.1, 0.0, 0.0)
self.follower = None
self.threshold = 0.5
#self.camera_type = os.environ['DEPTH_CAMERA_TYPE']
self.empty = 0
if self.image_sub is None:
self.image_sub = self.create_subscription(Image, '/camera/image_raw', self.image_callback, 1) # 摄像头订阅(subscribe to the camera)
if self.lidar_sub is None:
qos = QoSProfile(depth=1, reliability=QoSReliabilityPolicy.BEST_EFFORT)
self.lidar_sub = self.create_subscription(LaserScan, '/scan_raw', self.lidar_callback, qos) # (subscribe to Lidar data)
set_servo_position(self.joints_pub, 1, ((10, 200), (5, 500), (4, 90), (3, 150), (2, 645), (1, 500))) # Use this to edit the arm position, parameters start from the gripper and end at the arm base
self.mecanum_pub.publish(Twist())
response.success = True
response.message = "enter"
return response
def exit_srv_callback(self, request, response):
self.get_logger().info('\033[1;32m%s\033[0m' % "line following exit")
try:
if self.image_sub is not None:
self.destroy_subscription(self.image_sub)
self.image_sub = None
if self.lidar_sub is not None:
self.destroy_subscription(self.lidar_sub)
self.lidar_sub = None
except Exception as e:
self.get_logger().error(str(e))
with self.lock:
self.is_running = False
self.color_picker = None
self.pid = pid.PID(0.00, 0.001, 0.0)
self.follower = None
self.threshold = 0.5
self.mecanum_pub.publish(Twist())
response.success = True
response.message = "exit"
return response
def set_target_color_srv_callback(self, request, response):
self.get_logger().info('\033[1;32m%s\033[0m' % "set_target_color")
with self.lock:
self.use_color_picker = True
x, y = request.data.x, request.data.y
self.follower = None
if x == -1 and y == -1:
self.color_picker = None
else:
self.color_picker = ColorPicker(request.data, 5)
self.mecanum_pub.publish(Twist())
response.success = True
response.message = "set_target_color"
return response
def get_target_color_srv_callback(self, request, response):
self.get_logger().info('\033[1;32m%s\033[0m' % "get_target_color")
response.success = False
response.message = "get_target_color"
with self.lock:
if self.follower is not None:
response.success = True
rgb = self.follower.target_rgb
response.message = "{},{},{}".format(int(rgb[0]), int(rgb[1]), int(rgb[2]))
return response
def set_running_srv_callback(self, request, response):
self.get_logger().info('\033[1;32m%s\033[0m' % "set_running")
with self.lock:
self.is_running = request.data
self.empty = 0
if not self.is_running:
self.mecanum_pub.publish(Twist())
response.success = True
response.message = "set_running"
return response
def set_threshold_srv_callback(self, request, response):
self.get_logger().info('\033[1;32m%s\033[0m' % "set threshold")
with self.lock:
self.threshold = request.data
response.success = True
response.message = "set_threshold"
return response
def set_color_srv_callback(self, request, response):
self.get_logger().info('\033[1;32m%s\033[0m' % 'set_color')
with self.lock:
self.color = request.data
self.use_color_picker = False
response.success = True
response.message = "set_color"
return response
def lidar_callback(self, lidar_data):
# (data size= scanning angle/ the increased angle per scan)
if self.lidar_type != 'G4':
min_index = int(math.radians(MAX_SCAN_ANGLE / 2.0) / lidar_data.angle_increment)
max_index = int(math.radians(MAX_SCAN_ANGLE / 2.0) / lidar_data.angle_increment)
left_ranges = lidar_data.ranges[:max_index] # (left data)
right_ranges = lidar_data.ranges[::-1][:max_index] # (right data)
elif self.lidar_type == 'G4':
min_index = int(math.radians((360 - MAX_SCAN_ANGLE) / 2.0) / lidar_data.angle_increment)
max_index = int(math.radians(180) / lidar_data.angle_increment)
left_ranges = lidar_data.ranges[min_index:max_index][::-1] # (the left data)
right_ranges = lidar_data.ranges[::-1][min_index:max_index][::-1] # (the right data)
# (Get data according to settings)
angle = self.scan_angle / 2
angle_index = int(angle / lidar_data.angle_increment + 0.50)
left_range, right_range = np.array(left_ranges[:angle_index]), np.array(right_ranges[:angle_index])
left_nonzero = left_range.nonzero()
right_nonzero = right_range.nonzero()
left_nonan = np.isfinite(left_range[left_nonzero])
right_nonan = np.isfinite(right_range[right_nonzero])
# (Take the nearest distance left and right)
min_dist_left_ = left_range[left_nonzero][left_nonan]
min_dist_right_ = right_range[right_nonzero][right_nonan]
if len(min_dist_left_) > 1 and len(min_dist_right_) > 1:
min_dist_left = min_dist_left_.min()
min_dist_right = min_dist_right_.min()
if min_dist_left < self.stop_threshold or min_dist_right < self.stop_threshold:
self.stop = True
else:
self.count += 1
if self.count > 5:
self.count = 0
self.stop = False
def image_callback(self, ros_image):
cv_image = self.bridge.imgmsg_to_cv2(ros_image, "rgb8")
rgb_image = np.array(cv_image, dtype=np.uint8)
self.image_height, self.image_width = rgb_image.shape[:2]
result_image = np.copy(rgb_image) # (the image used to display the result)
with self.lock:
if self.use_color_picker:
# (color picker and line recognition are exclusive. If there is color picker, start picking)
if self.color_picker is not None: # (color picker exists)
try:
target_color, result_image = self.color_picker(rgb_image, result_image)
if target_color is not None:
self.color_picker = None
self.follower = LineFollower(target_color, self)
self.get_logger().info("target color: {}".format(target_color))
except Exception as e:
self.get_logger().error(str(e))
else:
twist = Twist()
twist.linear.x = 0.05 # Speed control variable
if self.follower is not None:
try:
result_image, deflection_angle = self.follower(rgb_image, result_image, self.threshold)
if deflection_angle is not None and self.is_running and not self.stop:
self.pid.update(deflection_angle)
if 'Acker' in self.machine_type:
steering_angle = common.set_range(-self.pid.output, -math.radians(40), math.radians(40))
if steering_angle != 0:
R = 0.145/math.tan(steering_angle)
twist.angular.z = twist.linear.x/R
else:
twist.angular.z = common.set_range(-self.pid.output, -1.0, 1.0)
self.mecanum_pub.publish(twist)
elif self.stop:
self.mecanum_pub.publish(Twist())
else:
self.pid.clear()
except Exception as e:
self.get_logger().error(str(e))
else:
twist = Twist()
if self.color in common.range_rgb:
twist.linear.x = 0.05 # Speed control variable
self.follower = LineFollower([None, common.range_rgb[self.color]], self)
result_image, deflection_angle = self.follower(rgb_image, result_image, self.threshold, self.lab_data['lab'][self.camera_type][self.color], False)
if deflection_angle is not None and self.is_running and not self.stop:
self.pid.update(deflection_angle)
if 'Acker' in self.machine_type:
steering_angle = common.set_range(-self.pid.output, -math.radians(40), math.radians(40))
if steering_angle != 0:
R = 0.145/math.tan(steering_angle)
twist.angular.z = twist.linear.x/R
else:
twist.angular.z = common.set_range(-self.pid.output, -1.0, 1.0)
self.mecanum_pub.publish(twist)
elif self.stop:
self.mecanum_pub.publish(Twist())
else:
self.pid.clear()
else:
self.mecanum_pub.publish(twist)
if self.debug:
if self.image_queue.full():
# (if the queue is full, remove the oldest image)
self.image_queue.get()
# (put the image into the queue)
self.image_queue.put(result_image)
else:
self.result_publisher.publish(self.bridge.cv2_to_imgmsg(result_image, "rgb8"))
def main():
node = LineFollowingNode('line_following')
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == "__main__":
main()