200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 无人驾驶五 使用pure pursuit实现无人车轨迹追踪(python)

无人驾驶五 使用pure pursuit实现无人车轨迹追踪(python)

时间:2021-06-17 18:26:42

相关推荐

无人驾驶五 使用pure pursuit实现无人车轨迹追踪(python)

/adamshan/article/details/80555174

# coding=utf-8import numpy as npimport mathimport matplotlib.pyplot as pltk = 0.1 # 前视距离系数Lfc = 2.0 # 前视距离Kp = 1.0 # 速度P控制器系数dt = 0.1 # 时间间隔,单位:sL = 2.9 # 车辆轴距,单位:mclass VehicleState:def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0):self.x = xself.y = yself.yaw = yawself.v = vdef update(state, a, delta):state.x = state.x + state.v * math.cos(state.yaw) * dtstate.y = state.y + state.v * math.sin(state.yaw) * dtstate.yaw = state.yaw + state.v / L * math.tan(delta) * dtstate.v = state.v + a * dtreturn statedef PControl(target, current):a = Kp * (target - current)return adef pure_pursuit_control(state, cx, cy, pind):ind = calc_target_index(state, cx, cy)if pind >= ind:ind = pindif ind < len(cx):tx = cx[ind]ty = cy[ind]else:tx = cx[-1]ty = cy[-1]ind = len(cx) - 1alpha = math.atan2(ty - state.y, tx - state.x) - state.yawif state.v < 0: # backalpha = math.pi - alphaLf = k * state.v + Lfcdelta = math.atan2(2.0 * L * math.sin(alpha) / Lf, 1.0)return delta, inddef calc_target_index(state, cx, cy):# 搜索最临近的路点dx = [state.x - icx for icx in cx]dy = [state.y - icy for icy in cy]d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]ind = d.index(min(d))L = 0.0Lf = k * state.v + Lfcwhile Lf > L and (ind + 1) < len(cx):dx = cx[ind + 1] - cx[ind]dy = cx[ind + 1] - cx[ind]L += math.sqrt(dx ** 2 + dy ** 2)ind += 1return inddef main():# 设置目标路点cx = np.arange(0, 50, 1)cy = [math.sin(ix / 5.0) * ix / 2.0 for ix in cx]target_speed = 10.0 / 3.6 # [m/s]T = 100.0 # 最大模拟时间# 设置车辆的出事状态state = VehicleState(x=-0.0, y=-3.0, yaw=0.0, v=0.0)lastIndex = len(cx) - 1time = 0.0x = [state.x]y = [state.y]yaw = [state.yaw]v = [state.v]t = [0.0]target_ind = calc_target_index(state, cx, cy)while T >= time and lastIndex > target_ind:ai = PControl(target_speed, state.v)di, target_ind = pure_pursuit_control(state, cx, cy, target_ind)state = update(state, ai, di)time = time + dtx.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_ind], cy[target_ind], "go", label="target")plt.axis("equal")plt.grid(True)plt.title("Speed[km/h]:" + str(state.v * 3.6)[:4])plt.pause(0.001)if __name__ == '__main__':main()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。