def test_gforces(beamng): with beamng as bng: scenario = Scenario('west_coast_usa', 'gforce_test') vehicle = Vehicle('test_car', model='etk800') gforces = GForces() vehicle.attach_sensor('gforces', gforces) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) gx = [] gy = [] bng.load_scenario(scenario) bng.start_scenario() bng.step(120) vehicle.ai_set_aggression(2) vehicle.ai_set_mode('span') for _ in range(64): bng.step(30) vehicle.poll_sensors() gx.append(gforces.data['gx']) gy.append(gforces.data['gy']) assert np.var(gx) > 1 and np.var(gy) > 1
def test_noise(beamng): with beamng as bng: scenario = Scenario('west_coast_usa', 'camera_test') vehicle = Vehicle('test_car', model='etk800') pos = (-0.3, 1, 1.0) direction = (0, 1, 0) fov = 120 resolution = (64, 64) cam = Camera(pos, direction, fov, resolution, colour=True, depth=True) noise_cam = RandomImageNoise(cam) vehicle.attach_sensor('noise_cam', noise_cam) lidar = Lidar() noise_lidar = RandomLIDARNoise(lidar, mean=0, var=.5) vehicle.attach_sensor('noise_lidar', noise_lidar) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) bng.load_scenario(scenario) bng.step(120) time.sleep(20) vehicle.poll_sensors() reference_img = np.array(noise_cam._sensor.data['colour']) noise_img = np.array(noise_cam.data['colour']) assert (not (np.array_equal(noise_img, reference_img))) ref_pc = lidar.data['points'] noise_pc = noise_lidar.data['points'] assert (not (np.array_equal(ref_pc, noise_pc)))
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 test_noise(beamng): scenario = Scenario('west_coast_usa', 'camera_test') vehicle = Vehicle('test_car', model='etk800') pos = (-0.3, 1, 1.0) direction = (0, 1, 0) fov = 120 resolution = (64, 64) reference_camera = Camera(pos, direction, fov, resolution, colour=True) vehicle.attach_sensor('reference_camera', reference_camera) noise_camera = Camera(pos, direction, fov, resolution, colour=True) noise_camera = WhiteGaussianRGBNoise(noise_camera, .5, 0) vehicle.attach_sensor('noise_cam', noise_camera) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) with beamng as bng: bng.load_scenario(scenario) bng.step(120) vehicle.poll_sensors() noise_img = np.asarray(noise_camera.data['colour']) reference_img = np.asarray(reference_camera.data['colour']) # since this is based on two different renders this will always be different assert (not (np.array_equal(noise_img, reference_img)))
def test_camera(beamng): scenario = Scenario('west_coast_usa', 'camera_test') vehicle = Vehicle('test_car', model='etk800') pos = (-0.3, 1, 1.0) direction = (0, 1, 0) fov = 120 resolution = (64, 64) front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) vehicle.attach_sensor('front_cam', front_camera) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) with beamng as bng: bng.load_scenario(scenario) bng.step(120) sensors = bng.poll_sensors(vehicle) assert_image_different(sensors['front_cam']['colour']) assert_image_different(sensors['front_cam']['depth']) assert_image_different(sensors['front_cam']['annotation'])
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()
def test_ultrasonic(beamng): with beamng as bng: scenario = Scenario('smallgrid', 'ultrasonic_test') cube_dist = 4 cube = ProceduralCube(name='cube', pos=(0, -cube_dist, 5), rot=None, rot_quat=(0, 0, 0, 1), size=(1, 20, 10)) scenario.add_procedural_mesh(cube) pos = (0, 1, 2) rot = (0, 1, 0) ultrasonic = Ultrasonic(pos, rot) vehicle = Vehicle('test', model='pickup') vehicle.attach_sensor('ultrasonic', ultrasonic) scenario.add_vehicle(vehicle, pos=(0, 0, 0), rot_quat=(0, 0, 0, 1)) scenario.make(beamng) bng.load_scenario(scenario) bng.start_scenario() vehicle.poll_sensors() assert 0 < vehicle.sensors['ultrasonic'].data['distance'] < cube_dist
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 create_vehicle(vehicle_objs): vehicles = [] for v in vehicle_objs: # Creates vehicle with associated id and attaches damage sensor to each vehicle vehicle = Vehicle(str(v.id)) damage = Damage() vehicle.attach_sensor('damage', damage) # Attach Sensor vehicles.append(vehicle) return vehicles
def test_bboxes(beamng): with beamng as bng: scenario = Scenario('west_coast_usa', 'bbox_test') ego = Vehicle('ego', model='etk800', color='White') scenario.add_vehicle(ego, pos=(-725.365, 92.4684, 118.437), rot_quat=(-0.006, -0.0076, 0.921, -0.389)) camera = Camera((-0.3, 1, 1), (0, 1, 0), 75, (1024, 1024), colour=True, depth=True, annotation=True, instance=True) ego.attach_sensor('camera', camera) car1 = Vehicle('car1', model='etk800', color='Green') scenario.add_vehicle(car1, pos=(-710.76, 101.50, 118.56), rot_quat=(-0.006, -0.0076, 0.921, -0.389)) car2 = Vehicle('car2', model='etk800', color='Red') scenario.add_vehicle(car2, pos=(-715.83, 96.69, 118.53), rot_quat=(-0.006, -0.0076, 0.921, -0.389)) car3 = Vehicle('car3', model='etki', color='Blue') scenario.add_vehicle(car3, pos=(-696.96, 126.9, 118.44), rot_quat=(0.0181, -0.0065, 0.3816, 0.924)) car4 = Vehicle('car4', model='miramar', color='Black') scenario.add_vehicle(car4, pos=(-708.58203, 115.326, 118.60), rot_quat=(0.0181, -0.00645, 0.3818, 0.9240)) scenario.make(beamng) bng.load_scenario(scenario) bng.step(120) time.sleep(5) ego.poll_sensors() classes = bng.get_annotation_classes(bng.get_annotations()) annotation = ego.sensors['camera'].data['annotation'] instance = ego.sensors['camera'].data['instance'] bboxes = Camera.extract_bboxes(annotation, instance, classes) bboxes = [b for b in bboxes if b['class'] == 'CAR'] assert len(bboxes) == 5
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 add_sensor_to(self, vehicle: Vehicle) -> None: from beamngpy.sensors import Camera from cmath import pi # NOTE rotation range: -pi to pi # NOTE Reference point is the point of the model # NOTE First rotate camera, then shift based on rotated axis of the camera # NOTE x-axis points rightwards, y-axis points forwards, z-axis points upwards # FIXME These values are specialized for etk800 if self.direction is CameraDirection.FRONT: x_pos = -0.3 y_pos = 2.1 z_pos = 0.3 x_rot = 0 y_rot = pi z_rot = 0 elif self.direction is CameraDirection.RIGHT: x_pos = 0 y_pos = 0.5 z_pos = 0.3 x_rot = pi y_rot = 0 z_rot = 0 elif self.direction is CameraDirection.BACK: x_pos = 0 y_pos = 2.6 z_pos = 0.3 x_rot = 0 y_rot = -pi z_rot = 0 elif self.direction is CameraDirection.LEFT: x_pos = 0 y_pos = 1.2 z_pos = 0.3 x_rot = -pi y_rot = 0 z_rot = 0 elif self.direction is CameraDirection.DASH: x_pos = 0 y_pos = 0.4 z_pos = 1 x_rot = 0 y_rot = pi z_rot = 0 else: _logger.warning("The camera direction " + str(self.direction.value) + " is not implemented.") return vehicle.attach_sensor(self.rid, Camera((x_pos, y_pos, z_pos), (x_rot, y_rot, z_rot), self.fov, (self.width, self.height), colour=True, depth=True, annotation=False))
def test_state(beamng): with beamng as bng: scenario = Scenario('smallgrid', 'vehicle_state_test') vehicle = Vehicle('test_car', model='pickup') state = State() vehicle.attach_sensor('newstate', state) scenario.add_vehicle(vehicle, pos=(0, 0, 0)) scenario.make(beamng) bng.load_scenario(scenario) bng.start_scenario() bng.step(20) vehicle.poll_sensors() assert state.data['pos'][0] < 0.1
def test_state(beamng): scenario = Scenario('smallgrid', 'vehicle_state_test') vehicle = Vehicle('test_car', model='pickup') state = State() vehicle.attach_sensor('state', state) scenario.add_vehicle(vehicle, pos=(0, 0, 0)) scenario.make(beamng) with beamng as bng: bng.load_scenario(scenario) bng.start_scenario() bng.step(20) sensors = bng.poll_sensors(vehicle) assert sensors["state"] != None assert vehicle.state == sensors["state"]
def test_multicam(beamng): with beamng as bng: scenario = Scenario('west_coast_usa', 'camera_test') vehicle = Vehicle('test_car', model='etk800') pos = (-0.3, 1, 1.0) direction = (0, 1, 0) fov = 120 resolution = (64, 64) front_cam1 = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True, instance=True, shmem=True) vehicle.attach_sensor('front_cam1', front_cam1) front_cam2 = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True, instance=True, shmem=True) vehicle.attach_sensor('front_cam2', front_cam2) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) bng.load_scenario(scenario) bng.start_scenario() bng.pause() bng.step(120) time.sleep(20) vehicle.poll_sensors()
def test_lidar(beamng): scenario = Scenario('west_coast_usa', 'lidar_test') vehicle = Vehicle('test_car', model='etk800') lidar = Lidar() vehicle.attach_sensor('lidar', lidar) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) with beamng as bng: bng.load_scenario(scenario) bng.step(120) sensors = bng.poll_sensors(vehicle) arr = sensors['lidar']['points'] ref = arr[0] eq = arr[np.where(arr == ref)] assert eq.size != arr.size
def getSquareRoadScenario(): beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory() ) # This is the host & port used to communicate over squareRoadScenario = Scenario('smallgrid', 'Straight_Foward_Test') testRoad = Road('track_editor_C_center', rid='Test_Road', looped=True) roadNode = [(0, 0, 0, 7), (250, 0, 0, 7), (250, 250, 0, 7), (0, 250, 0, 7)] testRoad.nodes.extend(roadNode) testVehicle = Vehicle('Test_Vehicule', model='etkc', licence='LIFLAB', colour='Blue') # Create an Electrics sensor and attach it to the vehicle electrics = Electrics() testVehicle.attach_sensor('electrics', electrics) # Create a Damage sensor and attach it to the vehicle if module is selected damage = Damage() testVehicle.attach_sensor('damage', damage) # Create a Gforce sensor and attach it to the vehicle if module is selected gForce = GForces() testVehicle.attach_sensor('GForces', gForce) squareRoadScenario.add_road(testRoad) squareRoadScenario.add_vehicle(testVehicle, pos=(0, 0, 0), rot=(0, 0, -90)) scenarioDict = { 'beamng': beamng, 'scenario': squareRoadScenario, 'vehicule': testVehicle } return scenarioDict
def getStaticScenario(testName): beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory() ) # This is the host & port used to communicate over staticScenario = Scenario('smallgrid', str(testName)) testVehicle = Vehicle('Test_Vehicule', model=SelectCar(), licence='LIFLAB', colour='Blue') # Create an Electrics sensor and attach it to the vehicle electrics = Electrics() testVehicle.attach_sensor('electrics', electrics) # Create a Damage sensor and attach it to the vehicle if module is selected damage = Damage() testVehicle.attach_sensor('damage', damage) # Create a Gforce sensor and attach it to the vehicle if module is selected gForce = GForces() testVehicle.attach_sensor('GForces', gForce) staticScenario.add_vehicle(testVehicle, (0, 0, 0), (0, 0, -90)) scenarioDict = { 'beamng': beamng, 'scenario': staticScenario, 'vehicule': testVehicle } return scenarioDict
def test_camera(beamng, shmem): with beamng as bng: scenario = Scenario('west_coast_usa', 'camera_test') vehicle = Vehicle('test_car', model='etk800') pos = (-0.3, 1, 1.0) direction = (0, 1, 0) fov = 120 resolution = (64, 64) front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True, instance=True, shmem=shmem) vehicle.attach_sensor('front_cam', front_camera) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) bng.load_scenario(scenario) bng.start_scenario() bng.pause() bng.step(120) time.sleep(20) sensors = bng.poll_sensors(vehicle) assert_image_different(sensors['front_cam']['colour']) assert_image_different(sensors['front_cam']['depth']) assert_image_different(sensors['front_cam']['annotation']) assert_image_different(sensors['front_cam']['instance']) annotation = sensors['front_cam']['annotation'] instance = sensors['front_cam']['instance'] assert not (np.array(annotation) == np.array(instance)).all()
def test_lidar(beamng, shmem): with beamng as bng: scenario = Scenario('west_coast_usa', 'lidar_test') vehicle = Vehicle('test_car', model='etk800') lidar = Lidar(shmem=shmem) vehicle.attach_sensor('lidar', lidar) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.make(beamng) bng.load_scenario(scenario) bng.step(120) time.sleep(20) vehicle.poll_sensors() arr = lidar.data['points'] ref = arr[0] eq = arr[np.where(arr == ref)] assert eq.size != arr.size
def test_electrics(beamng): with beamng as bng: scenario = Scenario('smallgrid', 'electrics_test') vehicle = Vehicle('test_car', model='etk800') electrics = Electrics() vehicle.attach_sensor('electrics', electrics) scenario.add_vehicle(vehicle, pos=(0, 0, 0)) scenario.make(beamng) bng.load_scenario(scenario) bng.start_scenario() bng.step(120) vehicle.control(throttle=1.0) bng.step(360) vehicle.poll_sensors() assert electrics.data['airspeed'] > 0 assert electrics.data['wheelspeed'] > 0 assert electrics.data['throttle_input'] > 0
def test_damage(beamng): with beamng as bng: scenario = Scenario('smallgrid', 'damage_test') dummy = Vehicle('dummy', model='pickup') scenario.add_vehicle(dummy, pos=(0, 0, 0)) scenario.make(beamng) vehicle = Vehicle('test_car', model='etk800') damage = Damage() vehicle.attach_sensor('damage', damage) bng.load_scenario(scenario) bng.start_scenario() scenario.add_vehicle(vehicle, pos=(0, 0, 32), rot=(-90, 0, 0), cling=False) bng.step(600) vehicle.poll_sensors() assert damage.data['damage'] > 100
def getWallCrashScenario(testName): beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory() ) # This is the host & port used to communicate over wallCrashScenario = Scenario('smallgrid', str(testName)) wall = StaticObject( name="Crash_Test_Wall", pos=(420, 0, 0), rot=(0, 0, 0), scale=(1, 10, 75), shape='/levels/smallgrid/art/shapes/misc/gm_alpha_barrier.dae') testRoad = Road('track_editor_B_center', rid='Test_Road') roadNode = [(-2, 0, 0, 7), (420, 0, 0, 7)] testRoad.nodes.extend(roadNode) testVehicle = Vehicle('Test_Vehicule', model=SelectCar(), licence='LIFLAB', colour='Blue') # Create an Electrics sensor and attach it to the vehicle electrics = Electrics() testVehicle.attach_sensor('electrics', electrics) # Create a Damage sensor and attach it to the vehicle if module is selected damage = Damage() testVehicle.attach_sensor('damage', damage) # Create a Gforce sensor and attach it to the vehicle if module is selected gForce = GForces() testVehicle.attach_sensor('GForces', gForce) wallCrashScenario.add_road(testRoad) wallCrashScenario.add_object(wall) wallCrashScenario.add_vehicle(testVehicle, pos=(0, 0, 0), rot=(0, 0, -90)) scenarioDict = { 'beamng': beamng, 'scenario': wallCrashScenario, 'vehicule': testVehicle } return scenarioDict
raise Exception('Wrong number of arguments. This program takes 2 arguments and it received the following number of argument: {}.'.format(len(sys.argv)-1)) print(str(testTime)) print(str(dataRate)) actualisationTime=getActualisationTime(dataRate) # Instantiate a BeamNGpy instance the other classes use for reference & communication beamng = BeamNGpy('localhost', 64256,home=beamNGPAth) # This is the host & port used to communicate over # Create a blue vehicle instance that will be called 'LIF Mobile' in the simulation # using the etkc model the simulator ships with 'LIFLAB' licence plate vehicle = Vehicle('LIF_Mobile', model='etkc', licence='LIFLAB', colour='Blue') # Create an Electrics sensor and attach it to the vehicle electrics = Electrics() vehicle.attach_sensor('electrics', electrics) #Create a Damage sensor and attach it to the vehicle if module is selected damage = Damage() vehicle.attach_sensor('damage',damage) #Create a Gforce sensor and attach it to the vehicle if module is selected gForce=GForces() vehicle.attach_sensor('GForces',gForce) # Create a scenario called 'LIF TEST' taking place in the gridmap map the simulator ships with scenario = Scenario('gridmap', 'LIF_TEST') # 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
from beamngpy import BeamNGpy, Vehicle, Scenario, Road from beamngpy.sensors import Electrics, Damage from IPython.display import display # Instantiate a BeamNGpy instance the other classes use for reference & communication beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN' ) # This is the host & port used to communicate over # Create a vehile instance that will be called 'ego' in the simulation # using the etk800 model the simulator ships with vehicle = Vehicle('ego', model='etk800', licence='PYTHON', colour='white') vehicleV = Vehicle('victim', model='etk800', licence='PYTHON', colour='red') # Create an Electrics sensor and attach it to the vehicle electrics = Electrics() vehicle.attach_sensor('electrics', electrics) damage = Damage() 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
def getDonutScenario(): beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory() ) # This is the host & port used to communicate over donutScenario = Scenario('smallgrid', 'road_test') concreteWallSide1 = StaticObject( name="Crash_Test_Wall", pos=(20, 10, 0), rot=(0, 0, 0), scale=(10, 1, 1), shape= '/levels/driver_training/art/shapes/race/concrete_road_barrier_a.dae') concreteWallSide2 = StaticObject( name="Crash_Test_Wall2", pos=(35, -5, 0), rot=(0, 0, 90), scale=(10, 1, 1), shape= '/levels/driver_training/art/shapes/race/concrete_road_barrier_a.dae') concreteWallSide3 = StaticObject( name="Crash_Test_Wall3", pos=(20, -20, 0), rot=(0, 0, 0), scale=(10, 1, 1), shape= '/levels/driver_training/art/shapes/race/concrete_road_barrier_a.dae') concreteWallSide4 = StaticObject( name="Crash_Test_Wall4", pos=(5, -5, 0), rot=(0, 0, 90), scale=(10, 1, 1), shape= '/levels/driver_training/art/shapes/race/concrete_road_barrier_a.dae') testRoad = Road('track_editor_C_center', rid='Test_Road') roadNode = [(*(-25, 25, 0), 45), (*(15, 25, 0), 45)] testRoad.nodes.extend(roadNode) testVehicle = Vehicle('Test_Vehicule', model='etkc', licence='LIFLAB', colour='Blue') # Create an Electrics sensor and attach it to the vehicle electrics = Electrics() testVehicle.attach_sensor('electrics', electrics) # Create a Damage sensor and attach it to the vehicle if module is selected damage = Damage() testVehicle.attach_sensor('damage', damage) # Create a Gforce sensor and attach it to the vehicle if module is selected gForce = GForces() testVehicle.attach_sensor('GForces', gForce) donutScenario.add_vehicle(testVehicle, pos=(20, 0, 0), rot=(0, 0, 0)) donutScenario.add_object(concreteWallSide1) donutScenario.add_object(concreteWallSide2) donutScenario.add_object(concreteWallSide3) donutScenario.add_object(concreteWallSide4) donutScenario.add_road(testRoad) scenarioDict = { 'beamng': beamng, 'scenario': donutScenario, 'vehicule': testVehicle } return scenarioDict
from beamngpy import BeamNGpy, Scenario, Vehicle, StaticObject from beamngpy.sensors import Electrics, Damage import numpy as np from time import sleep, time 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()
nodes0 = [ ( point1[0], point1[1], 0, 8 ), # method to get the road width from elastic search or number of lanes. (forward and backward) (point2[0], point2[1], 0, 8) ] road_a.nodes.extend(nodes0) scenario.add_road(road_a) vehicleStriker = Vehicle('striker', model='etk800', licence='Striker', colour='Yellow') damageStriker = Damage() vehicleStriker.attach_sensor('damagesS', damageStriker) vehicleVictim = Vehicle('victim', model='etk800', licence='Victim', colour='White') damageVictim = Damage() vehicleVictim.attach_sensor('damagesV', damageVictim) # road creation and vehicle initializatoin with sensors completed.------------------------------------------- def getDistance(node_a, node_b): dist = math.sqrt((node_a[1] - node_b[1])**2 + (node_a[0] - node_b[0])**2) return dist
import mmap
from beamngpy import BeamNGpy, Vehicle, Scenario, Road from beamngpy.sensors import Camera from getAIScript import getAIScript beamng = BeamNGpy('localhost', 64256, getBeamngDirectory()) scenario = Scenario('smallgrid', 'vehicle_bbox_example') road = Road('track_editor_A_center', rid='main_road') orig = (0, -2, 0) goal = (1150, -22, 0) nodes = [(*orig, 7), (*goal, 7)] road.nodes.extend(nodes) scenario.add_road(road) vehicle = Vehicle('ego_vehicle', model='etk800', licence='PYTHON') overhead = Camera((0, -10, 5), (0, 1, -0.75), 60, (1024, 1024)) vehicle.attach_sensor('overhead', overhead) scenario.add_vehicle(vehicle, pos=orig, rot=(0, 0, -90)) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.start_scenario() script = getAIScript() vehicle.ai_set_script(script) while (True): vehicle.update_vehicle() print(vehicle.state['pos']) input() finally: bng.close()