|
| 1 | +#! /usr/bin/python |
| 2 | +""" |
| 3 | +
|
| 4 | +Path tracking simulation with pure pursuit steering control and PID speed control. |
| 5 | +
|
| 6 | +author: Atsushi Sakai |
| 7 | +
|
| 8 | +""" |
| 9 | +# import numpy as np |
| 10 | +import math |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +import unicycle_model |
| 13 | +from pycubicspline import pycubicspline |
| 14 | + |
| 15 | +Kp = 1.0 # speed propotional gain |
| 16 | +Lf = 1.0 # look-ahead distance |
| 17 | +# animation = True |
| 18 | +animation = False |
| 19 | + |
| 20 | + |
| 21 | +def PIDControl(target, current): |
| 22 | + a = Kp * (target - current) |
| 23 | + |
| 24 | + return a |
| 25 | + |
| 26 | + |
| 27 | +def pure_pursuit_control(state, cx, cy, pind): |
| 28 | + |
| 29 | + ind = calc_target_index(state, cx, cy) |
| 30 | + |
| 31 | + if pind >= ind: |
| 32 | + ind = pind |
| 33 | + |
| 34 | + # print(pind, ind) |
| 35 | + if ind < len(cx): |
| 36 | + tx = cx[ind] |
| 37 | + ty = cy[ind] |
| 38 | + else: |
| 39 | + tx = cx[-1] |
| 40 | + ty = cy[-1] |
| 41 | + ind = len(cx) - 1 |
| 42 | + |
| 43 | + alpha = math.atan2(ty - state.y, tx - state.x) - state.yaw |
| 44 | + |
| 45 | + if state.v < 0: # back |
| 46 | + alpha = math.pi - alpha |
| 47 | + # if alpha > 0: |
| 48 | + # alpha = math.pi - alpha |
| 49 | + # else: |
| 50 | + # alpha = math.pi + alpha |
| 51 | + |
| 52 | + delta = math.atan2(2.0 * unicycle_model.L * math.sin(alpha) / Lf, 1.0) |
| 53 | + |
| 54 | + return delta, ind |
| 55 | + |
| 56 | + |
| 57 | +def calc_target_index(state, cx, cy): |
| 58 | + dx = [state.x - icx for icx in cx] |
| 59 | + dy = [state.y - icy for icy in cy] |
| 60 | + |
| 61 | + d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)] |
| 62 | + |
| 63 | + ind = d.index(min(d)) |
| 64 | + |
| 65 | + L = 0.0 |
| 66 | + |
| 67 | + while Lf > L and (ind + 1) < len(cx): |
| 68 | + dx = cx[ind + 1] - cx[ind] |
| 69 | + dy = cx[ind + 1] - cx[ind] |
| 70 | + L += math.sqrt(dx ** 2 + dy ** 2) |
| 71 | + ind += 1 |
| 72 | + |
| 73 | + return ind |
| 74 | + |
| 75 | + |
| 76 | +def closed_loop_prediction(cx, cy, cyaw, speed_profile, goal): |
| 77 | + |
| 78 | + T = 500.0 # max simulation time |
| 79 | + goal_dis = 0.3 |
| 80 | + stop_speed = 0.05 |
| 81 | + |
| 82 | + state = unicycle_model.State(x=-0.0, y=-0.0, yaw=0.0, v=0.0) |
| 83 | + |
| 84 | + # lastIndex = len(cx) - 1 |
| 85 | + time = 0.0 |
| 86 | + x = [state.x] |
| 87 | + y = [state.y] |
| 88 | + yaw = [state.yaw] |
| 89 | + v = [state.v] |
| 90 | + t = [0.0] |
| 91 | + target_ind = calc_target_index(state, cx, cy) |
| 92 | + |
| 93 | + while T >= time: |
| 94 | + di, target_ind = pure_pursuit_control(state, cx, cy, target_ind) |
| 95 | + ai = PIDControl(speed_profile[target_ind], state.v) |
| 96 | + state = unicycle_model.update(state, ai, di) |
| 97 | + |
| 98 | + if abs(state.v) <= stop_speed: |
| 99 | + target_ind += 1 |
| 100 | + |
| 101 | + time = time + unicycle_model.dt |
| 102 | + |
| 103 | + # check goal |
| 104 | + dx = state.x - goal[0] |
| 105 | + dy = state.y - goal[1] |
| 106 | + if math.sqrt(dx ** 2 + dy ** 2) <= goal_dis: |
| 107 | + print("Goal") |
| 108 | + break |
| 109 | + |
| 110 | + x.append(state.x) |
| 111 | + y.append(state.y) |
| 112 | + yaw.append(state.yaw) |
| 113 | + v.append(state.v) |
| 114 | + t.append(time) |
| 115 | + |
| 116 | + if target_ind % 20 == 0 and animation: |
| 117 | + plt.cla() |
| 118 | + plt.plot(cx, cy, "-r", label="course") |
| 119 | + plt.plot(x, y, "ob", label="trajectory") |
| 120 | + plt.plot(cx[target_ind], cy[target_ind], "xg", label="target") |
| 121 | + plt.axis("equal") |
| 122 | + plt.grid(True) |
| 123 | + plt.title("speed:" + str(round(state.v, 2)) + |
| 124 | + "tind:" + str(target_ind)) |
| 125 | + plt.pause(0.0001) |
| 126 | + |
| 127 | + return t, x, y, yaw, v |
| 128 | + |
| 129 | + |
| 130 | +def set_stop_point(target_speed, cx, cy, cyaw): |
| 131 | + speed_profile = [target_speed] * len(cx) |
| 132 | + |
| 133 | + d = [] |
| 134 | + direction = 1.0 |
| 135 | + |
| 136 | + # Set stop point |
| 137 | + for i in range(len(cx) - 1): |
| 138 | + dx = cx[i + 1] - cx[i] |
| 139 | + dy = cy[i + 1] - cy[i] |
| 140 | + td = math.sqrt(dx ** 2.0 + dy ** 2.0) |
| 141 | + d.append(td) |
| 142 | + dyaw = cyaw[i + 1] - cyaw[i] |
| 143 | + switch = math.pi / 4.0 <= dyaw < math.pi / 2.0 |
| 144 | + |
| 145 | + if switch: |
| 146 | + direction *= -1 |
| 147 | + |
| 148 | + if direction != 1.0: |
| 149 | + speed_profile[i] = - target_speed |
| 150 | + else: |
| 151 | + speed_profile[i] = target_speed |
| 152 | + |
| 153 | + if switch: |
| 154 | + speed_profile[i] = 0.0 |
| 155 | + |
| 156 | + speed_profile[0] = 0.0 |
| 157 | + speed_profile[-1] = 0.0 |
| 158 | + |
| 159 | + d.append(d[-1]) |
| 160 | + |
| 161 | + return speed_profile, d |
| 162 | + |
| 163 | + |
| 164 | +def calc_speed_profile(cx, cy, cyaw, target_speed): |
| 165 | + |
| 166 | + speed_profile, d = set_stop_point(target_speed, cx, cy, cyaw) |
| 167 | + |
| 168 | + # flg, ax = plt.subplots(1) |
| 169 | + # plt.plot(speed_profile, "-r") |
| 170 | + # plt.show() |
| 171 | + |
| 172 | + return speed_profile |
| 173 | + |
| 174 | + |
| 175 | +def main(): |
| 176 | + print("rear wheel feedback tracking start!!") |
| 177 | + ax = [0.0, 6.0, 12.5, 5.0, 7.5, 3.0, -1.0] |
| 178 | + ay = [0.0, 0.0, 5.0, 6.5, 0.0, 5.0, -2.0] |
| 179 | + goal = [ax[-1], ay[-1]] |
| 180 | + |
| 181 | + cx, cy, cyaw, ck, s = pycubicspline.calc_spline_course(ax, ay, ds=0.1) |
| 182 | + target_speed = 10.0 / 3.6 |
| 183 | + |
| 184 | + sp = calc_speed_profile(cx, cy, cyaw, target_speed) |
| 185 | + |
| 186 | + t, x, y, yaw, v = closed_loop_prediction(cx, cy, cyaw, sp, goal) |
| 187 | + |
| 188 | + flg, _ = plt.subplots(1) |
| 189 | + print(len(ax), len(ay)) |
| 190 | + plt.plot(ax, ay, "xb", label="input") |
| 191 | + plt.plot(cx, cy, "-r", label="spline") |
| 192 | + plt.plot(x, y, "-g", label="tracking") |
| 193 | + plt.grid(True) |
| 194 | + plt.axis("equal") |
| 195 | + plt.xlabel("x[m]") |
| 196 | + plt.ylabel("y[m]") |
| 197 | + plt.legend() |
| 198 | + |
| 199 | + flg, ax = plt.subplots(1) |
| 200 | + plt.plot(s, [math.degrees(iyaw) for iyaw in cyaw], "-r", label="yaw") |
| 201 | + plt.grid(True) |
| 202 | + plt.legend() |
| 203 | + plt.xlabel("line length[m]") |
| 204 | + plt.ylabel("yaw angle[deg]") |
| 205 | + |
| 206 | + flg, ax = plt.subplots(1) |
| 207 | + plt.plot(s, ck, "-r", label="curvature") |
| 208 | + plt.grid(True) |
| 209 | + plt.legend() |
| 210 | + plt.xlabel("line length[m]") |
| 211 | + plt.ylabel("curvature [1/m]") |
| 212 | + |
| 213 | + plt.show() |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == '__main__': |
| 217 | + main() |
0 commit comments