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_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 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 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(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 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 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 test_vehicle_ai(beamng): with beamng as bng: bng.set_deterministic() scenario = Scenario('west_coast_usa', 'ai_test') vehicle = Vehicle('test_car', model='etk800') other = Vehicle('other', model='etk800') pos = [-717.121, 101, 118.675] scenario.add_vehicle(vehicle, pos=pos, rot=(0, 0, 45)) scenario.add_vehicle(other, pos=(-453, 700, 75), rot=(0, 0, 45)) scenario.make(bng) bng.load_scenario(scenario) bng.start_scenario() bng.pause() vehicle.ai_set_mode('span') assert_continued_movement(bng, vehicle, pos) bng.restart_scenario() bng.pause() vehicle.ai_set_waypoint('Bridge4_B') assert_continued_movement(bng, vehicle, pos) bng.restart_scenario() bng.pause() vehicle.ai_set_target('other', mode='chase') assert_continued_movement(bng, vehicle, pos) bng.restart_scenario() bng.pause() vehicle.ai_set_target('other', mode='flee') assert_continued_movement(bng, vehicle, pos) scenario.delete(beamng)
def test_vehicle_bbox(beamng): scenario = Scenario('west_coast_usa', 'bbox_test') vehicle_a = Vehicle('vehicle_a', model='etk800') vehicle_b = Vehicle('vehicle_b', model='etk800') pos = [-717.121, 101, 118.675] scenario.add_vehicle(vehicle_a, pos=pos, rot=(0, 0, 45)) pos = [-453, 700, 75] scenario.add_vehicle(vehicle_b, pos=pos, rot=(0, 0, 45)) scenario.make(beamng) with beamng as bng: bng.load_scenario(scenario) bng.start_scenario() bng.pause() bbox_beg = bng.get_vehicle_bbox(vehicle_a) vehicle_a.ai_set_mode('span') bng.step(2000, True) bbox_end = bng.get_vehicle_bbox(vehicle_a) for k, v in bbox_beg.items(): assert k in bbox_end assert v != bbox_end[k]
scenario.add_vehicle(vehicle, pos=(0, 0, 0), rot=(0, 0, 0)) # Has to be the correct folder. destination_path = 'C:\\Users\\fraun\\Documents\\BeamNG\\levels\\drivebuild\\scenarios' scenario.path = Path(destination_path) prefab_path = Path(destination_path).joinpath(Path(scenario_name + ".prefab")) cov_collector = CoverageCollector(vehicle, bng, prefab_path) bng.open() bng.load_scenario(scenario) bng.start_scenario() vehicle.ai_drive_in_lane(True) vehicle.ai_set_speed(20, "limit") vehicle.ai_set_mode("span") # Data collecting loop. Collects every three steps data. counter = 0 last_counter = 0 steps = 2 while counter < 300: if counter > last_counter + steps: cov_collector.collect() counter += 1 if counter % 10 == 0: print(counter) # bng.stop_scenario() # not working? # bng.restart_scenario() bng.close() # adds binned behavior to dict of road
import mmap
def main(MAX_SPEED): setup_logging() # Gains to port TORCS actuators to BeamNG # steering_gain = translate_steering() acc_gain = 0.5 # 0.4 brake_gain = 1.0 # BeamNG images are too bright for DeepDrive brightness = 0.4 # Set up first vehicle # ! A vehicle with the name you specify here has to exist in the scenario vehicle = Vehicle('egovehicle') # Set up sensors resolution = (280, 210) # Original Settings #pos = (-0.5, 1.8, 0.8) # Left/Right, Front/Back, Above/Below # 0.4 is inside pos = (0, 2.0, 0.5) # Left/Right, Front/Back, Above/Below direction = (0, 1, 0) # direction = (180, 0, 180) # FOV 60, MAX_SPEED 100, 20 (3) Hz fails # FOV 60, MAX_SPEED 80, 20 (3) Hz Ok # FOV 60, MAX_SPEED 80, 12 Hz Ok-ish Oscillations # FOV 60, MAX_SPEED 80, 10 Hz Ok-ish Oscillations # FOV 40, MAX_SPEED 50, 12 Hz Seems to be fine but drives slower # FOV 40, MAX_SPEED 80, 10 Hz Seems to be fine but drives slower fov = 60 # MAX_SPEED = 70 MAX_FPS = 60 SIMULATION_STEP = 6 # Running the controller at 20 hz makes experiments 3 to 4 times slower ! 5 minutes of simulations end up sucking 20 minutes ! # # WORKING SETTINGS: 20 Freq, 90 FOV. front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) electrics = Electrics() vehicle.attach_sensor('front_cam', front_camera) vehicle.attach_sensor('electrics', electrics) # Setup the SHM with DeepDrive # Create shared memory object Memory = sd.CSharedMemory(TargetResolution=[280, 210]) # Enable Pause-Mode Memory.setSyncMode(True) Memory.Data.Game.UniqueRaceID = int(time.time()) print("Setting Race ID at ", Memory.Data.Game.UniqueRaceID) # Setting Max_Speed for the Vehicle. # TODO What's this? Maybe some hacky way to pass a parameter which is not supposed to be there... Memory.Data.Game.UniqueTrackID = int(MAX_SPEED) # Speed is KM/H print("Setting speed at ", Memory.Data.Game.UniqueTrackID) # Default for AsFault Memory.Data.Game.Lanes = 1 # By default the AI is in charge Memory.Data.Control.IsControlling = 1 deep_drive_engaged = True STATE = "NORMAL" Memory.waitOnRead() if Memory.Data.Control.Breaking == 3.0 or Memory.Data.Control.Breaking == 2.0: print("\n\n\nState not reset ! ", Memory.Data.Control.Breaking) Memory.Data.Control.Breaking = 0.0 # Pass the computation to DeepDrive # Not sure this will have any effect Memory.indicateWrite() Memory.waitOnRead() if Memory.Data.Control.Breaking == 3.0 or Memory.Data.Control.Breaking == 2.0: print("\n\n\nState not reset Again! ", Memory.Data.Control.Breaking) Memory.Data.Control.Breaking = 0.0 # Pass the computation to DeepDrive Memory.indicateWrite() # Connect to running beamng beamng = BeamNGpy( 'localhost', 64256, home='C://Users//Alessio//BeamNG.research_unlimited//trunk') bng = beamng.open(launch=False) try: bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(MAX_FPS) # With 60hz temporal resolution # Connect to the existing vehicle (identified by the ID set in the vehicle instance) bng.connect_vehicle(vehicle) # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt # Road interface is not available in BeamNG.research yet # Get the road map from the level # roads = bng.get_roads() # # find the actual road. Dividers lane markings are all represented as roads # theRoad = None # for road in enumerate(roads): # # ((left, centre, right), (left, centre, right), ...) # # Compute the width of the road # left = Point(road[0][0]) # right = Point(road[0][1]) # distance = left.distance( right ) # if distance < 2.0: # continue # else: # theRoad = road; # break # # if theRoad is None: # print("WARNING Cannot find the main road of the map") while True: # Resume the execution # 6 steps correspond to 10 FPS with a resolution of 60FPS # 5 steps 12 FPS # 3 steps correspond to 20 FPS bng.step(SIMULATION_STEP) # Retrieve sensor data and show the camera data. sensors = bng.poll_sensors(vehicle) # print("vehicle.state", vehicle.state) # # TODO: Is there a way to query for the speed directly ? speed = math.sqrt( vehicle.state['vel'][0] * vehicle.state['vel'][0] + vehicle.state['vel'][1] * vehicle.state['vel'][1]) # Speed is M/S ? # print("Speed from BeamNG is: ", speed, speed*3.6) imageData = preprocess(sensors['front_cam']['colour'], brightness) Height, Width = imageData.shape[:2] # print("Image size ", Width, Height) # TODO Size of image should be right since the beginning Memory.write(Width, Height, imageData, speed) # Pass the computation to DeepDrive Memory.indicateWrite() # Wait for the control commands to send to the vehicle # This includes a sleep and will be unlocked by writing data to it Memory.waitOnRead() # TODO Assumption. As long as the car is out of the road for too long this value stays up if Memory.Data.Control.Breaking == 3.0: if STATE != "DISABLED": print( "Abnormal situation detected. Disengage DeepDrive and enable BeamNG AI" ) vehicle.ai_set_mode("manual") vehicle.ai_drive_in_lane(True) vehicle.ai_set_speed(MAX_SPEED) vehicle.ai_set_waypoint("waypoint_goal") deep_drive_engaged = False STATE = "DISABLED" elif Memory.Data.Control.Breaking == 2.0: if STATE != "GRACE": print("Grace period. Deep Driving still disengaged") vehicle.ai_set_mode("manual") vehicle.ai_set_waypoint("waypoint_goal") # vehicle.ai_drive_in_lane(True) STATE = "GRACE" else: if STATE != "NORMAL": print("DeepDrive re-enabled") # Disable BeamNG AI driver vehicle.ai_set_mode("disabled") deep_drive_engaged = True STATE = "NORMAL" # print("State ", STATE, "Memory ",Memory.Data.Control.Breaking ) if STATE == "NORMAL": vehicle.ai_set_mode("disabled") # Get commands from SHM # Apply Control - not sure cutting at 3 digit makes a difference steering = round( translate_steering(Memory.Data.Control.Steering), 3) throttle = round(Memory.Data.Control.Accelerating * acc_gain, 3) brake = round(Memory.Data.Control.Breaking * brake_gain, 3) # Apply commands vehicle.control(throttle=throttle, steering=steering, brake=brake) # # print("Suggested Driving Actions: ") # print(" Steer: ", steering) # print(" Accel: ", throttle) # print(" Brake: ", brake) 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 main(): """ Generate a bunch of images by driving along a predefined sequence of points, while capturing camera images to JPG files. :return: (None) """ 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') # Asphalt and lines are actually considered as differently colored roads by beamngpy. 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') # Create a dash cam that is somewhat down-sloped. 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) # Get a spawn point and initial rotation of the vehicle. 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 < NUMBER_OF_IMAGES: bng.poll_sensors(vehicle) number_of_images += 1 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 run_scenario(traffic=2, run_number=0): global sp setup_logging() beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.1') 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') vehicle = setup_sensors(vehicle) lidar = setup_lidar('hood') 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(traffic) ps = generate_vehicle_positions(ego_pos, ego_rot_quat, tvs) 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() #print("Bounding box: {}".format(vehicle.get_bbox())) 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.9) bng.start_traffic(tvs) bng.switch_vehicle(vehicle) start = time.time() end = time.time() damage_prev = None with open( 'H:/traffic_traces/traffic_lidar_data_{}traffic_run{}.csv'. format(traffic, run_number), 'w') as f: f.write( "TIMESTAMP,VEHICLE_POSITION,VEHICLE_ORIENTATION,VELOCITY,LIDAR,CRASH,EXTERNAL_VEHICLES \n" ) for _ in range(1024): sensors = bng.poll_sensors(vehicle) points = sensors['lidar']['points'] damage = sensors['damage'] v_state = vehicle.state #print("vehicle_state = {}".format(v_state)) ai_state = vehicle.ai_get_state() #print("ai_state = {}".format(ai_state)) new_damage = diff_damage(damage, damage_prev) damage_prev = damage if new_damage > 0: print("new damage = {}".format(new_damage)) f.write("{},{},{},{},{},{},{}\n".format( _ * 0.5, v_state['pos'], v_state['dir'], v_state['vel'], points.tolist(), str(new_damage), traffic)) #print("Time passed since last step: {}".format(time.time() - end)) end = time.time() #print("Time passed since scenario begun: {}\n".format(end - start)) print() if end - start >= 45: bng.close() bng.step(30, wait=False) except Exception as e: print(e) finally: bng.close()
def main() -> None: from beamngpy import BeamNGpy, Scenario, Vehicle, beamngcommon from beamngpy.sensors import Damage, Camera, Electrics from time import sleep bng = BeamNGpy("localhost", 64523) ego_vehicle = Vehicle('ego_vehicle', model='etk800', licence='EGO', color='Red') scenario = Scenario( "west_coast_usa", "DamageSensorTest", authors="Stefan Huber", description="Test usage and check output of the damage sensor") direction = (0, 1, 0) fov = 120 resolution = (320, 160) y, z = (1.7, 1.0) cam_center = Camera((-0.3, y, z), direction, fov, resolution, colour=True, depth=True, annotation=True) cam_left = Camera((-1.3, y, z), direction, fov, resolution, colour=True, depth=True, annotation=True) cam_right = Camera((0.4, y, z), direction, fov, resolution, colour=True, depth=True, annotation=True) #cameras_array = [camera_center, camera_left, camera_right] ego_vehicle.attach_sensor('cam_center', cam_center) ego_vehicle.attach_sensor('cam_left', cam_left) ego_vehicle.attach_sensor('cam_right', cam_right) ego_vehicle.attach_sensor("electrics", Electrics()) #electrics_data = Electrics.encode_vehicle_request(Electrics) #print(electrics_data) #scenario.add_vehicle(ego_vehicle,pos=(-717.121, 101, 118.675), rot=(0, 0, 45)) scenario.add_vehicle(ego_vehicle, pos=(-725.7100219726563, 554.3270263671875, 121.0999984741211), rot=(0, 0, 45)) scenario.make(bng) bng.open(launch=True) def save_image(data, ind, cam_name): img = data[cam_name]['colour'].convert('RGB') file_name = str(ind) + "_" + cam_name + ".jpg" filepath = os.path.join('C:/Users/HP/Documents/Data_Log/10_lap_log', file_name) img.save(filepath) return file_name def save(data, ind): cam_left_file_name = save_image(data, ind, 'cam_left') cam_right_file_name = save_image(data, ind, 'cam_right') cam_center_file_name = save_image(data, ind, 'cam_center') steering_in_value = data['electrics']['values']['steering_input'] steering_value = data['electrics']['values']['steering'] throttle_in_value = data['electrics']['values']['throttle_input'] throttle_value = data['electrics']['values']['throttle'] clutch_value = data['electrics']['values']['clutch'] clutch_in_value = data['electrics']['values']['clutch_input'] wheel_speed_value = data['electrics']['values']['wheelspeed'] rpmspin_value = data['electrics']['values']['rpmspin'] #add here print(cam_left_file_name, cam_right_file_name, cam_center_file_name, steering_in_value, steering_value, throttle_in_value, throttle_value, clutch_in_value, clutch_value, rpmspin_value, wheel_speed_value) print() temp_df = temp_dataframe() temp_df.loc[0] = [ cam_left_file_name, cam_right_file_name, cam_center_file_name, steering_in_value, steering_value, throttle_in_value, throttle_value, clutch_in_value, clutch_value, wheel_speed_value, rpmspin_value ] # modify #append with existing and save df_orig = pd.read_csv('my_data_lap3.csv') df_orig = pd.concat([df_orig, temp_df]) df_orig.to_csv('my_data_lap3.esc', index=False) del [[df_orig, temp_df]] def temp_dataframe(): df1 = pd.DataFrame(columns=[ 'cam_left', 'cam_right', 'cam_centre', 'steering_in_value', 'steering_value', 'throttle_in_value', 'throttle_value', 'clutch_in_value', 'clutch_value', 'wheelspeed_value', 'rpmspin_value' ]) # modify return df1 try: bng.load_scenario(scenario) bng.set_steps_per_second(60) bng.set_deterministic() bng.start_scenario() bng.hide_hud() ego_vehicle.ai_drive_in_lane(lane=True) ego_vehicle.ai_set_mode('span') ego_vehicle.ai_set_speed(10) ind = 0 while True: sensor_data = bng.poll_sensors(ego_vehicle) save(sensor_data, ind) ind += 1 finally: bng.close()
class DataCollector: def __init__(self): self.collected_data = {"image_name": [], "steering": []} self.scenario = Scenario(config.get("data_collecting.scenario_level"), config.get("data_collecting.scenario_name")) self.vehicle = Vehicle( 'ego_vehicle', model=config.get("data_collecting.vehicle_model"), licence='PYTHON') self.bng = BeamNGpy(config.get("beamNG.host"), int(config.get("beamNG.port")), home=config.get("beamNG.home")) def launch_beam_ng(self, mode="manual_mode"): # Create an ETK800 with the licence plate 'PYTHON' electrics = Electrics() # attach to get steering angles self.vehicle.attach_sensor('electrics', electrics) # attach to get images from camera self.vehicle.attach_sensor('front_cam', self.create_camera_sensor()) # Add it to our scenario at this position and rotation # self.scenario.add_vehicle(self.vehicle) self.scenario.add_vehicle( self.vehicle, pos=tuple(map(float, config.get("data_collecting.pos").split(","))), rot_quat=tuple( map(float, config.get("data_collecting.rot_quat").split(",")))) # Place files defining our scenario for the simulator to read self.scenario.make(self.bng) # Launch BeamNG.research self.bng.open() # Load and start our scenario self.bng.load_scenario(self.scenario) self.bng.start_scenario() if mode == "ai_mode": # Make the vehicle's AI span the map self.vehicle.ai_drive_in_lane(True) self.vehicle.ai_set_mode('span') def save_image_manually(self, cam_name='front_cam'): img = self.bng.poll_sensors(self.vehicle)[cam_name]['colour'] steering = self.bng.poll_sensors(self.vehicle)['electrics']['steering'] self.save_data(img, steering) @staticmethod def create_camera_sensor( pos=(-0.3, 2, 1.0), direction=(0, 1, 0), fov=100, res=None): # Set up camera sensor resolution = res if res is None: resolution = (int(config.get("data_collecting.image_width")), int(config.get("data_collecting.image_height"))) pos = pos direction = direction fov = fov front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) return front_camera def save_data(self, img, steering, i: str = "0"): file_name = str(int(time.time())) + i + ".jpg" try: image_path = definitions.ROOT_DIR + config.get( 'data_collecting.data_path') + file_name imageio.imwrite(image_path, np.asarray(img.convert('RGB')), "jpg") self.collected_data["image_name"].append(file_name) self.collected_data["steering"].append(steering) except Exception as ex: logger.info(f"Error while saving data -- {ex}") raise Exception def collect_data(self, number_of_images: int, mode="manual_mode"): self.launch_beam_ng(mode) logger.info("Start after 3 seconds...") time.sleep(5) logger.info( f"Start collecting {config.get('data_collecting.number_of_images')} training images" ) i = 0 exit_normally = True try: while i < number_of_images: # image is training image and steering is label img = self.bng.poll_sensors( self.vehicle)['front_cam']['colour'] steering = self.bng.poll_sensors( self.vehicle)['electrics']['steering'] logger.info(f"Saving data {i + 1} ...") self.save_data(img, steering, str(i)) logger.info("Saved data successfully") i = i + 1 time.sleep(int(config.get("data_collecting.sleep"))) except Exception as ex: exit_normally = False logger.info(f"Error while collecting data {ex}") finally: self.bng.close() return exit_normally def save_csv_data(self): logger.info("Start saving csv file......") csv_path = definitions.ROOT_DIR + config.get( 'data_collecting.csv_path') df = pd.DataFrame(self.collected_data, columns=['image_name', 'steering']) if not os.path.isfile(csv_path): df.to_csv(csv_path, index=False, header=True) else: # else it exists so append without writing the header df.to_csv(csv_path, index=False, mode='a', header=False)
def test_vehicle_ai(beamng): with beamng as bng: bng.set_deterministic() scenario = Scenario('west_coast_usa', 'ai_test') vehicle = Vehicle('test_car', model='etk800') other = Vehicle('other', model='etk800') pos = [-717.121, 101, 118.675] scenario.add_vehicle(vehicle, pos=pos, rot=(0, 0, 45)) scenario.add_vehicle(other, pos=(-453, 700, 75), rot=(0, 0, 45)) scenario.make(bng) bng.load_scenario(scenario) bng.start_scenario() bng.pause() vehicle.ai_set_mode('span') assert_continued_movement(bng, vehicle, pos) bng.restart_scenario() bng.pause() vehicle.ai_set_waypoint('Bridge4_B') assert_continued_movement(bng, vehicle, pos) bng.restart_scenario() bng.pause() vehicle.ai_set_target('other', mode='chase') assert_continued_movement(bng, vehicle, pos) bng.restart_scenario() bng.pause() vehicle.ai_set_target('other', mode='flee') assert_continued_movement(bng, vehicle, pos) bng.restart_scenario() bng.pause() script = [ { 'x': -735, 'y': 86.7, 'z': 119, 't': 0 }, { 'x': -752, 'y': 70, 'z': 119, 't': 5 }, { 'x': -762, 'y': 60, 'z': 119, 't': 8 }, ] vehicle.ai_set_script(script) bng.step(600, wait=True) vehicle.update_vehicle() ref = [script[1]['x'], script[1]['y'], script[1]['z']] pos = vehicle.state['pos'] ref, pos = np.array(ref), np.array(pos) assert np.linalg.norm(ref - pos) < 2.5 scenario.delete(beamng)
def main(): setup_logging() beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.1') scenario = Scenario( 'west_coast_usa', 'lidar_tour', description='Tour through the west coast gathering Lidar data') vehicle = Vehicle('ego_vehicle', model='etk800', licence='LIDAR') vehicle = setup_sensors(vehicle) lidar = setup_lidar('hood') vehicle.attach_sensor('lidar', lidar) spawn_pt = spawn_point() ego_pos = spawn_pt['pos'] ego_rot_quat = spawn_pt['rot_quat'] scenario.add_vehicle(vehicle, pos=ego_pos, rot=None, rot_quat=ego_rot_quat) tvs = traffic_vehicles() 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_set_aggression(0.5) vehicle.ai_drive_in_lane(True) #vehicle.ai_set_speed(16, mode='limit') #vehicle.ai_set_target('traffic') bng.switch_vehicle(vehicle) damage_prev = None start = time.time() end = time.time() bng.pause() print("Bounding box: {}".format(vehicle.get_bbox())) with open('lidar_data.csv', 'w') as f: f.write( "TIMESTAMP,VEHICLE_POSITION,VEHICLE_ORIENTATION,VELOCITY,LIDAR,CRASH\n" ) for _ in range(1024): sensors = bng.poll_sensors(vehicle) points = sensors['lidar']['points'] damage = sensors['damage'] v_state = vehicle.state print("vehicle_state = {}".format(v_state)) print("vehicle_state[pos] = {}".format(v_state['pos'])) print("vehicle_state[dir] = {}".format(v_state['dir'])) print("Vehicle bounding box:{}".format(vehicle.get_bbox())) #ai_state = vehicle.ai_get_state() #print("ai_state = {}".format(ai_state)) new_damage = diff_damage(damage, damage_prev) damage_prev = damage #print("new damage = {}".format(new_damage)) print() f.write("{},{},{},{},{},{}\n".format(_ * 0.5, v_state['pos'], v_state['dir'], v_state['vel'], points.tolist(), str(new_damage))) #bng.step(30, wait=False) bng.step(30) #print("Time passed since last step: {}".format(time.time() - end)) end = time.time() #print("Time passed since scenario begun: {}\n".format(end - start)) #screenrecord() if end - start >= 30: #bng.close() continue
# 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=(-717.121, 101, 118.675), rot=(0, 0, 45)) # 45 degree rotation around the z-axis # 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') positions = list() directions = list() wheel_speeds = list() throttles = list() brakes = list() 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'])
def GenerateTrack(trackLength, sampleRate, show, startBeamNG): populationSize = 100 maxAcc = 1 times = 20 relNewSize = 0.6 duplicatesThreshold = 0.05 intersectionDelta = 0.01 mutationProb = 0.1 mutationDeviation = 0.01 print("Generating Tracks") pop = initPop(populationSize, trackLength, maxAcc, 20) pop = evolve(pop, times, relNewSize, duplicatesThreshold, intersectionDelta, mutationProb, mutationDeviation, 10) print("eliminating intersecting tracks") pop = elminateIntersecting(pop, intersectionDelta) if show: plotTrack(pop, 100) tracks = [] nr = 0 first = True for track in pop: track = np.vstack((track, completeAcc(track))) poly = polysFromAccs(track) bez = bezFromPolys(poly) smpl = sampleBezSeries(bez, sampleRate).transpose() smpl = scaleTrack(smpl, 100, 100) smpl = np.array(smpl) smpl = np.vstack((smpl, [smpl[0]])) if(first): writeCriteria(smpl) first = False tracks.append(smpl) writeTrack(smpl, nr) nr += 1 if startBeamNG: nodes = [] for p in tracks[0]: nodes.append((p[0], p[1], 0, 7)) beamng = BeamNGpy('localhost', 64256) vehicle = Vehicle('ego', model='etk800', licence='PYTHON', colour='Green') scenario = Scenario('GridMap_v0422', 'track test') scenario.add_vehicle(vehicle, pos=(0, 0, -16.64), rot=(0, 0, 180)) road = Road(material='track_editor_C_center', rid='main_road', texture_length=5, looped=True) road.nodes.extend(nodes) scenario.add_road(road) scenario.make(beamng) beamng.open() beamng.load_scenario(scenario) beamng.start_scenario() vehicle.ai_set_mode('span') while 1: vehicle.update_vehicle() print(vehicle.state['pos']) sleep(1)
# scenario.add_object(target_for_ai) scenario.make(bng) # Launch BeamNG.research bng.open() # Load and start our scenario bng.load_scenario(scenario) bng.start_scenario() # skt = bng.start_server() # Make the vehicle's AI span the map # vehicle.ai_set_mode('span') # vehicle.connect(bng, skt.host, skt.port) # ai_vehicle.connect(bng, skt.host, skt.port) # ai_vehicle.ai_set_target(target=str(vehicle.vid)) ai_vehicle.ai_set_mode('manual') ai_vehicle.ai_set_target(target=str(wood_obj.vid)) # print(ai_vehicle.options) log.debug("State of the vehicle: " + str(ai_vehicle.state)) if int(vehicle.state['pos'][0]) in range(int(ai_vehicle.state['pos'][0]), int(ai_vehicle.state['pos'][0]) + 10): print( 'Good!sfgshjkssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss' ) # ai_vehicle.state[0] = -700 ai_vehicle.ai_set_mode('stopping') log.debug('Super!')
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 scenario.update() dist_car_1 = np.linalg.norm( np.array(vut.state['pos']) - np.array(car_1.state['pos']))
scenario.add_vehicle(vehicle, pos=(104.647, -1.92827, -3.54338), rot=(0, 0, 90)) #add vehicle as Obstacle vehicle1 = Vehicle('test_vehicle', model='etk800', licence='RED', color='Blue') scenario.add_vehicle(vehicle1, pos=(-9.66595, -90.7465, -3.45737), rot=(0, 0, 180)) scenario.make(beamng) bng = beamng.open() bng.load_scenario(scenario) #set the ego vehicle to AI mode vehicle.ai_set_mode('Manual') vehicle.ai_set_waypoint('DecalRoad5248_4') vehicle1.control(throttle=0.44) bng.start_scenario() # After loading, the simulator waits for further input to actually start positions = list() directions = list() wheel_speeds = list() speed = list() brakes = list() damage1 = list() vehicle.update_vehicle() sensors = bng.poll_sensors(vehicle) print('The vehicle position is:')
def main(): global base_filename, training_dir, default_model f = setup_dir(training_dir) spawn_pt = spawn_point(default_scenario) random.seed(1703) setup_logging() beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.1') scenario = Scenario(default_scenario, 'research_test') vehicle = Vehicle('ego_vehicle', model=default_model, licence='RED', color='Red') vehicle = setup_sensors(vehicle) scenario.add_vehicle(vehicle, pos=spawn_pt['pos'], rot=None, rot_quat=spawn_pt['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) images = [] 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() vehicle.ai_set_mode('span') vehicle.ai_drive_in_lane(True) vehicle.ai_set_aggression(0.1) # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt # Send random inputs to vehice and advance the simulation 20 steps imagecount = 0 wheelvel = [0.1, 0.1, 0.1] with open(f, 'w') as datafile: datafile.write('filename,timestamp,steering_input,throttle_input,brake_input,driveshaft,engine_load,fog_lights,fuel,' 'lowpressure,oil,oil_temperature,parkingbrake,rpm,water_temperature\n') #for _ in range(1024): for _ in range(32768): #throttle = 1.0 #random.uniform(0.0, 1.0) #steering = random.uniform(-1.0, 1.0) #brake = random.choice([0, 0, 0, 1]) #vehicle.control(throttle=throttle) # collect images sensors = bng.poll_sensors(vehicle) image = sensors['front_cam']['colour'].convert('RGB') imagecount += 1 filename = "{}{}.bmp".format(base_filename, imagecount) # collect ancillary data datafile.write('{},{},{},{},{},{},{},{},{},{},{},{},{},{}\n'.format(filename, str(round(sensors['timer']['time'], 2)), sensors['electrics']['steering_input'], sensors['electrics']['throttle_input'], sensors['electrics']['brake_input'], sensors['electrics']['driveshaft'], sensors['electrics']['engine_load'], sensors['electrics']['fog_lights'], sensors['electrics']['fuel'], sensors['electrics']['lowpressure'], sensors['electrics']['oil'], sensors['electrics']['oil_temperature'], sensors['electrics']['parkingbrake'], sensors['electrics']['rpm'], sensors['electrics']['water_temperature'],)) # save the image image.save(filename) # step sim forward bng.step(20) print('{} seconds passed.'.format(str(round(sensors['timer']['time'], 2)))) # check for stopping condition for i in range(len(wheelvel)-1): wheelvel[i] = wheelvel[i+1] wheelvel[2] = float(sensors['electrics']['wheelspeed']) print('wheelvel = {}'.format(sum(wheelvel) / 3.0 )) if sum(wheelvel) / 3.0 == 0.0: print("avg wheelspeed is zero; exiting...") bng.close() break
def scenario(beamNG_path): if (beamNG_path == ""): beamNG_path = 'D:/BeamNGReasearch/Unlimited_Version/trunk' bng = BeamNGpy('localhost', 64256, beamNG_path) #scenario = Scenario('west_coast_usa', 'example') scenario = Scenario('GridMap', 'example') # Create an ETK800 with the licence plate 'PYTHON' vehicle1 = Vehicle('ego_vehicle', model='etk800', licence='PYTHON', color='Red') vehicle2 = Vehicle('vehicle', model='etk800', licence='CRASH', color='Blue') electrics = Electrics() vehicle1.attach_sensor('electrics', electrics) pos2 = (-13.04469108581543, -107.0409164428711, 0.202297180891037) pos1 = (-4.270055770874023, -115.30198669433594, 0.20322345197200775) rot2 = (0.0009761620895005763, -0.9999936819076538, -0.0034209610894322395) rot1 = (0.872300386428833, -0.48885437846183777, 0.01065115723758936) scenario.add_vehicle(vehicle1, pos=pos1, rot=rot1) scenario.add_vehicle(vehicle2, pos=pos2, rot=rot2) scenario.make(bng) # Launch BeamNG.research bng.open(launch=True) SIZE = 500 try: bng.load_scenario(scenario) bng.start_scenario() vehicle1.ai_set_speed(10.452066507339481,mode = 'set') vehicle1.ai_set_mode('span') vehicle2.ai_set_mode('chase') #collect data positions = list() directions = list() wheel_speeds = list() for _ in range(100): time.sleep(0.1) vehicle1.update_vehicle() # Synchs the vehicle's "state" variable with the simulator sensors = bng.poll_sensors(vehicle1) # Polls the data of all sensors attached to the vehicle positions.append(vehicle1.state['pos']) directions.append(vehicle1.state['dir']) wheel_speeds.append(sensors['electrics']['values']['wheelspeed']) print('The Final result - position :') print(positions) print('The Final result - direction :') print(directions) print('The Final result - speed :') print(wheel_speeds) bng.stop_scenario() bng.close() finally: bng.close()
def testrun(speed=11, risk=0.6): global base_filename, training_dir, default_model, setpoint global spawnpoint f = setup_dir(training_dir) spawn_pt = spawn_point(default_scenario, spawnpoint) random.seed(1703) setup_logging() beamng = BeamNGpy('localhost', 64256, home='H:/BeamNG.research.v1.7.0.1clean', user='******') scenario = Scenario(default_scenario, 'research_test') # add barriers and cars to get the ego vehicle to avoid the barriers add_barriers(scenario) # add_barrier_cars(scenario) vehicle = Vehicle('ego_vehicle', model=default_model, licence='RED', color='White') vehicle = setup_sensors(vehicle) scenario.add_vehicle(vehicle, pos=spawn_pt['pos'], rot=None, rot_quat=spawn_pt['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_nondeterministic() # Set simulator to be deterministic bng.set_steps_per_second(36) # # Load and start the scenario bng.load_scenario(scenario) bng.start_scenario() bng.switch_vehicle(vehicle) vehicle.ai_set_mode('traffic') vehicle.ai_drive_in_lane(False) vehicle.ai_set_speed(speed, mode='set') vehicle.ai_set_aggression(risk) # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt # bng.resume() start_time = time.time() # Send random inputs to vehicle and advance the simulation 20 steps imagecount = 0 with open(f, 'w') as datafile: datafile.write( 'filename,timestamp,steering_input,throttle_input,brake_input,driveshaft,engine_load,fog_lights,fuel,' 'lowpressure,oil,oil_temperature,parkingbrake,rpm,water_temperature,wheelspeed\n' ) reached_start = False vels = [] vel_dict = {} while imagecount < 10000: # collect images sensors = bng.poll_sensors(vehicle) image = sensors['front_cam']['colour'].convert('RGB') full_filename = "{}{}{}.jpg".format(training_dir, base_filename, imagecount) qualified_filename = "{}{}.jpg".format(base_filename, imagecount) # collect ancillary data datafile.write( '{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}\n'.format( qualified_filename, str( round(sensors['timer']['time'], 2)), sensors['electrics']['steering_input'], sensors['electrics']['throttle_input'], sensors['electrics']['brake_input'], sensors['electrics']['driveshaft'], sensors['electrics']['engine_load'], sensors['electrics']['fog_lights'], sensors['electrics']['fuel'], sensors['electrics']['lowpressure'], sensors['electrics']['oil'], sensors['electrics']['oil_temperature'], sensors['electrics']['parkingbrake'], sensors['electrics']['rpm'], sensors['electrics']['water_temperature'], sensors['electrics']['wheelspeed'])) if sensors['timer']['time'] > 10: kph = sensors['electrics']['wheelspeed'] * 3.6 vels.append(kph) vel_dict[sensors['timer']['time']] = kph if distance(spawn_pt['pos'], vehicle.state['pos'] ) < 5 and sensors['timer']['time'] > 10: reached_start = True break if sensors['damage']['damage'] > 0: print("CRASHED; QUITTING") break # save the image image.save(full_filename) imagecount += 1 # plt.title("90 degrees FOV") # plt.imshow(image) # plt.pause(0.01) # plt.title("120 degrees FOV") # plt.imshow(sensors['back_cam']['colour'].convert('RGB')) # plt.pause(0.01) # step sim forward bng.step(1, wait=True) wheelspeed_avg = round((sum(vels) / float(len(vels))), 3) wheelspeed_var = round(np.var(vels), 3) return_str = '' if sensors['damage']['damage'] > 0: # print("QUIT DUE TO CRASH VALUE {}".format(sensors['damage']['damage'])) return_str = "QUIT DUE TO CRASH VALUE {}".format( sensors['damage']['damage']) print(return_str) maxtime = from_val_get_key(vel_dict, max(vels)) mintime = from_val_get_key(vel_dict, min(vels)) info = "IMAGE COUNT:{}\nSIM TIME:{} WALLCLOCK TIME:{}\nWHEELSPEED AVG:{} VAR:{} \nMAX:{} @ timestep {} MIN:{} @ timestep {} ".format( imagecount, str(round(sensors['timer']['time'], 2)), time.time() - start_time, wheelspeed_avg, wheelspeed_var, round(max(vels), 3), round(maxtime, 3), round(min(vels), 3), round(mintime, 3)) print("SPEED:{} RISK:{}".format(speed, risk)) print(info) return_str = "{}\n{}".format(return_str, info) bng.close() return return_str
# 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=(0, 0, 0), rot=(0, 0, 45)) # 45 degree rotation around the z-axis print() # 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('disabled') vehicle.update_vehicle() sensors = bng.poll_sensors(vehicle) loopStartTime=datetime.datetime.now() for x in range(testTime*dataRate): loopIterationStartTime=time.time() 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 data=VehicleData(sensors['electrics'],sensors['damage'],sensors['GForces'],vehicle.state['pos'], vehicle.state['dir'],sensors['electrics']['steering']).getData() data={'time':str(((datetime.datetime.now()-loopStartTime))),'data':data}
def run_scenario_ai_version(vehicle_model='etk800', deflation_pattern=[0, 0, 0, 0], parts_config=None): global base_filename, default_color, default_scenario, default_spawnpoint, setpoint, steps_per_sec global prev_error 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='AI', color=default_color) vehicle = setup_sensors(vehicle) spawn = get_spawn_point(default_scenario, default_spawnpoint) # 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.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(steps_per_sec) # With 100hz temporal resolution # Load and start the scenario bng.load_scenario(scenario) bng.start_scenario() # create vehicle to be chased chase_vehicle = Vehicle('chase_vehicle', model='miramar', licence='CHASEE', color='Red') bng.spawn_vehicle(chase_vehicle, pos=(469.784, 346.391, 144.982), rot=None, rot_quat=(-0.0037852677050978, -0.0031219546217471, -0.78478640317917, 0.61974692344666)) bng.spawn_vehicle(vehicle, pos=spawn['pos'], rot=None, rot_quat=spawn['rot_quat'], partConfig=parts_config) # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt #bng.resume() # perturb vehicle print("vehicle position before deflation via beamstate:{}".format( vehicle.get_object_position())) print("vehicle position before deflation via vehicle state:{}".format( vehicle.state)) image = bng.poll_sensors(vehicle)['front_cam']['colour'].convert('RGB') plt.imshow(image) plt.pause(0.01) vehicle.deflate_tires(deflation_pattern) bng.step(steps_per_sec * 6) vehicle.update_vehicle() # print("vehicle position after deflation via beamstate:{}".format(vehicle.get_object_position())) # print("vehicle position after deflation via vehicle state:{}".format(vehicle.state)) pitch = vehicle.state['pitch'][0] roll = vehicle.state['roll'][0] z = vehicle.state['pos'][2] image = bng.poll_sensors(vehicle)['front_cam']['colour'].convert('RGB') plt.imshow(image) plt.pause(0.01) bng.resume() vehicle.ai_set_mode('chase') vehicle.ai_set_target('chase_vehicle') vehicle.ai_drive_in_lane(True) damage_prev = None runtime = 0.0 traj = [] kphs = [] for _ in range(650): image = bng.poll_sensors(vehicle)['front_cam']['colour'].convert('RGB') damage = bng.poll_sensors(vehicle)['damage'] wheelspeed = bng.poll_sensors(vehicle)['electrics']['wheelspeed'] new_damage = diff_damage(damage, damage_prev) damage_prev = damage runtime = bng.poll_sensors(vehicle)['timer']['time'] vehicle.update_vehicle() traj.append(vehicle.state['pos']) kphs.append(ms_to_kph(wheelspeed)) if new_damage > 0.0: break bng.step(5) bng.close() results = { 'runtime': round(runtime, 3), 'damage': damage, 'kphs': kphs, 'traj': traj, 'pitch': round(pitch, 3), 'roll': round(roll, 3), "z": round(z, 3), 'final_img': image } return results