-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidterm-beta.py
More file actions
153 lines (134 loc) · 4.13 KB
/
Midterm-beta.py
File metadata and controls
153 lines (134 loc) · 4.13 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
import socket
import threading
from SSL_Lib.Robot import Robot
from SSL_Lib.Camera import Camera
from SSL_Lib.DStar import DStar
from SSL_Lib.utils import *
from SSL_Lib.DBG import DBG
from SSL_Lib.DWA1 import *
import serial
import sys
import time
serialPort = "COM4" # 串口
# 初始化控制和读取的IP地址、端口号
localhost = '127.0.0.1'
control_addr = (localhost, 20011)
read_addr = (localhost, 23333)
camera = Camera(read_addr)
debug = DBG()
# ser=None
ser = serial.Serial(serialPort, 115200, timeout=0.5)
# ser=config_serial(serialPort)
# 主逻辑
# 1.初始化要控制的机器人
ro_b_0 = Robot('blue', 0, 0.15,ser=ser, control_addr=control_addr)
blue, yellow = camera.getRobotDict() # 读取初始信息
start_point = [blue[0].x, blue[0].y] # 设置机器人开始的位置
end_point = [-start_point[0], -start_point[1]] # 设置机器人终点为对称点
print('start at: ', start_point)
print('goal at: ', end_point)
# 2.目前只测试静态避障,所以只生成一次路径规划
ob_temp = []
for ro in blue.values():
if ro.robot_id is not 0:
ob_temp.append([ro.x, ro.y])
# debug.addCircle(ro.x/10,ro.y/10,20)
for ro in yellow.values():
ob_temp.append(([ro.x, ro.y]))
# debug.addCircle(ro.x/10,ro.y/10,20)
ob = np.array(ob_temp)
print('ob = ', ob)
u = np.array([0.0, 0.0])
config = Config()
# 2.1 新建地图
radius = 0.2
pf = statics_map(start_point, end_point, blue, yellow, radius) # 从Dstar获取路径信息
pf.shorter_the_path(2,10)
path=pf.get_path()
x = np.array([blue[0].x, blue[0].y, blue[0].orientation, 0.0, 0.0])
traj = np.array(x)
while path is None: # 如果障碍物膨胀太多,就逐渐减小
radius = radius - 0.01
if radius < 0.05:
print('No way out!')
break
print('Now trying radius = ', radius)
pf = statics_map(start_point, end_point, blue, yellow, radius)
path=pf.get_path()
goal = np.array([path[0].x , path[0].y])
#path=path[::10] #精简一下路径
print('get path!')
print('path start at: ', path[0])
print('path end at: ', path[-1])
print('length of path: ', len(path))
print('length of path(reduced): ', len(path))
debug.addPath(path,4) # 将路径画出来
debug.sendDebugMessage() # debug信息发送
i = 0
speed = 1
def updatePath():
radius = 4
global path
global blue, yellow
while True:
print('update path!')
pf = DStar(int(blue[0].x / 10), int(blue[0].y / 10), 900, 0)
pf.initialize_map(1200, 900)
for ro in blue.values():
if ro.robot_id is not 0:
rx = int(ro.x)
ry = int(ro.y)
if np.hypot(blue[0].x - rx, blue[0].y - ry) < 2000:
pf.set_obstract(int(ro.x / 10), int(ro.y / 10), radius, -1)
for ro in yellow.values():
rx = int(ro.x)
ry = int(ro.y)
print(np.hypot(blue[0].x - rx, blue[0].y - ry))
if np.hypot(blue[0].x - rx, blue[0].y - ry) < 2000:
pf.set_obstract(int(ro.x / 10), int(ro.y / 10), radius, -1)
pf.replan()
pf.shorter_the_path(2)
path = pf.get_path()
thread2 = threading.Thread(target=updatePath)
# 3. 新建一个进程
# 用来另开一个线程的函数
def getblue0():
global blue, yellow
while True:
# print('update robot info!')
# thread2.join()
blue, yellow = camera.getRobotDict()
thread1 = threading.Thread(target=getblue0)
thread1.start()
k = 1
# 4. 主循环
while True:
# 4.1 根据DWA计算所应该施加的控制指令
# u[0]是机器人x轴速度,u[1]是机器人y轴速度
u, ltraj = dwa_control(x, u, config, goal, ob, ro_b_0, camera)
print(u)
ro_b_0.setSpeed(u[1], u[0], 0)
x = np.array([blue[0].x, blue[0].y, blue[0].orientation, u[0], u[1]])
if math.sqrt((x[0] - goal[0]) ** 2 + (x[1] - goal[1]) ** 2) <= 1.0:
# print("Goal!!")
if i == len(path)-1:
if math.sqrt((x[0] - goal[0]) ** 2 + (x[1] - goal[1]) ** 2) <= config.robot_radius:
i = len(path) - 2
k = -1
else:
continue
if i == 0:
if math.sqrt((x[0] - goal[0]) ** 2 + (x[1] - goal[1]) ** 2) <= config.robot_radius:
i = 0
k = 1
else:
continue
i = i + k
goal = np.array([path[i].x , path[i].y ])
print(ltraj)
debug = DBG()
debug.addPath(path,4) # 将路径画出来
debug.addpath_dwa(ltraj)
debug.sendDebugMessage() # debug信息发送
# time.sleep(0.015)
# chase2(blue[0],[path_x,path_y],ro_b_0,1,1)