def _init_beamNG(self) -> None: """ Initialize beamNG: Create the scenario, vehicle, sensors, and load everything """ self.scenario = Scenario(self.OPTIONS['scenario_map'], self.OPTIONS['scenario_name'], description=self.OPTIONS['scenario_desc']) self.vehicle = Vehicle(self.OPTIONS['vehicle_name'], model=self.OPTIONS['vehicle_model'], license=self.OPTIONS['vehicle_license']) self.lidar_sensor = Lidar(max_dist=180, vres=24, vangle=25) self.vehicle.attach_sensor('lidar', self.lidar_sensor) self.front_camera = Camera(self.OPTIONS['f_cam_pos'], self.OPTIONS['f_cam_dir'], self.OPTIONS['f_cam_fov'], self.OPTIONS['f_cam_res'], colour=True, annotation=True) self.vehicle.attach_sensor('front_camera', self.front_camera) self.scenario.add_vehicle(self.vehicle, self.OPTIONS['vehicle_pos'], self.OPTIONS['vehicle_rot'], self.OPTIONS['vehicle_rot_quat']) self.scenario.make(self.beamng) self.beamng.load_scenario(self.scenario)
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): 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(): 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 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 _init_beamNG(self) -> None: self.scenario = Scenario(self.scenario_map, self.scenario_name, description=self.scenario_desc) self.vehicle = Vehicle(self.vehicle_name, model=self.vehicle_model, license=self.vehicle_license) self.lidar_sensor = Lidar() self.vehicle.attach_sensor('lidar', self.lidar_sensor) self.scenario.add_vehicle(self.vehicle, self.vehicle_pos, self.vehicle_rot, self.vehicle_rot_quat) self.scenario.make(self.beamng) self.beamng.load_scenario(self.scenario)
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 setup_lidar(mountpoint='roof'): lidar = Lidar() if mountpoint == 'hood': #lidar.__init__(offset=(-0.3, 1.0, 1.0), direction=(-0.707, -0.707, 0), vres=2, #lidar.__init__(offset=(-5, 0, 1.0), direction=(-0.707, -0.707, 0), vres=2, lidar.__init__(offset=(2.4, 1, 1), direction=(-0.707, -0.707, 0), vres=2, vangle=0.1, rps=10000, hz=60, angle=180, max_dist=200, visualized=True) elif mountpoint == 'roof': lidar.__init__(offset=(0, 0, 1.7), direction=(-0.707, -0.707, -0.0625), vres=3, vangle=0.1, rps=100000, hz=20, angle=180, max_dist=200, visualized=True) elif mountpoint == 'orig': lidar.__init__(offset=(0, 0, 1.7), direction=(0, -1, 0), vres=32, vangle=26.9, rps=2200000, hz=20, angle=360, max_dist=200, visualized=True) else: print("returning lidar") return lidar return lidar
def setup_sensors(vehicle): # Set up sensors pos = (-0.3, 1, 1.0) direction = (0, 1, 0) fov = 120 resolution = (512, 512) front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) pos = (0.0, 3, 1.0) direction = (0, -1, 0) fov = 90 resolution = (512, 512) back_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) gforces = GForces() electrics = Electrics() damage = Damage() damage.encode_vehicle_request() lidar = Lidar(visualized=False) timer = Timer() # Attach them vehicle.attach_sensor('front_cam', front_camera) vehicle.attach_sensor('back_cam', back_camera) vehicle.attach_sensor('gforces', gforces) vehicle.attach_sensor('electrics', electrics) vehicle.attach_sensor('damage', damage) vehicle.attach_sensor('timer', timer) return vehicle
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 setup_lidar(mountpoint='roof'): lidar = Lidar() if mountpoint == 'hood': lidar.__init__(offset=(-0.4, 1, 1.0), direction=(-0.707, -0.707, 0), vres=2, vangle=0.1, rps=10000, hz=20, angle=180, max_dist=200, visualized=True) else: lidar.__init__(offset=(0, 0, 1.7), direction=(-0.707, -0.707, -0.0625), vres=2, vangle=0.1, rps=10000, hz=20, angle=180, max_dist=200, visualized=True) return lidar
import mmap
def main(): random.seed(1703) setup_logging() # Plotting code setting up a 3x2 figure fig = plt.figure(1, figsize=(10, 5)) axarr = fig.subplots(2, 3) a_colour = axarr[0, 0] b_colour = axarr[1, 0] a_depth = axarr[0, 1] b_depth = axarr[1, 1] a_annot = axarr[0, 2] b_annot = axarr[1, 2] plt.ion() beamng = BeamNGpy('localhost', 64256) bng = beamng.open(launch=True) # Create a scenario in west_coast_usa scenario = Scenario('west_coast_usa', 'tech_test', description='Random driving for research') # Set up first vehicle, with two cameras, gforces sensor, lidar, electrical # sensors, and damage sensors vehicle = Vehicle('ego_vehicle', model='etk800', licence='RED', color='Red') # Set up sensors pos = (-0.3, 1, 1.0) direction = (0, 1, 0) fov = 120 resolution = (512, 512) front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) pos = (0.0, 3, 1.0) direction = (0, -1, 0) fov = 90 resolution = (512, 512) back_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) gforces = GForces() electrics = Electrics() damage = Damage() lidar = Lidar(visualized=False) timer = Timer() # Attach them vehicle.attach_sensor('front_cam', front_camera) vehicle.attach_sensor('back_cam', back_camera) vehicle.attach_sensor('gforces', gforces) vehicle.attach_sensor('electrics', electrics) vehicle.attach_sensor('damage', damage) vehicle.attach_sensor('timer', timer) scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=None, rot_quat=(0, 0, 0.3826834, 0.9238795)) # Compile the scenario and place it in BeamNG's map folder scenario.make(bng) # Start BeamNG and enter the main loop try: bng.hide_hud() bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(60) # With 60hz temporal resolution # Load and start the scenario bng.load_scenario(scenario) bng.start_scenario() # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt # Send random inputs to vehice and advance the simulation 20 steps for _ in range(1024): throttle = random.uniform(0.0, 1.0) steering = random.uniform(-1.0, 1.0) brake = random.choice([0, 0, 0, 1]) vehicle.control(throttle=throttle, steering=steering, brake=brake) bng.step(20) # Retrieve sensor data and show the camera data. sensors = bng.poll_sensors(vehicle) print('{} seconds passed.'.format(sensors['timer']['time'])) a_colour.imshow(sensors['front_cam']['colour'].convert('RGB')) a_depth.imshow(sensors['front_cam']['depth'].convert('L')) a_annot.imshow(sensors['front_cam']['annotation'].convert('RGB')) b_colour.imshow(sensors['back_cam']['colour'].convert('RGB')) b_depth.imshow(sensors['back_cam']['depth'].convert('L')) b_annot.imshow(sensors['back_cam']['annotation'].convert('RGB')) plt.pause(0.00001) finally: bng.close()
def main(): random.seed(1703) setup_logging() beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.1') config = Config() loaded_config = config.load("{}/config.json".format(beamng.home)) # Create a scenario in west_coast_usa scenario = Scenario('west_coast_usa', 'research_test', description='Random driving for research') # Set up first vehicle, with two cameras, gforces sensor, lidar, electrical # sensors, and damage sensors vehicle = Vehicle('ego_vehicle', model='etk800', licence='RED', color='Red') # Set up sensors pos = (-0.3, 1, 1.0) direction = (0, 1, 0) fov = 120 resolution = (512, 512) front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) pos = (0.0, 3, 1.0) direction = (0, -1, 0) fov = 90 resolution = (512, 512) back_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) gforces = GForces() electrics = Electrics() damage = Damage() lidar = Lidar(visualized=False) timer = Timer() # Attach them vehicle.attach_sensor('front_cam', front_camera) vehicle.attach_sensor('back_cam', back_camera) vehicle.attach_sensor('gforces', gforces) vehicle.attach_sensor('electrics', electrics) vehicle.attach_sensor('damage', damage) vehicle.attach_sensor('timer', timer) #scenario.add_vehicle(vehicle, pos=(-717.121, 101, 118.675), rot=None, rot_quat=(0, 0, 0.3826834, 0.9238795)) #config_rot_quat = beamngpy.angle_to_quat(config.dir) # N.B. using rot is deprecated in favor of rot_quat #scenario.add_vehicle(vehicle, pos=tuple(config.pos), rot=tuple(config.dir), rot_quat=None) scenario.add_vehicle(vehicle, pos=tuple(config.pos), rot=None, rot_quat=beamngpy.angle_to_quat(config.dir)) # 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) try: bng.hide_hud() bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(60) # With 60hz temporal resolution # Load and start the scenario bng.load_scenario(scenario) bng.start_scenario() # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt # Send random inputs to vehicle and advance the simulation 20 steps #for _ in range(1024): for _ in range(100): throttle = random.uniform(0.0, 1.0) steering = random.uniform(-1.0, 1.0) brake = random.choice([0, 0, 0, 1]) vehicle.control(throttle=throttle, steering=steering, brake=brake) # bng.step(20) bng.step(20) # Retrieve sensor data and show the camera data. sensors = bng.poll_sensors(vehicle) print('\n{} seconds passed.'.format(sensors['timer']['time'])) print("step in loop {}".format(_)) for s in sensors.keys(): print("{} : {}".format(s, sensors[s])) damage_dict = vehicle.sensors['damage'].encode_vehicle_request() damage_dict = sensors['damage'] config.update(sensors['damage']) config.update(vehicle.state) config.save('{}/config.json'.format(beamng.home)) gamestate = beamng.get_gamestate() if _ > 990: print("late in sim") finally: bng.close()
def main(): global sp setup_logging() beamng = BeamNGpy('localhost', 64256, home='H:/BeamNG.research.v1.7.0.1clean') scenario = Scenario('west_coast_usa', 'lidar_tour', description='Tour through the west coast gathering ' 'Lidar data') vehicle = Vehicle('ego_vehicle', model='etk800', licence='LIDAR', color='Red') lidar = Lidar() lidar.__init__(offset=(0, 0, 1.7), direction=(-0.707, -0.707, 0), vres=32, vangle=0.01, rps=2200000, hz=20, angle=360, max_dist=200, visualized=True) vehicle.attach_sensor('lidar', lidar) ego_sp = spawn_point(sp) ego_pos = ego_sp['pos'] ego_rot_quat = ego_sp['rot_quat'] scenario.add_vehicle(vehicle, pos=ego_pos, rot=None, rot_quat=ego_rot_quat) tvs = traffic_vehicles() #ps = [(-722.121, 103, 118.675), (-714.121, 101, 118.675), (-715.121, 105, 118.675), (-721.121, 100, 118.675)] ps = [(ego_pos[0]-10, ego_pos[1]+2, ego_pos[2]), (ego_pos[0]-10, ego_pos[1]-2, ego_pos[2]), (ego_pos[0]-14, ego_pos[1]+5, ego_pos[2]), (ego_pos[0]+5, ego_pos[1]-1, ego_pos[2]), (ego_pos[0]+8, ego_pos[1]-4, ego_pos[2]), (ego_pos[0]+10, ego_pos[1]+9, ego_pos[2]), (ego_pos[0]+1, ego_pos[1]+11, ego_pos[2])] ps1 = [(ego_pos[0] - 10, ego_pos[1] + 3, ego_pos[2]), (ego_pos[0] - 10, ego_pos[1] - 1, ego_pos[2]), (ego_pos[0] - 14, ego_pos[1] + 5, ego_pos[2]), (ego_pos[0] + 5, ego_pos[1] - 1, ego_pos[2]), (ego_pos[0] + 8, ego_pos[1] - 4, ego_pos[2]), (ego_pos[0] + 10, ego_pos[1] + 9, ego_pos[2]), (ego_pos[0] + 1, ego_pos[1] + 11, ego_pos[2])] ps_oldorig = [(ego_pos[0]-7, ego_pos[1]+3, ego_pos[2]), (ego_pos[0]+5, ego_pos[1]-1, ego_pos[2]), (ego_pos[0]+4, ego_pos[1]+5, ego_pos[2]), (ego_pos[0]+5, ego_pos[1]-1, ego_pos[2]), (ego_pos[0]+8, ego_pos[1]-4, ego_pos[2]), (ego_pos[0]+10, ego_pos[1]+9, ego_pos[2]), (ego_pos[0]+1, ego_pos[1]+11, ego_pos[2])] for i in range(len(tvs)): scenario.add_vehicle(tvs[i], pos=ps[i], rot_quat=ego_rot_quat) scenario.make(beamng) bng = beamng.open(launch=True) try: bng.load_scenario(scenario) bng.set_steps_per_second(60) bng.set_deterministic() bng.hide_hud() bng.start_scenario() vehicle.ai_set_mode('traffic') vehicle.ai_drive_in_lane(True) #vehicle.ai_set_speed(16, mode='limit') #vehicle.ai_set_target('traffic') vehicle.ai_set_aggression(0.5) #0.7) bng.start_traffic(tvs) bng.switch_vehicle(vehicle) start = time.time() end = time.time() damage_prev = bng.poll_sensors(vehicle) with open('lidar_data.csv', 'w') as f: for _ in range(1024): sensors = bng.poll_sensors(vehicle) points = sensors['lidar']['points'] #print(points.tolist()) #print() #f.write("{}\n".format(points.tolist())) print("Time passed since last step: {}".format(time.time() - end)) end = time.time() print("Time passed since scenario begun: {}\n".format(end - start)) if end - start >= 30: bng.close() bng.step(30, wait=False) except Exception as e: print(e) finally: bng.close()
def add_sensor_to(self, vehicle: Vehicle) -> None: from beamngpy.sensors import Lidar vehicle.attach_sensor(self.rid, Lidar(max_dist=self.max_dist))