-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
82 lines (64 loc) · 2.23 KB
/
main.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
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from helpers import (
detect_vehicles,
obstacles_to_world,
bounding_ellipse,
path_to_pix,
plot_path_world,
plot_path_pix,
plot_timeseries,
generate_result_directory,
ego_center,
)
from mpc import simulate
### Parameters ###
image_name = "image3"
pos_goal = np.array([4.7, 45.6])
initial_heading = np.pi / 2
def main():
# Read in image
image = cv2.imread("./data/" + image_name + ".png")
# Find starting coordinate
ego_vehicle_x, ego_vehicle_y = ego_center(image)
# Extract list of obstacles in image in pixel space
obstacles_pixel_space = detect_vehicles(image)
# Convert obstacles to world space
ppm = 30 / 4
obstacles_world_space = obstacles_to_world(
obstacles=obstacles_pixel_space,
ppm=ppm,
x0=ego_vehicle_x,
y0=ego_vehicle_y,
)
# Compute general form bounding ellipses
ellipse_coefs = bounding_ellipse(obstacles_world_space)
# Set initial parameters
x0 = [0, 0, initial_heading, 0]
parameters = np.concatenate((x0, pos_goal, np.array([0])))
# Simulate vehicle trajectory under obstacle-aware NMPC policy
xlog, ulog, tlog = simulate(ellipse_coefs, parameters)
# Convert path to pixel coordinates
path_log_pix = path_to_pix(xlog[:, :2], ppm, ego_vehicle_x, ego_vehicle_y)
pos_goal_pix = path_to_pix(pos_goal, ppm, ego_vehicle_x, ego_vehicle_y)
# Plot the results in world space
world_plot = plot_path_world(xlog, pos_goal, ellipse_coefs)
# Plot the path in the pixel space on the image
bev_plot = plot_path_pix(image, path_log_pix, pos_goal_pix)
# Plot control signals
state_control_plot = plot_timeseries(tlog, xlog, ulog)
# Save plots
result_subdir = generate_result_directory(image_name)
os.mkdir("./results/" + result_subdir)
plt.figure(world_plot)
plt.savefig(os.path.join("results", result_subdir, "world.png"))
plt.figure(bev_plot)
plt.savefig(os.path.join("results", result_subdir, "bev.png"))
plt.figure(state_control_plot)
plt.savefig(os.path.join("results", result_subdir, "state_control.png"))
# Show all plots
plt.show()
if __name__ == "__main__":
main()