diff --git a/demo/demo3.py b/demo/demo3.py index 4500d5a003208005853b4d228217455a12374397..fcb73ea77b7f72fb6dad643de2fd3910c6847411 100644 --- a/demo/demo3.py +++ b/demo/demo3.py @@ -35,14 +35,14 @@ class State: v = 0.0 vehicle_mode: VehicleMode = VehicleMode.Normal lane_mode: LaneMode = LaneMode.Lane0 - type: LaneObjectMode = LaneObjectMode.Vehicle + type_mode: LaneObjectMode = LaneObjectMode.Vehicle - def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode, type: LaneObjectMode): + def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode, type_mode: LaneObjectMode): pass if __name__ == "__main__": - input_code_name = './demo/example_controller4.py' + input_code_name = './example_controller4.py' scenario = Scenario() car = CarAgent('car1', file_name=input_code_name) @@ -63,10 +63,10 @@ if __name__ == "__main__": [[30, 0, 0, 0.5],[30, 0, 0, 0.5]], ], [ - (VehicleMode.Normal, LaneMode.Lane1), - (VehicleMode.Normal, LaneMode.Lane1), - (VehicleMode.Normal, LaneMode.Lane0), - (VehicleMode.Normal, LaneMode.Lane1), + (VehicleMode.Normal, LaneMode.Lane1, LaneObjectMode.Vehicle), + (VehicleMode.Normal, LaneMode.Lane1, LaneObjectMode.Vehicle), + (VehicleMode.Normal, LaneMode.Lane0, LaneObjectMode.Vehicle), + (VehicleMode.Normal, LaneMode.Lane1, LaneObjectMode.Vehicle), ] ) traces = scenario.simulate(70) diff --git a/demo/demo8.py b/demo/demo8.py new file mode 100644 index 0000000000000000000000000000000000000000..ffd87b016c7929792c8bba3389910b04b6117918 --- /dev/null +++ b/demo/demo8.py @@ -0,0 +1,78 @@ +from dryvr_plus_plus.example.example_agent.car_agent import CarAgent, NPCAgent +from dryvr_plus_plus.example.example_agent.sign_agent import SignAgent +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.plotter.plotter2D import * +from dryvr_plus_plus.example.example_sensor.fake_sensor import FakeSensor3 + +import matplotlib.pyplot as plt +import plotly.graph_objects as go +import numpy as np +from enum import Enum, auto + +class LaneObjectMode(Enum): + Vehicle = auto() + Ped = auto() # Pedestrians + Sign = auto() # Signs, stop signs, merge, yield etc. + Signal = auto() # Traffic lights + Obstacle = auto() # Static (to road/lane) obstacles + +class VehicleMode(Enum): + Normal = auto() + SwitchLeft = auto() + SwitchRight = auto() + Brake = auto() + +class LaneMode(Enum): + Lane0 = auto() + Lane1 = auto() + Lane2 = auto() + +class State: + x = 0.0 + y = 0.0 + theta = 0.0 + v = 0.0 + vehicle_mode: VehicleMode = VehicleMode.Normal + lane_mode: LaneMode = LaneMode.Lane0 + type_mode: LaneObjectMode = LaneObjectMode.Vehicle + + def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode, type_mode: LaneObjectMode): + pass + + +if __name__ == "__main__": + input_code_name = './example_controller9.py' + scenario = Scenario() + + car = CarAgent('car1', file_name=input_code_name) + scenario.add_agent(car) + sign = SignAgent('stop_sign') + scenario.add_agent(sign) + tmp_map = SimpleMap3() + scenario.set_map(tmp_map) + scenario.set_init( + [ + [[0, 0, 0, 1.0],[0, 0, 0, 1.0]], + [[15, 0, 0, 0.5],[15, 0, 0, 0.5]], + ], + [ + (VehicleMode.Normal, LaneMode.Lane1, LaneObjectMode.Vehicle), + (VehicleMode.Normal, LaneMode.Lane1, LaneObjectMode.Sign), + ] + ) + traces = scenario.simulate(70) + # traces = scenario.verify(70) + + # fig = plt.figure(2) + # fig = plot_map(tmp_map, 'g', fig) + # fig = plot_reachtube_tree(traces, 'car1', 1, [2], 'b', fig) + # fig = plot_reachtube_tree(traces, 'car2', 1, [2], 'r', fig) + # fig = plot_reachtube_tree(traces, 'car3', 1, [2], 'r', fig) + # fig = plot_reachtube_tree(traces, 'car4', 1, [2], 'r', fig) + # plt.show() + + fig = go.Figure() + fig = plotly_simulation_anime(traces, tmp_map, fig) + fig.show() + diff --git a/demo/example_controller4.py b/demo/example_controller4.py index 5f7f8f5a1885648d2e7a100b19073961cb6c9450..8645d7d812ffefe3237801a8aeb563d5deb01deb 100644 --- a/demo/example_controller4.py +++ b/demo/example_controller4.py @@ -27,15 +27,15 @@ class State: v = 0.0 vehicle_mode: VehicleMode = VehicleMode.Normal lane_mode: LaneMode = LaneMode.Lane0 - type: LaneObjectMode = LaneObjectMode.Vehicle + type_mode: LaneObjectMode = LaneObjectMode.Vehicle - def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode, type: LaneObjectMode): + def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode, type_mode: LaneObjectMode): pass def controller(ego:State, others:List[State], lane_map): output = copy.deepcopy(ego) if ego.vehicle_mode == VehicleMode.Normal: - if any((other.x-ego.x > 3 and other.x-ego.x < 5 and ego.lane_mode == other.lane_mode) for other in others): + if any((other.x-ego.x > 3 and other.x-ego.x < 5 and ego.lane_mode == other.lane_mode and other.type_mode==LaneObjectMode.Vehicle) for other in others): if lane_map.has_left(ego.lane_mode): output.vehicle_mode = VehicleMode.SwitchLeft if any((other.x-ego.x > 3 and other.x-ego.x < 5 and ego.lane_mode == other.lane_mode) for other in others): diff --git a/demo/example_controller9.py b/demo/example_controller9.py new file mode 100644 index 0000000000000000000000000000000000000000..cda3ac6bcf4ef9b639a315bd30d86cc4ae476acd --- /dev/null +++ b/demo/example_controller9.py @@ -0,0 +1,49 @@ +from enum import Enum, auto +import copy +from typing import List + +class LaneObjectMode(Enum): + Vehicle = auto() + Ped = auto() # Pedestrians + Sign = auto() # Signs, stop signs, merge, yield etc. + Signal = auto() # Traffic lights + Obstacle = auto() # Static (to road/lane) obstacles + +class VehicleMode(Enum): + Normal = auto() + SwitchLeft = auto() + SwitchRight = auto() + Brake = auto() + Stop = auto() + +class LaneMode(Enum): + Lane0 = auto() + Lane1 = auto() + Lane2 = auto() + +class State: + x = 0.0 + y = 0.0 + theta = 0.0 + v = 0.0 + clock = 0.0 + vehicle_mode: VehicleMode = VehicleMode.Normal + lane_mode: LaneMode = LaneMode.Lane0 + type_mode: LaneObjectMode = LaneObjectMode.Vehicle + + def __init__(self, x, y, theta, v, vehicle_mode: VehicleMode, lane_mode: LaneMode, type_mode: LaneObjectMode): + pass + +def controller(ego:State, others:List[State], lane_map): + output = copy.deepcopy(ego) + # Detect the stop sign + if ego.vehicle_mode == VehicleMode.Normal: + if any(ego.x - other.x < 5 and ego.x - other.x > -1 and other.type_mode==LaneObjectMode.Sign for other in others): + output.vehicle_mode = VehicleMode.Brake + output.clock = 0 + if ego.vehicle_mode == VehicleMode.Brake: + if ego.v <= 0: + output.vehicle_mode = VehicleMode.Stop + output.v = 0 + return output + diff --git a/dryvr_plus_plus/scene_verifier/code_parser/pythonparser.py b/dryvr_plus_plus/scene_verifier/code_parser/pythonparser.py index 3e5370ffbdb5922c880c29c6b4c009c722856e17..780c4bd2a2a06f5a730d6774de9ac82b3080e91f 100644 --- a/dryvr_plus_plus/scene_verifier/code_parser/pythonparser.py +++ b/dryvr_plus_plus/scene_verifier/code_parser/pythonparser.py @@ -249,7 +249,7 @@ class ControllerAst(): self.code = code self.tree = ast.parse(code) - self.statementtree, self.variables, self.modes, self.discrete_variables, self.state_object_dict, self.vars_dict, self.type_vars = self.initalwalktree(code, self.tree) + self.statementtree, self.variables, self.modes, self.discrete_variables, self.state_object_dict, self.vars_dict = self.initalwalktree(code, self.tree) self.vertices = [] self.vertexStrings = [] for vertex in itertools.product(*self.modes.values()): @@ -367,7 +367,6 @@ class ControllerAst(): def initalwalktree(self, code, tree): vars = [] discrete_vars = [] - type_vars = [] out = [] mode_dict = {} state_object_dict = {} @@ -390,9 +389,7 @@ class ControllerAst(): if "init" in item.name: for arg in item.args.args: if "self" not in arg.arg: - if "type" == arg.arg: - state_object_dict[node.name]["type"].append(arg.arg) - elif "mode" not in arg.arg: + if "mode" not in arg.arg: state_object_dict[node.name]['cont'].append(arg.arg) # vars.append(arg.arg) else: @@ -431,11 +428,8 @@ class ControllerAst(): for var in state_object_dict[arg_annotation]['disc']: discrete_vars.append(arg_name+"."+var) vars_dict[arg_name]['disc'].append(var) - for var in state_object_dict[arg_annotation]['type']: - type_vars.append(arg_name+"."+var) - vars_dict[arg_name]['type'].append(var) - - return [statementtree, vars, mode_dict, discrete_vars, state_object_dict, vars_dict, type_vars] + + return [statementtree, vars, mode_dict, discrete_vars, state_object_dict, vars_dict] ''' @@ -495,7 +489,6 @@ class EmptyAst(ControllerAst): 'type':[] } } - self.type_vars = [] self.variables = [] self.vars_dict = [] self.vertexStrings = ['Null,Normal']