def run_sim(street_1: DecalRoad, ai_aggression): waypoint_goal = BeamNGWaypoint('waypoint_goal', get_node_coords(street_1.nodes[-1])) maps.beamng_map.generated().write_items(street_1.to_json() + '\n' + waypoint_goal.to_json()) beamng = BeamNGpy('localhost', 64256) scenario = Scenario('tig', 'tigscenario') vehicle = Vehicle('ego_vehicle', model='etk800', licence='TIG', color='Red') sim_data_collector = TrainingDataCollectorAndWriter(vehicle, beamng, street_1) scenario.add_vehicle(vehicle, pos=get_node_coords(street_1.nodes[0]), rot=get_rotation(street_1)) scenario.make(beamng) beamng.open() beamng.set_deterministic() beamng.load_scenario(scenario) beamng.pause() beamng.start_scenario() vehicle.ai_set_aggression(ai_aggression) vehicle.ai_drive_in_lane(False) vehicle.ai_set_speed(25) vehicle.ai_set_waypoint(waypoint_goal.name) #vehicle.ai_set_mode("manual") steps = 5 def start(): for idx in range(1000): if (idx * 0.05 * steps) > 3.: sim_data_collector.collect_and_write_current_data() if sim_data_collector.oob_monitor.is_oob(wrt="center"): raise ValueError("OOB detected during training") dist = distance(sim_data_collector.last_state.pos, waypoint_goal.position) if dist < 15.0: beamng.resume() break # one step is 0.05 seconds (5/100) beamng.step(steps) try: start() finally: beamng.close()
def run_sim(street_1: DecalRoad): brewer = BeamNGBrewer(street_1.nodes) waypoint_goal = BeamNGWaypoint('waypoint_goal', get_node_coords(street_1.nodes[-1])) vehicle = brewer.setup_vehicle() camera = brewer.setup_scenario_camera() beamng = brewer.beamng brewer.setup_road_nodes(street_1.nodes) maps.beamng_map.generated().write_items(brewer.decal_road.to_json() + '\n' + waypoint_goal.to_json()) cameras = BeamNGCarCameras() brewer.vehicle_start_pose = brewer.road_points.vehicle_start_pose() #brewer.vehicle_start_pose = BeamNGPose() sim_data_collector = TrainingDataCollectorAndWriter(vehicle, beamng, street_1, cameras) brewer.bring_up() print('bring up ok') script = calculate_script(brewer.road_points.middle) # Trick: we start from the road center vehicle.ai_set_script(script[4:]) #vehicle.ai_drive_in_lane(True) beamng.pause() beamng.step(1) def start(): for idx in range(1000): if (idx * 0.05 * STEPS) > 3.: sim_data_collector.collect_and_write_current_data() dist = distance(sim_data_collector.last_state.pos, waypoint_goal.position) if dist < 15.0: beamng.resume() break # one step is 0.05 seconds (5/100) beamng.step(STEPS) try: start() finally: beamng.close()
def run_sim(nodes): print(nodes) brewer = BeamNGBrewer(road_nodes=nodes) beamng = brewer.beamng waypoint_goal = BeamNGWaypoint('waypoint_goal', get_node_coords(nodes[-1])) maps.install_map_if_needed() maps.beamng_map.generated().write_items(brewer.decal_road.to_json() + '\n' + waypoint_goal.to_json()) vehicle = brewer.setup_vehicle() brewer.vehicle_start_pose = brewer.road_points.vehicle_start_pose() #camera = brewer.setup_scenario_camera() street_1 = brewer.decal_road sim_data_collector = TrainingDataCollectorAndWriter( vehicle, beamng, street_1) brewer.bring_up() steps = 5 def start(): for idx in range(1000): if (idx * 0.05 * steps) > 3.: sim_data_collector.collect_and_write_current_data() dist = points_distance(sim_data_collector.last_state.pos, waypoint_goal.position) if dist < 15.0: beamng.resume() break # one step is 0.05 seconds (5/100) beamng.step(steps) try: start() finally: beamng.close()
def _run_simulation(self, nodes) -> SimulationData: if not self.brewer: self.brewer = BeamNGBrewer() self.vehicle = self.brewer.setup_vehicle() self.camera = self.brewer.setup_scenario_camera() brewer = self.brewer brewer.setup_road_nodes(nodes) beamng = brewer.beamng waypoint_goal = BeamNGWaypoint('waypoint_goal', get_node_coords(nodes[-1])) maps.install_map_if_needed() maps.beamng_map.generated().write_items(brewer.decal_road.to_json() + '\n' + waypoint_goal.to_json()) cameras = BeamNGCarCameras() vehicle_state_reader = VehicleStateReader(self.vehicle, beamng, additional_sensors=cameras.cameras_array) brewer.vehicle_start_pose = brewer.road_points.vehicle_start_pose() steps = brewer.params.beamng_steps simulation_id = time.strftime('%Y-%m-%d--%H-%M-%S', time.localtime()) name = self.config.simulation_name.replace('$(id)', simulation_id) sim_data_collector = SimulationDataCollector(self.vehicle, beamng, brewer.decal_road, brewer.params, vehicle_state_reader=vehicle_state_reader, camera=self.camera, simulation_name=name) sim_data_collector.get_simulation_data().start() try: brewer.bring_up() from keras.models import load_model if not self.model: self.model = load_model(self.model_file) predict = NvidiaPrediction(self.model, self.config) iterations_count = 1000 idx = 0 while True: idx += 1 if idx >= iterations_count: sim_data_collector.save() raise Exception('Timeout simulation ', sim_data_collector.name) sim_data_collector.collect_current_data(oob_bb=False) last_state: SimulationDataRecord = sim_data_collector.states[-1] if points_distance(last_state.pos, waypoint_goal.position) < 15.0: break if last_state.is_oob: break img = vehicle_state_reader.sensors['cam_center']['colour'].convert('RGB') steering_angle, throttle = predict.predict(img, last_state) self.vehicle.control(throttle=throttle, steering=steering_angle, brake=0) beamng.step(steps) sim_data_collector.get_simulation_data().end(success=True) except Exception as ex: sim_data_collector.get_simulation_data().end(success=False, exception=ex) traceback.print_exception(type(ex), ex, ex.__traceback__) finally: if self.config.simulation_save: sim_data_collector.save() try: sim_data_collector.take_car_picture_if_needed() except: pass self.end_iteration() return sim_data_collector.simulation_data
def _run_simulation(self, the_test) -> SimulationData: if not self.brewer: self.brewer = BeamNGBrewer(beamng_home=self.beamng_home, beamng_user=self.beamng_user) self.vehicle = self.brewer.setup_vehicle() # For the execution we need the interpolated points nodes = the_test.interpolated_points brewer = self.brewer brewer.setup_road_nodes(nodes) beamng = brewer.beamng waypoint_goal = BeamNGWaypoint('waypoint_goal', get_node_coords(nodes[-1])) # TODO Make sure that maps points to the right folder ! if self.beamng_user is not None: beamng_levels = LevelsFolder( os.path.join(self.beamng_user, 'levels')) maps.beamng_levels = beamng_levels maps.beamng_map = maps.beamng_levels.get_map('tig') # maps.print_paths() maps.install_map_if_needed() maps.beamng_map.generated().write_items(brewer.decal_road.to_json() + '\n' + waypoint_goal.to_json()) vehicle_state_reader = VehicleStateReader(self.vehicle, beamng, additional_sensors=None) brewer.vehicle_start_pose = brewer.road_points.vehicle_start_pose() steps = brewer.params.beamng_steps simulation_id = time.strftime('%Y-%m-%d--%H-%M-%S', time.localtime()) name = 'beamng_executor/sim_$(id)'.replace('$(id)', simulation_id) sim_data_collector = SimulationDataCollector( self.vehicle, beamng, brewer.decal_road, brewer.params, vehicle_state_reader=vehicle_state_reader, simulation_name=name) # TODO: Hacky - Not sure what's the best way to set this... sim_data_collector.oob_monitor.tolerance = self.oob_tolerance sim_data_collector.get_simulation_data().start() try: #start = timeit.default_timer() brewer.bring_up() # iterations_count = int(self.test_time_budget/250) # idx = 0 brewer.vehicle.ai_set_aggression(self.risk_value) # TODO This does not seem to take any effect... brewer.vehicle.ai_set_speed(self.maxspeed, mode='limit') brewer.vehicle.ai_drive_in_lane(True) brewer.vehicle.ai_set_waypoint(waypoint_goal.name) while True: # idx += 1 # assert idx < iterations_count, "Timeout Simulation " + str(sim_data_collector.name) sim_data_collector.collect_current_data(oob_bb=True) last_state: SimulationDataRecord = sim_data_collector.states[ -1] # Target point reached if points_distance(last_state.pos, waypoint_goal.position) < 8.0: break assert self._is_the_car_moving( last_state), "Car is not moving fast enough " + str( sim_data_collector.name) assert not last_state.is_oob, "Car drove out of the lane " + str( sim_data_collector.name) beamng.step(steps) sim_data_collector.get_simulation_data().end(success=True) #end = timeit.default_timer() #run_elapsed_time = end-start #run_elapsed_time = float(last_state.timer) self.total_elapsed_time = self.get_elapsed_time() except AssertionError as aex: sim_data_collector.save() # An assertion that trigger is still a successful test execution, otherwise it will count as ERROR sim_data_collector.get_simulation_data().end(success=True, exception=aex) traceback.print_exception(type(aex), aex, aex.__traceback__) except Exception as ex: sim_data_collector.save() sim_data_collector.get_simulation_data().end(success=False, exception=ex) traceback.print_exception(type(ex), ex, ex.__traceback__) finally: sim_data_collector.save() try: sim_data_collector.take_car_picture_if_needed() except: pass self.end_iteration() return sim_data_collector.simulation_data
def run_sim(street_1: DecalRoad): waypoints = [] for node in street_1.nodes: waypoint = BeamNGWaypoint("waypoint_" + str(node), get_node_coords(node)) waypoints.append(waypoint) print(len(waypoints)) maps.beamng_map.generated().write_items( street_1.to_json() + '\n' + "\n".join([waypoint.to_json() for waypoint in waypoints])) beamng = BeamNGpy('localhost', 64256) scenario = Scenario('tig', 'tigscenario') vehicle = Vehicle('ego_vehicle', model='etk800', licence='TIG', color='Red') sim_data_collector = TrainingDataCollectorAndWriter( vehicle, beamng, street_1) scenario.add_vehicle(vehicle, pos=get_node_coords(street_1.nodes[0]), rot=get_rotation(street_1)) scenario.make(beamng) beamng.open() beamng.set_deterministic() beamng.load_scenario(scenario) beamng.pause() beamng.start_scenario() vehicle.ai_drive_in_lane(True) vehicle.ai_set_mode("disabled") vehicle.ai_set_speed(10 / 4) steps = 5 def start(): for waypoint in waypoints[10:-1:20]: vehicle.ai_set_waypoint(waypoint.name) for idx in range(1000): if (idx * 0.05 * steps) > 3.: sim_data_collector.collect_and_write_current_data() dist = distance(sim_data_collector.last_state.pos, waypoint.position) if dist < 15.0: beamng.resume() break # one step is 0.05 seconds (5/100) beamng.step(steps) try: start() finally: beamng.close()