Skip to content
Snippets Groups Projects
Commit a7bdfd6f authored by keyis2's avatar keyis2
Browse files

plotly-based visualization

parent f7e6c2b2
No related branches found
No related tags found
No related merge requests found
Showing
with 642 additions and 54 deletions
...@@ -8,17 +8,20 @@ import matplotlib.pyplot as plt ...@@ -8,17 +8,20 @@ import matplotlib.pyplot as plt
import numpy as np import numpy as np
from enum import Enum, auto from enum import Enum, auto
class VehicleMode(Enum): class VehicleMode(Enum):
Normal = auto() Normal = auto()
SwitchLeft = auto() SwitchLeft = auto()
SwitchRight = auto() SwitchRight = auto()
Brake = auto() Brake = auto()
class LaneMode(Enum): class LaneMode(Enum):
Lane0 = auto() Lane0 = auto()
Lane1 = auto() Lane1 = auto()
Lane2 = auto() Lane2 = auto()
class State: class State:
x = 0.0 x = 0.0
y = 0.0 y = 0.0
...@@ -30,6 +33,7 @@ class State: ...@@ -30,6 +33,7 @@ class State:
def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode): def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode):
self.data = [] self.data = []
if __name__ == "__main__": if __name__ == "__main__":
input_code_name = 'example_controller1.py' input_code_name = 'example_controller1.py'
scenario = Scenario() scenario = Scenario()
...@@ -38,31 +42,38 @@ if __name__ == "__main__": ...@@ -38,31 +42,38 @@ if __name__ == "__main__":
scenario.add_agent(car) scenario.add_agent(car)
car = CarAgent('car2', file_name=input_code_name) car = CarAgent('car2', file_name=input_code_name)
scenario.add_agent(car) scenario.add_agent(car)
tmp_map = SimpleMap3() tmp_map = SimpleMap2()
scenario.set_map(tmp_map) scenario.set_map(tmp_map)
scenario.set_sensor(FakeSensor2()) scenario.set_sensor(FakeSensor2())
scenario.set_init( scenario.set_init(
[ [
[[10.0, 0, 0, 0.5],[10.0, 0, 0, 0.5]], [[10.0, 0, 0, 0.5], [10.0, 0, 0, 0.5]],
[[5.0, -0.2, 0, 2.0],[6.0, 0.2, 0, 3.0]], [[5.0, -0.2, 0, 2.0], [6.0, 0.2, 0, 3.0]],
], ],
[ [
(VehicleMode.Normal, LaneMode.Lane1), (VehicleMode.Normal, LaneMode.Lane1),
(VehicleMode.Normal, LaneMode.Lane1), (VehicleMode.Normal, LaneMode.Lane1),
] ]
) )
res_list = scenario.simulate_multi(10,1) res_list = scenario.simulate_multi(10, 1)
# traces = scenario.verify(10) # traces = scenario.verify(10)
fig = plt.figure(2) # fig = plt.figure(2)
# fig = plot_map(tmp_map, 'g', fig) # fig = plot_map(tmp_map, 'g', fig)
# fig = plot_reachtube_tree(traces, 'car1', 0, [1], 'b', fig, (1000,-1000), (1000,-1000)) # plt.show()
# fig = plot_reachtube_tree(traces, 'car2', 0, [1], 'r', fig) # # fig = plot_reachtube_tree(traces, 'car1', 0, [1], 'b', fig, (1000,-1000), (1000,-1000))
for traces in res_list: # # fig = plot_reachtube_tree(traces, 'car2', 0, [1], 'r', fig)
# fig = plot_simulation_tree(traces, 'car1', 0, [1], 'b', fig, (1000,-1000), (1000,-1000)) # # AnalysisTreeNode
# fig = plot_simulation_tree(traces, 'car2', 0, [1], 'r', fig) # for traces in res_list:
generate_simulation_anime(traces, tmp_map, fig) # # fig = plot_simulation_tree(
# # traces, 'car1', 0, [1], 'b', fig, (1000, -1000), (1000, -1000))
# fig = plot_simulation_tree(traces, 'car2', 1, [2], 'r', fig)
# # generate_simulation_anime(traces, tmp_map, fig)
# plt.show()
plt.show() fig = go.Figure()
for traces in res_list:
# plotly_map(tmp_map, 'g', fig)
fig = plotly_simulation_anime(traces, tmp_map, fig)
fig.show()
...@@ -3,22 +3,25 @@ from dryvr_plus_plus.scene_verifier.scenario.scenario import Scenario ...@@ -3,22 +3,25 @@ from dryvr_plus_plus.scene_verifier.scenario.scenario import Scenario
from dryvr_plus_plus.example.example_map.simple_map2 import SimpleMap2, SimpleMap3, SimpleMap5, SimpleMap6 from dryvr_plus_plus.example.example_map.simple_map2 import SimpleMap2, SimpleMap3, SimpleMap5, SimpleMap6
from dryvr_plus_plus.plotter.plotter2D import * from dryvr_plus_plus.plotter.plotter2D import *
from dryvr_plus_plus.example.example_sensor.fake_sensor import FakeSensor2 from dryvr_plus_plus.example.example_sensor.fake_sensor import FakeSensor2
import plotly.graph_objects as go
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
from enum import Enum, auto from enum import Enum, auto
class VehicleMode(Enum): class VehicleMode(Enum):
Normal = auto() Normal = auto()
SwitchLeft = auto() SwitchLeft = auto()
SwitchRight = auto() SwitchRight = auto()
Brake = auto() Brake = auto()
class LaneMode(Enum): class LaneMode(Enum):
Lane0 = auto() Lane0 = auto()
Lane1 = auto() Lane1 = auto()
Lane2 = auto() Lane2 = auto()
class State: class State:
x = 0.0 x = 0.0
y = 0.0 y = 0.0
...@@ -30,6 +33,7 @@ class State: ...@@ -30,6 +33,7 @@ class State:
def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode): def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode):
self.data = [] self.data = []
if __name__ == "__main__": if __name__ == "__main__":
input_code_name = 'example_controller2.py' input_code_name = 'example_controller2.py'
scenario = Scenario() scenario = Scenario()
...@@ -43,8 +47,8 @@ if __name__ == "__main__": ...@@ -43,8 +47,8 @@ if __name__ == "__main__":
scenario.set_sensor(FakeSensor2()) scenario.set_sensor(FakeSensor2())
scenario.set_init( scenario.set_init(
[ [
[[0, -0.2, 0, 1.0],[0.1, 0.2, 0, 1.0]], [[0, -0.2, 0, 1.0], [0.1, 0.2, 0, 1.0]],
[[10, 0, 0, 0.5],[10, 0, 0, 0.5]], [[10, 0, 0, 0.5], [10, 0, 0, 0.5]],
], ],
[ [
(VehicleMode.Normal, LaneMode.Lane1), (VehicleMode.Normal, LaneMode.Lane1),
...@@ -52,15 +56,22 @@ if __name__ == "__main__": ...@@ -52,15 +56,22 @@ if __name__ == "__main__":
] ]
) )
# res_list = scenario.simulate_multi(40,1) # res_list = scenario.simulate_multi(40,1)
traces = scenario.verify(40) # traces = scenario.verify(40)
traces = scenario.simulate(40)
fig = plt.figure(2) # fig = plt.figure(2)
fig = plot_map(tmp_map, 'g', fig) # fig = plot_map(tmp_map, 'g', fig)
fig = plot_reachtube_tree(traces, 'car1', 1, [2], 'b', fig) # # fig = plot_simulation_tree(traces, 'car1', 1, [2], 'b', fig)
fig = plot_reachtube_tree(traces, 'car2', 1, [2], 'r', fig) # # fig = plot_simulation_tree(traces, 'car2', 1, [2], 'r', fig)
# for traces in res_list: # # # fig = plot_reachtube_tree(traces, 'car1', 1, [2], 'b', fig)
# generate_simulation_anime(traces, tmp_map, fig) # # # fig = plot_reachtube_tree(traces, 'car2', 1, [2], 'r', fig)
# # fig,x_lim,y_lim = plot_simulation_tree(traces, 'car1', 1, [2], 'b', fig,x_lim,y_lim) # # plt.show()
# # fig,x_lim,y_lim = plot_simulation_tree(traces, 'car2', 1, [2], 'r', fig,x_lim,y_lim) # # # fig1 = plt.figure(2)
# fig = generate_simulation_anime(traces, tmp_map, fig)
# plt.show()
plt.show() fig = go.Figure()
# fig = plotly_simulation_tree(traces, 'car1', 1, [2], 'b', fig)
# fig.show()
fig = plotly_simulation_anime(traces, tmp_map, fig)
fig.show()
File moved
from typing import Tuple, List from typing import Tuple, List
import numpy as np import numpy as np
from scipy.integrate import ode from scipy.integrate import ode
from dryvr_plus_plus.scene_verifier.agents.base_agent import BaseAgent from dryvr_plus_plus.scene_verifier.agents.base_agent import BaseAgent
from dryvr_plus_plus.scene_verifier.map.lane_map import LaneMap from dryvr_plus_plus.scene_verifier.map.lane_map import LaneMap
from dryvr_plus_plus.scene_verifier.code_parser.pythonparser import EmptyAst from dryvr_plus_plus.scene_verifier.code_parser.pythonparser import EmptyAst
class NPCAgent(BaseAgent): class NPCAgent(BaseAgent):
def __init__(self, id): def __init__(self, id):
self.id = id self.id = id
...@@ -15,73 +16,77 @@ class NPCAgent(BaseAgent): ...@@ -15,73 +16,77 @@ class NPCAgent(BaseAgent):
@staticmethod @staticmethod
def dynamic(t, state, u): def dynamic(t, state, u):
x, y, theta, v = state x, y, theta, v = state
delta, a = u delta, a = u
x_dot = v*np.cos(theta+delta) x_dot = v*np.cos(theta+delta)
y_dot = v*np.sin(theta+delta) y_dot = v*np.sin(theta+delta)
theta_dot = v/1.75*np.sin(delta) theta_dot = v/1.75*np.sin(delta)
v_dot = a v_dot = a
return [x_dot, y_dot, theta_dot, v_dot] return [x_dot, y_dot, theta_dot, v_dot]
def action_handler(self, mode, state, lane_map:LaneMap)->Tuple[float, float]: def action_handler(self, mode, state, lane_map: LaneMap) -> Tuple[float, float]:
x,y,theta,v = state x, y, theta, v = state
vehicle_mode = mode[0] vehicle_mode = mode[0]
vehicle_lane = mode[1] vehicle_lane = mode[1]
vehicle_pos = np.array([x,y]) vehicle_pos = np.array([x, y])
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos) d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos)
psi = lane_map.get_lane_heading(vehicle_lane, vehicle_pos)-theta psi = lane_map.get_lane_heading(vehicle_lane, vehicle_pos)-theta
steering = psi + np.arctan2(0.45*d, v) steering = psi + np.arctan2(0.45*d, v)
steering = np.clip(steering, -0.61, 0.61) steering = np.clip(steering, -0.61, 0.61)
a = 0 a = 0
return steering, a return steering, a
def TC_simulate(self, mode: List[str], initialCondition, time_bound, lane_map:LaneMap=None)->np.ndarray: def TC_simulate(self, mode: List[str], initialCondition, time_bound, lane_map: LaneMap = None) -> np.ndarray:
time_step = 0.05 time_step = 0.05
time_bound = float(time_bound) time_bound = float(time_bound)
number_points = int(np.ceil(time_bound/time_step)) number_points = int(np.ceil(time_bound/time_step))
t = [i*time_step for i in range(0,number_points)] t = [i*time_step for i in range(0, number_points)]
init = initialCondition init = initialCondition
trace = [[0]+init] trace = [[0]+init]
for i in range(len(t)): for i in range(len(t)):
steering, a = self.action_handler(mode, init, lane_map) steering, a = self.action_handler(mode, init, lane_map)
r = ode(self.dynamic) r = ode(self.dynamic)
r.set_initial_value(init).set_f_params([steering, a]) r.set_initial_value(init).set_f_params([steering, a])
res:np.ndarray = r.integrate(r.t + time_step) res: np.ndarray = r.integrate(r.t + time_step)
init = res.flatten().tolist() init = res.flatten().tolist()
trace.append([t[i] + time_step] + init) trace.append([t[i] + time_step] + init)
return np.array(trace) return np.array(trace)
class CarAgent(BaseAgent): class CarAgent(BaseAgent):
def __init__(self, id, code = None, file_name = None): def __init__(self, id, code=None, file_name=None):
super().__init__(id, code, file_name) super().__init__(id, code, file_name)
@staticmethod @staticmethod
def dynamic(t, state, u): def dynamic(t, state, u):
x, y, theta, v = state x, y, theta, v = state
delta, a = u delta, a = u
x_dot = v*np.cos(theta+delta) x_dot = v*np.cos(theta+delta)
y_dot = v*np.sin(theta+delta) y_dot = v*np.sin(theta+delta)
theta_dot = v/1.75*np.sin(delta) theta_dot = v/1.75*np.sin(delta)
v_dot = a v_dot = a
return [x_dot, y_dot, theta_dot, v_dot] return [x_dot, y_dot, theta_dot, v_dot]
def action_handler(self, mode: List[str], state, lane_map:LaneMap)->Tuple[float, float]: def action_handler(self, mode: List[str], state, lane_map: LaneMap) -> Tuple[float, float]:
x,y,theta,v = state x, y, theta, v = state
vehicle_mode = mode[0] vehicle_mode = mode[0]
vehicle_lane = mode[1] vehicle_lane = mode[1]
vehicle_pos = np.array([x,y]) vehicle_pos = np.array([x, y])
a = 0 a = 0
if vehicle_mode == "Normal": if vehicle_mode == "Normal":
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos) d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos)
# # keyi: just toy mod
# if v <= 2:
# a = 0.2
elif vehicle_mode == "SwitchLeft": elif vehicle_mode == "SwitchLeft":
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos) + 3 d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos) + 3
elif vehicle_mode == "SwitchRight": elif vehicle_mode == "SwitchRight":
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos) - 3 d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos) - 3
elif vehicle_mode == "Brake": elif vehicle_mode == "Brake":
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos) d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos)
a = -1 a = -1
if v<=0.02: if v <= 0.02:
a = 0 a = 0
elif vehicle_mode == 'Stop': elif vehicle_mode == 'Stop':
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos) d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos)
...@@ -89,24 +94,25 @@ class CarAgent(BaseAgent): ...@@ -89,24 +94,25 @@ class CarAgent(BaseAgent):
psi = lane_map.get_lane_heading(vehicle_lane, vehicle_pos)-theta psi = lane_map.get_lane_heading(vehicle_lane, vehicle_pos)-theta
steering = psi + np.arctan2(0.45*d, v) steering = psi + np.arctan2(0.45*d, v)
steering = np.clip(steering, -0.61, 0.61) steering = np.clip(steering, -0.61, 0.61)
return steering, a return steering, a
def TC_simulate(self, mode: List[str], initialCondition, time_bound, lane_map:LaneMap=None)->np.ndarray: def TC_simulate(self, mode: List[str], initialCondition, time_bound, lane_map: LaneMap = None) -> np.ndarray:
time_step = 0.05 time_step = 0.05
time_bound = float(time_bound) time_bound = float(time_bound)
number_points = int(np.ceil(time_bound/time_step)) number_points = int(np.ceil(time_bound/time_step))
t = [i*time_step for i in range(0,number_points)] t = [i*time_step for i in range(0, number_points)]
init = initialCondition init = initialCondition
# [time, x, y, theta, v]
trace = [[0]+init] trace = [[0]+init]
for i in range(len(t)): for i in range(len(t)):
steering, a = self.action_handler(mode, init, lane_map) steering, a = self.action_handler(mode, init, lane_map)
r = ode(self.dynamic) r = ode(self.dynamic)
r.set_initial_value(init).set_f_params([steering, a]) r.set_initial_value(init).set_f_params([steering, a])
res:np.ndarray = r.integrate(r.t + time_step) res: np.ndarray = r.integrate(r.t + time_step)
init = res.flatten().tolist() init = res.flatten().tolist()
if init[3] < 0: if init[3] < 0:
init[3] = 0 init[3] = 0
trace.append([t[i] + time_step] + init) trace.append([t[i] + time_step] + init)
return np.array(trace) return np.array(trace)
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment