Пример #1
0
def getSquareRoadScenario():
    beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory()
                      )  # This is the host & port used to communicate over
    squareRoadScenario = Scenario('smallgrid', 'Straight_Foward_Test')

    testRoad = Road('track_editor_C_center', rid='Test_Road', looped=True)
    roadNode = [(0, 0, 0, 7), (250, 0, 0, 7), (250, 250, 0, 7), (0, 250, 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)

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

    return scenarioDict
def setup_sensors(vehicle, pos=(-0.5, 0.38, 1.3), direction=(0, 1.0, 0)):
    # Set up sensors
    # pos = (-0.3, 1, 1.0) # default
    #pos = (-0.5, 2, 1.0) #center edge of hood
    # pos = (-0.5, 1, 1.0)  # center middle of hood
    # pos = (-0.5, 0.4, 1.0)  # dashboard
    # pos = (-0.5, 0.38, 1.5) # roof
    # pos = (-0.5, 0.38, 1.3) # windshield
    # direction = (0, 1.0, 0)
    fov = 50
    resolution = (200, 150)  #(1280,960) #(512, 512)
    front_camera = Camera(pos,
                          direction,
                          fov,
                          resolution,
                          colour=True,
                          depth=True,
                          annotation=True)

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

    # Attach them
    vehicle.attach_sensor('front_cam', front_camera)
    vehicle.attach_sensor('gforces', gforces)
    vehicle.attach_sensor('electrics', electrics)
    vehicle.attach_sensor('damage', damage)
    vehicle.attach_sensor('timer', timer)
    return vehicle
Пример #3
0
    def setup_sensors(self, vehicle):
        # Set up sensors
        pos = (-0.3, 1, 1.0)
        direction = (0, 0.75, 0) #(0, 0.75, -1.5) #(0, 0.75, 0) #(0,1,0)
        fov = 120
        resolution = (512, 512)
        front_camera = Camera(pos, direction, fov, resolution,
                              colour=True, depth=True, annotation=True)
        pos = (0.0, 3, 1.0)
        direction = (0, -1, 0)
        fov = 90
        resolution = (512, 512)
        back_camera = Camera(pos, direction, fov, resolution,
                             colour=True, depth=True, annotation=True)

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

        # Attach them
        vehicle.attach_sensor('front_cam', front_camera)
        vehicle.attach_sensor('back_cam', back_camera)
        vehicle.attach_sensor('gforces', gforces)
        vehicle.attach_sensor('electrics', electrics)
        vehicle.attach_sensor('damage', damage)
        vehicle.attach_sensor('timer', timer)
        return vehicle
Пример #4
0
def test_gforces(beamng):
    with beamng as bng:
        scenario = Scenario('west_coast_usa', 'gforce_test')
        vehicle = Vehicle('test_car', model='etk800')

        gforces = GForces()
        vehicle.attach_sensor('gforces', gforces)

        scenario.add_vehicle(vehicle,
                             pos=(-717.121, 101, 118.675),
                             rot=(0, 0, 45))
        scenario.make(beamng)

        gx = []
        gy = []
        bng.load_scenario(scenario)
        bng.start_scenario()
        bng.step(120)

        vehicle.ai_set_aggression(2)
        vehicle.ai_set_mode('span')

        for _ in range(64):
            bng.step(30)
            vehicle.poll_sensors()
            gx.append(gforces.data['gx'])
            gy.append(gforces.data['gy'])

    assert np.var(gx) > 1 and np.var(gy) > 1
Пример #5
0
def getStaticScenario(testName):
    beamng = BeamNGpy('localhost', 64256, home=getBeamngDirectory()
                      )  # This is the host & port used to communicate over
    staticScenario = Scenario('smallgrid', str(testName))

    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)

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

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

    return scenarioDict
def setup_sensors(vehicle):
    # Set up sensors
    # pos = (-0.3, 1, 1.0) # default
    pos = (-0.5, 2, 1.0)  #center edge of hood
    # pos = (-0.5, 1, 1.0)  # center middle of hood
    # pos = (-0.5, 0.4, 1.0)  # dashboard
    # pos = (-0.5, 0.38, 1.5) # roof
    # pos = (-0.5, 0.38, 1.1) # windshield
    pos = (-0.5, 0.38, 1.3)  # windshield
    # direction = (0, 1, 0)
    direction = (0, 1.0, 0)
    fov = 50
    resolution = (1280, 960)  #(512, 512)
    front_camera = Camera(pos,
                          direction,
                          fov,
                          resolution,
                          colour=True,
                          depth=True,
                          annotation=True)
    pos = (-0.5, 0.38, 1.1)  # windshield
    direction = (0, 1, 0)
    fov = 120
    resolution = (512, 512)
    back_camera = Camera(pos,
                         direction,
                         fov,
                         resolution,
                         colour=True,
                         depth=True,
                         annotation=True)

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

    # Attach them
    # vehicle.attach_sensor('headlight_cam', front_camera)
    vehicle.attach_sensor('front_cam', front_camera)

    vehicle.attach_sensor('windshield_cam', back_camera)
    vehicle.attach_sensor('gforces', gforces)
    vehicle.attach_sensor('electrics', electrics)
    vehicle.attach_sensor('damage', damage)
    vehicle.attach_sensor('timer', timer)
    return vehicle
Пример #7
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
Пример #8
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)
Пример #9
0
beamng = BeamNGpy('localhost', 64256,home=beamNGPAth)  # This is the host & port used to communicate over

# Create a blue vehicle instance that will be called 'LIF Mobile' in the simulation
# using the etkc model the simulator ships with 'LIFLAB' licence plate
vehicle = Vehicle('LIF_Mobile', model='etkc', licence='LIFLAB', colour='Blue')

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

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

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


# Create a scenario called 'LIF TEST' taking place in the gridmap map the simulator ships with
scenario = Scenario('gridmap', 'LIF_TEST')
# 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=(0, 0, 0), rot=(0, 0, 45))  # 45 degree rotation around the z-axis
print()
# The make function of a scneario is used to compile the scenario and produce a scenario file the simulator can load
scenario.make(beamng)

Пример #10
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
Пример #11
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()
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()