def main(): bng_home = os.environ['BEAMNG_HOME'] road = TrainingRoad(ASFAULT_PREFAB) road.calculate_road_line(back=True) bng = BeamNGpy('localhost', 64256, home=bng_home) scenario = Scenario('smallgrid', 'train') scenario.add_road(road.asphalt) scenario.add_road(road.mid_line) scenario.add_road(road.left_line) scenario.add_road(road.right_line) vehicle = Vehicle('ego_vehicle', model='etk800', licence='PYTHON') front_camera = Camera(pos=(0, 1.4, 1.8), direction=(0, 1, -0.23), fov=FOV, resolution=(CAMERA_WIDTH, CAMERA_HEIGHT), colour=True, depth=False, annotation=False) vehicle.attach_sensor("front_camera", front_camera) # Add it to our scenario at this position and rotation spawn_point = road.spawn_point() scenario.add_vehicle(vehicle, pos=spawn_point.pos(), rot=spawn_point.rot()) # Place files defining our scenario for the simulator to read scenario.make(bng) prefab_path = scenario.get_prefab_path() update_prefab(prefab_path) bng.open() bng.set_nondeterministic() bng.set_steps_per_second(60) # Load and start our scenario bng.load_scenario(scenario) bng.start_scenario() vehicle.ai_set_mode('span') vehicle.ai_set_speed(5) vehicle.ai_set_line([{ 'pos': node.pos(), 'speed': 10 } for node in road.road_line]) number_of_images = 0 while number_of_images < 9000: bng.poll_sensors(vehicle) number_of_images += 1 #print(number_of_images) bng.step(1) sensors = bng.poll_sensors(vehicle) image = sensors['front_camera']['colour'].convert('RGB') image = np.array(image) image = image[:, :, ::-1] dist = road.dist_to_center(vehicle.state['pos']) cv2.imwrite('datasets\\{}.jpg'.format(number_of_images), image) bng.close()
class BeamNGBrewer: def __init__(self, beamng_home=None, road_nodes: List4DTuple = None): self.beamng = BeamNGpy('localhost', 64256, home=beamng_home) self.vehicle: Vehicle = None self.camera: BeamNGCamera = None if road_nodes: self.setup_road_nodes(road_nodes) steps = 5 self.params = SimulationParams(beamng_steps=steps, delay_msec=int(steps * 0.05 * 1000)) self.vehicle_start_pose = BeamNGPose() def setup_road_nodes(self, road_nodes): self.road_nodes = road_nodes self.decal_road: DecalRoad = DecalRoad('street_1').add_4d_points( road_nodes) self.road_points = RoadPoints().add_middle_nodes(road_nodes) def setup_vehicle(self) -> Vehicle: assert self.vehicle is None self.vehicle = Vehicle('ego_vehicle', model='etk800', licence='TIG', color='Red') return self.vehicle def setup_scenario_camera(self, resolution=(1280, 1280), fov=120) -> BeamNGCamera: assert self.camera is None self.camera = BeamNGCamera(self.beamng, 'brewer_camera') return self.camera def bring_up(self): self.scenario = Scenario('tig', 'tigscenario') if self.vehicle: self.scenario.add_vehicle(self.vehicle, pos=self.vehicle_start_pose.pos, rot=self.vehicle_start_pose.rot) if self.camera: self.scenario.add_camera(self.camera.camera, self.camera.name) self.scenario.make(self.beamng) if not self.beamng.server: self.beamng.open() self.beamng.pause() self.beamng.set_deterministic() self.beamng.load_scenario(self.scenario) self.beamng.start_scenario() def __del__(self): if self.beamng: try: self.beamng.close() except: pass
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 main(): beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.0') scenario = Scenario('GridMap', 'road_test') road_a = Road('track_editor_C_center', rid='circle_road', looped=True) nodes = [ (-25, 300, 0, 5), (25, 300, 0, 6), (25, 350, 0, 4), (-25, 350, 0, 5), ] road_a.nodes.extend(nodes) scenario.add_road(road_a) road_b = Road('track_editor_C_center', rid='center_road') nodes = [ (0, 325, 0, 5), (50, 375, 0, 5), ] road_b.nodes.extend(nodes) scenario.add_road(road_b) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() input('Press enter when done...') finally: bng.close()
def main(): random.seed(1703) setup_logging() beamng = BeamNGpy('localhost', 64256) scenario = Scenario('west_coast_usa', 'lidar_demo', description='Spanning the map with a lidar sensor') vehicle = Vehicle('ego_vehicle', model='etk800', licence='RED', color='Red') lidar = Lidar(offset=(0, 0, 1.6)) vehicle.attach_sensor('lidar', lidar) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(60) # With 60hz temporal resolution bng.load_scenario(scenario) bng.hide_hud() bng.start_scenario() vehicle.ai_set_mode('span') print('Driving around for 60 seconds...') sleep(60) finally: bng.close()
def main(): beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN') scenario = Scenario('GridMap', 'road_test') road_a = Road('custom_track_center', looped=False) nodes = [ (0, 0, 0, 4), (0, 400, 0, 4), ] road_a.nodes.extend(nodes) scenario.add_road(road_a) vehicle = Vehicle('ego', model='etk800', licence='PYTHON', colour='Black') scenario.add_vehicle(vehicle, pos = ( 0, 0, 0.163609) , rot=(0,0,180)) # 0.163609 , scenario.make(beamng) script = list() node0 = { 'x': 0, 'y': 2, 'z': 0.163609, 't': 0.01, } node1 = { 'x': 0, 'y': 3, 'z': 0.163609, 't': 0.01, } node2 = { 'x': 0, 'y': 350, 'z': 0.163609, 't': 15, } node3 = { 'x': 0, 'y': 400, 'z': 0.163609, 't': 5, } script.append(node0) script.append(node1) script.append(node2) script.append(node3) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() vehicle.ai_set_script(script) input('Press enter when done...') finally: bng.close()
def main(): beamng = BeamNGpy('localhost', 64256) scenario = Scenario('GridMap', 'road_test') road_a = Road('track_editor_C_center', looped=True) nodes = [ (-25, 300, 0, 5), (25, 300, 0, 6), (25, 350, 0, 4), (-25, 350, 0, 5), ] road_a.nodes.extend(nodes) scenario.add_road(road_a) road_b = Road('track_editor_C_center') nodes = [ (0, 325, 0, 5), (50, 375, 0, 5), ] road_b.nodes.extend(nodes) scenario.add_road(road_b) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() input('Press enter when done...') finally: bng.close()
def main(): beamng = BeamNGpy('localhost', 64256) scenario = Scenario('smallgrid', 'mesh_test') cylinder = ProceduralCylinder(pos=(10, -10, 0), rot=(0, 0, 0), radius=3.5, height=5) scenario.add_procedural_mesh(cylinder) cone = ProceduralCone(pos=(-10, -10, 0), rot=(45, 0, 0), radius=3.5, height=5) scenario.add_procedural_mesh(cone) cube = ProceduralCube(pos=(0, -20, 0), rot=(0, 0, 0), size=(5, 2, 3)) scenario.add_procedural_mesh(cube) vehicle = Vehicle('ego_vehicle', model='etk800') scenario.add_vehicle(vehicle, pos=(0, 0, 0), rot=(0, 0, 0)) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() input('Press enter when done...') finally: bng.close()
def main(): setup_logging() beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.1') #beamng.change_setting('research', True) scenario = Scenario('west_coast_usa', 'lidar_tour', description='Tour through the west coast gathering ' 'Lidar data') vehicle = Vehicle('ego_vehicle', model='etk800', licence='LIDAR') lidar = Lidar() vehicle.attach_sensor('lidar', lidar) #beamng.open_lidar('lidar', vehicle, 'shmem', 8000) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=None, rot_quat=(0, 0, 0.3826834, 0.9238795)) scenario.make(beamng) bng = beamng.open(launch=True) #bng.open_lidar('lidar', vehicle, 'shmem', 8000) #lidar.connect(bng, vehicle) try: bng.load_scenario(scenario) # this is where the error happens window = open_window(SIZE, SIZE) lidar_vis = LidarVisualiser(Lidar.max_points) lidar_vis.open(SIZE, SIZE) bng.set_steps_per_second(60) bng.set_deterministic() bng.hide_hud() bng.start_scenario() bng.pause() vehicle.ai_set_mode('span') def update(): sensors = bng.poll_sensors(vehicle) points = sensors['lidar']['points'] bng.step(3, wait=False) lidar_vis.update_points(points, vehicle.state) glutPostRedisplay() glutReshapeFunc(lidar_resize) glutIdleFunc(update) glutMainLoop() except Exception as e: print(e) finally: bng.close()
def setup_beamng(vehicle_model='etk800', camera_pos=(-0.5, 0.38, 1.3), camera_direction=(0, 1.0, 0), pitch_euler=0.0): global base_filename, default_color, default_scenario, default_spawnpoint, steps_per_sec global integral, prev_error, setpoint integral = 0.0 prev_error = 0.0 # version==keras # sm = DAVE2Model() # model = sm.define_dual_model_BeamNG() # model = sm.load_weights("BeamNGmodel-racetrackdual-comparison100K-PIDcontrolset-2.h5") # model = sm.load_weights("BeamNGmodel-racetrackdual-comparison100K-PIDcontrolset-3-sanitycheckretraining-model.h5") # version==pytorch # model = torch.load("C:/Users/merie/Downloads/dave/test_model_20.pt", map_location=torch.device('cpu')).eval() # model = torch.load("F:/MODELS-FOR-MERIEL/models/model-100000D-1000B-100E-1p000000e-04LR-100.pt.pt", map_location=torch.device('cpu')).eval() # model = torch.load("F:/MODELS-FOR-MERIEL/models/model-292757D-1000B-100E-1p000000e-04LR-100.pt", map_location=torch.device('cpu')).eval() model = torch.load( "H:/GitHub/DAVE2-Keras/test7-trad-50epochs-64batch-1e4lr-ORIGDATASET-singleoutput-model.pt", map_location=torch.device('cpu')).eval() random.seed(1703) setup_logging() beamng = BeamNGpy('localhost', 64256, home='H:/BeamNG.research.v1.7.0.1clean', user='******') scenario = Scenario(default_scenario, 'research_test') vehicle = Vehicle('ego_vehicle', model=vehicle_model, licence='EGO', color=default_color) vehicle = setup_sensors(vehicle, pos=camera_pos, direction=camera_direction) spawn = spawn_point(default_scenario, default_spawnpoint) scenario.add_vehicle( vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat']) #, partConfig=parts_config) add_barriers(scenario) add_qr_cubes(scenario) # Compile the scenario and place it in BeamNG's map folder scenario.make(beamng) # Start BeamNG and enter the main loop bng = beamng.open(launch=True) #bng.hide_hud() bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(steps_per_sec) # With 60hz temporal resolution bng.load_scenario(scenario) bng.start_scenario() # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt # bng.resume() # find_width_of_road(bng) return vehicle, bng, model
def test_multi_vehicle(beamng): """ Test that a second client can connect to a running instance, check for active vehicles, connect to one, and control it """ with beamng as a_client: scenario = Scenario('smallgrid', 'multi_vehicle') first = Vehicle('first', model='etk800') scenario.add_vehicle(first, pos=(2, 2, 0)) second = Vehicle('second', model='etki') scenario.add_vehicle(second, pos=(-2, -2, 0)) scenario.make(a_client) a_client.load_scenario(scenario) a_client.start_scenario() b_client = BeamNGpy('localhost', 64256) # Do not deploy mod zip or launch new process b_client.open(deploy=False, launch=False) vehicles = b_client.get_current_vehicles() assert 'second' in vehicles vehicle = vehicles['second'] vehicle.connect(b_client) assert vehicle.skt is not None a_veh = second b_veh = vehicle b_veh.control(throttle=1.0) for _ in range(8): # Verify position updating in both clients a_veh.poll_sensors() b_veh.poll_sensors() a_ref = a_veh.sensors['state'].data['pos'] b_ref = b_veh.sensors['state'].data['pos'] b_client.step(100) a_veh.poll_sensors() b_veh.poll_sensors() a_new = a_veh.sensors['state'].data['pos'] b_new = b_veh.sensors['state'].data['pos'] assert a_ref[0] != a_new[0] or a_ref[1] != a_new[1] assert b_ref[0] != b_new[0] or b_ref[1] != b_new[1]
def runScenario(scenario): setup_logging() bng = BeamNGpy('localhost', 64256, home='C:/Users/apsara.murali-simha/beamng_research/trunk') # Create an ETK800 with the licence plate 'PYTHON' vehicle = Vehicle('ego_vehicle', model='etk800', licence='PYTHON') # Add it to our scenario at this position and rotation scenario.add_vehicle(vehicle, pos=(-717, 101, 118), rot=(0, 0, 45)) # Place files defining our scenario for the simulator to read scenario.make(bng) # Launch BeamNG.research bng.open() # Load and start our scenario bng.load_scenario(scenario) bng.start_scenario() # Make the vehicle's AI span the map vehicle.ai_set_mode('span')
def setup_beamng(traffic=2): global sp, beamng_home, beamng_user setup_logging() beamng = BeamNGpy('localhost', 64256, home=beamng_home, user=beamng_user) scenario = Scenario( 'west_coast_usa', 'lidar_tour', description= 'Tour through highway with variable traffic vehicles gathering ' 'Lidar data') # setup vehicle vehicle = Vehicle('ego_vehicle', model='etk800', licence='LIDAR', color='Red') vehicle = setup_sensors(vehicle) # setup vehicle poses lane = random.choice([1, 2, 3, 4]) ego_sp = spawn_point(sp, lane) ego_pos = ego_sp['pos'] ego_rot_quat = ego_sp['rot_quat'] lane = ego_sp['lane'] # add vehicles to scenario # print("adding vehicle to scenario...") scenario.add_vehicle(vehicle, pos=ego_pos, rot=None, rot_quat=ego_rot_quat) tvs = traffic_vehicles(traffic) ps = generate_vehicle_positions(ego_pos, ego_rot_quat, lane, tvs) for i in range(len(tvs)): print("adding vehicle {}...".format(i)) scenario.add_vehicle(tvs[i], pos=ps[i], rot_quat=ego_rot_quat) print("making scenario...") scenario.make(beamng) print("opening beamNG...") bng = beamng.open(launch=True) st = time.time() print("loading scenario...") bng.load_scenario(scenario) bng.set_steps_per_second(60) bng.set_deterministic() #print("Bounding box: {}".format(vehicle.get_bbox())) bng.hide_hud() print("starting scenario...") bng.start_scenario() vehicle.ai_set_mode('traffic') #"DecalRoad31765_8" vehicle.ai_drive_in_lane(False) vehicle.ai_set_aggression(2.0) bng.start_traffic(tvs) bng.switch_vehicle(vehicle) bng.pause() return vehicle, bng
def test_multi_scenario(beamng): """ Test that a second client can connect to a running instance and ask information about the loaded scenario. """ with beamng as a_client: scenario = Scenario('gridmap_v2', 'multi_scenario') vehicle = Vehicle('vehicle', model='etk800') scenario.add_vehicle(vehicle) scenario.make(a_client) b_client = BeamNGpy('localhost', 64256) b_client.open(deploy=False, launch=False) with pytest.raises(BNGValueError): running = b_client.get_current_scenario() a_client.load_scenario(scenario) a_client.start_scenario() running = b_client.get_current_scenario() assert running.level == scenario.level assert running.name == scenario.name
def main(): setup_logging() beamng = BeamNGpy('localhost', 64256) scenario = Scenario('west_coast_usa', 'lidar_tour', description='Tour through the west coast gathering ' 'Lidar data') vehicle = Vehicle('ego_vehicle', model='etk800', licence='LIDAR') lidar = Lidar() vehicle.attach_sensor('lidar', lidar) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=None, rot_quat=(0, 0, 0.3826834, 0.9238795)) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) window = open_window(SIZE, SIZE) lidar_vis = LidarVisualiser(Lidar.max_points) lidar_vis.open(SIZE, SIZE) bng.set_steps_per_second(60) bng.set_deterministic() bng.hide_hud() bng.start_scenario() bng.pause() vehicle.ai_set_mode('span') def update(): sensors = bng.poll_sensors(vehicle) points = sensors['lidar']['points'] bng.step(3, wait=False) lidar_vis.update_points(points, vehicle.state) glutPostRedisplay() glutReshapeFunc(lidar_resize) glutIdleFunc(update) glutMainLoop() finally: bng.close()
def createBeamNGLanes(): beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN') scenario = Scenario('GridMap', 'road_test') graph_edges = graph.edges for sample in graph_edges: road_a = Road('track_editor_C_border', looped=False) road_b = Road('track_editor_C_border', looped=False) point1 = list(beamng_dict[sample[0]]) point2 = list(beamng_dict[sample[1]]) lane_marking_points = getRoadEndLaneMarking(point1, point2, 4) nodes0 = [ (lane_marking_points[0], lane_marking_points[1], -0.05, 0.05), (lane_marking_points[4], lane_marking_points[5], -0.05, 0.05) ] nodes1 = [ (lane_marking_points[2], lane_marking_points[3], -0.05, 0.05), (lane_marking_points[6], lane_marking_points[7], -0.05, 0.05) ] road_a.nodes.extend(nodes0) road_b.nodes.extend(nodes1) scenario.add_road(road_a) scenario.add_road(road_b) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() input('Press enter when done...') finally: bng.close()
def main(): beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN') scenario = Scenario('GridMap', 'road_test') road_a = Road('custom_track_center', looped=False) nodes = [ (0, 0, 0, 8), (125, 0, 0, 8), (250, -10, 0, 12) ] road_a.nodes.extend(nodes) scenario.add_road(road_a) road_b = Road('custom_track_center', looped=False) nodes = [ (125, 0, 0,12 ), (250, -10, 0, 12) ] road_b.nodes.extend(nodes) scenario.add_road(road_b) road_c = Road('custom_track_center', looped=False) nodes = [ (125, 125, 0, 8), (125, 0, 0, 8), ] road_c.nodes.extend(nodes) scenario.add_road(road_c) vehicle = Vehicle('ego', model='etk800', licence='PYTHON', colour='Black') scenario.add_vehicle(vehicle, pos=(0, 0, 0.163609), rot=(0, 0, 180)) # 0.163609 , scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() input('Press enter when done...') finally: bng.close()
def createBeamNGLanes(): beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN') scenario = Scenario('GridMap', 'road_test') graph_edges = graph.edges for sample in graph_edges: point1 = list(beamng_dict[sample[0]]) point2 = list(beamng_dict[sample[1]]) print(point1) print(point2) lane_marking_points = getRoadLaneMarking(point1, point2, 10, 3) # for loop iterate to put polylines. for polyline in lane_marking_points: print(polyline) road_a = Road('track_editor_C_border', looped=False) nodes0 = [(polyline[0], polyline[1], -0.05, 0.05), (polyline[2], polyline[3], -0.05, 0.05)] road_a.nodes.extend(nodes0) scenario.add_road(road_a) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() input('Press enter when done...') finally: bng.close()
def main(): setup_logging() beamng = BeamNGpy('localhost', 64256) scenario = Scenario('west_coast_usa', 'ai_sine') vehicle = Vehicle('ego_vehicle', model='etk800', licence='AI') orig = (-769.1, 400.8, 142.8) scenario.add_vehicle(vehicle, pos=orig, rot=None, rot_quat=(0, 0, 1, 0)) scenario.make(beamng) script = list() for i in range(3600): node = { # Calculate the position as a sinus curve that makes the vehicle # drive from left to right. The z-coordinate is not calculated in # any way because `ai_set_script` by default makes the polyline to # follow cling to the ground, meaning the z-coordinate will be # filled in automatically. 'x': 4 * np.sin(np.radians(i)) + orig[0], 'y': i * 0.2 + orig[1], 'z': orig[2], # Calculate timestamps for each node such that the speed between # points has a sinusoidal variance to it. 't': (2 * i + (np.abs(np.sin(np.radians(i)))) * 64) / 64, } script.append(node) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() vehicle.ai_set_script(script) while True: bng.step(60) finally: bng.close()
def main(): path='C:/Users/marc/Desktop/BeamNG' beamng = BeamNGpy('localhost', 64256,path) scenario = Scenario('GridMap', 'vehicle_bbox_example') road = Road('track_editor_C_center', rid='main_road', texture_length=5) orig = (-107, 70, 0) goal = (-300, 70, 0) road.nodes = [ (*orig, 7), (*goal, 7), ] scenario.add_road(road) script = [{'x': orig[0], 'y': orig[1], 'z': .3, 't': 0}] h=0 i = 0.2 while script[h]['x'] > goal[0]: node = { 'x': -10 * i + orig[0], 'y': 8 * np.sin(i) + orig[1], 'z': 0.3, 't': 1.5 * i, } script.append(node) i += 0.2 h+=1 print(script) vehicle = Vehicle('ego_vehicle', model='etkc', licence='PYTHON') scenario.add_vehicle(vehicle, pos=orig) scenario.make(beamng) bng = beamng.open() bng.load_scenario(scenario) bng.start_scenario() vehicle.ai_set_script(script) bng.pause() bng.step(1)
def run_scenario_spawns_backwards(vehicle_model='etk800'): global base_filename, default_color, spawn, steps_per_sec, home random.seed(1703) setup_logging() beamng = BeamNGpy('localhost', 64256, home=home) scenario = Scenario(default_scenario, 'research_test') vehicle = Vehicle('vehicle', model=vehicle_model, licence='CORRECT', color='Red') vehicle = setup_sensors(vehicle) # Compile the scenario and place it in BeamNG's map folder scenario.make(beamng) bng = beamng.open(launch=True) bng.hide_hud() bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(steps_per_sec) # Load and start the scenario bng.load_scenario(scenario) bng.start_scenario() bng.spawn_vehicle(vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat']) # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt bng.resume() return_str = "SPAWNED VEHICLE STATE:\n{}".format(vehicle.state) print(return_str) bng.close() return return_str
def main(): beamng = BeamNGpy('localhost', 64256,getBeamngDirectory()) scenario = Scenario('smallgrid', 'road_test') vehicle = Vehicle('LIF_Mobile', model='etkc', licence='LIFLAB', colour='Blue') ramp = StaticObject(name='pyramp', pos=(250,0, 0), rot=(0, 0, 90), scale=(0.5, 0.5, 0.5), shape='/levels/west_coast_usa/art/shapes/objects/ramp_massive.dae') ring = ProceduralRing(name='pyring', pos=(380, 0, 60), rot=(0, 0, 0), radius=5, thickness=2.5) wall= StaticObject(name="trumps_wall",pos=(420,0,0),rot=(0,0,0), scale=(1,10,75), shape='/levels/smallgrid/art/shapes/misc/gm_alpha_barrier.dae') road_c = Road('track_editor_B_center', rid='jump_road') roadC_Nodes=[(-2,0,0,10),(420,0,0,7)] road_c.nodes.extend(roadC_Nodes) scenario.add_road(road_c) scenario.add_procedural_mesh(ring) scenario.add_object(ramp) scenario.add_object(wall) scenario.add_vehicle(vehicle,(0,0,0),(0,0,-90)) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() input('Press enter when done...') finally: bng.close()
def createBeamNGRoads(): beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN') scenario = Scenario('GridMap', 'road_test') print(graph.edges) graph_edges = graph.edges print(len(graph_edges)) sample_list = [] for sample in graph_edges: road_a = Road('track_editor_C_center', looped=False) point1 = list(beamng_dict[sample[0]]) point2 = list(beamng_dict[sample[1]]) nodes0 = [(point1[0], point1[1], -4, 4), (point2[0], point2[1], -4, 4)] road_a.nodes.extend(nodes0) scenario.add_road(road_a) sample_list.append(point1) sample_list.append(point2) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() input('Press enter when done...') finally: bng.close()
vehicle.attach_sensor('damages', damage) # Create a scenario called vehicle_state taking place in the west_coast_usa map the simulator ships with scenario = Scenario('west_coast_usa', 'vehicle_state') # Add the vehicle and specify that it should start at a certain position and orientation. # The position & orientation values were obtained by opening the level in the simulator, # hitting F11 to open the editor and look for a spot to spawn and simply noting down the # corresponding values. scenario.add_vehicle(vehicle, pos=(-727.121, 101, 118.675), rot=(0, 0, 45)) # 45 degree rotation around the z-axis #scenario.add_vehicle(vehicleV,pos=(-707.121, 101, 118.675), rot=(0, 0, 45)) # The make function of a scneario is used to compile the scenario and produce a scenario file the simulator can load scenario.make(beamng) bng = beamng.open() bng.load_scenario(scenario) bng.start_scenario( ) # After loading, the simulator waits for further input to actually start #vehicle.ai_set_mode('span') # vehicle.update_vehicle() # sensors = bng.poll_sensors(vehicle) # # print('The vehicle position is:') # display(vehicle.state['pos']) # # print('The vehicle direction is:') # display(vehicle.state['dir']) #
class BeamNGBrewer: def __init__(self, beamng_home=None, beamng_user=None, reuse_beamng=True, road_nodes: List4DTuple = None): self.reuse_beamng = reuse_beamng if self.reuse_beamng: # This represents the running BeamNG simulator. Since we use launch=True this should automatically # shut down when the main python process exits or when we call self.beamng_process.stop() self.beamng_process = BeamNGpy('localhost', 64256, home=beamng_home, user=beamng_user) self.beamng_process = self.beamng_process.open(launch=True) # This is used to bring up each simulation without restarting the simulator self.beamng = BeamNGpy('localhost', 64256, home=beamng_home, user=beamng_user) # We need to wait until this point otherwise the BeamNG loggers's level will be (re)configured by BeamNGpy log.info("Disabling BEAMNG logs") for id in [ "beamngpy", "beamngpy.beamngpycommon", "beamngpy.BeamNGpy", "beamngpy.beamng", "beamngpy.Scenario", "beamngpy.Vehicle" ]: logger = log.getLogger(id) logger.setLevel(log.CRITICAL) logger.disabled = True self.vehicle: Vehicle = None self.camera: BeamNGCamera = None if road_nodes: self.setup_road_nodes(road_nodes) steps = 5 self.params = SimulationParams(beamng_steps=steps, delay_msec=int(steps * 0.05 * 1000)) self.vehicle_start_pose = BeamNGPose() def setup_road_nodes(self, road_nodes): self.road_nodes = road_nodes self.decal_road: DecalRoad = DecalRoad('street_1').add_4d_points( road_nodes) self.road_points = RoadPoints().add_middle_nodes(road_nodes) def setup_vehicle(self) -> Vehicle: assert self.vehicle is None self.vehicle = Vehicle('ego_vehicle', model='etk800', licence='TIG', color='Red') return self.vehicle def setup_scenario_camera(self, resolution=(1280, 1280), fov=120) -> BeamNGCamera: assert self.camera is None self.camera = BeamNGCamera(self.beamng, 'brewer_camera') return self.camera # TODO COnsider to transform brewer into a ContextManager or get rid of it... def bring_up(self): if self.reuse_beamng: # This assumes BeamNG is already running self.beamng.open(launch=False) else: self.beamng.open(launch=True) # After 1.18 to make a scenario one needs a running instance of BeamNG self.scenario = Scenario('tig', 'tigscenario') if self.vehicle: self.scenario.add_vehicle(self.vehicle, pos=self.vehicle_start_pose.pos, rot=self.vehicle_start_pose.rot) if self.camera: self.scenario.add_camera(self.camera.camera, self.camera.name) self.scenario.make(self.beamng) self.beamng.set_deterministic() self.beamng.load_scenario(self.scenario) self.beamng.start_scenario() # Pause the simulator only after loading and starting the scenario self.beamng.pause() def __del__(self): if self.beamng: try: self.beamng.close() except: pass
beamng = BeamNGpy('localhost', 64256, home=r'C:\BeamNG_unlimited\trunk') scenario = Scenario('west_coast_usa', 'sudden_obstruction') vut = Vehicle('vut', model='coupe', licence='VUT', colour='Red') electrics = Electrics() damage = Damage() vut.attach_sensor('electrics', electrics) vut.attach_sensor('damage', damage) scenario.add_vehicle(vut, pos=(-198.5, -164.189, 119.7), rot=(0, 0, -126.25)) car_1 = Vehicle('car_1', model='etk800', licence='CAR 1', colour='Blue') scenario.add_vehicle(car_1, pos=(-140, -121.233, 119.586), rot=(0, 0, 55)) scenario.make(beamng) bng = beamng.open(launch=True) bng.load_scenario(scenario) bng.start_scenario() vut.ai_set_mode('span') vut.ai_drive_in_lane(True) car_1.ai_set_line([{'pos': (-198.5, -164.189, 119.7), 'speed': 2000}]) for _ in range(240): sleep(0.1) vut.update_vehicle() sensors = bng.poll_sensors(vut) dmg = sensors['damage'] # Below code snippet is generated form 'detect_obstacle_car' function for car_1
def run_scenario(self): global base_filename, default_model, default_color, default_scenario, setpoint global prev_error #vehicle_loadfile = 'vehicles/pickup/pristine.save.json' # setup DNN model + weights m = Model() model = m.define_model_BeamNG("BeamNGmodel-4.h5") random.seed(1703) setup_logging() #beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.1') beamng = BeamNGpy('localhost', 64256, home='H:/BeamNG.research.v1.7.0.1clean') scenario = Scenario(default_scenario, 'research_test') vehicle = Vehicle('ego_vehicle', model=default_model, licence='LOWPRESS', color=default_color) vehicle = setup_sensors(vehicle) spawn = spawn_point(default_scenario, 'highway') scenario.add_vehicle(vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat']) # Compile the scenario and place it in BeamNG's map folder scenario.make(beamng) # Start BeamNG and enter the main loop bng = beamng.open(launch=True) bng.hide_hud() bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(100) # With 60hz temporal resolution # Load and start the scenario bng.load_scenario(scenario) bng.start_scenario() # perturb vehicle #vehicle.ai_set_mode('span') #vehicle.ai_drive_in_lane(True) #vehicle_loadfile = 'vehicles/etk800/fronttires_0psi.pc' # vehicle_loadfile = 'vehicles/etk800/backtires_0psi.pc' # vehicle_loadfile = 'vehicles/etk800/chassis_forcefeedback201.pc' # vehicle.load_pc(vehicle_loadfile, False) vehicle.deflate_tires([1,1,1,1]) #vehicle.break_all_breakgroups() #vehicle.break_hinges() # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt bng.resume() wheelspeed = 0.0; throttle = 0.0; prev_error = setpoint; damage_prev = None; runtime = 0.0 kphs = [] damage = None final_img = None # Send random inputs to vehice and advance the simulation 20 steps for _ in range(1024): # collect images image = bng.poll_sensors(vehicle)['front_cam']['colour'].convert('RGB') img = m.process_image(image) prediction = model.predict(img) # control params kph = ms_to_kph(wheelspeed) throttle = throttle_PID(kph, dt) brake = 0 #if throttle < 0: if setpoint < kph: brake = throttle / 1000.0 throttle = 0.0 # throttle = 0.2 # random.uniform(0.0, 1.0) # brake = random.choice([0, 0, 0.1 , 0.2]) steering = float(prediction[0][0]) #random.uniform(-1.0, 1.0) vehicle.control(throttle=throttle, steering=steering, brake=brake) steering_state = bng.poll_sensors(vehicle)['electrics']['steering'] steering_input = bng.poll_sensors(vehicle)['electrics']['steering_input'] avg_wheel_av = bng.poll_sensors(vehicle)['electrics']['avg_wheel_av'] wheelspeed = bng.poll_sensors(vehicle)['electrics']['wheelspeed'] damage = bng.poll_sensors(vehicle)['damage'] new_damage = diff_damage(damage, damage_prev) damage_prev = damage print("\n") # #print("steering state: {}".format(steering_state)) # print("AI steering_input: {}".format(steering_input)) #print("avg_wheel_av: {}".format(avg_wheel_av)) # print("DAVE2 steering prediction: {}".format(float(prediction[0][0]))) print("throttle:{}".format(throttle)) print("brake:{}".format(brake)) print("kph: {}".format(ms_to_kph(wheelspeed))) print("new_damage:{}".format(new_damage)) kphs.append(ms_to_kph(wheelspeed)) if new_damage > 0.0: final_img = image break bng.step(5) runtime += (0.05) # print("runtime:{}".format(round(runtime, 2))) # print("time to crash:{}".format(round(runtime, 2))) bng.close() avg_kph = float(sum(kphs)) / len(kphs) plt.imshow(final_img) plt.pause(0.01) return runtime, avg_kph, damage['damage'], kphs
def run_spawn_for_parts_config( vehicle_model='etk800', loadfile='C:/Users/merie/Documents/BeamNG.research/vehicles/hopper/front17x8rear17x9.json' ): global base_filename, default_color, default_scenario, setpoint global prev_error global fcam # setup DNN model + weights m = Model() model = m.define_model_BeamNG("BeamNGmodel-5.h5") random.seed(1703) setup_logging() beamng = BeamNGpy('localhost', 64256, home='H:/BeamNG.research.v1.7.0.1clean') scenario = Scenario(default_scenario, 'research_test') vehicle = Vehicle('ego_vehicle', model=vehicle_model, licence='LOWPRESS', color=default_color) vehicle = setup_sensors(vehicle) spawn = spawn_point(default_scenario, 'highway') # scenario.add_vehicle(vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat']) # Compile the scenario and place it in BeamNG's map folder scenario.make(beamng) # Start BeamNG and enter the main loop bng = beamng.open(launch=True) # bng.hide_hud() bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(100) # With 60hz temporal resolution # Load and start the scenario bng.load_scenario(scenario) bng.start_scenario() # vehicle.load('C:/Users/merie/Documents/BeamNG.research/vehicles/hopper/pristine.json') if loadfile: bng.spawn_vehicle(vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat'], partConfig=loadfile) else: bng.spawn_vehicle(vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat'], partConfig='vehicles/hopper/custom.pc') #bng.set_relative_camera() # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt bng.resume() bng.set_steps_per_second(100) bng.set_deterministic() totalsecs = 0.0 pitch_traj = [] while totalsecs <= 30: vehicle.update_vehicle() camera_state = bng.poll_sensors(vehicle)['front_cam'] camera_state['roll'] = vehicle.state['roll'] camera_state['pitch'] = vehicle.state['pitch'] camera_state['yaw'] = vehicle.state['yaw'] pitch_traj.append(round(math.degrees(vehicle.state['pitch'][0]), 4)) print("roll:{}, pitch:{}, yaw:{}".format(vehicle.state['roll'], vehicle.state['pitch'], vehicle.state['yaw'])) bng.step(100) totalsecs += 1 # print("Camera state:{}".format(camera_state)) image = bng.poll_sensors(vehicle)['front_cam']['colour'].convert('RGB') #get_camera_rot_and_pos([-0.3, 1, 1.0], [0, 0.75, 0], before_state['pos'], before_state['dir'], before_state['up']) img = m.process_image(image) before_prediction = model.predict(img) camera_state["prediction"] = before_prediction plt.imshow(image) plt.pause(0.01) bng.close() return camera_state, vehicle.state, pitch_traj
def run_spawn_for_deflation(vehicle_model='etk800', deflation_pattern=[0, 0, 0, 0]): global base_filename, default_color, default_scenario, setpoint global prev_error global fcam vehicle_loadfile = 'vehicles/hopper/crawler.pc' # setup DNN model + weights m = Model() model = m.define_model_BeamNG("BeamNGmodel-5.h5") random.seed(1703) setup_logging() # beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.1') beamng = BeamNGpy('localhost', 64256, home='H:/BeamNG.research.v1.7.0.1clean') scenario = Scenario(default_scenario, 'research_test') vehicle = Vehicle('ego_vehicle', model=vehicle_model, licence='LOWPRESS', color=default_color) vehicle = setup_sensors(vehicle) spawn = spawn_point(default_scenario, 'highway') # scenario.add_vehicle(vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat']) # Compile the scenario and place it in BeamNG's map folder scenario.make(beamng) # Start BeamNG and enter the main loop bng = beamng.open(launch=True) # bng.hide_hud() bng.set_deterministic() # Set simulator to be deterministic # Load and start the scenario bng.load_scenario(scenario) bng.start_scenario() # load part config #pc = vehicle.get_part_config() # loadfile = 'C:/Users/merie/Documents/BeamNG.research/vehicles/hopper/front17x8rear17x9.json' # vehicle.load(loadfile) bng.spawn_vehicle(vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat'], partConfig='vehicles/hopper/custom.pc') #bng.set_relative_camera() # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt # vehicle.control(throttle=0.0, steering=0.0, brake=1.0) # perturb vehicle # before_state = copy.deepcopy(vehicle.state) before_state = bng.poll_sensors(vehicle)['front_cam'] before_state['vel'] = vehicle.state['vel'] before_state['roll'] = vehicle.state['roll'] before_state['pitch'] = vehicle.state['pitch'] before_state['yaw'] = vehicle.state['yaw'] # print("vehicle position before deflation via beamstate:{}".format(vehicle.get_object_position())) # print("vehicle position before deflation via vehicle state:{}".format(vehicle.state)) print( "vehicle position before deflation via camera:{}".format(before_state)) image = bng.poll_sensors(vehicle)['front_cam']['colour'].convert('RGB') # print("camera sensor before deflation: {}".format(bng.poll_sensors(vehicle)['front_cam'])) #get_camera_rot_and_pos([-0.3, 1, 1.0], [0, 0.75, 0], before_state['pos'], before_state['dir'], before_state['up']) img = m.process_image(image) before_prediction = model.predict(img) before_state["prediction"] = before_prediction plt.imshow(image) plt.pause(0.01) if deflation_pattern != [0, 0, 0, 0]: print("deflating according to pattern {}".format(deflation_pattern)) vehicle.deflate_tires(deflation_pattern) time.sleep(1) bng.resume() bng.set_steps_per_second(100) bng.set_deterministic() totalsecs = 0.0 deflation_traj = [] while totalsecs <= 30: vehicle.update_vehicle() # vehicle.control(throttle=0.0, steering=0.0, brake=1.0) after_state = bng.poll_sensors(vehicle)['front_cam'] after_state['vel'] = vehicle.state['vel'] after_state['roll'] = vehicle.state['roll'] after_state['pitch'] = vehicle.state['pitch'] after_state['yaw'] = vehicle.state['yaw'] print("roll:{}, pitch:{}, yaw:{}".format(vehicle.state['roll'], vehicle.state['pitch'], vehicle.state['yaw'])) deflation_traj.append(round(math.degrees(vehicle.state['pitch'][0]), 4)) bng.step(100) totalsecs += 1 # after_state = copy.deepcopy(vehicle.state) # print("vehicle position after deflation via beamstate:{}".format(vehicle.get_object_position())) # print("vehicle position after deflation via vehicle state:{}".format(vehicle.state)) image = bng.poll_sensors(vehicle)['front_cam']['colour'].convert('RGB') print("vehicle position after deflation via camera:{}".format(after_state)) #print("camera sensor output: {}".format(bng.poll_sensors(vehicle)['front_cam']['rot'])) #print("camera pos output: {}".format(bng.poll_sensors(vehicle)['front_cam']['pos'])) # print("camera rot output: {}".format(bng.poll_sensors(vehicle)['front_cam']['direction'])) # print("fcam.encode_engine_request() = {}".format(fcam.encode_engine_request())) damages = bng.poll_sensors(vehicle)['damage']['deform_group_damage'] d = ["{}={}".format(k, damages[k]['damage']) for k in damages.keys()] print("vehicle pressure after deflation: lowpressure={} damages:".format( bng.poll_sensors(vehicle)['damage']['lowpressure'], d)) img = m.process_image(image) after_state["prediction"] = model.predict(img) plt.imshow(image) plt.pause(0.01) bng.close() return before_state, after_state, deflation_traj
def main(): beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN') scenario = Scenario('GridMap', 'road_test') road_a = Road('custom_track_center', looped=False) nodes = [ (0, 0, 0, 4), (0, 400, 0, 4), ] road_a.nodes.extend(nodes) scenario.add_road(road_a) vehicle = Vehicle('ego', model='etk800', licence='PYTHON', colour='Black') scenario.add_vehicle(vehicle, pos=(0, 0, 0.163609), rot=(0, 0, 180)) # 0.163609 , scenario.make(beamng) script = list() node0 = { 'pos': (0, 0, 0.163609), 'speed': 20, } node1 = { 'pos': (0, 100, 0.163609), 'speed': 30, } script.append(node0) script.append(node1) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() vehicle.ai_set_line(script) # update the state of vehicle at impact. for _ in range(100): time.sleep(0.1) vehicle.update_vehicle( ) # Synchs the vehicle's "state" variable with the simulator sensors = bng.poll_sensors( vehicle ) # Polls the data of all sensors attached to the vehicle print(vehicle.state['pos']) if vehicle.state['pos'][1] > 99: print('free state') vehicle.control(throttle=0, steering=0, brake=0, parkingbrake=0) vehicle.update_vehicle() bng.stop_scenario() for _ in range(20): time.sleep(0.1) bng.load_scenario(scenario) bng.start_scenario() node0 = { 'pos': (0, 50, 0.163609), 'speed': 20, } node1 = { 'pos': (0, 100, 0.163609), 'speed': 30, } script.append(node0) script.append(node1) vehicle.ai_set_line(script) input('Press enter when done...') finally: bng.close()