drawcar.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import math
  2. import numpy as np
  3. from input import make_car
  4. import utils.draw as draw
  5. C=make_car.C
  6. def draw_car(ax, x, y, yaw, steer, color='red'):
  7. car = np.array([[-C.RB, -C.RB, C.RF, C.RF, -C.RB], [C.W / 2, -C.W / 2, -C.W / 2, C.W / 2, C.W / 2]])
  8. wheel = np.array([[-C.TR, -C.TR, C.TR, C.TR, -C.TR], [C.TW / 4, -C.TW / 4, -C.TW / 4, C.TW / 4, C.TW / 4]])
  9. rlWheel = wheel.copy()
  10. rrWheel = wheel.copy()
  11. frWheel = wheel.copy()
  12. flWheel = wheel.copy()
  13. Rot1 = np.array([[math.cos(yaw), -math.sin(yaw)], [math.sin(yaw), math.cos(yaw)]])
  14. Rot2 = np.array([[math.cos(steer), math.sin(steer)], [-math.sin(steer), math.cos(steer)]])
  15. frWheel = np.dot(Rot2, frWheel)
  16. flWheel = np.dot(Rot2, flWheel)
  17. frWheel += np.array([[C.WB], [-C.WD / 2]])
  18. flWheel += np.array([[C.WB], [C.WD / 2]])
  19. rrWheel[1, :] -= C.WD / 2
  20. rlWheel[1, :] += C.WD / 2
  21. frWheel = np.dot(Rot1, frWheel)
  22. flWheel = np.dot(Rot1, flWheel)
  23. rrWheel = np.dot(Rot1, rrWheel)
  24. rlWheel = np.dot(Rot1, rlWheel)
  25. car = np.dot(Rot1, car)
  26. frWheel += np.array([[x], [y]])
  27. flWheel += np.array([[x], [y]])
  28. rrWheel += np.array([[x], [y]])
  29. rlWheel += np.array([[x], [y]])
  30. car += np.array([[x], [y]])
  31. ax.plot(car[0, :], car[1, :], color)
  32. ax.plot(frWheel[0, :], frWheel[1, :], color)
  33. ax.plot(rrWheel[0, :], rrWheel[1, :], color)
  34. ax.plot(flWheel[0, :], flWheel[1, :], color)
  35. ax.plot(rlWheel[0, :], rlWheel[1, :], color)
  36. draw.Arrow(ax,x, y, yaw, C.WB * 0.8, color) # 修改为在指定的ax上绘制箭头
  37. # import math
  38. # import numpy as np
  39. # import matplotlib.pyplot as plt
  40. # import utils.draw as draw
  41. # from input import make_car
  42. # C=make_car.C
  43. # def draw_car(x, y, yaw, steer, color='red'):
  44. # car = np.array([[-C.RB, -C.RB, C.RF, C.RF, -C.RB],
  45. # [C.W / 2, -C.W / 2, -C.W / 2, C.W / 2, C.W / 2]])
  46. # wheel = np.array([[-C.TR, -C.TR, C.TR, C.TR, -C.TR],
  47. # [C.TW / 4, -C.TW / 4, -C.TW / 4, C.TW / 4, C.TW / 4]])
  48. # rlWheel = wheel.copy()
  49. # rrWheel = wheel.copy()
  50. # frWheel = wheel.copy()
  51. # flWheel = wheel.copy()
  52. # Rot1 = np.array([[math.cos(yaw), -math.sin(yaw)],
  53. # [math.sin(yaw), math.cos(yaw)]])
  54. # Rot2 = np.array([[math.cos(steer), math.sin(steer)],
  55. # [-math.sin(steer), math.cos(steer)]])
  56. # frWheel = np.dot(Rot2, frWheel)
  57. # flWheel = np.dot(Rot2, flWheel)
  58. # frWheel += np.array([[C.WB], [-C.WD / 2]])
  59. # flWheel += np.array([[C.WB], [C.WD / 2]])
  60. # rrWheel[1, :] -= C.WD / 2
  61. # rlWheel[1, :] += C.WD / 2
  62. # frWheel = np.dot(Rot1, frWheel)
  63. # flWheel = np.dot(Rot1, flWheel)
  64. # rrWheel = np.dot(Rot1, rrWheel)
  65. # rlWheel = np.dot(Rot1, rlWheel)
  66. # car = np.dot(Rot1, car)
  67. # frWheel += np.array([[x], [y]])
  68. # flWheel += np.array([[x], [y]])
  69. # rrWheel += np.array([[x], [y]])
  70. # rlWheel += np.array([[x], [y]])
  71. # car += np.array([[x], [y]])
  72. # plt.plot(car[0, :], car[1, :], color)
  73. # plt.plot(frWheel[0, :], frWheel[1, :], color)
  74. # plt.plot(rrWheel[0, :], rrWheel[1, :], color)
  75. # plt.plot(flWheel[0, :], flWheel[1, :], color)
  76. # plt.plot(rlWheel[0, :], rlWheel[1, :], color)
  77. # draw.Arrow(x, y, yaw, C.WB * 0.8, color)