Multi-Purpose-MPC/simulation.py

107 lines
3.0 KiB
Python
Raw Normal View History

2019-11-24 05:27:57 +08:00
from map import Map
import numpy as np
from reference_path import ReferencePath
2019-11-29 16:36:30 +08:00
from spatial_bicycle_models import BicycleModel
2019-11-24 05:27:57 +08:00
import matplotlib.pyplot as plt
2019-11-29 16:36:30 +08:00
from MPC import MPC, MPC_OSQP
from scipy import sparse
import time
2019-11-24 05:27:57 +08:00
if __name__ == '__main__':
# Select Simulation Mode | 'Race' or 'Q'
sim_mode = 'Race'
2019-11-24 05:27:57 +08:00
# Create Map
if sim_mode == 'Race':
map = Map(file_path='map_race.png', origin=[-1, -2], resolution=0.005)
# Specify waypoints
wp_x = [-0.75, -0.25, -0.25, 0.25, 0.25, 1.25, 1.25, 0.75, 0.75, 1.25,
1.25, -0.75, -0.75, -0.25]
wp_y = [-1.5, -1.5, -0.5, -0.5, -1.5, -1.5, -1, -1, -0.5, -0.5, 0, 0,
-1.5, -1.5]
# Specify path resolution
path_resolution = 0.05 # m / wp
elif sim_mode == 'Q':
map = Map(file_path='map_floor2.png')
wp_x = [-9.169, 11.9, 7.3, -6.95]
wp_y = [-15.678, 10.9, 14.5, -3.31]
# Specify path resolution
path_resolution = 0.20 # m / wp
else:
print('Invalid Simulation Mode!')
map, wp_x, wp_y, path_resolution = None, None, None, None
exit(1)
# Create smoothed reference path
2019-11-24 05:27:57 +08:00
reference_path = ReferencePath(map, wp_x, wp_y, path_resolution,
smoothing_distance=5)
################
# Motion Model #
################
# Initial state
2019-11-24 05:27:57 +08:00
e_y_0 = 0.0
e_psi_0 = 0.0
2019-11-29 16:36:30 +08:00
t_0 = 0.0
v = 1.0
car = BicycleModel(reference_path=reference_path,
e_y=e_y_0, e_psi=e_psi_0, t=t_0)
2019-11-24 05:27:57 +08:00
##############
# Controller #
##############
2019-11-29 16:36:30 +08:00
N = 20
Q = sparse.diags([0.01, 0.0, 0.4])
R = sparse.diags([0.01])
QN = Q
InputConstraints = {'umin': np.array([-np.tan(0.44)/car.l]), 'umax': np.array([np.tan(0.44)/car.l])}
StateConstraints = {'xmin': np.array([-0.2, -np.inf, 0]), 'xmax': np.array([0.2, np.inf, np.inf])}
mpc = MPC_OSQP(car, N, Q, R, QN, StateConstraints, InputConstraints)
2019-11-24 05:27:57 +08:00
##############
# Simulation #
##############
# logging containers
x_log = [car.temporal_state.x]
y_log = [car.temporal_state.y]
# iterate over waypoints
2019-11-29 16:36:30 +08:00
for wp_id in range(len(car.reference_path.waypoints)-mpc.N-1):
2019-11-24 05:27:57 +08:00
# get control signals
2019-11-29 16:36:30 +08:00
start = time.time()
delta = mpc.get_control(v)
end = time.time()
u = np.array([v, delta])
2019-11-24 05:27:57 +08:00
# drive car
2019-11-29 16:36:30 +08:00
car.drive(u)
2019-11-24 05:27:57 +08:00
2019-11-29 16:36:30 +08:00
# log
2019-11-24 05:27:57 +08:00
x_log.append(car.temporal_state.x)
y_log.append(car.temporal_state.y)
###################
# Plot Simulation #
###################
# plot path
car.reference_path.show()
# plot car trajectory and velocity
2019-11-29 16:36:30 +08:00
plt.scatter(x_log[:-1], y_log[:-1], c='g', s=15)
2019-11-24 05:27:57 +08:00
2019-11-29 16:36:30 +08:00
plt.scatter(mpc.current_prediction[0], mpc.current_prediction[1], c='b', s=5)
2019-11-24 05:27:57 +08:00
2019-11-29 16:36:30 +08:00
plt.title('MPC Simulation: Position: {:.2f} m, {:.2f} m'.
format(car.temporal_state.x, car.temporal_state.y))
2019-11-24 05:27:57 +08:00
plt.xticks([])
plt.yticks([])
2019-11-29 16:36:30 +08:00
plt.pause(0.00000001)
2019-11-24 05:27:57 +08:00
plt.close()