def test_find_lane(self): xodr_parser = XodrParser("modules/runtime/tests/data/urban_road.xodr") params = ParameterServer() world = World(params) map_interface = MapInterface() map_interface.SetOpenDriveMap(xodr_parser.map) world.SetMap(map_interface) lane_sw = map_interface.FindLane(Point2d(46, 180)) assert lane_sw.lane_type == XodrLaneType.sidewalk lane_rl = map_interface.FindLane(Point2d(52, 130)) assert lane_rl.lane_type == XodrLaneType.driving lane_no_lane = map_interface.FindLane(Point2d(120, 140)) assert lane_no_lane == None xodr_parser = XodrParser( "modules/runtime/tests/data/city_highway_straight.xodr") np.set_printoptions(precision=8) params = ParameterServer() world = World(params) map_interface = MapInterface() map_interface.SetOpenDriveMap(xodr_parser.map) world.SetMap(map_interface) point = Point2d(5114, 5072) viewer = MPViewer(params=params, use_world_bounds=True) viewer.drawWorld(world) viewer.drawPoint2d(point, 'red', 1.0) viewer.show(block=True) time.sleep(0.1) lane_sw = map_interface.FindLane(point) self.assertIsNotNone(lane_sw, "This point is clearly on a lane!")
def test_one_agent_at_goal_state_limits(self): param_server = ParameterServer() # Model Definition behavior_model = BehaviorConstantVelocity(param_server) execution_model = ExecutionModelInterpolate(param_server) dynamic_model = SingleTrackModel(param_server) # Agent Definition agent_2d_shape = CarLimousine() init_state = np.array( [0, -191.789, -50.1725, 3.14 * 3.0 / 4.0, 150 / 3.6]) agent_params = param_server.AddChild("agent1") goal_polygon = Polygon2d( [0, 0, 0], [Point2d(-1, -1), Point2d(-1, 1), Point2d(1, 1), Point2d(1, -1)]) goal_polygon = goal_polygon.Translate(Point2d(-191.789, -50.1725)) agent = Agent( init_state, behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, GoalDefinitionStateLimits( goal_polygon, (3.14 * 3.0 / 4.0 - 0.08, 3.14 * 3.0 / 4.0 + 0.08)), None) world = World(param_server) world.AddAgent(agent) evaluator = EvaluatorGoalReached(agent.id) world.AddEvaluator("success", evaluator) info = world.Evaluate() self.assertEqual(info["success"], True)
def test_lane_change(self): # World Definition params = ParameterServer() world = World(params) # Model Definitions behavior_model = BehaviorMobil(params) execution_model = ExecutionModelInterpolate(params) dynamic_model = SingleTrackModel(params) behavior_model2 = BehaviorIDMLaneTracking(params) execution_model2 = ExecutionModelInterpolate(params) dynamic_model2 = SingleTrackModel(params) # Map Definition map_interface = MapInterface() xodr_map = MakeXodrMapOneRoadTwoLanes() map_interface.SetOpenDriveMap(xodr_map) world.SetMap(map_interface) #agent_2d_shape = CarLimousine() agent_2d_shape = CarRectangle() init_state = np.array([0, 3, -1.75, 0, 5]) agent_params = params.AddChild("agent1") goal_polygon = Polygon2d( [1, 1, 0], [Point2d(0, 0), Point2d(0, 2), Point2d(2, 2), Point2d(2, 0)]) goal_polygon = goal_polygon.Translate(Point2d(50, -2)) agent = Agent(init_state, behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, GoalDefinitionPolygon(goal_polygon), map_interface) world.AddAgent(agent) init_state2 = np.array([0, 15, -1.75, 0, 2]) agent2 = Agent(init_state2, behavior_model2, dynamic_model2, execution_model2, agent_2d_shape, agent_params, GoalDefinitionPolygon(goal_polygon), map_interface) world.AddAgent(agent2) # viewer viewer = MPViewer(params=params, use_world_bounds=True) # World Simulation sim_step_time = params["simulation"]["step_time", "Step-time in simulation", 0.05] sim_real_time_factor = params["simulation"][ "real_time_factor", "execution in real-time or faster", 100] # Draw map for _ in range(0, 10): viewer.clear() world.Step(sim_step_time) viewer.drawWorld(world) viewer.show(block=False) time.sleep(sim_step_time / sim_real_time_factor)
def goal_definition_from_track(track, end): states = list(dict_utils.get_item_iterator(track.motion_states)) motion_state = states[-1][1] bark_state = bark_state_from_motion_state(motion_state) goal_polygon = Polygon2d( np.array([0.0, 0.0, 0.0]), [Point2d(-1.5, 0), Point2d(-1.5, 8), Point2d(1.5, 8), Point2d(1.5, 0)]) goal_polygon = goal_polygon.Translate( Point2d(bark_state[0, int(StateDefinition.X_POSITION)], bark_state[0, int(StateDefinition.Y_POSITION)])) goal_definition = GoalDefinitionPolygon(goal_polygon) return goal_definition
def create_cpp_plan_view(self, plan_view, header): new_plan_view = PlanView() # create plan view.. for geometry in plan_view["geometries"]: starting_point = Point2d(float(geometry["x"]), float(geometry["y"])) if geometry["geometry"]["type"] == "line": new_plan_view.AddLine(starting_point, float(geometry["hdg"]), float(geometry["length"])) if geometry["geometry"]["type"] == "arc": new_plan_view.AddArc(starting_point, float(geometry["hdg"]), float(geometry["length"]), float(geometry["geometry"]["curvature"]), 0.25) # TODO: s_inc if geometry["geometry"]["type"] == "spiral": new_plan_view.AddSpiral( starting_point, float(geometry["hdg"]), float(geometry["length"]), float(geometry["geometry"]["curv_start"]), float(geometry["geometry"]["curv_end"]), 2) # TODO: s_inc # now use header/ offset to modify plan view if "offset" in header: off_x = header["offset"]["x"] off_y = header["offset"]["y"] off_hdg = header["offset"]["hdg"] logger.info("Transforming PlanView with given offset", header["offset"]) new_plan_view.ApplyOffsetTransform(off_x, off_y, off_hdg) return new_plan_view
def test_road(self): newXodrRoad = XodrRoad() newXodrRoad.id = 1 newXodrRoad.name = "Autobahn A9" newPlanView = PlanView() newPlanView.AddLine(Point2d(0, 0), 1.57079632679, 10) newXodrRoad.plan_view = newPlanView line = newXodrRoad.plan_view.GetReferenceLine().ToArray() # Spiral p = Point2d(line[-1][0], line[-1][1]) newXodrRoad.plan_view.AddSpiral(p, 1.57079632679, 50.0, 0.0, 0.3, 0.4) line = newXodrRoad.plan_view.GetReferenceLine().ToArray()
def test_write_params_agent(self): params = ParameterServer() behavior = BehaviorConstantVelocity(params) execution = ExecutionModelInterpolate(params) dynamic = SingleTrackModel(params) shape = Polygon2d([1.25, 1, 0], [ Point2d(0, 0), Point2d(0, 2), Point2d(4, 2), Point2d(4, 0), Point2d(0, 0) ]) init_state = np.zeros(4) agent = Agent(init_state, behavior, dynamic, execution, shape, params.AddChild("agent")) params.Save("written_agents_param_test.json")
def test_road(self): newRoad = Road() newRoad.id = 1 newRoad.name = "Autobahn A9" newPlanView = PlanView() newPlanView.add_line(Point2d(0, 0), 1.57079632679, 10) newRoad.plan_view = newPlanView line = newRoad.plan_view.get_reference_line().toArray() # Spiral p = Point2d(line[-1][0], line[-1][1]) newRoad.plan_view.add_spiral(p, 1.57079632679, 50.0, 0.0, 0.3, 0.4) line = newRoad.plan_view.get_reference_line().toArray()
def test_between_lanes(self): xodr_parser = XodrParser( "modules/runtime/tests/data/city_highway_straight.xodr") np.set_printoptions(precision=8) params = ParameterServer() world = World(params) map_interface = MapInterface() map_interface.SetOpenDriveMap(xodr_parser.map) world.SetMap(map_interface) # Simple test point_close = Point2d(5114.68262, 5086.44971) lane_sw = map_interface.FindLane(point_close) self.assertIsNotNone( lane_sw, "This point is still in the left lane! XodrLane boundary is 5114.683" ) switched_lane = False lng_coord = 5086.44971 i = 5114.5 lane_sw = map_interface.FindLane(Point2d(i, lng_coord)) assert lane_sw != None prev = lane_sw.lane_id prev_i = i while (i < 5117.5): lane_sw = map_interface.FindLane(Point2d(i, lng_coord)) self.assertIsNotNone( lane_sw, "Should always be on at least one lane! Currently at ({}, {})". format(i, lng_coord)) if prev != lane_sw.lane_id: # print(prev) # print(prev_i) # print(lane_sw.lane_id) # print(i) self.assertFalse(switched_lane, "XodrLane switch should only happens once!") switched_lane = True prev_i = i prev = lane_sw.lane_id i = i + 0.01 self.assertTrue(switched_lane, "Eventually should have switched lanes!")
def test_one_agent_at_goal_sequential(self): param_server = ParameterServer() # Model Definition dynamic_model = SingleTrackModel(param_server) behavior_model = BehaviorMPContinuousActions(param_server) idx = behavior_model.AddMotionPrimitive(np.array([1, 0])) behavior_model.ActionToBehavior(idx) execution_model = ExecutionModelInterpolate(param_server) # Agent Definition agent_2d_shape = CarLimousine() init_state = np.array([0, 0, 0, 0, 0]) agent_params = param_server.AddChild("agent1") goal_frame = Polygon2d([0, 0, 0], [Point2d(-1,-1), Point2d(-1,1), Point2d(1,1), Point2d(1,-1)]) goal_polygon1 = goal_frame.Translate(Point2d(10, 0)) goal_polygon2 = goal_frame.Translate(Point2d(20, 0)) goal_polygon3 = goal_frame.Translate(Point2d(30, 0)) goal_def1 = GoalDefinitionStateLimits(goal_polygon1, [-0.08, 0.08]) goal_def2 = GoalDefinitionStateLimits(goal_polygon2, [-0.08, 0.08]) goal_def3 = GoalDefinitionStateLimits(goal_polygon3, [-0.08, 0.08]) goal_definition = GoalDefinitionSequential([goal_def1, goal_def2, goal_def3]) self.assertEqual(len(goal_definition.sequential_goals),3) agent = Agent(init_state, behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, None) world = World(param_server) world.AddAgent(agent) evaluator = EvaluatorGoalReached(agent.id) world.AddEvaluator("success", evaluator) # just drive with the single motion primitive should be successful for _ in range(0,1000): world.Step(0.2) info = world.Evaluate() if info["success"]: break self.assertEqual(info["success"], True) self.assertAlmostEqual(agent.state[int(StateDefinition.X_POSITION)], 30, delta=0.5)
def test_draw_agents(self): params = ParameterServer() behavior = BehaviorConstantVelocity(params) execution = ExecutionModelInterpolate(params) dynamic = SingleTrackModel(params) shape = Polygon2d([1.25, 1, 0], [ Point2d(0, 0), Point2d(0, 2), Point2d(4, 2), Point2d(4, 0), Point2d(0, 0) ]) shape2 = CarLimousine() init_state = [0, 3, 2, 1] init_state2 = [0, 0, 5, 4] agent = Agent(init_state, behavior, dynamic, execution, shape, params.AddChild("agent")) agent2 = Agent(init_state2, behavior, dynamic, execution, shape2, params.AddChild("agent"))
def test_Crossing8Course(self): xodr_parser = XodrParser( "modules/runtime/tests/data/Crossing8Course.xodr") params = ParameterServer() world = World(params) map_interface = MapInterface() map_interface.SetOpenDriveMap(xodr_parser.map) world.SetMap(map_interface) start_point = Point2d(0, -11) lanes_near_start = map_interface.find_nearest_lanes(start_point, 1) assert (len(lanes_near_start) == 1) goal_point = Point2d(-191.789, -50.1725) lanes_near_goal = map_interface.find_nearest_lanes(goal_point, 1) assert (len(lanes_near_goal) == 1) time.sleep( 2 ) # if this is not here, the second unit test is not executed (maybe parsing takes too long?)
def __find_first_ts_on_map__(self, id_ego): traj = trajectory_from_track(self._track_dict[id_ego]) for state in traj: point_agent = Point2d(state[1], state[2]) lane_list = self._map_interface.find_nearest_lanes(point_agent, 3) for lane in lane_list: polygon = self._map_interface.GetRoadgraph( ).GetLanePolygonForLaneId(lane.lane_id) if Collide(polygon, point_agent): time_ego_first = state[0] * 1e3 # use timestamp in ms return time_ego_first return None
def test_line(self): pv = PlanView() # Line pv.AddLine(Point2d(0, 0), 1.57079632679, 10) line = pv.GetReferenceLine().ToArray() # Spiral p = Point2d(line[-1][0], line[-1][1]) pv.AddSpiral(p, 1.57079632679, 50.0, 0.0, 0.3, 0.4) line = pv.GetReferenceLine().ToArray() offset = XodrLaneOffset(1.5, 0, 0, 0) lane_width = XodrLaneWidth(0.0, 59.9, offset) lane = XodrLane.CreateLaneFromLaneWidth(-1, pv.GetReferenceLine(), lane_width, 0.5) print(lane) lane = XodrLane.CreateLaneFromLaneWidth(1, pv.GetReferenceLine(), lane_width, 0.5) print(lane)
def test_line(self): pv = PlanView() # Line pv.add_line(Point2d(0, 0), 1.57079632679, 10) line = pv.get_reference_line().toArray() # Spiral p = Point2d(line[-1][0], line[-1][1]) pv.add_spiral(p, 1.57079632679, 50.0, 0.0, 0.3, 0.4) line = pv.get_reference_line().toArray() offset = LaneOffset(1.5, 0, 0, 0) lane_width = LaneWidth(0.0, 59.9, offset) lane = Lane.create_lane_from_lane_width(-1, pv.get_reference_line(), lane_width, 0.5) print(lane) lane = Lane.create_lane_from_lane_width( 1, pv.get_reference_line(), lane_width, 0.5) print(lane)
def test_two_roads_one_lane(self): params = ParameterServer() world = World(params) xodr_map = MakeXodrMapOneRoadTwoLanes() map_interface = MapInterface() map_interface.SetOpenDriveMap(xodr_map) world.SetMap(map_interface) start_point = Point2d(0, -11) lanes_near_start = map_interface.find_nearest_lanes(start_point, 1) assert(len(lanes_near_start) == 1) goal_point = Point2d(-191.789, -50.1725) lanes_near_goal = map_interface.find_nearest_lanes(goal_point, 1) assert(len(lanes_near_goal) == 1) viewer = MPViewer(params=params, use_world_bounds=True) viewer.drawWorld(world) time.sleep(2) # if this is not here, the second unit test is not executed (maybe parsing takes too long?)
def test_world(self): # create agent params = ParameterServer() behavior = BehaviorConstantVelocity(params) execution = ExecutionModelInterpolate(params) dynamic = SingleTrackModel(params) shape = Polygon2d([1.25, 1, 0], [ Point2d(0, 0), Point2d(0, 2), Point2d(4, 2), Point2d(4, 0), Point2d(0, 0) ]) init_state = np.array([0, 0, 0, 0, 5]) agent = Agent(init_state, behavior, dynamic, execution, shape, params.AddChild("agent")) road_map = OpenDriveMap() newXodrRoad = XodrRoad() newXodrRoad.id = 1 newXodrRoad.name = "Autobahn A9" newPlanView = PlanView() newPlanView.AddLine(Point2d(0, 0), 1.57079632679, 10) newXodrRoad.plan_view = newPlanView line = newXodrRoad.plan_view.GetReferenceLine().ToArray() p = Point2d(line[-1][0], line[-1][1]) newXodrRoad.plan_view.AddSpiral(p, 1.57079632679, 50.0, 0.0, 0.3, 0.4) line = newXodrRoad.plan_view.GetReferenceLine() lane_section = XodrLaneSection(0) lane = XodrLane() lane.line = line lane_section.AddLane(lane) newXodrRoad.AddLaneSection(lane_section) road_map.AddRoad(newXodrRoad) r = Roadgraph() map_interface = MapInterface() map_interface.SetOpenDriveMap(road_map) map_interface.SetRoadgraph(r) world = World(params) world.AddAgent(agent)
behavior_model2 = BehaviorConstantVelocity(param_server) execution_model2 = ExecutionModelInterpolate(param_server) dynamic_model2 = SingleTrackModel(param_server) # Map Definition xodr_parser = XodrParser("modules/runtime/tests/data/Crossing8Course.xodr") map_interface = MapInterface() map_interface.SetOpenDriveMap(xodr_parser.map) world.SetMap(map_interface) # Agent Definition agent_2d_shape = CarLimousine() init_state = np.array([0, -15, -13, 3.14 * 3.0 / 4.0, 50 / 3.6]) goal_polygon = Polygon2d( [0, 0, 0], [Point2d(-1, -1), Point2d(-1, 1), Point2d(1, 1), Point2d(1, -1)]) goal_polygon = goal_polygon.Translate(Point2d(-63, -61)) agent_params = param_server.addChild("agent1") agent1 = Agent(init_state, behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, GoalDefinitionPolygon(goal_polygon), map_interface) world.AddAgent(agent1) agent_2d_shape2 = CarLimousine() init_state2 = np.array([0, -15, -13, 3.14 * 3.0 / 4.0, 5.2]) agent_params2 = param_server.addChild("agent2") agent2 = Agent(init_state2, behavior_model2, dynamic_model2, execution_model2, agent_2d_shape2, agent_params2,
def test_one_agent_at_goal_state_limits_frenet(self): param_server = ParameterServer() # Model Definition behavior_model = BehaviorConstantVelocity(param_server) execution_model = ExecutionModelInterpolate(param_server) dynamic_model = SingleTrackModel(param_server) # Agent Definition agent_2d_shape = CarLimousine() agent_params = param_server.AddChild("agent1") center_line = Line2d() center_line.AddPoint(Point2d(5.0, 5.0)) center_line.AddPoint(Point2d(10.0, 10.0)) center_line.AddPoint(Point2d(20.0, 10.0)) max_lateral_dist = (0.4, 1) max_orientation_diff = (0.08, 0.1) velocity_range = (20.0, 25.0) goal_definition = GoalDefinitionStateLimitsFrenet( center_line, max_lateral_dist, max_orientation_diff, velocity_range) # not at goal x,y, others yes agent1 = Agent(np.array([0, 6, 8, 3.14 / 4.0, velocity_range[0]]), behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, None) # at goal x,y and others agent2 = Agent(np.array([0, 5.0, 5.5, 3.14 / 4.0, velocity_range[1]]), behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, None) # not at goal x,y,v yes but not orientation agent3 = Agent( np.array( [0, 5, 5.5, 3.14 / 4.0 + max_orientation_diff[1] + 0.001, 20]), behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, None) # not at goal x,y, orientation but not v agent4 = Agent( np.array([ 0, 5, 4.5, 3.14 / 4 - max_orientation_diff[0], velocity_range[0] - 0.01 ]), behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, None) # at goal x,y, at lateral limit agent5 = Agent( np.array([ 0, 15, 10 - max_lateral_dist[0] + 0.05, 0, velocity_range[1] ]), behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, None) # not at goal x,y slightly out of lateral limit agent6 = Agent( np.array([ 0, 15, 10 + max_lateral_dist[0] + 0.05, 3.14 / 4 + max_orientation_diff[0], velocity_range[0] ]), behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, None) # not at goal x,y,v yes but not orientation agent7 = Agent( np.array( [0, 5, 5.5, 3.14 / 4.0 - max_orientation_diff[0] - 0.001, 20]), behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, None) world = World(param_server) world.AddAgent(agent1) world.AddAgent(agent2) world.AddAgent(agent3) world.AddAgent(agent4) world.AddAgent(agent5) world.AddAgent(agent6) world.AddAgent(agent7) evaluator1 = EvaluatorGoalReached(agent1.id) evaluator2 = EvaluatorGoalReached(agent2.id) evaluator3 = EvaluatorGoalReached(agent3.id) evaluator4 = EvaluatorGoalReached(agent4.id) evaluator5 = EvaluatorGoalReached(agent5.id) evaluator6 = EvaluatorGoalReached(agent6.id) evaluator7 = EvaluatorGoalReached(agent7.id) world.AddEvaluator("success1", evaluator1) world.AddEvaluator("success2", evaluator2) world.AddEvaluator("success3", evaluator3) world.AddEvaluator("success4", evaluator4) world.AddEvaluator("success5", evaluator5) world.AddEvaluator("success6", evaluator6) world.AddEvaluator("success7", evaluator7) info = world.Evaluate() self.assertEqual(info["success1"], False) self.assertEqual(info["success2"], True) self.assertEqual(info["success3"], False) self.assertEqual(info["success4"], False) self.assertEqual(info["success5"], True) self.assertEqual(info["success6"], False) self.assertEqual(info["success7"], False)
# # plot plan_view # road_id = roadgraph.GetRoadForLaneId(lane_id) # road = map_interface.GetOpenDriveMap().GetRoad(road_id) # plan_view_reference = road.plan_view.GetReferenceLine() # # plot polygon with center line # viewer.drawWorld(world) # color = list(np.random.choice(range(256), size=3)/256) # viewer.drawPolygon2d(lane_polygon, color, 1.0) # viewer.drawLine2d(plan_view_reference, color="red") # viewer.saveFig(output_dir + "/" + "roadgraph_laneid_" + str(lane_id) + ".png") # viewer.show() # viewer.clear() comb_all = [] start_point = [Point2d(-115+1117, -158+1107)] end_point_list = [Point2d(27+1117, -158+1107)] comb = list(itertools.product(start_point, end_point_list)) comb_all = comb_all + comb # OpenDrive8 # comb_all = [] # start_point = [Point2d(1003, 1007)] # end_point_list = [Point2d(892, 1008)] # comb = list(itertools.product(start_point, end_point_list)) # comb_all = comb_all + comb # starting on the left # three_way_plain # comb_all = []
# plot plan_view road_id = roadgraph.GetRoadForLaneId(lane_id) road = map_interface.GetOpenDriveMap().GetRoad(road_id) plan_view_reference = road.plan_view.GetReferenceLine() # plot polygon with center line viewer.drawWorld(world) color = list(np.random.choice(range(256), size=3) / 256) viewer.drawPolygon2d(lane_polygon, color, 1.0) viewer.drawLine2d(plan_view_reference, color="red") viewer.saveFig(output_dir + "/" + "roadgraph_laneid_" + str(lane_id) + ".png") viewer.show() viewer.clear() comb_all = [] start_point = [Point2d(1004, 1003), Point2d(1004, 1006)] end_point_list = [Point2d(886, 1008)] comb = list(itertools.product(start_point, end_point_list)) comb_all = comb_all + comb # OpenDrive8 # comb_all = [] # start_point = [Point2d(1003, 1007)] # end_point_list = [Point2d(892, 1008)] # comb = list(itertools.product(start_point, end_point_list)) # comb_all = comb_all + comb # starting on the left # three_way_plain # comb_all = [] # start_point = [Point2d(-30, -2)]
def test_python_behavior_model(self): # World Definition scenario_param_file = "macro_actions_test.json" # must be within examples params folder params = ParameterServer(filename=os.path.join( "modules/world/tests/params/", scenario_param_file)) world = World(params) # Define two behavior models one python one standard c++ model behavior_model = PythonDistanceBehavior(params) execution_model = ExecutionModelInterpolate(params) dynamic_model = SingleTrackModel(params) behavior_model2 = BehaviorConstantVelocity(params) execution_model2 = ExecutionModelInterpolate(params) dynamic_model2 = SingleTrackModel(params) # Define the map interface and load a testing map map_interface = MapInterface() xodr_map = MakeXodrMapOneRoadTwoLanes() map_interface.SetOpenDriveMap(xodr_map) world.SetMap(map_interface) # Define the agent shapes agent_2d_shape = CarRectangle() init_state = np.array([0, 3, -5.25, 0, 20]) # Define the goal definition for agents center_line = Line2d() center_line.AddPoint(Point2d(0.0, -1.75)) center_line.AddPoint(Point2d(100.0, -1.75)) max_lateral_dist = (0.4, 0.5) max_orientation_diff = (0.08, 0.1) velocity_range = (5.0, 20.0) goal_definition = GoalDefinitionStateLimitsFrenet( center_line, max_lateral_dist, max_orientation_diff, velocity_range) # define two agents with the different behavior models agent_params = params.AddChild("agent1") agent = Agent(init_state, behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, map_interface) world.AddAgent(agent) init_state2 = np.array([0, 25, -5.25, 0, 15]) agent2 = Agent(init_state2, behavior_model2, dynamic_model2, execution_model2, agent_2d_shape, agent_params, goal_definition, map_interface) world.AddAgent(agent2) # viewer viewer = MPViewer(params=params, use_world_bounds=True) # World Simulation sim_step_time = params["simulation"]["step_time", "Step-time in simulation", 0.2] sim_real_time_factor = params["simulation"][ "real_time_factor", "execution in real-time or faster", 1] # Draw map video_renderer = VideoRenderer(renderer=viewer, world_step_time=sim_step_time) for _ in range(0, 20): world.Step(sim_step_time) viewer.clear() video_renderer.drawWorld(world) video_renderer.drawGoalDefinition(goal_definition, "red", 0.5, "red") time.sleep(sim_step_time / sim_real_time_factor) video_renderer.export_video(filename="./test_video_intermediate", remove_image_dir=True)
execution_model = ExecutionModelInterpolate(param_server) dynamic_model = SingleTrackModel(param_server) # Map Definition xodr_parser = XodrParser("modules/runtime/tests/data/Crossing8Course.xodr") map_interface = MapInterface() map_interface.SetOpenDriveMap(xodr_parser.map) world.SetMap(map_interface) # Agent Definition agent_2d_shape = CarLimousine() init_state = np.array([0, -15, -13, 3.14 * 5.0 / 4.0, 10 / 3.6]) agent_params = param_server.addChild("agent1") goal_polygon = Polygon2d( [0, 0, 0], [Point2d(-1, -1), Point2d(-1, 1), Point2d(1, 1), Point2d(1, -1)]) goal_polygon = goal_polygon.Translate(Point2d(-191.789, -50.1725)) agent = Agent( init_state, behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, GoalDefinitionPolygon(goal_polygon), # goal_lane_id map_interface) world.AddAgent(agent)
self.cosimulation_viewer.show() sim = Cosimulation() try: sim.launch_carla_server() sim.connect_carla_server() sim.spawn_npc_agents(10) # [TIME_POSITION, X_POSITION, Y_POSITION, THETA_POSITION, VEL_POSITION, ...] ego_initial = np.array([0, 90, -197, 0, 0]) goal_polygon = Polygon2d( [0, 0, 0], [Point2d(-1, -1), Point2d(-1, 1), Point2d(1, 1), Point2d(1, -1)]) goal_polygon = goal_polygon.Translate(Point2d(2, -300)) bp_lib = sim.carla_client.get_blueprint_library() bp = bp_lib.filter("vehicle.dodge_charger.police")[0] tf = sim.carla_client.generate_tranformation(x=ego_initial[1], y=ego_initial[2], z=0.3, pitch=0, yaw=ego_initial[3], roll=0) carla_ego_id = sim.carla_client.spawn_actor(bp, tf)
def test_uct_single_agent(self): try: from bark.models.behavior import BehaviorUCTSingleAgentMacroActions except: print("Rerun with --define planner_uct=true") return # World Definition scenario_param_file = "macro_actions_test.json" # must be within examples params folder params = ParameterServer(filename=os.path.join( "modules/world/tests/params/", scenario_param_file)) world = World(params) # Model Definitions behavior_model = BehaviorUCTSingleAgentMacroActions(params) execution_model = ExecutionModelInterpolate(params) dynamic_model = SingleTrackModel(params) behavior_model2 = BehaviorConstantVelocity(params) execution_model2 = ExecutionModelInterpolate(params) dynamic_model2 = SingleTrackModel(params) # Map Definition map_interface = MapInterface() xodr_map = MakeXodrMapOneRoadTwoLanes() map_interface.SetOpenDriveMap(xodr_map) world.SetMap(map_interface) # agent_2d_shape = CarLimousine() agent_2d_shape = CarRectangle() init_state = np.array([0, 3, -5.25, 0, 20]) agent_params = params.AddChild("agent1") # goal_polygon = Polygon2d( # [1, 1, 0], [Point2d(0, 0), Point2d(0, 2), Point2d(2, 2), Point2d(2, 0)]) # goal_definition = GoalDefinitionPolygon(goal_polygon) # goal_polygon = goal_polygon.Translate(Point2d(90, -2)) center_line = Line2d() center_line.AddPoint(Point2d(0.0, -1.75)) center_line.AddPoint(Point2d(100.0, -1.75)) max_lateral_dist = (0.4, 0.5) max_orientation_diff = (0.08, 0.1) velocity_range = (5.0, 20.0) goal_definition = GoalDefinitionStateLimitsFrenet( center_line, max_lateral_dist, max_orientation_diff, velocity_range) agent = Agent(init_state, behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, goal_definition, map_interface) world.AddAgent(agent) init_state2 = np.array([0, 25, -5.25, 0, 0]) agent2 = Agent(init_state2, behavior_model2, dynamic_model2, execution_model2, agent_2d_shape, agent_params, goal_definition, map_interface) world.AddAgent(agent2) # viewer viewer = MPViewer(params=params, use_world_bounds=True) # World Simulation sim_step_time = params["simulation"]["step_time", "Step-time in simulation", 0.2] sim_real_time_factor = params["simulation"][ "real_time_factor", "execution in real-time or faster", 1] # Draw map video_renderer = VideoRenderer(renderer=viewer, world_step_time=sim_step_time) for _ in range(0, 5): world.Step(sim_step_time) viewer.clear() video_renderer.drawWorld(world) video_renderer.drawGoalDefinition(goal_definition) time.sleep(sim_step_time / sim_real_time_factor) video_renderer.export_video(filename="./test_video_intermediate", remove_image_dir=True)
def test_evaluator_drivable_area(self): # World Definition params = ParameterServer() world = World(params) # Model Definitions behavior_model = BehaviorConstantVelocity(params) execution_model = ExecutionModelInterpolate(params) dynamic_model = SingleTrackModel(params) # Map Definition map_interface = MapInterface() xodr_map = MakeXodrMapOneRoadTwoLanes() map_interface.SetOpenDriveMap(xodr_map) world.SetMap(map_interface) #open_drive_map = world.map.GetOpenDriveMap() #agent_2d_shape = CarLimousine() agent_2d_shape = Polygon2d( [1.25, 1, 0], [Point2d(-1, -1), Point2d(-1, 1), Point2d(3, 1), Point2d(3, -1)]) init_state = np.array([0, 3, -1.75, 0, 5]) agent_params = params.AddChild("agent1") goal_polygon = Polygon2d( [1, 1, 0], [Point2d(0, 0), Point2d(0, 2), Point2d(2, 2), Point2d(2, 0)]) goal_polygon = goal_polygon.Translate(Point2d(50, -2)) agent = Agent( init_state, behavior_model, dynamic_model, execution_model, agent_2d_shape, agent_params, GoalDefinitionPolygon(goal_polygon), # goal_lane_id map_interface) world.AddAgent(agent) evaluator = EvaluatorDrivableArea() world.AddEvaluator("drivable_area", evaluator) info = world.Evaluate() self.assertFalse(info["drivable_area"]) viewer = MPViewer(params=params, use_world_bounds=True) # Draw map viewer.drawGoalDefinition(goal_polygon, color=(1, 0, 0), alpha=0.5, facecolor=(1, 0, 0)) viewer.drawWorld(world) viewer.drawRoadCorridor(agent.road_corridor) viewer.show(block=False)
self.cosimulation_viewer.show() sim = Cosimulation() try: sim.launch_carla_server() sim.connect_carla_server() sim.spawn_npc_agents(1) # [TIME_POSITION, X_POSITION, Y_POSITION, THETA_POSITION, VEL_POSITION, ...] ego_initial = np.array([0, 200, 0, 0, 0]) goal_polygon = Polygon2d( [0, 0, 0], [Point2d(-2, -2), Point2d(-2, 2), Point2d(2, 2), Point2d(2, -2)]) goal_polygon = goal_polygon.Translate(Point2d(0, 0)) bp_lib = sim.carla_client.get_blueprint_library() bp = bp_lib.filter("vehicle.dodge_charger.police")[0] tf = sim.carla_client.generate_tranformation(x=ego_initial[1], y=ego_initial[2], z=0.3, pitch=0, yaw=math.degrees( ego_initial[3]), roll=0)