Ejemplo n.º 1
0
def test_save_episode(vis=False):
    setup_logger(True)

    test_dump = True
    save_episode = True,
    vis = True
    env = PGDriveEnv(
        {
            "environment_num": 1,
            "traffic_density": 0.1,
            "start_seed": 5,
            # "manual_control": vis,
            "use_render": vis,
            "traffic_mode": TrafficMode.Trigger,
            "record_episode": save_episode,
            "map_config": {
                BaseMap.GENERATE_TYPE: MapGenerateMethod.BIG_BLOCK_SEQUENCE,
                BaseMap.GENERATE_CONFIG: "XTXTXTXTXT",
                BaseMap.LANE_WIDTH: 3.5,
                BaseMap.LANE_NUM: 3,
            }
        }
    )
    try:
        o = env.reset()
        epi_info = None
        for i in range(1, 100000 if vis else 2000):
            o, r, d, info = env.step([0, 1])
            if vis:
                env.render()
            if d:
                epi_info = env.engine.dump_episode()

                # test dump json
                if test_dump:
                    with open("test.json", "w") as f:
                        json.dump(epi_info, f)
                break

        o = env.reset(epi_info)
        for i in range(1, 100000 if vis else 2000):
            o, r, d, info = env.step([0, 1])
            if vis:
                env.render()
            if d:
                break
    finally:
        env.close()
Ejemplo n.º 2
0
def test_top_down_rendering():
    env = PGDriveEnv(
        dict(environment_num=20,
             start_seed=0,
             map=10,
             use_topdown=True,
             use_render=False))
    try:
        env.reset()
        for i in range(5):
            env.step(env.action_space.sample())
            env.render(mode="human")
            env.render(mode="rgb_array")
    finally:
        pygame.image.save(env.pg_world.highway_render.frame_surface,
                          "save_offscreen.jpg")
        env.close()
Ejemplo n.º 3
0
def vis_highway_render_with_panda_render():
    setup_logger(True)

    env = PGDriveEnv({
        "environment_num": 1,
        "manual_control": True,
        "use_render": True,
        "use_image": False,
        "use_topdown": True,
    })
    o = env.reset()
    s = time.time()
    for i in range(1, 100000):
        o, r, d, info = env.step(env.action_space.sample())
        env.render()
        if d:
            env.reset()
        if i % 1000 == 0:
            print("Steps: {}, Time: {}".format(i, time.time() - s))
    env.close()
Ejemplo n.º 4
0
from pgdrive.envs.pgdrive_env import PGDriveEnv

if __name__ == "__main__":
    env = PGDriveEnv({
        "environment_num": 1,
        "traffic_density": 0.3,
        "traffic_mode": "reborn",
        "start_seed": 5,
        # "controller": "joystick",
        "manual_control": True,
        "use_render": True,
        "use_saver": True,
        "map": 16
    })

    o = env.reset()
    env.pg_world.force_fps.toggle()
    for i in range(1, 100000):
        o, r, d, info = env.step([0, 1])
        text = {"save": env.save_mode}
        env.render(text=text)
        if info["arrive_dest"]:
            env.reset()
    env.close()
Ejemplo n.º 5
0
from pgdrive.component.map.base_map import BaseMap, MapGenerateMethod
from pgdrive.envs.pgdrive_env import PGDriveEnv
from pgdrive.utils import setup_logger

setup_logger(debug=True)

if __name__ == "__main__":
    env = PGDriveEnv({
        "environment_num": 4,
        "traffic_density": 0.1,
        "start_seed": 3,
        "image_source": "mini_map",
        "manual_control": True,
        "use_render": True,
        "offscreen_render": False,
        "decision_repeat": 5,
        "rgb_clip": True,
        "map_config": {
            BaseMap.GENERATE_TYPE: MapGenerateMethod.BIG_BLOCK_NUM,
            BaseMap.GENERATE_CONFIG: 12,
            BaseMap.LANE_WIDTH: 3.5,
            BaseMap.LANE_NUM: 3,
        }
    })

    env.reset()
    for i in range(1, 100000):
        o, r, d, info = env.step([0, 1])
        env.render(text={"Frame": i, "Speed": env.vehicle.speed})
    env.close()
Ejemplo n.º 6
0
import time

from pgdrive.envs.pgdrive_env import PGDriveEnv
from pgdrive.scene_creator.algorithm.BIG import BigGenerateMethod
from pgdrive.scene_creator.map import Map

if __name__ == "__main__":
    env = PGDriveEnv(
        dict(use_render=True,
             map_config={
                 Map.GENERATE_METHOD: BigGenerateMethod.BLOCK_NUM,
                 Map.GENERATE_PARA: 7
             },
             traffic_density=0.5,
             manual_control=True,
             traffic_mode=0))

    start = time.time()
    env.reset()
    env.render()
    print("Render cost time: ", time.time() - start)
    while True:
        o, r, d, info = env.step([0, 1])
        env.render()
        # if d:
        #     env.reset()
    env.close()
Ejemplo n.º 7
0
        }
    })
    o = env.reset()

    depth_camera = env.config["vehicle_config"]["depth_camera"]
    depth_camera = DepthCamera(*depth_camera,
                               chassis_np=env.vehicle.chassis,
                               engine=env.engine)
    env.vehicle.add_image_sensor("depth_camera", depth_camera)
    depth_camera.remove_display_region(env.engine)

    # for sensor in env.vehicle.image_sensors.values():
    #     sensor.remove_display_region(env.engine)
    # env.vehicle.vehicle_panel.remove_display_region(env.engine)
    # env.vehicle.contact_result_render.detachNode()
    # env.vehicle.navigation._right_arrow.detachNode()

    env.vehicle.chassis.setPos(244, 0, 1.5)
    for i in range(1, 100000):
        o, r, d, info = env.step([0, 1])
        env.render(
            # text={
            #     "vehicle_num": len(env.engine.traffic_manager.traffic_vehicles),
            #     "dist_to_left:": env.vehicle.dist_to_left,
            #     "dist_to_right:": env.vehicle.dist_to_right,
            # }
        )
        if d:
            env.reset()
    env.close()
Ejemplo n.º 8
0
        "rgb_clip":
        True,
        "vehicle_config":
        dict(depth_camera=(200, 88, False), image_source="depth_camera"),
        "headless_machine_render":
        False,
        "map_config": {
            BaseMap.GENERATE_TYPE: MapGenerateMethod.BIG_BLOCK_NUM,
            BaseMap.GENERATE_CONFIG: 12,
            BaseMap.LANE_WIDTH: 3.5,
            BaseMap.LANE_NUM: 3,
        }
    })
    env.reset()
    env.engine.accept("m",
                      env.vehicle.image_sensors["depth_camera"].save_image,
                      extraArgs=[env.vehicle])

    for i in range(1, 100000):
        o, r, d, info = env.step([0, 1])
        assert env.observation_space.contains(o)
        if env.config["use_render"]:
            # for i in range(ImageObservation.STACK_SIZE):
            #     ObservationType.show_gray_scale_array(o["image"][:, :, i])
            env.render(text={"can you see me": i})
        if d:
            env.vehicle.image_sensors["rgb_camera"].save_image(env.vehicle)
            print("Reset")
            env.reset()
    env.close()
Ejemplo n.º 9
0
def test_navigation(vis=False):
    env = PGDriveEnv({
        "environment_num": 10,
        "traffic_density": 0.0,
        "use_render": vis,
        "start_seed": 5,
        "map_config": {
            BaseMap.GENERATE_TYPE: MapGenerateMethod.BIG_BLOCK_NUM,
            BaseMap.GENERATE_CONFIG: 7,
            BaseMap.LANE_WIDTH: 3.5,
            BaseMap.LANE_NUM: 3,
        }
    })
    target = Target(0.375, 30)
    o = env.reset()
    if vis:
        env.engine.accept('d', target.go_right)
        env.engine.accept('a', target.go_left)
        env.engine.accept('w', target.faster)
        env.engine.accept('s', target.slower)

    steering_controller = PIDController(1.6, 0.0008, 27.3)
    acc_controller = PIDController(0.1, 0.001, 0.3)

    steering_error = o[0] - target.lateral
    steering = steering_controller.get_result(steering_error)

    acc_error = env.vehicles[env.DEFAULT_AGENT].speed - target.speed
    acc = acc_controller.get_result(acc_error)
    for i in range(1, 1000000 if vis else 2000):
        o, r, d, info = env.step([-steering, acc])
        # calculate new action

        steering_error = o[0] - target.lateral
        steering = steering_controller.get_result(steering_error)

        t_speed = target.speed if abs(o[12] -
                                      0.5) < 0.01 else target.speed - 10
        acc_error = env.vehicles[env.DEFAULT_AGENT].speed - t_speed
        acc = acc_controller.get_result(acc_error)
        if vis:
            if i < 700:
                env.render(
                    text={
                        "W": "Target speed +",
                        "S": "Target speed -",
                        "A": "Change to left lane",
                        "D": "Change to right lane"
                    })
            if i == 500:
                env.engine.on_screen_message.data.clear()
            else:
                env.render()
        if d:
            print("Reset")
            o = env.reset()

            steering_controller.reset()
            steering_error = o[0] - target.lateral
            steering = steering_controller.get_result(steering_error, o[11])

            acc_controller.reset()
            acc_error = env.vehicles[env.DEFAULT_AGENT].speed - target.speed
            acc = acc_controller.get_result(acc_error)
    env.close()
Ejemplo n.º 10
0
            # # "show_line_to_dest": True,
            # "show_dest_mark": True
        }
    })
    import time

    start = time.time()
    o = env.reset()

    for s in range(1, 100000):
        o, r, d, info = env.step(env.action_space.sample())
        # info["fuel"] = env.vehicle.energy_consumption
        env.render(
            text={
                "heading_diff": env.vehicle.heading_diff(env.vehicle.lane),
                "engine_force": env.vehicle.config["max_engine_force"],
                "current_seed": env.current_seed,
                "lane_width": env.vehicle.lane.width
            })
        print({
            env.engine.get_policy(
                env.vehicle.id).controller.joystick.get_button(4)
        })
        # assert env.observation_space.contains(o)
        # if (s + 1) % 100 == 0:
        #     print(
        #         "Finish {}/10000 simulation steps. Time elapse: {:.4f}. Average FPS: {:.4f}".format(
        #             s + 1,f
        #             time.time() - start, (s + 1) / (time.time() - start)
        #         )
        #     )
Ejemplo n.º 11
0
from pgdrive.component.map.base_map import BaseMap, MapGenerateMethod
from pgdrive.envs.pgdrive_env import PGDriveEnv
from pgdrive.utils import setup_logger

setup_logger(debug=True)

if __name__ == "__main__":
    import numpy as np

    env = PGDriveEnv({
        "environment_num": 4,
        "traffic_density": 0.0,
        "use_render": True,
        "map_config": {
            BaseMap.GENERATE_TYPE: MapGenerateMethod.BIG_BLOCK_SEQUENCE,
            BaseMap.GENERATE_CONFIG: "SSSSSSSSSSSSS",
        },
        "manual_control": True
    })
    acc = [0, 1]
    brake = [-1, -np.nan]
    env.reset()
    for i in range(1, 100000):
        o, r, d, info = env.step(acc)
        print("new:{}, old:{}, diff:{}".format(
            env.vehicle.speed, env.vehicle.system.get_current_speed_km_hour(),
            env.vehicle.speed -
            env.vehicle.system.get_current_speed_km_hour()))
        env.render("Test: {}".format(i))
    env.close()
Ejemplo n.º 12
0
                BaseMap.LANE_NUM: 3,
            },
            "driving_reward": 1.0,
            "vehicle_config": {
                "show_lidar": False,
                "show_side_detector": True,
                "show_lane_line_detector": True,
                "lane_line_detector": {
                    "num_lasers": 100
                }
            }
        }
    )

    o = env.reset()
    print("vehicle num", len(env.engine.traffic_manager.vehicles))
    for i in range(1, 100000):
        o, r, d, info = env.step([0, 1])
        info["fuel"] = env.vehicle.energy_consumption
        env.render(
            text={
                "left": env.vehicle.dist_to_left_side,
                "right": env.vehicle.dist_to_right_side,
                "white_lane_line": env.vehicle.on_white_continuous_line
            }
        )
        if d:
            print("Reset")
            env.reset()
    env.close()