1 """Vehicle methods copied from esmini."""
2 from math import atan, cos, fmod, pi, sin, tan
10 st_return_factor = 4.0
11 st_max_angle = (60.0 * pi / 180)
15 def __init__(self, x, y, h, sp, wa):
16 """Create new `Car` object.
18 :param x: ``x`` coordinate.
19 :param y: ``y`` coordinate.
20 :param h: The heading.
22 :param wa: Wheel angle.
29 self.wr = 0.0 # wheel rotation
30 self.va = 0.0 # angular velocity
31 self.vx = 0.0 # velocity in x
32 self.vy = 0.0 # velocity in y
34 def ctrl(self, dt=0, throttle=0, steering=0):
35 """Corresponds to vehicle's analog control.
37 :param dt: GetSimTimeStep.
38 :param throttle: Longitudinal control
41 +1: maximum acceleration
42 :param steering: Lateral control
47 acc = self.max_dec if throttle < 0 else self.max_acc
48 self.sp += acc * throttle * dt
50 if abs(throttle) < 1e-3:
53 self.sp = min(self.max_sp, max(0, self.sp))
55 # Make steering slightly wheel speed dependent
56 st_scale = 1.0 / (1 + 0.005 * self.sp**2)
57 self.wa += st_scale * self.st_rate * steering * dt
60 self.wa *= (1.0 - self.st_return_factor * dt)
63 ma = st_scale * self.st_max_angle
64 self.wa = min(ma, max(-ma, self.wa))
68 def update(self, dt=0):
69 # Calculate wheel rot: https://en.wikipedia.org/wiki/Arc_(geometry)
70 self.wr = fmod(self.wr + self.sp * dt / self.w_rad, 2 * pi)
72 # Calculate vehicle kinematics according to simple bicycle model, see
73 # http://www.me.berkeley.edu/~frborrel/pdfpub/IV_KinematicMPC_jason.pdf
75 var = atan(0.15 * tan(self.wa))
76 self.va = var + self.h
77 hd = self.sp * sin(var) / (self.length * 0.5)
79 self.vx = self.sp * cos(self.va)
80 self.vy = self.sp * sin(self.va)
82 self.h = fmod(self.h + dt * hd, 2 * pi)
86 self.x += dt * self.vx
87 self.y += dt * self.vy