-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathing.py
151 lines (131 loc) · 5.11 KB
/
pathing.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
#!/usr/bin/env python3
import time
import numpy as np
import cv2
import dwa
class Demo(object):
def __init__(self):
#1 px = 0.1 m
# That's why everything is multiplied or divided by 10.
cv2.namedWindow('cvwindow')
cv2.setMouseCallback('cvwindow', self.callback)
self.drawing = False
self.point_cloud = []
self.draw_points = []
# Planner Settings
self.vel = (0.0, 0.0)
self.pose = (30.0, 30.0, 0)
self.goal = None
self.goal = [1.0, 3.0]
self.base = [-3.0, -2.5, +3.0, +2.5]
self.base = [-3.0, 0.0, 0.0, 0.0]
self.config = dwa.Config(
max_speed = 5.0,
min_speed = -1.0,
max_yawrate = np.radians(40.0),
max_accel = 15.0,
max_dyawrate = np.radians(110.0),
velocity_resolution = 0.1,
yawrate_resolution = np.radians(1.0),
dt = 0.1,
predict_time = 3.0,
heading = 0.15,
clearance = 1.0,
velocity = 2.0,
base = self.base
)
def callback(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
self.drawing = True
elif event == cv2.EVENT_MOUSEMOVE:
if self.drawing:
if [x, y] not in self.draw_points:
self.draw_points.append([x, y])
self.point_cloud.append([x/10, y/10])
self.goal = None
#com1.append([x/10,y/10])
else:
self.goal = (x/10, y/10) #here is where the goal is defined to create the pathing
com1.append([x,y])
com1.remove(com1[0])
elif event == cv2.EVENT_LBUTTONUP:
self.drawing = False
#com1 = [x/10,y/10]
#print(self.goal[0]*10,self.goal[1]*10)
def main(self):
import argparse
#self.draw_points = []
#self.goal = []
parser = argparse.ArgumentParser(description='DWA Demo')
parser.add_argument('--save', dest='save', action='store_true')
parser.set_defaults(save=False)
args = parser.parse_args()
if args.save:
import imageio
writer = imageio.get_writer('./dwa.gif', mode='I', duration=0.05)
cond = True
while cond:
prev_time = time.time()
self.map = np.zeros((600, 600, 3), dtype=np.uint8)
for point in self.draw_points:
cv2.circle(self.map, tuple(point), 4, (255, 255, 255), -1)
if self.goal is not None:
cv2.circle(self.map, (int(self.goal[0]*10), int(self.goal[1]*10)),
4, (0, 255, 0), -1)
if len(self.point_cloud):
# Planning
self.vel = dwa.planning(self.pose, self.vel, self.goal,
np.array(self.point_cloud, np.float32), self.config)
# print(self.pose)
# print("<-->")
# print(self.vel)
# Simulate motion
self.pose = dwa.motion(self.pose, self.vel, self.config.dt)
pose = np.ndarray((3,))
pose[0:2] = np.array(self.pose[0:2]) * 10
pose[2] = self.pose[2]
#print('pose is: ',self.pose)
base = np.array(self.base) * 10
base[0:2] += pose[0:2]
base[2:4] += pose[0:2]
# Not the correct rectangle but good enough for the demo
width = base[2] - base[0]
height = base[3] - base[1]
rect = ((pose[0], pose[1]), (width, height), np.degrees(pose[2]))
#print(rect)
box = cv2.boxPoints(rect)
box = np.int0(box)
#print('coordinates: ',box)
cv2.drawContours(self.map,[box],0,(0,0,255),-1)
fps = int(1.0 / (time.time() - prev_time))
cv2.putText(self.map, f'FPS: {fps}', (20, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.putText(self.map, f'Point Cloud Size: {len(self.point_cloud)}',
(20, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
if args.save:
writer.append_data(self.map)
cv2.imshow('cvwindow', self.map)
key = cv2.waitKey(1)
if key == 27:
break
elif key == ord('r'):
self.point_cloud = []
self.draw_points = []
#print(rect[0])
trail.append(rect[0])
compare = [rect[0][0],rect[0][1]]
c1 = com1[0]
'''print(c1)'''
if c1 == compare or (c1[0] in range((int(rect[0][0])-80),int((rect[0][0])+80))) and (c1[1] in range((int(rect[0][1])-80),(int(rect[0][1])+80))):
cond = False
if args.save:
writer.close()
#def make_trail(a):
if __name__ == '__main__':
global trail
trail = []
global com1
com1 =[[0,0]]
Demo().__init__()
Demo().main()
print(trail)