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 __init__(self): direction = (0, 1, 0) fov = 120 resolution = (320, 160) y, z = 1.7, 1.0 cam_center = 'cam_center', Camera((-0.3, y, z), direction, fov, resolution, colour=True, depth=True, annotation=True) cam_left = 'cam_left', Camera((-1.3, y, z), direction, fov, resolution, colour=True, depth=True, annotation=True) cam_right = 'cam_right', Camera((0.4, y, z), direction, fov, resolution, colour=True, depth=True, annotation=True) self.cameras_array = [cam_center, cam_left, cam_right]
def __init__(self, training=False): direction = (0, 1, 0) fov = 120 resolution = (320, 160) y, z = 1.7, 1.0 cam_center = 'cam_center', Camera((-0.3, y, z), direction, fov, resolution, colour=True) if training: cam_left = 'cam_left', Camera((-1.3, y, z), direction, fov, resolution, colour=True) cam_right = 'cam_right', Camera((0.4, y, z), direction, fov, resolution, colour=True) self.cameras_array = [cam_center, cam_left, cam_right] else: self.cameras_array = [cam_center]
def setup_sensors(self, vehicle): # Set up sensors pos = (-0.3, 1, 1.0) direction = (0, 0.75, 0) #(0, 0.75, -1.5) #(0, 0.75, 0) #(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) return vehicle
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 setup_sensors(vehicle): # Set up sensors # pos = (-0.3, 1, 1.0) # default pos = (-0.5, 2, 1.0) #center edge of hood # pos = (-0.5, 1, 1.0) # center middle of hood # pos = (-0.5, 0.4, 1.0) # dashboard # pos = (-0.5, 0.38, 1.5) # roof # pos = (-0.5, 0.38, 1.1) # windshield pos = (-0.5, 0.38, 1.3) # windshield # direction = (0, 1, 0) direction = (0, 1.0, 0) fov = 50 resolution = (1280, 960) #(512, 512) front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) pos = (-0.5, 0.38, 1.1) # windshield direction = (0, 1, 0) fov = 120 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('headlight_cam', front_camera) vehicle.attach_sensor('front_cam', front_camera) vehicle.attach_sensor('windshield_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 _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 setup_sensors(vehicle, pos=(-0.5, 0.38, 1.3), direction=(0, 1.0, 0)): # Set up sensors # pos = (-0.3, 1, 1.0) # default #pos = (-0.5, 2, 1.0) #center edge of hood # pos = (-0.5, 1, 1.0) # center middle of hood # pos = (-0.5, 0.4, 1.0) # dashboard # pos = (-0.5, 0.38, 1.5) # roof # pos = (-0.5, 0.38, 1.3) # windshield # direction = (0, 1.0, 0) fov = 50 resolution = (200, 150) #(1280,960) #(512, 512) front_camera = Camera(pos, direction, fov, resolution, colour=True, depth=True, annotation=True) gforces = GForces() electrics = Electrics() damage = Damage() timer = Timer() # Attach them vehicle.attach_sensor('front_cam', front_camera) vehicle.attach_sensor('gforces', gforces) vehicle.attach_sensor('electrics', electrics) vehicle.attach_sensor('damage', damage) vehicle.attach_sensor('timer', timer) return vehicle
def __init__(self, beamng: BeamNGpy, name: str, camera: Camera = None): self.name = name self.pose: BeamNGPose = BeamNGPose() self.camera = camera if not self.camera: self.camera = Camera((0, 0, 0), (0, 0, 0), 120, (1280, 1280), colour=True, depth=True, annotation=True) self.beamng = beamng
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 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(): 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 __init__(self): super().__init__() # Break an episode upon user input. thread = Thread(target=self._intervene) thread.start() self.step = 0 self.dist_driven = 0 self.done = False self.last_action = (0.0, 0.0) self.bng = BeamNGpy('localhost', 64257, home=BEAMNG_HOME) self.scenario = Scenario('train', 'train', authors='Vsevolod Tymofyeyev', description='Reinforcement learning') self.road = TrainingRoad(ASFAULT_PREFAB) self.road.calculate_road_line() spawn_point = self.road.spawn_point() self.last_pos = spawn_point.pos() self.scenario.add_road(self.road.asphalt) self.scenario.add_road(self.road.mid_line) self.scenario.add_road(self.road.left_line) self.scenario.add_road(self.road.right_line) self.vehicle = Vehicle('ego_vehicle', model='etk800', licence='RL FTW', color='Red') 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) self.vehicle.attach_sensor("front_camera", front_camera) self.scenario.add_vehicle(self.vehicle, pos=spawn_point.pos(), rot=spawn_point.rot()) # At this point, BeamNG creates a prefab file which contains the specified roads. self.scenario.make(self.bng) prefab_path = self.scenario.get_prefab_path() # Update the road definitions in the prefab. update_prefab(prefab_path) self.bng.open() self.bng.set_deterministic() self.bng.set_steps_per_second(SPS) self.bng.load_scenario(self.scenario) self.bng.start_scenario() # self.bng.hide_hud() self.bng.pause()
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 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 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 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 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()
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:
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()
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 getOob(oobList, test, port): # 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') scenario = Scenario('asfault', 'asfault', True) # 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) with open(pathToScenarioBeamng + '/asfault.json', 'r') as f: jsonScenario = json.load(f) with open(pathToScenarioBeamng + '/asfault.lua', 'r') as f: luaScenario = f.readlines() with open(pathToScenarioBeamng + '/asfault.prefab', 'r') as f: prefabScenario = f.readlines() with open(pathToScenarioDocuments + '/asfault.json', 'w') as outfile: json.dump(jsonScenario, outfile) with open(pathToScenarioDocuments + '/asfault.lua', 'w') as outfile: outfile.writelines(luaScenario) with open(pathToScenarioDocuments + '/asfault.prefab', 'w') as outfile: outfile.writelines(prefabScenario) deep_drive_engaged = True STATE = "NORMAL" notFinished = True portCount = 1 multiplier = 2 turtleMode = False turtleSpeed = 10 offRoad = False oobSegKey = None oobSearch = False print(oobList) speedList = [] currentSpeed = oobList[0][0][0] for speed in oobList[0][0]: speedList.append(speed) if speed > currentSpeed: currentSpeed = speed currentSpeed = currentSpeed * 3.6 waypointList = [] resultList = [] oobWaypoint = [] newOobWaypointList = [] for key in oobList[0][1]: waypointList.append(key) print(waypointList) for waypoint in waypointList: for key in waypointList: print(key) for k in key: oobWaypoint.append(k) resultList.append(k) while notFinished: # Connect to running beamng setup_logging() beamng = BeamNGpy('localhost', port) bng = beamng.open(launch=True) scenario.make(bng) bng.load_scenario(scenario) try: bng.set_deterministic() # Set simulator to be deterministic bng.set_steps_per_second(60) # With 60hz temporal resolution # Connect to the existing vehicle (identified by the ID set in the vehicle instance) bng.connect_vehicle(vehicle, port + portCount) bng.start_scenario() # Put simulator in pause awaiting further inputs bng.pause() assert vehicle.skt testDic = RoadTest.to_dict(test) parentage = testDic['network']['parentage'] for parent in parentage: roadKeys = [] for keys in parentage: for key in keys: roadKeys.append(key) nodesDict = testDic['network']['nodes'] nodes = [] for node in nodesDict: nodes.append(node) vehicle.update_vehicle() oobCount = 0 inOob = False vehicle.ai_set_speed(70) if oobSearch: if turtleMode: currentSpeed = turtleSpeed else: currentSpeed = currentSpeed - 10 print(currentSpeed) running = True while running: # 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) # # 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) #vehicle.ai_drive_in_lane(True) vehicle.update_vehicle() tup = get_segment(test, vehicle.state) currentSegmentDict = None currentSegKey = None if tup is not None: currentSegmentDict = NetworkNode.to_dict(tup) currentSegKey = currentSegmentDict['key'] if offRoad: if oobSegKey in resultList: index = resultList.index(oobSegKey) lostSeg = resultList.pop(index) print('resList without seg', resultList) print(lostSeg) if oobSegKey not in newOobWaypointList: newOobWaypointList.append(oobSegKey) print('new OOB list', newOobWaypointList) offRoad = False if inOob and off_track(test, vehicle.state): print('off track') offRoad = True oobCount = oobCount + 1 if currentSegKey in oobWaypoint: vehicle.ai_drive_in_lane(True) inOob = True oobSegKey = currentSegKey vehicle.ai_set_speed(currentSpeed) else: inOob = False vehicle.ai_set_speed(70) vehicle.ai_set_waypoint('waypoint_goal') vehicle.ai_drive_in_lane(True) if goal_reached(test, vehicle.state): print(turtleMode) for res in resultList: segOobDict = testDic['execution']['seg_oob_count'] segOobDict[res] = currentSpeed / 3.6 if oobCount == 0: testDic['execution']['oobs'] = 0 print('waypointlist', newOobWaypointList) print('resultlist', resultList) test = RoadTest.from_dict(testDic) return test else: running = False portCount = portCount + 1 multiplier = multiplier + 1 oobSearch = True oobWaypoint = newOobWaypointList newOobWaypointList = [] if currentSpeed < 20: print(currentSpeed) print('NewoobWay: ', newOobWaypointList) print('resultlist: ', resultList) if turtleMode: for res in oobWaypoint: print(res) segOobDict = testDic['execution'][ 'seg_oob_count'] segOobDict[res] = 12 testDic['execution']['oobs'] = 0 test = RoadTest.from_dict(testDic) print(segOobDict) print(resultList) running = False oobCount = 0 return test else: turtleMode = True print('mode on') bng.close() finally: if oobCount == 0: bng.close() else: print('restart')
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()
scale=(1, 1, 1), shape='/levels/west_coast_usa/art/shapes/objects/ramp_massive.dae') scenario.add_object(ramp) ring = ProceduralRing(name='pyring', pos=(445, 301, 218), rot=(0, 0, 100), radius=5, thickness=2.5) scenario.add_procedural_mesh(ring) cam_pos = (391.5, 251, 197.8) cam_dir = (445 - cam_pos[0], 301 - cam_pos[1], 208 - cam_pos[2]) cam = Camera(cam_pos, cam_dir, 60, (2048, 2048), near_far=(1, 4000), colour=True) scenario.add_camera(cam, 'cam') scenario.make(beamng) bng = beamng.open() bng.set_deterministic() bng.load_scenario(scenario) bng.start_scenario() meshes = scenario.find_procedural_meshes() ring_pos = None for mesh in meshes: if mesh.name == 'pyring':
from beamngpy.sensors import Camera import os beamng = BeamNGpy('localhost', 64256, home='C:/Users/merie/Documents/BeamNG.research.v1.7.0.1') # Create a vehicle with a camera sensor attached to it vehicle = Vehicle('ego_vehicle', model='etki', licence='PYTHON', color='Green') cam_pos = np.array([0, 0, 0]) # placeholder values that will be recomputed later cam_dir = np.array([0, 0, 0]) # placeholder values that will be recomputed later cam_fov = 70 cam_res = (1024, 1024) #(512, 512) camera = Camera(cam_pos, cam_dir, cam_fov, cam_res, colour=True) vehicle.attach_sensor('camera', camera) # Simple scenario with the vehicle we just created standing at the origin car_pos = np.array([0, 0, 0]) scenario = Scenario( 'smallgrid', 'multishot', description='Demo of the camera sensor used like a multishot camera') scenario.add_vehicle(vehicle, pos=car_pos, rot=(0, 0, 0)) scenario.make(beamng) variations = [np.radians(a) for a in [-10, 0, 10]] def get_bbox_center(bbox):
pos = (-0.5, 1.8, 0.6) direction = (0, 1, 0) fov = 50 # Simulation settings MAX_FPS = 60 SIMULATION_STEP = 1 # Create controller and set desired max speed (>25 not recommended) MAX_SPEED = 25 controller = Controller(MAX_SPEED) setup_logging() front_camera = Camera(pos, direction, fov, resolution, near_far=(0.5, 300), colour=True, depth=True, annotation=True) electrics = Electrics() vehicle.attach_sensor('front_cam', front_camera) vehicle.attach_sensor('electrics', electrics) beamng = BeamNGpy('localhost', 64256) bng = beamng.open(launch=False) bng.set_deterministic() bng.set_steps_per_second(MAX_FPS) bng.connect_vehicle(vehicle) bng.pause() while True: bng.step(SIMULATION_STEP)
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()