| 13 | |
| 14 | |
| 15 | class VehicleSimulator(): |
| 16 | |
| 17 | def __init__(self, ix, iy, iyaw, iv, max_v, w, L): |
| 18 | self.x = ix |
| 19 | self.y = iy |
| 20 | self.yaw = iyaw |
| 21 | self.v = iv |
| 22 | self.max_v = max_v |
| 23 | self.W = w |
| 24 | self.L = L |
| 25 | self._calc_vehicle_contour() |
| 26 | |
| 27 | def update(self, dt, a, omega): |
| 28 | self.x += self.v * np.cos(self.yaw) * dt |
| 29 | self.y += self.v * np.sin(self.yaw) * dt |
| 30 | self.yaw += omega * dt |
| 31 | self.v += a * dt |
| 32 | if self.v >= self.max_v: |
| 33 | self.v = self.max_v |
| 34 | |
| 35 | def plot(self): |
| 36 | plt.plot(self.x, self.y, ".b") |
| 37 | |
| 38 | # convert global coordinate |
| 39 | gx, gy = self.calc_global_contour() |
| 40 | plt.plot(gx, gy, "--b") |
| 41 | |
| 42 | def calc_global_contour(self): |
| 43 | gx = [(ix * np.cos(self.yaw) + iy * np.sin(self.yaw)) + |
| 44 | self.x for (ix, iy) in zip(self.vc_x, self.vc_y)] |
| 45 | gy = [(ix * np.sin(self.yaw) - iy * np.cos(self.yaw)) + |
| 46 | self.y for (ix, iy) in zip(self.vc_x, self.vc_y)] |
| 47 | |
| 48 | return gx, gy |
| 49 | |
| 50 | def _calc_vehicle_contour(self): |
| 51 | |
| 52 | self.vc_x = [] |
| 53 | self.vc_y = [] |
| 54 | |
| 55 | self.vc_x.append(self.L / 2.0) |
| 56 | self.vc_y.append(self.W / 2.0) |
| 57 | |
| 58 | self.vc_x.append(self.L / 2.0) |
| 59 | self.vc_y.append(-self.W / 2.0) |
| 60 | |
| 61 | self.vc_x.append(-self.L / 2.0) |
| 62 | self.vc_y.append(-self.W / 2.0) |
| 63 | |
| 64 | self.vc_x.append(-self.L / 2.0) |
| 65 | self.vc_y.append(self.W / 2.0) |
| 66 | |
| 67 | self.vc_x.append(self.L / 2.0) |
| 68 | self.vc_y.append(self.W / 2.0) |
| 69 | |
| 70 | self.vc_x, self.vc_y = self._interporate(self.vc_x, self.vc_y) |
| 71 | |
| 72 | def _interporate(self, x, y): |