def closed_loop_prediction(cx, cy, cyaw, speed_profile, goal): state = unicycle_model.State(x=-0.0, y=-0.0, yaw=0.0, v=0.0) # lastIndex = len(cx) - 1 time = 0.0 x = [state.x] y = [state.y] yaw = [state.yaw] v = [state.v] t = [0.0] a = [0.0] d = [0.0] target_ind, mindis = calc_target_index(state, cx, cy) find_goal = False maxdis = 0.5 while T >= time: di, target_ind, dis = pure_pursuit_control(state, cx, cy, target_ind) target_speed = speed_profile[target_ind] target_speed = target_speed * \ (maxdis - min(dis, maxdis - 0.1)) / maxdis ai = PIDControl(target_speed, state.v) state = unicycle_model.update(state, ai, di) if abs(state.v) <= stop_speed and target_ind <= len(cx) - 2: target_ind += 1 time = time + unicycle_model.dt # check goal dx = state.x - goal[0] dy = state.y - goal[1] if math.sqrt(dx**2 + dy**2) <= goal_dis: find_goal = True break x.append(state.x) y.append(state.y) yaw.append(state.yaw) v.append(state.v) t.append(time) a.append(ai) d.append(di) if target_ind % 1 == 0 and animation: plt.cla() plt.plot(cx, cy, "-r", label="course") plt.plot(x, y, "ob", label="trajectory") plt.plot(cx[target_ind], cy[target_ind], "xg", label="target") plt.axis("equal") plt.grid(True) plt.title("speed:" + str(round(state.v, 2)) + "tind:" + str(target_ind)) plt.pause(0.0001) else: print("Time out!!") return t, x, y, yaw, v, a, d, find_goal
def main(): # target course import numpy as np cx = np.arange(0, 50, 0.1) cy = [math.sin(ix / 5.0) * ix / 2.0 for ix in cx] target_speed = 5.0 / 3.6 T = 15.0 # max simulation time state = unicycle_model.State(x=-0.0, y=-3.0, yaw=0.0, v=0.0) # state = unicycle_model.State(x=-1.0, y=-5.0, yaw=0.0, v=-30.0 / 3.6) # state = unicycle_model.State(x=10.0, y=5.0, yaw=0.0, v=-30.0 / 3.6) # state = unicycle_model.State( # x=3.0, y=5.0, yaw=math.radians(-40.0), v=-10.0 / 3.6) # state = unicycle_model.State( # x=3.0, y=5.0, yaw=math.radians(40.0), v=50.0 / 3.6) lastIndex = len(cx) - 1 time = 0.0 x = [state.x] y = [state.y] yaw = [state.yaw] v = [state.v] t = [0.0] target_index = calc_target_index(state, cx, cy) while T >= time and lastIndex > target_index: ai = PIDControl(target_speed, state.v) di, target_index = pure_pursuit_control(state, cx, cy, target_index) state = unicycle_model.update(state, ai, di) time = time + unicycle_model.dt x.append(state.x) y.append(state.y) yaw.append(state.yaw) v.append(state.v) t.append(time) # plt.cla() # plt.plot(cx, cy, ".r", label="course") # plt.plot(x, y, "-b", label="trajectory") # plt.plot(cx[target_index], cy[target_index], "xg", label="target") # plt.axis("equal") # plt.grid(True) # plt.pause(0.1) # input() flg, ax = plt.subplots(1) plt.plot(cx, cy, ".r", label="course") plt.plot(x, y, "-b", label="trajectory") plt.legend() plt.xlabel("x[m]") plt.ylabel("y[m]") plt.axis("equal") plt.grid(True) flg, ax = plt.subplots(1) plt.plot(t, [iv * 3.6 for iv in v], "-r") plt.xlabel("Time[s]") plt.ylabel("Speed[km/h]") plt.grid(True) plt.show()
def closed_loop_prediction(cx, cy, cyaw, speed_profile, goal): state = unicycle_model.State(x=-0.0, y=-0.0, yaw=0.0, v=0.0) # lastIndex = len(cx) - 1 time = 0.0 x = [state.x] y = [state.y] yaw = [state.yaw] v = [state.v] t = [0.0] a = [0.0] d = [0.0] target_ind, mindis = calc_target_index(state, cx, cy) find_goal = False maxdis = 0.5 while T >= time: di, target_ind, dis = pure_pursuit_control(state, cx, cy, target_ind) target_speed = speed_profile[target_ind] target_speed = target_speed * \ (maxdis - min(dis, maxdis - 0.1)) / maxdis ai = PIDControl(target_speed, state.v) state = unicycle_model.update(state, ai, di) if abs(state.v) <= stop_speed and target_ind <= len(cx) - 2: target_ind += 1 time = time + unicycle_model.dt # check goal dx = state.x - goal[0] dy = state.y - goal[1] if math.hypot(dx, dy) <= goal_dis: find_goal = True break x.append(state.x) y.append(state.y) yaw.append(state.yaw) v.append(state.v) t.append(time) a.append(ai) d.append(di) if target_ind % 1 == 0 and animation: # pragma: no cover plt.cla() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect( 'key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) plt.plot(cx, cy, "-r", label="course") plt.plot(x, y, "ob", label="trajectory") plt.plot(cx[target_ind], cy[target_ind], "xg", label="target") plt.axis("equal") plt.grid(True) plt.title("speed:" + str(round(state.v, 2)) + "tind:" + str(target_ind)) plt.pause(0.0001) else: print("Time out!!") return t, x, y, yaw, v, a, d, find_goal