当前位置:   article > 正文

自动驾驶算法详解(4): 横向LQR、纵向PID控制进行轨迹跟踪以及python实现_车辆lqr+pid控制算法

车辆lqr+pid控制算法

前言

在量产ADAS或者自动驾驶算法中,横纵向控制往往都是分开控制的,上一篇文章中介绍了如何使用LQR同时进行横纵向的控制,本文将介绍一种横纵向分开控制的思路,将使用LQR算法进行横向控制,同时使用PID算法进行纵向控制。这种方法在很多自动驾驶科技公司比较常见,百度apollo的控制节点conrol也是使用同样的思路。

如果对自动驾驶算法感兴趣,可以关注我的主页和专栏。

https://blog.csdn.net/nn243823163/category_11685852.htmlhttps://blog.csdn.net/nn243823163/category_11685852.html最新文章推荐:

Apollo Planning决策规划算法代码详细解析 (1):Scenario选择

Apollo Planning决策规划算法代码详细解析 (5):规划算法流程介绍

Apollo Planning决策规划算法代码详解 (22):决策规划算法最完整介绍

自动驾驶算法详解(2): prescan联合simulink进行FCW的仿真

自动驾驶算法详解(3): LQR算法进行轨迹跟踪,lqr_speed_steering_control( )的python实现

自动驾驶算法详解(5): 贝塞尔曲线进行路径规划的python实现

 Apollo算法仿真调试(1): 使用Vscode断点调试apollo的方法

Apollo规划决策算法仿真调试(4):  动态障碍物绕行

Apollo规划决策算法仿真调试(7):pnc_map模块详解GetRouteSegments规划局部地图生成下篇_

正文如下:

一、横向LQR问题模型建立:

理论部分比较成熟,这里只介绍demo所使用的建模方程:

使用离散代数黎卡提方程求解

系统状态矩阵与LQR同时控制横纵向相比有所简化,状态矩阵如下,X = [距离差,距离差导数,角度差,角度差导数]:

输入矩阵变为1个变量,只有前轮转角。

A矩阵和B矩阵如下:

二、纵向PID控制

纵向上由PID算法来计算加速度,本demo中只保留P项:

三、结果分析

从状态更新的方法可以看到,纵向控制的速度会影响横向控制的结果

1、纵向参数P = 1 时的控制结果:

速度控制结果:

轨迹跟踪结果:

2、纵向参数P = 5 时的控制结果:

速度控制结果:

轨迹跟踪结果:

3、纵向参数P = 20时的控制结果:

速度控制结果:

轨迹跟踪结果:

4、纵向参数P = 30时的控制结果:

速度控制结果:

四、代码实现

1、参数初始化

  1. Kp = 1.0 # speed proportional gain
  2. # LQR parameter
  3. Q = np.eye(4)
  4. R = np.eye(1)
  5. # parameters
  6. dt = 0.1 # time tick[s]
  7. L = 0.5 # Wheel base of the vehicle [m]
  8. max_steer = np.deg2rad(45.0) # maximum steering angle[rad]
  9. show_animation = True
  10. # show_animation = False

2、相关函数定义

  1. def PIDControl(target, current):
  2. a = Kp * (target - current)
  3. return a
  4. def lqr_steering_control(state, cx, cy, cyaw, ck, pe, pth_e):
  5. ind, e = calc_nearest_index(state, cx, cy, cyaw)
  6. k = ck[ind]
  7. v = state.v
  8. th_e = pi_2_pi(state.yaw - cyaw[ind])
  9. A = np.zeros((4, 4))
  10. A[0, 0] = 1.0
  11. A[0, 1] = dt
  12. A[1, 2] = v
  13. A[2, 2] = 1.0
  14. A[2, 3] = dt
  15. # print(A)
  16. B = np.zeros((4, 1))
  17. B[3, 0] = v / L
  18. K, _, _ = dlqr(A, B, Q, R)
  19. x = np.zeros((4, 1))
  20. x[0, 0] = e
  21. x[1, 0] = (e - pe) / dt
  22. x[2, 0] = th_e
  23. x[3, 0] = (th_e - pth_e) / dt
  24. ff = math.atan2(L * k, 1)
  25. fb = pi_2_pi((-K @ x)[0, 0])
  26. delta = ff + fb
  27. return delta, ind, e, th_e
  28. def closed_loop_prediction(cx, cy, cyaw, ck, speed_profile, goal):
  29. T = 500.0 # max simulation time
  30. goal_dis = 0.3
  31. stop_speed = 0.05
  32. state = State(x=-0.0, y=-0.0, yaw=0.0, v=0.0)
  33. time = 0.0
  34. x = [state.x]
  35. y = [state.y]
  36. yaw = [state.yaw]
  37. v = [state.v]
  38. t = [0.0]
  39. e, e_th = 0.0, 0.0
  40. while T >= time:
  41. dl, target_ind, e, e_th = lqr_steering_control(
  42. state, cx, cy, cyaw, ck, e, e_th)
  43. ai = PIDControl(speed_profile[target_ind], state.v)
  44. state = update(state, ai, dl)
  45. if abs(state.v) <= stop_speed:
  46. target_ind += 1
  47. time = time + dt
  48. # check goal
  49. dx = state.x - goal[0]
  50. dy = state.y - goal[1]
  51. if math.hypot(dx, dy) <= goal_dis:
  52. print("Goal")
  53. break
  54. x.append(state.x)
  55. y.append(state.y)
  56. yaw.append(state.yaw)
  57. v.append(state.v)
  58. t.append(time)
  59. if target_ind % 100 == 0 and show_animation:
  60. plt.cla()
  61. # for stopping simulation with the esc key.
  62. plt.gcf().canvas.mpl_connect('key_release_event',
  63. lambda event: [exit(0) if event.key == 'escape' else None])
  64. plt.plot(cx, cy, "-r", label="course")
  65. plt.plot(x, y, "ob", label="trajectory")
  66. plt.plot(cx[target_ind], cy[target_ind], "xg", label="target")
  67. plt.axis("equal")
  68. plt.grid(True)
  69. plt.title("speed[km/h]:" + str(round(state.v * 3.6, 2))
  70. + ",target index:" + str(target_ind))
  71. plt.pause(0.0001)
  72. return t, x, y, yaw, v

3、主函数

  1. def main():
  2. print("LQR steering control tracking start!!")
  3. # ax = [0.0, 6.0, 12.5, 10.0, 7.5, 3.0, -1.0]
  4. # ay = [0.0, -3.0, -5.0, 6.5, 3.0, 5.0, -2.0]
  5. ax = [0.0, 6.0, 12.5, 10.0, 17.5, 20.0, 25.0]
  6. ay = [0.0, -3.0, -5.0, 6.5, 3.0, 0.0, 0.0]
  7. goal = [ax[-1], ay[-1]]
  8. cx, cy, cyaw, ck, s = calc_spline_course(
  9. ax, ay, ds=0.1)
  10. target_speed = 10.0 / 3.6 # simulation parameter km/h -> m/s
  11. sp = calc_speed_profile(cx, cy, cyaw, target_speed)
  12. t, x, y, yaw, v = closed_loop_prediction(cx, cy, cyaw, ck, sp, goal)
  13. if show_animation: # pragma: no cover
  14. plt.close()
  15. plt.subplots(1)
  16. plt.plot(ax, ay, "xb", label="input")
  17. plt.plot(cx, cy, "-r", label="spline")
  18. plt.plot(x, y, "-g", label="tracking")
  19. plt.grid(True)
  20. plt.axis("equal")
  21. plt.xlabel("x[m]")
  22. plt.ylabel("y[m]")
  23. plt.legend()
  24. plt.show()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/429141
推荐阅读
相关标签
  

闽ICP备14008679号