class BeamNGBrewer:
    def __init__(self, beamng_home=None, road_nodes: List4DTuple = None):
        self.beamng = BeamNGpy('localhost', 64256, home=beamng_home)

        self.vehicle: Vehicle = None
        self.camera: BeamNGCamera = None
        if road_nodes:
            self.setup_road_nodes(road_nodes)
        steps = 5
        self.params = SimulationParams(beamng_steps=steps,
                                       delay_msec=int(steps * 0.05 * 1000))
        self.vehicle_start_pose = BeamNGPose()

    def setup_road_nodes(self, road_nodes):
        self.road_nodes = road_nodes
        self.decal_road: DecalRoad = DecalRoad('street_1').add_4d_points(
            road_nodes)
        self.road_points = RoadPoints().add_middle_nodes(road_nodes)

    def setup_vehicle(self) -> Vehicle:
        assert self.vehicle is None
        self.vehicle = Vehicle('ego_vehicle',
                               model='etk800',
                               licence='TIG',
                               color='Red')
        return self.vehicle

    def setup_scenario_camera(self,
                              resolution=(1280, 1280),
                              fov=120) -> BeamNGCamera:
        assert self.camera is None
        self.camera = BeamNGCamera(self.beamng, 'brewer_camera')
        return self.camera

    def bring_up(self):
        self.scenario = Scenario('tig', 'tigscenario')
        if self.vehicle:
            self.scenario.add_vehicle(self.vehicle,
                                      pos=self.vehicle_start_pose.pos,
                                      rot=self.vehicle_start_pose.rot)

        if self.camera:
            self.scenario.add_camera(self.camera.camera, self.camera.name)

        self.scenario.make(self.beamng)
        if not self.beamng.server:
            self.beamng.open()
        self.beamng.pause()
        self.beamng.set_deterministic()
        self.beamng.load_scenario(self.scenario)
        self.beamng.start_scenario()

    def __del__(self):
        if self.beamng:
            try:
                self.beamng.close()
            except:
                pass
Exemplo n.º 2
0
class BeamNGBrewer:
    def __init__(self,
                 beamng_home=None,
                 beamng_user=None,
                 reuse_beamng=True,
                 road_nodes: List4DTuple = None):
        self.reuse_beamng = reuse_beamng
        if self.reuse_beamng:
            # This represents the running BeamNG simulator. Since we use launch=True this should automatically
            # shut down when the main python process exits or when we call self.beamng_process.stop()
            self.beamng_process = BeamNGpy('localhost',
                                           64256,
                                           home=beamng_home,
                                           user=beamng_user)
            self.beamng_process = self.beamng_process.open(launch=True)

        # This is used to bring up each simulation without restarting the simulator
        self.beamng = BeamNGpy('localhost',
                               64256,
                               home=beamng_home,
                               user=beamng_user)

        # We need to wait until this point otherwise the BeamNG loggers's level will be (re)configured by BeamNGpy
        log.info("Disabling BEAMNG logs")
        for id in [
                "beamngpy", "beamngpy.beamngpycommon", "beamngpy.BeamNGpy",
                "beamngpy.beamng", "beamngpy.Scenario", "beamngpy.Vehicle"
        ]:
            logger = log.getLogger(id)
            logger.setLevel(log.CRITICAL)
            logger.disabled = True

        self.vehicle: Vehicle = None
        self.camera: BeamNGCamera = None
        if road_nodes:
            self.setup_road_nodes(road_nodes)
        steps = 5
        self.params = SimulationParams(beamng_steps=steps,
                                       delay_msec=int(steps * 0.05 * 1000))
        self.vehicle_start_pose = BeamNGPose()

    def setup_road_nodes(self, road_nodes):
        self.road_nodes = road_nodes
        self.decal_road: DecalRoad = DecalRoad('street_1').add_4d_points(
            road_nodes)
        self.road_points = RoadPoints().add_middle_nodes(road_nodes)

    def setup_vehicle(self) -> Vehicle:
        assert self.vehicle is None
        self.vehicle = Vehicle('ego_vehicle',
                               model='etk800',
                               licence='TIG',
                               color='Red')
        return self.vehicle

    def setup_scenario_camera(self,
                              resolution=(1280, 1280),
                              fov=120) -> BeamNGCamera:
        assert self.camera is None
        self.camera = BeamNGCamera(self.beamng, 'brewer_camera')
        return self.camera

    # TODO COnsider to transform brewer into a ContextManager or get rid of it...
    def bring_up(self):

        if self.reuse_beamng:
            # This assumes BeamNG is already running
            self.beamng.open(launch=False)
        else:
            self.beamng.open(launch=True)

        # After 1.18 to make a scenario one needs a running instance of BeamNG
        self.scenario = Scenario('tig', 'tigscenario')
        if self.vehicle:
            self.scenario.add_vehicle(self.vehicle,
                                      pos=self.vehicle_start_pose.pos,
                                      rot=self.vehicle_start_pose.rot)

        if self.camera:
            self.scenario.add_camera(self.camera.camera, self.camera.name)

        self.scenario.make(self.beamng)

        self.beamng.set_deterministic()

        self.beamng.load_scenario(self.scenario)

        self.beamng.start_scenario()

        # Pause the simulator only after loading and starting the scenario
        self.beamng.pause()

    def __del__(self):
        if self.beamng:
            try:
                self.beamng.close()
            except:
                pass
Exemplo n.º 3
0
ring = ProceduralRing(name='pyring',
                      pos=(445, 301, 218),
                      rot=(0, 0, 100),
                      radius=5,
                      thickness=2.5)
scenario.add_procedural_mesh(ring)

cam_pos = (391.5, 251, 197.8)
cam_dir = (445 - cam_pos[0], 301 - cam_pos[1], 208 - cam_pos[2])
cam = Camera(cam_pos,
             cam_dir,
             60, (2048, 2048),
             near_far=(1, 4000),
             colour=True)
scenario.add_camera(cam, 'cam')

scenario.make(beamng)

bng = beamng.open()
bng.set_deterministic()
bng.load_scenario(scenario)
bng.start_scenario()

meshes = scenario.find_procedural_meshes()
ring_pos = None
for mesh in meshes:
    if mesh.name == 'pyring':
        ring_pos = np.array(mesh.position)

for i in range(5)[1:]: