forked from winstxnhdw/KinematicBicycleModel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulation_info.py
187 lines (140 loc) · 6 KB
/
simulation_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# pylint: skip-file
from csv import reader
from dataclasses import dataclass
from math import radians
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from models.bicycle_model import BicycleModel
from libs import CarDescription, StanleyController, generate_cubic_spline
from models.model_factory import ModelFactory
class Simulation:
def __init__(self, dt: float, max_size_x: int, max_size_y: int, frames: int, loop: bool = False):
self.dt = dt
self.map_size_x = max_size_x
self.map_size_y = max_size_y
self.frames = frames
self.loop = loop
class Path:
def __init__(self, file: str):
# Get path to waypoints.csv
with open("data/" + file, newline='') as f:
rows = list(reader(f, delimiter=','))
ds = 0.05
time_temp, x_temp, y_temp, acceleration_temp = [[float(i) for i in row] for row in zip(*rows[1:])]
non_duplicates = [i for i in range(1, len(x_temp)) if x_temp[i] != x_temp[i - 1]]
x = [x_temp[i] for i in non_duplicates]
y = [y_temp[i] for i in non_duplicates]
self.time = [time_temp[i] for i in non_duplicates]
self.delta_time = self.time[-1] / len(self.time)
acceleration = [acceleration_temp[i] for i in non_duplicates]
self.acceleration = acceleration
self.px, self.py, self.pyaw, _ = generate_cubic_spline(x, y, ds)
class Car:
def __init__(self, init_x, init_y, init_yaw, px, py, pyaw, acceleration, delta_time, max_time, vmax, model, vconstant = False):
# Model parameters
self.x = init_x
self.y = init_y
self.yaw = init_yaw
self.acceleration = acceleration
self.delta_time = delta_time
self.time = 0.0
self.max_time = max_time
if vconstant:
self.velocity = vmax
else:
self.velocity = vmax / 2
self.vmax = vmax
self.wheel_angle = 0.0
self.angular_velocity = 0.0
max_steer = radians(31)
wheelbase = 0.406
# Tracker parameters
self.px = px
self.py = py
self.pyaw = pyaw
self.k = 8.0
self.ksoft = 1.0
self.kyaw = 0.01
self.ksteer = 0.0
self.crosstrack_error = None
self.all_crosstrack_errors = []
self.target_id = None
self.prev_target_id = None
self.iteration = 0
self.continue_animation = True
# Description parameters
self.colour = 'black'
overall_length = 0.71
overall_width = 0.30
tyre_diameter = 0.107
tyre_width = 0.053
axle_track = 0.30 - 0.053
rear_overhang = 0.5 * (overall_length - wheelbase)
self.tracker = StanleyController(self.k, self.ksoft, self.kyaw, self.ksteer, max_steer, wheelbase, self.px, self.py, self.pyaw)
self.model = ModelFactory(wheelbase, max_steer, self.delta_time).create_model(model)
self.description = CarDescription(overall_length, overall_width, rear_overhang, tyre_diameter, tyre_width, axle_track, wheelbase)
def get_acceleration(self, time):
acceleration = self.acceleration[time]
return acceleration * 9.82
def plot_car(self):
return self.description.plot_car(self.x, self.y, self.yaw, self.wheel_angle)
def gen(self):
while self.continue_animation:
yield self.delta_time * self.iteration
def drive(self):
acceleration = self.get_acceleration(self.iteration) if self.velocity < self.vmax else 0
self.wheel_angle, self.target_id, self.crosstrack_error = self.tracker.stanley_control(self.x, self.y, self.yaw, self.velocity, self.wheel_angle)
self.x, self.y, self.yaw, self.velocity, _, _ = self.model.update(self.x, self.y, self.yaw, self.velocity, acceleration, self.wheel_angle)
if self.target_id == self.prev_target_id and self.iteration * self.delta_time > 0.25 * self.max_time or self.crosstrack_error > 5:
self.continue_animation = False
self.prev_target_id = self.target_id
self.iteration += 1
self.all_crosstrack_errors.append(self.crosstrack_error)
@dataclass
class Fargs:
ax: plt.Axes
sim: Simulation
path: Path
car: Car
car_outline: plt.Line2D
front_right_wheel: plt.Line2D
front_left_wheel: plt.Line2D
rear_right_wheel: plt.Line2D
rear_left_wheel: plt.Line2D
rear_axle: plt.Line2D
annotation: plt.Annotation
target: plt.Line2D
def animate(frame, fargs):
ax = fargs.ax
sim = fargs.sim
path = fargs.path
car = fargs.car
car_outline = fargs.car_outline
front_right_wheel = fargs.front_right_wheel
front_left_wheel = fargs.front_left_wheel
rear_right_wheel = fargs.rear_right_wheel
rear_left_wheel = fargs.rear_left_wheel
rear_axle = fargs.rear_axle
annotation = fargs.annotation
target = fargs.target
# Camera tracks car
ax.set_xlim(car.x - sim.map_size_x, car.x + sim.map_size_x)
ax.set_ylim(car.y - sim.map_size_y, car.y + sim.map_size_y)
# Drive and draw car
car.drive()
outline_plot, fr_plot, rr_plot, fl_plot, rl_plot = car.plot_car()
car_outline.set_data(*outline_plot)
front_right_wheel.set_data(*fr_plot)
rear_right_wheel.set_data(*rr_plot)
front_left_wheel.set_data(*fl_plot)
rear_left_wheel.set_data(*rl_plot)
rear_axle.set_data(car.x, car.y)
# Show car's target
target.set_data(path.px[car.target_id], path.py[car.target_id])
# Annotate car's coordinate above car
annotation.set_text(f'{car.x:.1f}, {car.y:.1f}')
annotation.set_position((car.x, car.y + 5))
plt.title(f'{sim.dt*car.iteration:.2f}s', loc='right')
plt.xlabel(f'Speed: {car.velocity:.2f} m/s', loc='left')
# plt.savefig(f'image/visualisation_{frame:03}.png', dpi=300)
return car_outline, front_right_wheel, rear_right_wheel, front_left_wheel, rear_left_wheel, rear_axle, target,