controller = PurePursuitControl() controller.set_path(path) while(True): print("\rState: "+car.state_str(), end="\t") # ================= Control Algorithm ================= # PID Longitude Control end_dist = np.hypot(path[-1,0]-car.x, path[-1,1]-car.y) target_v = 40 if end_dist > 265 else 0 next_a = 0.1*(target_v - car.v) # Pure Pursuit Lateral Control state = {"x":car.x, "y":car.y, "yaw":car.yaw, "v":car.v} next_delta, target = controller.feedback(state) car.control(next_a,next_delta) # ===================================================== # Update & Render car.update() img = img_path.copy() cv2.circle(img,(int(target[0]),int(target[1])),3,(1,0.3,0.7),2) # target points img = car.render(img) img = cv2.flip(img, 0) cv2.imshow("Pure-Pursuit Control Test", img) k = cv2.waitKey(1) if k == ord('r'): car.init_state(start) if k == 27: print() break
start = (50,300,0) car.init_state(start) controller = StanleyControl(kp=0.5) controller.set_path(path) while(True): print("\rState: "+car.state_str(), end="\t") # Longitude Control end_dist = np.hypot(path[-1,0]-car.x, path[-1,1]-car.y) next_v = 20 if end_dist > 10 else 0 # Stanley Lateral Control state = {"x":car.x, "y":car.y, "yaw":car.yaw, "v":car.v} next_w, target = controller.feedback(state) car.control(next_v, next_w) # Update State & Render car.update() img = img_path.copy() cv2.circle(img,(int(target[0]),int(target[1])),3,(1,0.3,0.7),2) # target points img = car.render(img) img = cv2.flip(img, 0) cv2.imshow("Stanley Control Test", img) k = cv2.waitKey(1) if k == ord('r'): car.init_state(start) if k == 27: print() break
class NavigationEnv: def __init__(self, path="Maps/map.png"): # Read Map self.img = cv2.flip(cv2.imread(path), 0) self.img[self.img > 128] = 255 self.img[self.img <= 128] = 0 self.m = np.asarray(self.img) self.m = cv2.cvtColor(self.m, cv2.COLOR_RGB2GRAY) self.m = self.m.astype(float) / 255. self.img = self.img.astype(float) / 255. self.lmodel = LidarModel(self.m) def initialize(self): # Set Mobile Car self.car = KinematicModel(d=12, wu=12, wv=4, car_w=18, car_f=14, car_r=10, dt=0.1) #self.car = KinematicModel() self.car.x, self.car.y = self._search_target() self.car.yaw = 360 * np.random.random() self.pos = (self.car.x, self.car.y, self.car.yaw) # Set Navigation Target self.target = self._search_target() self.target_dist = np.sqrt((self.car.x - self.target[0])**2 + (self.car.y - self.target[1])**2) target_orien = np.arctan2(self.target[1] - self.car.y, self.target[0] - self.car.x) - np.deg2rad(self.car.yaw) target_rel = [ self.target_dist * np.cos(target_orien), self.target_dist * np.sin(target_orien) ] # Initialize Measurement self.sdata, self.plist = self.lmodel.measure_2d(self.pos) state = self._construct_state(self.sdata, target_rel) return state def step(self, action): # Control and Update self.car.control((action[0] + 1) / 2 * self.car.v_range, action[1] * self.car.w_range) self.car.update() # Collision Handling p1, p2, p3, p4 = self.car.car_box l1 = Bresenham(p1[0], p2[0], p1[1], p2[1]) l2 = Bresenham(p1[0], p3[0], p1[1], p3[1]) l3 = Bresenham(p3[0], p4[0], p3[1], p4[1]) l4 = Bresenham(p4[0], p2[0], p4[1], p2[1]) check = l1 + l2 + l3 + l4 collision = False for pts in check: if self.m[int(pts[1]), int(pts[0])] < 0.5: collision = True self.car.redo() self.car.v = -0.5 * self.car.v break # Measure Lidar self.pos = (self.car.x, self.car.y, self.car.yaw) self.sdata, self.plist = self.lmodel.measure_2d(self.pos) # TODO(Lab-01): Reward Design # Distance Reward curr_target_dist = np.sqrt((self.car.x - self.target[0])**2 + (self.car.y - self.target[1])**2) reward_dist = self.target_dist - curr_target_dist # Orientation Reward orien = np.rad2deg( np.arctan2(self.target[1] - self.car.y, self.target[0] - self.car.x)) err_orien = (orien - self.car.yaw) % 360 if err_orien > 180: err_orien = 360 - err_orien reward_orien = np.deg2rad(err_orien) # Action Panelty reward_act = 0.05 if action[0] < -0.5 else 0 # Total reward = 0.6 * reward_dist - 0.3 * reward_orien - 0.1 * reward_act # Terminal State done = False if collision: reward = -1 done = True if curr_target_dist < 20: reward = 5 done = True # Relative Coordinate of Target self.target_dist = curr_target_dist target_orien = np.arctan2(self.target[1] - self.car.y, self.target[0] - self.car.x) - np.deg2rad(self.car.yaw) target_rel = [ self.target_dist * np.cos(target_orien), self.target_dist * np.sin(target_orien) ] state_next = self._construct_state(self.sdata, target_rel) return state_next, reward, done def render(self, gui=True): img_ = self.img.copy() for pts in self.plist: cv2.line(img_, (int(1 * self.pos[0]), int(1 * self.pos[1])), (int(1 * pts[0]), int(1 * pts[1])), (0.0, 1.0, 0.0), 1) cv2.circle(img_, (int(1 * self.target[0]), int(1 * self.target[1])), 10, (1.0, 0.5, 0.7), 3) img_ = self.car.render(img_) img_ = cv2.flip(img_, 0) if gui: cv2.imshow("Mapless Navigation", img_) k = cv2.waitKey(1) return img_.copy() def _search_target(self): im_h, im_w = self.m.shape[0], self.m.shape[1] tx = np.random.randint(0, im_w) ty = np.random.randint(0, im_h) kernel = np.ones((10, 10), np.uint8) m_dilate = 1 - cv2.dilate(1 - self.m, kernel, iterations=3) while (m_dilate[ty, tx] < 0.5): tx = np.random.randint(0, im_w) ty = np.random.randint(0, im_h) return tx, ty def _construct_state(self, sensor, target): state_s = [s / 200 for s in sensor] state_t = [t / 500 for t in target] return state_s + state_t
# Control # PID Longitude Control end_dist = np.hypot(path[-1,0]-car.x, path[-1,1]-car.y) # Control #state = {"x":car.x, "y":car.y, "yaw":car.yaw, "delta":car.delta, "v":car.v, "l":car.l, "dt":car.dt} state = {"x":car.x, "y":car.y, "yaw":car.yaw, "v":car.v, "dt":car.dt} #next_delta, target = controller.feedback((car.x,car.y,car.yaw),Kp=0.03, Ki=0.00005, Kd=0.08) #next_delta, target = controller.feedback((car.x,car.y,car.yaw),car.v,car.l,kp=0.7,Lfc=10) #next_delta, target = controller.feedback((car.x,car.y,car.yaw),car.v,kp=0.7,Lfc=10) next_delta, target = controller.feedback(state) cv2.circle(img_,(int(target[0]),int(target[1])),3,(1,0.3,0.7),2) #car.control(next_a, next_delta) next_v = 35 if end_dist > 5 else 0 car.control(next_v, next_delta) if collision_count > 0: target_v = -35 car.control(target_v,0) collision_count += 1 if collision_count > 10: way_points = rrt.planning((pos[0],pos[1]), nav_pos, 20) #way_points = astar.planning(pos_int((pos[0],pos[1])), pos_int(nav_pos),inter=20) path = np.array(cubic_spline_2d(way_points, interval=4)) controller.set_path(path) collision_count = 0 #img_ = car.render(img_) for i in range(len(pf.particle_list)): cv2.circle(img_,(int(pf.particle_list[i].pos[0]),int(pf.particle_list[i].pos[1])),2,(0,0,1),1)
car.init_state((50, 300, 0)) controller = PidControl() controller.set_path(path) while (True): print("\rState: " + car.state_str(), end="\t") # PID Longitude Control end_dist = np.hypot(path[-1, 0] - car.x, path[-1, 1] - car.y) target_v = 40 if end_dist > 262 else 0 next_a = 0.1 * (target_v - car.v) # PID Lateral Control state = {"x": car.x, "y": car.y, "yaw": car.yaw, "dt": car.dt} next_w, target = controller.feedback(state) car.control(next_a, next_w) car.update() # Update State & Render img = img_path.copy() cv2.circle(img, (int(target[0]), int(target[1])), 3, (0.7, 0.3, 1), 2) img = car.render(img) img = cv2.flip(img, 0) cv2.imshow("demo", img) k = cv2.waitKey(1) if k == ord('r'): init_state(car) if k == 27: print() break