Example #1
0
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) # rearview?
    direction = (0, 0, 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.5, 0.38, 1.1)  # rearview
    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('rearview_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
Example #2
0
def getWallCrashScenario(testName):
    beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory()
                      )  # This is the host & port used to communicate over
    wallCrashScenario = Scenario('smallgrid', str(testName))

    wall = StaticObject(
        name="Crash_Test_Wall",
        pos=(420, 0, 0),
        rot=(0, 0, 0),
        scale=(1, 10, 75),
        shape='/levels/smallgrid/art/shapes/misc/gm_alpha_barrier.dae')

    testRoad = Road('track_editor_B_center', rid='Test_Road')
    roadNode = [(-2, 0, 0, 7), (420, 0, 0, 7)]
    testRoad.nodes.extend(roadNode)

    testVehicle = Vehicle('Test_Vehicule',
                          model=SelectCar(),
                          licence='LIFLAB',
                          colour='Blue')

    # Create an Electrics sensor and attach it to the vehicle
    electrics = Electrics()
    testVehicle.attach_sensor('electrics', electrics)

    # Create a Damage sensor and attach it to the vehicle if module is selected
    damage = Damage()
    testVehicle.attach_sensor('damage', damage)

    # Create a Gforce sensor and attach it to the vehicle if module is selected
    gForce = GForces()
    testVehicle.attach_sensor('GForces', gForce)

    wallCrashScenario.add_road(testRoad)
    wallCrashScenario.add_object(wall)
    wallCrashScenario.add_vehicle(testVehicle, pos=(0, 0, 0), rot=(0, 0, -90))
    scenarioDict = {
        'beamng': beamng,
        'scenario': wallCrashScenario,
        'vehicule': testVehicle
    }

    return scenarioDict
Example #3
0
def setup_sensors(vehicle):
    # Set up sensors
    pos = (-0.3, 1, 1.0)
    direction = (0, 1, 0)
    fov = 120
    resolution = (512, 512)
    front_camera = Camera(pos,
                          direction,
                          fov,
                          resolution,
                          colour=True,
                          depth=True,
                          annotation=True)
    pos = (0.0, 3, 1.0)
    direction = (0, -1, 0)
    fov = 90
    resolution = (512, 512)
    back_camera = Camera(pos,
                         direction,
                         fov,
                         resolution,
                         colour=True,
                         depth=True,
                         annotation=True)

    gforces = GForces()
    electrics = Electrics()
    damage = Damage()
    damage.encode_vehicle_request()
    lidar = Lidar(visualized=False)
    timer = Timer()

    # Attach them
    vehicle.attach_sensor('front_cam', front_camera)
    vehicle.attach_sensor('back_cam', back_camera)
    vehicle.attach_sensor('gforces', gforces)
    vehicle.attach_sensor('electrics', electrics)
    vehicle.attach_sensor('damage', damage)
    vehicle.attach_sensor('timer', timer)
    return vehicle
Example #4
0
    def __init__(self,
                 vehicle: Vehicle,
                 beamng: BeamNGpy,
                 additional_sensors: List[Tuple[str, Sensor]] = None):
        self.vehicle = vehicle
        self.beamng = beamng
        self.state: VehicleState = None
        self.vehicle_state = {}

        gforces = GForces()
        electrics = Electrics()
        damage = Damage()
        timer = Timer()

        self.vehicle.attach_sensor('gforces', gforces)
        self.vehicle.attach_sensor('electrics', electrics)
        self.vehicle.attach_sensor('damage', damage)
        self.vehicle.attach_sensor('timer', timer)

        if additional_sensors:
            for (name, sensor) in additional_sensors:
                self.vehicle.attach_sensor(name, sensor)
Example #5
0
def getStraighFowardScenario():
    beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory()
                      )  # This is the host & port used to communicate over
    straightFowardScenario = Scenario('smallgrid', 'Straight_Foward_Test')

    testRoad = Road('track_editor_A_center', rid='Test_Road')
    roadNode = [(-2, 0, 0, 7), (2000, 0, 0, 7)]
    testRoad.nodes.extend(roadNode)

    testVehicle = Vehicle('Test_Vehicule',
                          model='etkc',
                          licence='LIFLAB',
                          colour='Blue')

    # Create an Electrics sensor and attach it to the vehicle
    electrics = Electrics()
    testVehicle.attach_sensor('electrics', electrics)

    # Create a Damage sensor and attach it to the vehicle if module is selected
    damage = Damage()
    testVehicle.attach_sensor('damage', damage)

    # Create a Gforce sensor and attach it to the vehicle if module is selected
    gForce = GForces()
    testVehicle.attach_sensor('GForces', gForce)

    straightFowardScenario.add_road(testRoad)
    straightFowardScenario.add_vehicle(testVehicle,
                                       pos=(0, 0, 0),
                                       rot=(0, 0, -90))
    scenarioDict = {
        'beamng': beamng,
        'scenario': straightFowardScenario,
        'vehicule': testVehicle
    }

    return scenarioDict
Example #6
0
def getStaticScenario():
    beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory())  # This is the host & port used to communicate over
    staticScenario=Scenario("smallgrid","Static_Scenario")

    testVehicle = Vehicle('Test_Vehicule', model='etkc', licence='LIFLAB', colour='Blue')

    # Create an Electrics sensor and attach it to the vehicle
    electrics = Electrics()
    testVehicle.attach_sensor('electrics', electrics)

    # Create a Damage sensor and attach it to the vehicle if module is selected
    damage = Damage()
    testVehicle.attach_sensor('damage', damage)

    # Create a Gforce sensor and attach it to the vehicle if module is selected
    gForce = GForces()
    testVehicle.attach_sensor('GForces', gForce)

    staticScenario.add_vehicle(testVehicle,(0,0,0),(0,0,-90))

    scenarioDict={'beamng':beamng,'scenario':staticScenario,'vehicule':testVehicle}


    return scenarioDict
Example #7
0
def test_electrics(beamng):
    with beamng as bng:
        scenario = Scenario('smallgrid', 'electrics_test')
        vehicle = Vehicle('test_car', model='etk800')

        electrics = Electrics()
        vehicle.attach_sensor('electrics', electrics)

        scenario.add_vehicle(vehicle, pos=(0, 0, 0))
        scenario.make(beamng)

        bng.load_scenario(scenario)
        bng.start_scenario()
        bng.step(120)

        vehicle.control(throttle=1.0)

        bng.step(360)

        vehicle.poll_sensors()

    assert electrics.data['airspeed'] > 0
    assert electrics.data['wheelspeed'] > 0
    assert electrics.data['throttle_input'] > 0
Example #8
0
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()
Example #9
0
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 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()
# Create BeamNG road Scenario
beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN')
scenario = Scenario('GridMap', 'crash_simulation_1')

# create vehicles
vehicleStriker = Vehicle('striker',
                         model='etk800',
                         licence='PYTHON',
                         colour='White')
vehicleVictim = Vehicle('victim',
                        model='etk800',
                        licence='PYTHON',
                        colour='White')

electricsStriker = Electrics()
damageStriker = Damage()
vehicleStriker.attach_sensor('electricsS', electricsStriker)
vehicleStriker.attach_sensor('damagesS', damageStriker)

electricsVictim = Electrics()
damageVictim = Damage()
vehicleVictim.attach_sensor('electricsV', electricsVictim)
vehicleVictim.attach_sensor('damagesV', damageVictim)

positions = list()
directions = list()
wheel_speeds = list()
throttles = list()
brakes = list()
damages = list()
Example #12
0
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')
Example #13
0
 def add_sensor_to(self, vehicle: Vehicle) -> None:
     from beamngpy.sensors import Electrics
     vehicle.attach_sensor(self.rid, Electrics())
Example #14
0
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)
Example #15
0
def getDonutScenario():
    beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory()
                      )  # This is the host & port used to communicate over
    donutScenario = Scenario('smallgrid', 'road_test')

    concreteWallSide1 = StaticObject(
        name="Crash_Test_Wall",
        pos=(20, 10, 0),
        rot=(0, 0, 0),
        scale=(10, 1, 1),
        shape=
        '/levels/driver_training/art/shapes/race/concrete_road_barrier_a.dae')

    concreteWallSide2 = StaticObject(
        name="Crash_Test_Wall2",
        pos=(35, -5, 0),
        rot=(0, 0, 90),
        scale=(10, 1, 1),
        shape=
        '/levels/driver_training/art/shapes/race/concrete_road_barrier_a.dae')

    concreteWallSide3 = StaticObject(
        name="Crash_Test_Wall3",
        pos=(20, -20, 0),
        rot=(0, 0, 0),
        scale=(10, 1, 1),
        shape=
        '/levels/driver_training/art/shapes/race/concrete_road_barrier_a.dae')

    concreteWallSide4 = StaticObject(
        name="Crash_Test_Wall4",
        pos=(5, -5, 0),
        rot=(0, 0, 90),
        scale=(10, 1, 1),
        shape=
        '/levels/driver_training/art/shapes/race/concrete_road_barrier_a.dae')

    testRoad = Road('track_editor_C_center', rid='Test_Road')
    roadNode = [(*(-25, 25, 0), 45), (*(15, 25, 0), 45)]
    testRoad.nodes.extend(roadNode)

    testVehicle = Vehicle('Test_Vehicule',
                          model='etkc',
                          licence='LIFLAB',
                          colour='Blue')

    # Create an Electrics sensor and attach it to the vehicle
    electrics = Electrics()
    testVehicle.attach_sensor('electrics', electrics)

    # Create a Damage sensor and attach it to the vehicle if module is selected
    damage = Damage()
    testVehicle.attach_sensor('damage', damage)

    # Create a Gforce sensor and attach it to the vehicle if module is selected
    gForce = GForces()
    testVehicle.attach_sensor('GForces', gForce)

    donutScenario.add_vehicle(testVehicle, pos=(20, 0, 0), rot=(0, 0, 0))

    donutScenario.add_object(concreteWallSide1)
    donutScenario.add_object(concreteWallSide2)
    donutScenario.add_object(concreteWallSide3)
    donutScenario.add_object(concreteWallSide4)

    donutScenario.add_road(testRoad)

    scenarioDict = {
        'beamng': beamng,
        'scenario': donutScenario,
        'vehicule': testVehicle
    }

    return scenarioDict
import seaborn as sns

from beamngpy import BeamNGpy, Vehicle, Scenario, Road
from beamngpy.sensors import Electrics, Damage
from IPython.display import display

# Instantiate a BeamNGpy instance the other classes use for reference & communication
beamng = BeamNGpy('localhost', 64256, home='F:\Softwares\BeamNG_Research_SVN'
                  )  # This is the host & port used to communicate over

# Create a vehile instance that will be called 'ego' in the simulation
# using the etk800 model the simulator ships with
vehicle = Vehicle('ego', model='etk800', licence='PYTHON', colour='white')
vehicleV = Vehicle('victim', model='etk800', licence='PYTHON', colour='red')
# Create an Electrics sensor and attach it to the vehicle
electrics = Electrics()
vehicle.attach_sensor('electrics', electrics)

damage = Damage()
vehicle.attach_sensor('damages', damage)

# Create a scenario called vehicle_state taking place in the west_coast_usa map the simulator ships with
scenario = Scenario('west_coast_usa', 'vehicle_state')
# Add the vehicle and specify that it should start at a certain position and orientation.
# The position & orientation values were obtained by opening the level in the simulator,
# hitting F11 to open the editor and look for a spot to spawn and simply noting down the
# corresponding values.
scenario.add_vehicle(vehicle, pos=(-727.121, 101, 118.675),
                     rot=(0, 0, 45))  # 45 degree rotation around the z-axis
#scenario.add_vehicle(vehicleV,pos=(-707.121, 101, 118.675), rot=(0, 0, 45))
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()
Example #18
0
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()
Example #19
0
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