Guest User

Untitled

a guest
Jan 21st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. from math import *
  2. import matplotlib.pyplot as plt
  3.  
  4. #Python is in rad
  5.  
  6. #Fx = f(k, Fz) = Fz * D * sin(C * atan[{Bk - E * [Bk - atan(Bk)]}])
  7.  
  8. class Wheel:
  9. def __init__(self):
  10. self.globalFrictionCoefficient = 1.0
  11.  
  12. #Pacejka parameters
  13.  
  14. #Lower value means that the curve will be flatter before the peak (and thus have a "wider" peak)
  15. #Value above 2 means a change of sign of the curve, meaning it has to be <= 2.0 for longitudal grip
  16. self.Cx = 1.65
  17. self.Cz = 2.8
  18. self.Cy = 1.3
  19.  
  20. #Essentially a max grip, might as well be constant 1 and only change the global friction coefficient
  21. self.D = 0.5
  22.  
  23. #Shapes the curve as an exponential, which means lower value will mean flatter curve
  24. #Compared to C it also moves the very beginning of the curve, rather than "only" affecting the area before the peak
  25. self.Ex = 0.9
  26. self.Ez = 0.98
  27. self.Ey = 0.95
  28.  
  29. #These two numbers move the peak acceleration during slip, per "radian" (slip) and per Newton of load, respectively
  30. #Moving the peak "back", i.e. having the peak being at a higher slip rate, also causes the "fallback" (the return to 0 acceleration as the slip approaches zero) to be slower (and thus more smooth)
  31.  
  32. #Higher a3 means later peak
  33. #Newtons per radians
  34. self.a3 = 55 * 1000 * 0.01
  35.  
  36. #Higher a4 moves the peak backwards (earlier fast acceleration, and decreased fallback rate)
  37. #Fluctuates the total time to reach zero slip depending on the value, so can in some cases increase fallback rate
  38. #Newtons
  39. self.a4 = 7000.0 * 1.0
  40.  
  41. #How much the global friction coefficient should change per newton of vertical load
  42. self.frictionCoefficientChange = -0.05 / 1000.0
  43.  
  44. #A constant we add to the longitudal velocity to avoid division by zero
  45. self.longitudalVelocityOffset = 0.1
  46.  
  47. self.radius = 0.25
  48.  
  49. self.angular_velocity = 0.0
  50. self.linear_velocity = 0.0
  51.  
  52. def getLongitudalSlip(self):
  53. return (self.radius * self.angular_velocity - self.linear_velocity) / (abs(self.linear_velocity) + self.longitudalVelocityOffset)
  54.  
  55. def getGlobalFrictionCoefficient(self, Fz):
  56. return self.globalFrictionCoefficient + Fz * self.frictionCoefficientChange
  57.  
  58. def getLongitudalForce(self, Fz, k):
  59. u = self.getGlobalFrictionCoefficient(Fz)
  60. D = self.D * u * Fz
  61. C = self.Cx
  62.  
  63. BCD = self.a3 * sin(2.0 * atan(Fz / self.a4))
  64. B = BCD / (C * D)
  65. E = self.Ex
  66.  
  67. return D * sin(C * atan(B * k - E * (B * k - atan(B * k))))
  68.  
  69.  
  70. def getVerticalMoment(self, Fz, k):
  71. u = self.getGlobalFrictionCoefficient(Fz)
  72. D = self.D * u * Fz
  73. C = self.Cz
  74. BCD = self.a3 * sin(2.0 * atan(Fz / self.a4))
  75. B = BCD / (C * D)
  76.  
  77. E = self.Ez
  78.  
  79. return D * sin(C * atan(B * k - E * (B * k - atan(B * k))))
  80.  
  81. def getLateralForce(self, Fz, k):
  82. u = self.getGlobalFrictionCoefficient(Fz)
  83. D = self.D * u * Fz
  84. C = self.Cy
  85. BCD = self.a3 * sin(2.0 * atan(Fz / self.a4))
  86. B = BCD / (C * D)
  87.  
  88. E = self.Ey
  89.  
  90. return D * sin(C * atan(B * k - E * (B * k - atan(B * k))))
  91.  
  92. def plotXY(x, y, title, xlabel, ylabel):
  93. plt.clf()
  94. plt.plot(x,y)
  95. plt.margins(0.1)
  96. plt.xlabel(xlabel)
  97. plt.ylabel(ylabel)
  98. plt.title(title)
  99. plt.show()
  100.  
  101.  
  102. #unsigned gravity acceleration
  103. g = 9.82
  104.  
  105. #Total sprung mass in kilograms
  106. carMass = 1120
  107.  
  108. simTime = 25.0
  109. simTimeStep = 0.15
  110.  
  111. wheels = [Wheel(), Wheel(), Wheel(), Wheel()]
  112.  
  113. tyreLoad = carMass / len(wheels) * g
  114. ang_acc = 0.0 / wheels[0].radius
  115.  
  116. printTime = -0.4
  117.  
  118. time = 0.0
  119. timeSincePrint = printTime
  120.  
  121. for wheel in wheels:
  122. wheel.linear_velocity = 60.0
  123. wheel.angular_velocity = wheel.linear_velocity / wheel.radius
  124. wheel.linear_velocity = 0.0
  125.  
  126. maxAngVel = -2.0# / wheels[0].radius
  127.  
  128.  
  129. timePlot = []
  130. linVelPlot = []
  131. angVelPlot = []
  132. accInGPlot = []
  133. tetsPlot = []
  134. longSlipPlot = []
  135.  
  136. simIteration = 0
  137. while time < simTime:
  138. force = 0.0
  139. avgLongSlip = 0.0
  140. avgAngVel = 0.0
  141.  
  142. for wheel in wheels:
  143. if simIteration > 0:
  144. wheel.angular_velocity += ang_acc * simTimeStep
  145. if maxAngVel >= 0.0:
  146. wheel.angular_velocity = min(wheel.angular_velocity, maxAngVel)
  147. longSlip = wheel.getLongitudalSlip()
  148. longF = wheel.getLongitudalForce(tyreLoad, longSlip)
  149. force += longF
  150. avgLongSlip += longSlip / len(wheels)
  151. avgAngVel += wheel.angular_velocity / len(wheels)
  152. acc = force * simTimeStep / carMass
  153.  
  154. longSlipPlot.append(avgLongSlip)
  155. timePlot.append(time)
  156. linVelPlot.append(wheels[0].linear_velocity)
  157. angVelPlot.append(avgAngVel)
  158. accInGPlot.append(force / (g * carMass))
  159.  
  160. for wheel in wheels:
  161. wheel.linear_velocity += acc
  162. #if abs(wheels[0].angular_velocity) > 0.0 and abs(wheels[0].linear_velocity / (wheels[0].angular_velocity * wheels[0].radius) > 0.99):
  163. # break
  164.  
  165. timeSincePrint += simTimeStep
  166. if printTime >= 0.0 and timeSincePrint >= printTime:
  167. print "Time: " + str(time)
  168. print "Ang. Vel: " + str(avgAngVel) + " (" + str(avgAngVel * wheels[0].radius) + " m/s)"
  169. print "Lin. Vel: " + str(wheels[0].linear_velocity)
  170. print "Average Slip: " + str(avgLongSlip)
  171. print "Acceleration: " + str(force / (g * carMass)) + " g"
  172. print "----------------------------------"
  173. timeSincePrint = 0.0
  174. simIteration += 1
  175. time += simTimeStep
  176.  
  177. #plotXY(longSlipPlot, accInGPlot, "Acc-Slip", "Slip", "Acceleration")
  178. #plotXY(angVelPlot, linVelPlot, "Linear-Angular Velocity", "Ang vel", "Lin vel")
  179. #plotXY(timePlot, longSlipPlot, "Slip-Time", "Time", "Slip")
  180. #plotXY(timePlot, angVelPlot, "Angular Velocity-Time", "Time", "Angular Velocity")
  181. #plotXY(timePlot, linVelPlot, "Linear Velocity-Time", "Time", "Linear Velocity")
  182. plotXY(timePlot, accInGPlot, "Acceleration-Time", "Time", "Acceleration (g)")
  183.  
  184.  
  185.  
  186. MzX = []
  187. MzY = []
  188. FyY = []
  189. FxY = []
  190. FxX = []
  191.  
  192. MzC = 0.25
  193. i = -MzC
  194.  
  195. while i <= MzC:
  196. MzX.append(i / 3.1415926535 * 180.0)
  197. MzY.append(wheels[0].getVerticalMoment(tyreLoad, i))
  198. FyY.append(wheels[0].getLateralForce(tyreLoad, i))
  199. i += 0.01
  200. FxC = 16.0
  201. i = -FxC
  202. while i <= FxC:
  203. longSlip = i
  204. FxX.append(longSlip)
  205. FxY.append(wheels[0].getLongitudalForce(tyreLoad, longSlip) / tyreLoad)
  206. i += 0.01
  207.  
  208. #plotXY(MzX, MzY, "Self-aligning Torque", "Lateral Slip", "Torque")
  209. plotXY(MzX, FyY, "Lateral Force", "Lateral Slip", "Force")
  210. plotXY(FxX, FxY, "Longitudal Force", "Longitudal Slip", "Force")
Add Comment
Please, Sign In to add comment