draw.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import math
  4. PI = np.pi
  5. class Arrow:
  6. def __init__(self,ax, x, y, theta, L, c):
  7. angle = np.deg2rad(30)
  8. d = 0.3 * L
  9. w = 2
  10. x_start = x
  11. y_start = y
  12. x_end = x + L * np.cos(theta)
  13. y_end = y + L * np.sin(theta)
  14. theta_hat_L = theta + PI - angle
  15. theta_hat_R = theta + PI + angle
  16. x_hat_start = x_end
  17. x_hat_end_L = x_hat_start + d * np.cos(theta_hat_L)
  18. x_hat_end_R = x_hat_start + d * np.cos(theta_hat_R)
  19. y_hat_start = y_end
  20. y_hat_end_L = y_hat_start + d * np.sin(theta_hat_L)
  21. y_hat_end_R = y_hat_start + d * np.sin(theta_hat_R)
  22. # plt.plot([x_start, x_end], [y_start, y_end], color=c, linewidth=w)
  23. # plt.plot([x_hat_start, x_hat_end_L],
  24. # [y_hat_start, y_hat_end_L], color=c, linewidth=w)
  25. # plt.plot([x_hat_start, x_hat_end_R],
  26. # [y_hat_start, y_hat_end_R], color=c, linewidth=w)
  27. ax.plot([x_start, x_end], [y_start, y_end], color=c, linewidth=w)
  28. ax.plot([x_hat_start, x_hat_end_L],
  29. [y_hat_start, y_hat_end_L], color=c, linewidth=w)
  30. ax.plot([x_hat_start, x_hat_end_R],
  31. [y_hat_start, y_hat_end_R], color=c, linewidth=w)
  32. class Car:
  33. def __init__(self, x, y, yaw, w, L):
  34. theta_B = PI + yaw
  35. xB = x + L / 4 * np.cos(theta_B)
  36. yB = y + L / 4 * np.sin(theta_B)
  37. theta_BL = theta_B + PI / 2
  38. theta_BR = theta_B - PI / 2
  39. x_BL = xB + w / 2 * np.cos(theta_BL) # Bottom-Left vertex
  40. y_BL = yB + w / 2 * np.sin(theta_BL)
  41. x_BR = xB + w / 2 * np.cos(theta_BR) # Bottom-Right vertex
  42. y_BR = yB + w / 2 * np.sin(theta_BR)
  43. x_FL = x_BL + L * np.cos(yaw) # Front-Left vertex
  44. y_FL = y_BL + L * np.sin(yaw)
  45. x_FR = x_BR + L * np.cos(yaw) # Front-Right vertex
  46. y_FR = y_BR + L * np.sin(yaw)
  47. plt.plot([x_BL, x_BR, x_FR, x_FL, x_BL],
  48. [y_BL, y_BR, y_FR, y_FL, y_BL],
  49. linewidth=1, color='black')
  50. Arrow(x, y, yaw, L / 2, 'black')
  51. # plt.axis("equal")
  52. # plt.show()
  53. def draw_car(x, y, yaw, steer, C, color='black'):
  54. car = np.array([[-C.RB, -C.RB, C.RF, C.RF, -C.RB],
  55. [C.W / 2, -C.W / 2, -C.W / 2, C.W / 2, C.W / 2]])
  56. wheel = np.array([[-C.TR, -C.TR, C.TR, C.TR, -C.TR],
  57. [C.TW / 4, -C.TW / 4, -C.TW / 4, C.TW / 4, C.TW / 4]])
  58. rlWheel = wheel.copy()
  59. rrWheel = wheel.copy()
  60. frWheel = wheel.copy()
  61. flWheel = wheel.copy()
  62. Rot1 = np.array([[math.cos(yaw), -math.sin(yaw)],
  63. [math.sin(yaw), math.cos(yaw)]])
  64. Rot2 = np.array([[math.cos(steer), math.sin(steer)],
  65. [-math.sin(steer), math.cos(steer)]])
  66. frWheel = np.dot(Rot2, frWheel)
  67. flWheel = np.dot(Rot2, flWheel)
  68. frWheel += np.array([[C.WB], [-C.WD / 2]])
  69. flWheel += np.array([[C.WB], [C.WD / 2]])
  70. rrWheel[1, :] -= C.WD / 2
  71. rlWheel[1, :] += C.WD / 2
  72. frWheel = np.dot(Rot1, frWheel)
  73. flWheel = np.dot(Rot1, flWheel)
  74. rrWheel = np.dot(Rot1, rrWheel)
  75. rlWheel = np.dot(Rot1, rlWheel)
  76. car = np.dot(Rot1, car)
  77. frWheel += np.array([[x], [y]])
  78. flWheel += np.array([[x], [y]])
  79. rrWheel += np.array([[x], [y]])
  80. rlWheel += np.array([[x], [y]])
  81. car += np.array([[x], [y]])
  82. plt.plot(car[0, :], car[1, :], color)
  83. plt.plot(frWheel[0, :], frWheel[1, :], color)
  84. plt.plot(rrWheel[0, :], rrWheel[1, :], color)
  85. plt.plot(flWheel[0, :], flWheel[1, :], color)
  86. plt.plot(rlWheel[0, :], rlWheel[1, :], color)
  87. Arrow(x, y, yaw, C.WB * 0.8, color)
  88. if __name__ == '__main__':
  89. # Arrow(-1, 2, 60)
  90. Car(0, 0, 1, 2, 60)