def main(): bng_home = os.environ['BEAMNG_HOME'] road = TrainingRoad(ASFAULT_PREFAB) road.calculate_road_line(back=True) bng = BeamNGpy('localhost', 64256, home=bng_home) scenario = Scenario('smallgrid', 'train') scenario.add_road(road.asphalt) scenario.add_road(road.mid_line) scenario.add_road(road.left_line) scenario.add_road(road.right_line) vehicle = Vehicle('ego_vehicle', model='etk800', licence='PYTHON') front_camera = Camera(pos=(0, 1.4, 1.8), direction=(0, 1, -0.23), fov=FOV, resolution=(CAMERA_WIDTH, CAMERA_HEIGHT), colour=True, depth=False, annotation=False) vehicle.attach_sensor("front_camera", front_camera) # Add it to our scenario at this position and rotation spawn_point = road.spawn_point() scenario.add_vehicle(vehicle, pos=spawn_point.pos(), rot=spawn_point.rot()) # Place files defining our scenario for the simulator to read scenario.make(bng) prefab_path = scenario.get_prefab_path() update_prefab(prefab_path) bng.open() bng.set_nondeterministic() bng.set_steps_per_second(60) # Load and start our scenario bng.load_scenario(scenario) bng.start_scenario() vehicle.ai_set_mode('span') vehicle.ai_set_speed(5) vehicle.ai_set_line([{ 'pos': node.pos(), 'speed': 10 } for node in road.road_line]) number_of_images = 0 while number_of_images < 9000: bng.poll_sensors(vehicle) number_of_images += 1 #print(number_of_images) bng.step(1) sensors = bng.poll_sensors(vehicle) image = sensors['front_camera']['colour'].convert('RGB') image = np.array(image) image = image[:, :, ::-1] dist = road.dist_to_center(vehicle.state['pos']) cv2.imwrite('datasets\\{}.jpg'.format(number_of_images), image) bng.close()
class BeamNG_ACC_Test(): def __init__(self, **kwargs): keys = kwargs.keys() if "test_controller" in keys: self.controller = kwargs.get("test_controller") else: self.controller = ACC_Test() config = BeamNG_Cruise_Control.Config(KP=CC_P, KI=CC_I, KD=CC_D, target=ACC_CAR_SPEED) config2 = BeamNG_Cruise_Control.Config(KP=CC_P, KI=CC_I, KD=CC_D, target=FRONT_CAR_SPEED) self.PID = BeamNG_Cruise_Control.PID_Controller(config=config) self.PID2 = BeamNG_Cruise_Control.PID_Controller(config=config2) def setup_bngServer(self): # Instantiate BeamNGpy instance running the simulator from the given path, if ('BNG_HOME' in os.environ): self.bng = BeamNGpy('localhost', 64256, home=os.environ['BNG_HOME']) else: print( "WARNING no BNG_HOME is set! Make sure to set BeamNG.research path to research\trunk\ as environment variable (or write the path in the script by yourself)" ) self.bng = BeamNGpy( 'localhost', 64256, home='C:\\Users\\Ingars\\Beamng-unlimited\\trunk') def setup_BeamNG(self): # Create a scenario in test map self.scenario = Scenario('cruise-control', 'example') # Create an ETK800 with the licence plate 'PID' self.vehicle = Vehicle('ACC', model='etk800', licence='ACC') electrics = Electrics() self.vehicle.attach_sensor('electrics', electrics) # Cars electric system # Add it to our scenario at this position and rotation self.scenario.add_vehicle(self.vehicle, pos=ACC_CAR_POS, rot=(0, 0, 0)) self.vehicle2 = Vehicle('FRONT', model='etk800', licence='FRONT') self.vehicle2.attach_sensor('electrics', electrics) # Cars electric system self.scenario.add_vehicle(self.vehicle2, pos=FRONT_CAR_POS, rot=(0, 0, 180)) # 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() def runTest(self): self.bng.restart_scenario() self.bng.teleport_vehicle(self.vehicle, pos=ACC_CAR_POS, rot=(0, 0, 180)) self.run() def run(self): self.setupTest() wanted_t = ACC_CAR_SPEED wanted_d = WANTED_DISTANCE soft_brake_d = wanted_d - (wanted_d / 2) wanted_d_buffer = wanted_d - (wanted_d / 5) # Wanted distance has a buffer of 25%, to compensate holding same constant speed at bigger distance deacc_d = wanted_d_buffer + 10 # +10 meters is buffer for starting to decelerate towards front car speed and wanted distance. # the buffer should be calculated dynamically instead to compensate for different speeds while self.controller.ended == False: start_time = datetime.datetime.now() sensors = self.bng.poll_sensors(self.vehicle) position = self.vehicle.state['pos'] position2 = self.vehicle2.state['pos'] sensors2 = self.bng.poll_sensors( self.vehicle2 ) # replace with self.vehicle.update_vehicle when not using pre-generated coordinates of vehicle to follow wheel_speed = sensors['electrics']['values']['wheelspeed'] distance = self.controller.setDistance(position, position2) front_car_speed = self.controller.getFrontCarSpeed(wheel_speed) curr_target = self.PID.get_target() print("distance: " + str(distance)) #FORMULA USED for deacceleration = front_car_speed^2 = wheel_speed^2 + (distance-20)*2*a #d = (front_car_speed - wheel_speed) / (-0.3 * 2) # 0.3 deacceleration? distance for reducing speed to front cars speed with -0.3 acceleration #print("d: " + str(d)) if distance < 5: # 5 is manually set as last allowed distance between both cars, will not work good if ACC car is driving too fast. Would be better to calculate it as braking distance. print("brake1") self.vehicle.control(brake=1) self.vehicle.control(throttle=0) elif distance < soft_brake_d: print("brake1") self.vehicle.control( brake=0.1 ) #Softness of brake could also be calculated dynamically to compensate different speeds self.vehicle.control(throttle=0) else: if distance <= wanted_d_buffer: print("wanted") if front_car_speed > curr_target + 3.5 or front_car_speed < curr_target - 2: #or front_car_speed < curr_target - 1 // calibrate 3.5 and 2 self.PID.change_target(max(front_car_speed, 0)) elif distance <= deacc_d: a = (front_car_speed - wheel_speed) / ( (distance - wanted_d_buffer) * 2) print("a:" + str(a)) self.PID.change_target(a + wheel_speed) elif curr_target != wanted_t: self.PID.change_target(wanted_t) print("throttle1") value = self.PID.calculate_throttle(wheel_speed) value = min(1, value) self.vehicle.control(brake=0) self.vehicle.control(throttle=value) #PID for front car wheel_speed2 = sensors2['electrics']['values']['wheelspeed'] # print("real front:" + str(wheel_speed2)) value2 = self.PID2.calculate_throttle(wheel_speed2) value2 = min(1, value2) self.vehicle2.control(brake=0) self.vehicle2.control(throttle=value2) elapsed_time = datetime.datetime.now() - start_time while (elapsed_time.total_seconds() * 1000) < 100: elapsed_time = datetime.datetime.now() - start_time elapsed_total = self.controller.calculateElapsed() # Change front car speed after 10 seconds and after 20 seconds if elapsed_total.total_seconds() > float(10): self.PID2.change_target(20) if elapsed_total.total_seconds() > float(20): self.PID2.change_target(10) print("Ending Test") def close(self): self.bng.close() def setupTest(self): self.vehicle.update_vehicle() self.controller.last_position = self.vehicle.state['pos'] self.controller.start()
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 BeamNG_Cruise_Controller_Test(): def __init__(self, **kwargs): keys = kwargs.keys() if "test_controller" in keys: self.controller = kwargs.get("test_controller") else: self.controller = PID_Controller_Test() if "testing_times" in keys: self.testing_times = kwargs.get("testing_times") else: self.testing_times = 1 if "test_name" in keys: self.test_name = kwargs.get("test_name") else: self.test_name = "test" if "targets" in keys: self.targets = kwargs.get("targets") else: self.targets = [0] def setup_bngServer(self): # Instantiate BeamNGpy instance running the simulator from the given path, if ('BNG_HOME' in os.environ): self.bng = BeamNGpy('localhost', 64256, home=os.environ['BNG_HOME']) else: print( "WARNING no BNG_HOME is set! Make sure to set BeamNG.research path to research\trunk\ as environment variable (or write the path in the script by yourself)" ) self.bng = BeamNGpy('localhost', 64256, home='') def setup_BeamNG(self): # Create a scenario in test map scenario = Scenario('cruise-control', 'example') # Create an ETK800 with the licence plate 'PID' self.vehicle = Vehicle('ego_vehicle', model='etk800', licence='PID') electrics = Electrics() self.vehicle.attach_sensor('electrics', electrics) # Cars electric system # Add it to our scenario at this position and rotation scenario.add_vehicle(self.vehicle, pos=(406.787, 1115.518, 0), rot=(0, 0, 0)) # Place files defining our scenario for the simulator to read scenario.make(self.bng) # Launch BeamNG.research self.bng.open() # Load and start our scenario self.bng.load_scenario(scenario) self.bng.start_scenario() def runTest(self, test_K_values, test_name): for test_K_value in test_K_values: KP_in, KI_in, KD_in = map(float, test_K_value) if KP_in == 0 and KI_in == 0 and KD_in == 0: config = Config() controller = On_Off_Controller(config=config) else: config = Config(KP=KP_in, KI=KI_in, KD=KD_in) controller = PID_Controller(config=config) self.controller.controller = controller self.test_name = str(test_name) + "_" + str(KP_in) + "_" + str( KI_in) + "_" + str(KD_in) self.runTestForPID() def runTestForPID(self): for speed in self.targets: self.controller.setTarget(speed) self.controller.setTime(float(30)) #self.runTestOfType("up_{0}_".format(speed), (406.787, 700.517, 0.214829), (0, 0, 0)) self.runTestOfType("straight_{0}_".format(speed), (406.787, 715.517, 0.214829), (0, 0, 180)) #self.runTestOfType("down_{0}_".format(speed), (406.787, 304.896, 100.211), (0, 0, 180)) def runTestOfType(self, type, pos, rot): i = 1 while i <= self.testing_times: self.controller.newLogFile("../logs/" + self.test_name + "_" + str(type) + "_" + str(i) + ".txt") self.bng.restart_scenario() self.bng.teleport_vehicle(self.vehicle, pos=pos, rot=rot) self.run() i += 1 def run(self): self.controller.start() while self.controller.ended == False: start_time = datetime.datetime.now() sensors = self.bng.poll_sensors(self.vehicle) wheel_speed = sensors['electrics']['values']['wheelspeed'] value = self.controller.calculate_speed_with_logs(wheel_speed) if value <= 0: value = max(-1, value - 0.1) * -1 self.vehicle.control(brake=value) self.vehicle.control(throttle=0) else: value = min(1, value) self.vehicle.control(brake=0) self.vehicle.control(throttle=value) elapsed_time = datetime.datetime.now() - start_time while (elapsed_time.total_seconds() * 1000) < 100: elapsed_time = datetime.datetime.now() - start_time print("Ending Test") def close(self): self.bng.close()
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()
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)
class Simulation(object): def __init__(self) -> None: super().__init__() 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()) self.scenario.make(self.bng) prefab_path = self.scenario.get_prefab_path() 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 _intervene(self): while True: a = input() self.done = not self.done def take_action(self, action): steering, throttle = action steering = steering.item() throttle = throttle.item() self.last_action = action self.step += 1 self.vehicle.control(steering=steering, throttle=throttle, brake=0.0) self.bng.step(STEPS_INTERVAL) def _reward(self, done, dist): steering = self.last_action[0] throttle = self.last_action[1] velocity = self.velocity() # km/h if not done: reward = REWARD_STEP + THROTTLE_REWARD_WEIGHT * throttle #- MID_DIST_PENALTY_WEIGHT * dist else: reward = REWARD_CRASH - CRASH_SPEED_WEIGHT * throttle return reward def observe(self): sensors = self.bng.poll_sensors(self.vehicle) image = sensors['front_camera']['colour'].convert("RGB") image = np.array(image) r = ROI # Cut to the relevant region image = image[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])] # Convert to BGR state = image[:, :, ::-1] #done = self.done pos = self.vehicle.state['pos'] dir = self.vehicle.state['dir'] self.refresh_dist(pos) self.last_pos = pos dist = self.road.dist_to_center(self.last_pos) #velocity = self.velocity() done = dist > MAX_DIST #or velocity > MAX_VELOCITY reward = self._reward(done, dist) return state, reward, done, {} def velocity(self): state = self.vehicle.state velocity = np.linalg.norm(state["vel"]) return velocity * 3.6 def position(self): return self.vehicle.state["pos"] def refresh_dist(self, pos): pos = np.array(pos) last_pos = np.array(self.last_pos) dist = np.linalg.norm(pos - last_pos) self.dist_driven += dist def close_connection(self): self.bng.close() def reset(self): self.vehicle.control(throttle=0, brake=0, steering=0) self.bng.poll_sensors(self.vehicle) self.dist_driven = 0 self.step = 0 self.done = False current_pos = self.vehicle.state['pos'] closest_point = self.road.closest_waypoint(current_pos) #closest_point = self.road.random_waypoint() self.bng.teleport_vehicle(vehicle=self.vehicle, pos=closest_point.pos(), rot=closest_point.rot()) self.bng.pause() # TODO delete def wait(self): from client.aiExchangeMessages_pb2 import SimStateResponse return SimStateResponse.SimState.RUNNING
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()
class WCARaceGeometry(gym.Env): sps = 50 rate = 5 front_dist = 800 front_step = 100 trail_dist = 104 trail_step = 13 starting_proj = 1710 max_damage = 100 def __init__(self, host='localhost', port=64256): self.steps = WCARaceGeometry.sps // WCARaceGeometry.rate self.host = host self.port = port self.action_space = self._action_space() self.observation_space = self._observation_space() self.episode_steps = 0 self.spine = None self.l_edge = None self.r_edge = None self.polygon = None front_factor = WCARaceGeometry.front_dist / WCARaceGeometry.front_step trail_factor = WCARaceGeometry.trail_dist / WCARaceGeometry.trail_step self.front = lambda step: +front_factor * step self.trail = lambda step: -trail_factor * step self.bng = BeamNGpy(self.host, self.port) self.vehicle = Vehicle('racecar', model='sunburst', licence='BEAMNG', colour='red', partConfig='vehicles/sunburst/hillclimb.pc') electrics = Electrics() damage = Damage() self.vehicle.attach_sensor('electrics', electrics) self.vehicle.attach_sensor('damage', damage) scenario = Scenario('west_coast_usa', 'wca_race_geometry_v0') scenario.add_vehicle(self.vehicle, pos=(394.5, -247.925, 145.25), rot=(0, 0, 90)) scenario.make(self.bng) self.bng.open(launch=True) self.bng.set_deterministic() self.bng.set_steps_per_second(WCARaceGeometry.sps) self.bng.load_scenario(scenario) self._build_racetrack() self.observation = None self.last_observation = None self.last_spine_proj = None self.bng.start_scenario() self.bng.pause() def __del__(self): self.bng.close() def _build_racetrack(self): roads = self.bng.get_roads() track = roads['race_ref'] l_vtx = [] s_vtx = [] r_vtx = [] for right, middle, left in track: r_vtx.append(right) s_vtx.append(middle) l_vtx.append(left) self.spine = LinearRing(s_vtx) self.r_edge = LinearRing(r_vtx) self.l_edge = LinearRing(l_vtx) r_vtx = [v[0:2] for v in r_vtx] l_vtx = [v[0:2] for v in l_vtx] self.polygon = Polygon(l_vtx, holes=[r_vtx]) def _action_space(self): action_lo = [-1., -1.] action_hi = [+1., +1.] return gym.spaces.Box(np.array(action_lo), np.array(action_hi), dtype=float) def _observation_space(self): # n vertices of left and right polylines ahead and behind, 3 floats per # vtx scope = WCARaceGeometry.trail_step + WCARaceGeometry.front_step obs_lo = [ -np.inf, ] * scope * 3 obs_hi = [ np.inf, ] * scope * 3 obs_lo.extend([ -np.inf, # Distance to left edge -np.inf, # Distance to right edge -2 * np.pi, # Inclination -2 * np.pi, # Angle -2 * np.pi, # Vertical angle -np.inf, # Spine speed 0, # RPM -1, # Gear 0, # Throttle 0, # Brake -1.0, # Steering 0, # Wheel speed -np.inf, # Altitude ]) obs_hi.extend([ np.inf, # Distance to left edge np.inf, # Distance to right edge 2 * np.pi, # Inclincation 2 * np.pi, # Angle 2 * np.pi, # Vertical angle np.inf, # Spine speed np.inf, # RPM 8, # Gear 1.0, # Throttle 1.0, # Brake 1.0, # Steering np.inf, # Wheel speed np.inf, # Altitude ]) return gym.spaces.Box(np.array(obs_lo), np.array(obs_hi), dtype=float) def _make_commands(self, action): brake = 0 throttle = action[1] steering = action[0] if throttle < 0: brake = -throttle throttle = 0 self.vehicle.control(steering=steering, throttle=throttle, brake=brake) def _project_vehicle(self, pos): r_proj = self.r_edge.project(pos) r_proj = self.r_edge.interpolate(r_proj) l_proj = self.l_edge.project(r_proj) l_proj = self.l_edge.interpolate(l_proj) s_proj = self.spine.project(r_proj) s_proj = self.spine.interpolate(s_proj) return l_proj, s_proj, r_proj def _get_vehicle_angles(self, vehicle_pos, spine_seg): spine_beg = spine_seg.coords[+0] spine_end = spine_seg.coords[-1] spine_angle = np.arctan2(spine_end[1] - spine_beg[1], spine_end[0] - spine_beg[0]) vehicle_angle = self.vehicle.state['dir'][0:2] vehicle_angle = np.arctan2(vehicle_angle[1], vehicle_angle[0]) vehicle_angle = normalise_angle(vehicle_angle - spine_angle) vehicle_angle -= np.pi elevation = np.arctan2(spine_beg[2] - spine_end[2], spine_seg.length) vehicle_elev = self.vehicle.state['dir'] vehicle_elev = np.arctan2(vehicle_elev[2], np.linalg.norm(vehicle_elev)) return vehicle_angle, vehicle_elev, elevation def _wrap_length(self, target): length = self.spine.length while target < 0: target += length while target > length: target -= length return target def _gen_track_scope_loop(self, it, fn, base, s_scope, s_width): for step in it: distance = base + fn(step) distance = self._wrap_length(distance) s_proj = self.spine.interpolate(distance) s_scope.append(s_proj) l_proj = self.l_edge.project(s_proj) l_proj = self.l_edge.interpolate(l_proj) r_proj = self.r_edge.project(s_proj) r_proj = self.r_edge.interpolate(r_proj) width = l_proj.distance(r_proj) s_width.append(width) def _gen_track_scope(self, pos, spine_seg): s_scope = [] s_width = [] base = self.spine.project(pos) it = range(WCARaceGeometry.trail_step, 0, -1) self._gen_track_scope_loop(it, self.trail, base, s_scope, s_width) it = range(1) self._gen_track_scope_loop(it, lambda x: x, base, s_scope, s_width) it = range(WCARaceGeometry.front_step + 1) self._gen_track_scope_loop(it, self.front, base, s_scope, s_width) s_proj = self.spine.interpolate(base) offset = (-s_proj.x, -s_proj.y, -s_proj.z) s_line = LineString(s_scope) s_line = affinity.translate(s_line, *offset) spine_beg = spine_seg.coords[+0] spine_end = spine_seg.coords[-1] direction = [spine_end[i] - spine_beg[i] for i in range(3)] angle = np.arctan2(direction[1], direction[0]) + np.pi / 2 s_line = affinity.rotate(s_line, -angle, origin=(0, 0), use_radians=True) ret = list() s_scope = s_line.coords for idx in range(1, len(s_scope) - 1): curvature = calculate_curvature(s_scope, idx) inclination = calculate_inclination(s_scope, idx) width = s_width[idx] ret.append(curvature) ret.append(inclination) ret.append(width) return ret def _spine_project_vehicle(self, vehicle_pos): proj = self.spine.project(vehicle_pos) - WCARaceGeometry.starting_proj if proj < 0: proj += self.spine.length proj = self.spine.length - proj return proj def _get_spine_speed(self, vehicle_pos, vehicle_dir, spine_seg): spine_beg = spine_seg.coords[0] future_pos = Point(vehicle_pos.x + vehicle_dir[0], vehicle_pos.y + vehicle_dir[1], vehicle_pos.z + vehicle_dir[2]) spine_end = self.spine.project(future_pos) spine_end = self.spine.interpolate(spine_end) return spine_end.distance(Point(*spine_beg)) def _make_observation(self, sensors): electrics = sensors['electrics']['values'] vehicle_dir = self.vehicle.state['dir'] vehicle_pos = self.vehicle.state['pos'] vehicle_pos = Point(*vehicle_pos) spine_beg = self.spine.project(vehicle_pos) spine_end = spine_beg spine_end += WCARaceGeometry.front_dist / WCARaceGeometry.front_step spine_beg = self.spine.interpolate(spine_beg) spine_end = self.spine.interpolate(spine_end) spine_seg = LineString([spine_beg, spine_end]) spine_speed = self._get_spine_speed(vehicle_pos, vehicle_dir, spine_seg) l_dist = self.l_edge.distance(vehicle_pos) r_dist = self.r_edge.distance(vehicle_pos) angle, vangle, elevation = self._get_vehicle_angles( vehicle_pos, spine_seg) l_proj, s_proj, r_proj = self._project_vehicle(vehicle_pos) s_scope = self._gen_track_scope(vehicle_pos, spine_seg) obs = list() obs.extend(s_scope) obs.append(l_dist) obs.append(r_dist) obs.append(elevation) obs.append(angle) obs.append(vangle) obs.append(spine_speed) obs.append(electrics['rpm']) obs.append(electrics['gearIndex']) obs.append(electrics['throttle']) obs.append(electrics['brake']) obs.append(electrics['steering']) obs.append(electrics['wheelspeed']) obs.append(electrics['altitude']) return np.array(obs) def _compute_reward(self, sensors): damage = sensors['damage'] vehicle_pos = self.vehicle.state['pos'] vehicle_pos = Point(*vehicle_pos) if damage['damage'] > WCARaceGeometry.max_damage: return -1, True if not self.polygon.contains(Point(vehicle_pos.x, vehicle_pos.y)): return -1, True score, done = -1, False spine_proj = self._spine_project_vehicle(vehicle_pos) if self.last_spine_proj is not None: diff = spine_proj - self.last_spine_proj if diff >= -0.10: if diff < 0.5: return -1, False else: score, done = diff / self.steps, False elif np.abs(diff) > self.spine.length * 0.95: score, done = 1, True else: score, done = -1, True self.last_spine_proj = spine_proj return score, done def reset(self): self.episode_steps = 0 self.vehicle.control(throttle=0, brake=0, steering=0) self.bng.restart_scenario() self.bng.step(30) self.bng.pause() self.vehicle.set_shift_mode(3) self.vehicle.control(gear=2) sensors = self.bng.poll_sensors(self.vehicle) self.observation = self._make_observation(sensors) vehicle_pos = self.vehicle.state['pos'] vehicle_pos = Point(*vehicle_pos) self.last_spine_proj = self._spine_project_vehicle(vehicle_pos) return self.observation def advance(self): self.bng.step(self.steps, wait=False) def observe(self): sensors = self.bng.poll_sensors(self.vehicle) new_observation = self._make_observation(sensors) return new_observation, sensors def step(self, action): action = [*np.clip(action, -1, 1), action[0], action[1]] action = [float(v) for v in action] self.episode_steps += 1 self._make_commands(action) self.advance() new_observation, sensors = self.observe() if self.observation is not None: self.last_observation = self.observation self.observation = new_observation score, done = self._compute_reward(sensors) print((' A: {:5.2f} B: {:5.2f} ' ' S: {:5.2f} T: {:5.2f} R: {:5.2f}').format( action[2], action[3], action[0], action[1], score)) # if done: # self.bng.step(WCARaceGeometry.sps * 1) return self.observation, score, done, {}
def collect_data(beamNG_path): bng = BeamNGpy('localhost', 64256, beamNG_path) #bng = BeamNGpy('localhost', 64256, home='D:/BeamNGReasearch/Unlimited_Version/trunk') 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) pos1 = (-4.270055770874023, -115.30198669433594, 0.20322345197200775) rot1 = (0.872300386428833, -0.48885437846183777, 0.01065115723758936) scenario.add_vehicle(vehicle1, pos=pos1, rot=rot1) # Place files defining our scenario for the simulator to read scenario.make(bng) # Launch BeamNG.research bng.open(launch=True) #SIZE = 50 try: bng.load_scenario(scenario) bng.start_scenario() #vehicle1.ai_set_speed(10.452066507339481,mode = 'set') vehicle1.ai_set_mode('span') #collect data print("\n Position and rotation of car \n ") # for _ in range(200): for _ in range(200): 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']) print([vehicle1.state['pos'], vehicle1.state['dir']]) #write data into file # print ("position :",positions) # print ("position :",directions) sys_output.print_title( "\n The position and rotation of the car is saved in \"position.txt and \"roation.txt\" \"" ) write_data(FILE_POS, positions) write_data(FILE_ROT, directions) bng.stop_scenario() bng.close() time.sleep(2) finally: bng.close() return (positions, directions)
class Driver: vehicle = None bng = None labeler = None left_path = None right_path = None driving_path = None road_profiler = None car_model = None road_model = None def __init__(self, car_model: dict, road_model: dict, control_model: dict): self.car_model = car_model self.road_model = road_model self.control_model = control_model # Note: Speed limit must be m/s self.road_profiler = RoadProfiler( road_model['mu'], road_model['speed_limit'] / 3.6, control_model['discretization_factor']) def _compute_driving_path(self, car_state, road_name): road_geometry = self.bng.get_road_edges(road_name) left_edge_x = np.array([e['left'][0] for e in road_geometry]) left_edge_y = np.array([e['left'][1] for e in road_geometry]) right_edge_x = np.array([e['right'][0] for e in road_geometry]) right_edge_y = np.array([e['right'][1] for e in road_geometry]) road_edges = dict() road_edges['left_edge_x'] = left_edge_x road_edges['left_edge_y'] = left_edge_y road_edges['right_edge_x'] = right_edge_x road_edges['right_edge_y'] = right_edge_y self.right_edge = LineString( zip(road_edges['right_edge_x'][::-1], road_edges['right_edge_y'][::-1])) self.left_edge = LineString( zip(road_edges['left_edge_x'], road_edges['left_edge_y'])) current_position = Point(car_state['pos'][0], car_state['pos'][1]) from shapely.ops import nearest_points from shapely.affinity import rotate projection_point_on_right = nearest_points(self.right_edge, current_position)[0] projection_point_on_left = nearest_points(self.left_edge, current_position)[0] # If the car is closest to the left, then we need to switch the direction of the road... if current_position.distance( projection_point_on_right) > current_position.distance( projection_point_on_left): # Swap the axis and recompute the projection points l.debug("Reverse traffic direction") temp = self.right_edge self.right_edge = self.left_edge self.left_edge = temp del temp projection_point_on_right = nearest_points(self.right_edge, current_position)[0] projection_point_on_left = nearest_points(self.left_edge, current_position)[0] # Traffic direction is always 90-deg counter clockwise from right # Now rotate right point 90-deg counter clockwise from left and we obtain the traffic direction rotated_right = rotate(projection_point_on_right, 90.0, origin=projection_point_on_left) # Vector defining the direction of the road traffic_direction = np.array([ rotated_right.x - projection_point_on_left.x, rotated_right.y - projection_point_on_left.y ]) # Find the segment containing the projection of current location # Starting point on right edge start_point = None for pair in pairs(list(self.right_edge.coords[:])): segment = LineString([pair[0], pair[1]]) # xs, ys = segment.coords.xy # plt.plot(xs, ys, color='green') if segment.distance(projection_point_on_right) < 1.8e-5: road_direction = np.array( [pair[1][0] - pair[0][0], pair[1][1] - pair[0][1]]) if dot(traffic_direction, road_direction) < 0: l.debug("Reverse order !") self.right_edge = LineString([ Point(p[0], p[1]) for p in self.right_edge.coords[::-1] ]) start_point = Point(pair[0][0], pair[0][1]) break else: l.debug("Original order !") start_point = Point(pair[1][0], pair[1][1]) break assert start_point is not None # At this point compute the driving path of the car (x, y, t) self.driving_path = [current_position] # plt.plot(current_position.x, current_position.y, color='black', marker="x") # # This might not be robust we need to get somethign close by # plt.plot([pair[0][0], pair[1][0]], [pair[0][1], pair[1][1]], marker="o") # plt.plot(projection_point_on_right.x, projection_point_on_right.y, color='b', marker="*") # started = False for right_position in [ Point(p[0], p[1]) for p in list(self.right_edge.coords) ]: if right_position.distance(start_point) < 1.8e-5: # print("Start to log positions") # plt.plot(right_position.x, right_position.y, color='blue', marker="o") started = True if not started: # print("Skip point") # plt.plot(right_position.x, right_position.y, color='red', marker="*") continue else: # print("Consider point") # plt.plot(right_position.x, right_position.y, color='green', marker="o") pass # Project right_position to left_edge projected_point = self.left_edge.interpolate( self.left_edge.project(right_position)) # Translate the right_position 2m toward the center line = LineString([(right_position.x, right_position.y), (projected_point.x, projected_point.y)]) self.driving_path.append(line.interpolate(2.0)) def plot_all(self, car_state): current_position = Point(car_state['pos'][0], car_state['pos'][1]) plt.figure(1, figsize=(5, 5)) plt.clf() ax = plt.gca() x, y = self.left_edge.coords.xy ax.plot(x, y, 'r-') x, y = self.right_edge.coords.xy ax.plot(x, y, 'b-') driving_lane = LineString([p for p in self.driving_path]) x, y = driving_lane.coords.xy ax.plot(x, y, 'g-') # node = { # 'x': target_position.x, # 'y': target_position.y, # 'z': 0.3, # 't': target_time, # } xs = [node['x'] for node in self.script] ys = [node['y'] for node in self.script] # print("{:.2f}".format(3.1415926)); vs = ['{:.2f}'.format(node['avg_speed'] * 3.6) for node in self.script] # plt.plot(xs, ys, marker='.') ax = plt.gca() for i, txt in enumerate(vs): ax.annotate(txt, (xs[i], ys[i])) plt.plot(current_position.x, current_position.y, marker="o", color="green") # Center around current_positions ax.set_xlim([current_position.x - 50, current_position.x + 50]) ax.set_ylim([current_position.y - 50, current_position.y + 50]) plt.draw() plt.pause(0.01) def run(self, debug=False): try: self.vehicle = Vehicle(car_model['id']) electrics = Electrics() self.vehicle.attach_sensor('electrics', electrics) # Connect to running beamng self.bng = BeamNGpy( 'localhost', 64256 ) #, home='C://Users//Alessio//BeamNG.research_unlimited//trunk') self.bng = self.bng.open(launch=False) # Put simulator in pause awaiting while planning the driving self.bng.pause() # Connect to the existing vehicle (identified by the ID set in the vehicle instance) self.bng.set_deterministic() # Set simulator to be deterministic self.bng.connect_vehicle(self.vehicle) assert self.vehicle.skt # Get Initial state of the car. This assumes that the script is invoked after the scenario is started self.bng.poll_sensors(self.vehicle) # Compute the "optimal" driving path and program the ai_script self._compute_driving_path(self.vehicle.state, self.road_model['street']) self.script = self.road_profiler.compute_ai_script( LineString(self.driving_path), self.car_model) # Enforce initial car direction nad up start_dir = (self.vehicle.state['dir'][0], self.vehicle.state['dir'][1], self.vehicle.state['dir'][2]) up_dir = (0, 0, 1) # Configure the ego car self.vehicle.ai_set_mode('disabled') # Note that set script teleports the car by default self.vehicle.ai_set_script(self.script, start_dir=start_dir, up_dir=up_dir) # Resume the simulation self.bng.resume() # At this point the controller can stop ? or wait till it is killed while True: if debug: self.bng.pause() self.bng.poll_sensors(self.vehicle) self.plot_all(self.vehicle.state) self.bng.resume() # Progress the simulation for some time... # self.bng.step(50) sleep(2) except Exception: # When we brutally kill this process there's no need to log an exception l.error("Fatal Error", exc_info=True) finally: self.bng.close()
def launch(beamNGPath, car1, car2, speed_car2): crash = False dist_2car = 20 speed_car2 = int(speed_car2) bng = BeamNGpy('localhost', 64256, beamNG_path) #bng = BeamNGpy('localhost', 64256, home='D:/BeamNGReasearch/Unlimited_Version/trunk') 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') electrics1 = Electrics() electrics2 = Electrics() vehicle1.attach_sensor('electrics1', electrics1) vehicle2.attach_sensor('electrics2', electrics2) #position to try drive then teleport #-365.2436828613281, -214.7460479736328, 1.2118444442749023], [0.9762436747550964, 0.20668958127498627, 0.0650215595960617]] #[[-362.4477844238281, -214.16226196289062, 1.32931387424469], [0.9824153780937195, 0.1852567195892334, 0.023236412554979324]] pos2 = (25.75335693359375, -127.78406524658203, 0.2072899490594864) pos1 = (-88.8136978149414, -261.0204162597656, 0.20253072679042816) #pos_tel1 = (53.35311508178711, -139.84017944335938, 0.20729705691337585) #change this #pos_tel2 = (75.94310760498047, -232.62135314941406, 0.20568031072616577) #change this pos_tel1 = car1[0] pos_tel2 = car2[0] rot2 = (0.9298766851425171, -0.36776003241539, 0.009040255099534988) rot1 = (-0.9998571872711182, 0.016821512952446938, -0.0016229493776336312) # rot_tel1= (0.8766672611236572, -0.4810631573200226, 0.005705998744815588) #change this # rot_tel2 = (-0.896364688873291, -0.4433068335056305, -0.0030648468527942896) #change this rot_tel1 = car1[1] rot_tel2 = car2[1] #initial postion of two car. # run 2cars on particular road until they reach particular speed which satisfies the condisiton of the given testcase 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) try: bng.load_scenario(scenario) bng.start_scenario() vehicle1.ai_set_speed(10, mode='set') vehicle1.ai_set_mode('span') vehicle2.ai_set_speed(speed_car2, mode='set') ##change this param of speed vehicle2.ai_set_mode('chase') sys_output.print_sub_tit("Initial Position and rotation of car") print("\nInitial Position and rotation of car1 %s %s " % (pos1, rot1)) print("\nInitial Position and rotation of car2 %s %s " % (pos2, rot2)) sys_output.print_sub_tit( "\n when speed of car 1 rearch 10 and speed car 2 reach %s. Two cars are teleport to new locations." % (speed_car2)) for _ in range(450): time.sleep(0.1) vehicle1.update_vehicle( ) # Synchs the vehicle's "state" variable with the simulator vehicle2.update_vehicle() sensors1 = bng.poll_sensors( vehicle1 ) # Polls the data of all sensors attached to the vehicle sensors2 = bng.poll_sensors(vehicle2) speed1 = sensors1['electrics1']['values']['wheelspeed'] speed2 = sensors2['electrics2']['values']['wheelspeed'] print("speed of car 1", speed1) print("speed of car 2", speed2) #print([vehicle1.state['pos'],vehicle1.state['dir']]) if speed1 > 9 and speed2 > speed_car2 - 1: #change speed here bng.teleport_vehicle(vehicle1, pos_tel1, rot_tel1) bng.teleport_vehicle(vehicle2, pos_tel2, rot_tel2) sys_output.print_sub_tit("Teleport 2 car to new location") print("\n Car1 : " + str(pos_tel1) + ", " + str(rot_tel1)) print("\n Car2 : " + str(pos_tel2) + ", " + str(rot_tel2)) print("\n Distance between two cars: " + str(distance.euclidean(pos_tel1, pos_tel2))) break # if speed > 19: # bng.teleport_vehicle(vehicle1,pos_tel,rot_tel ) # break for _ in range(100): #pos1 = [] time.sleep(0.1) vehicle1.update_vehicle( ) # Synchs the vehicle's "state" variable with the simulator vehicle2.update_vehicle() sensors1 = bng.poll_sensors( vehicle1 ) # Polls the data of all sensors attached to the vehicle sensors2 = bng.poll_sensors(vehicle2) speed1 = sensors1['electrics1']['values']['wheelspeed'] speed2 = sensors2['electrics2']['values']['wheelspeed'] #pos1.append(vehicle1.state['pos']) #pos2.append(vehicle2.state['pos']) dist_2car = distance.euclidean(vehicle1.state['pos'], vehicle2.state['pos']) if dist_2car < 5: #or int(speed2)== 0 : crash = True print( "\n Failed because distance of two cars are less than 5") break print("\n speed1 %s, speed2: %s, distance: %s" % (speed1, speed2, dist_2car)) if int(speed2) == 0: print("\n Pass because car2 stoped") break bng.stop_scenario() bng.close() finally: bng.close() return crash