args = get_ddpg_args_test()
seed(args.seed)

state_dim = env.get_features().shape[0]
action_dim = env.action_space.shape[0]
max_action = float(env.action_space.high[0])

# Initialize policy
policy = DDPG(state_dim, action_dim, max_action, net_type="dense")
policy.load("model", directory="./models", for_inference=True)

with torch.no_grad():
    while True:
        env.reset()
        obs = env.get_features()
        env.render()
        rewards = []
        while True:
            action = policy.predict(np.array(obs))
            _, rew, done, misc = env.step(action)
            obs = env.get_features()
            rewards.append(rew)
            env.render()

            try:
                lp = env.get_lane_pos2(env.cur_pos, env.cur_angle)
                dist = abs(lp.dist)
            except:
                dist = 0

            print(f"Reward: {rew:.2f}",
class ROSWrapper(DTROS):
    '''
    Class that creates a wrapper between the duckietown simulator and the ROS
    interface of a fake robot simulated in the host. The node serves as interface
    for the following parts:
        - Subscribes to the wheels command topic and transforms it into actions in simulation
        - Publishes the rendered observations in the simulator to the camera image topic
    '''
    def __init__(self, node_name):
        # initialize the DTROS parent class
        super(ROSWrapper, self).__init__(node_name=node_name,
                                         node_type=NodeType.GENERIC)

        # construct rendered observation publisher
        self.camera_pub = rospy.Publisher('~/' + VEHICLE_NAME +
                                          '/camera_node/image/compressed',
                                          CompressedImage,
                                          queue_size=1)

        # Construct subscriber for wheel_cmd topic
        self.wheels_cmd_sub = rospy.Subscriber(
            '~/' + VEHICLE_NAME + '/wheels_driver_node/wheels_cmd',
            WheelsCmdStamped,
            self.wheels_cmd_cb,
            queue_size=1)

        # Initialize the simulator
        self.env = Simulator(
            seed=123,  # random seed
            map_name="loop_empty",
            max_steps=500001,  # we don't want the gym to reset itself
            domain_rand=0,
            camera_width=640,
            camera_height=480,
            accept_start_angle_deg=4,  # start close to straight
            full_transparency=True,
            distortion=True,
        )
        # Create action variable
        self.action = [0.0, 0.0]

    def wheels_cmd_cb(self, msg):
        '''
        Callback that maps wheel commands into actions in the simulator.

            Creates the simulated robot actions based on the received command
            through the main topic in the duckietown infrastructure.

            Args:
                msg (WheelsCmdStamped): Velocity command
        '''

        vel_left = msg.vel_left
        vel_right = msg.vel_right

        self.action = [vel_left, vel_right]

    def run(self):
        '''
        Continuously run the wrapper to map ROS interface with simulator.

            This method runs the node, and begins an iterative process where the
            image is retrieved from the simulator and published in the camera image topic,
            and the wheels command received by subscribing to the wheels_cmd topic is translated
            into actions executed in the simulator. The process iterates until the node is terminated

        '''

        while not rospy.is_shutdown():

            # Update the simulator
            # the action message is updated every time the wheels_cmd_cb is called
            # that is, every time a message arrives.
            observation, reward, done, misc = self.env.step(self.action)
            # Render the new state
            self.env.render()

            # Set again action to stop, in case no more messages are being received
            self.action = [0.0, 0.0]

            if done:
                # Reset the simulator when the robot goes out of the road.
                self.env.reset()

            # Transalte the observation into a CompressedImage message to publish it
            bgr_obs = cv2.cvtColor(observation, cv2.COLOR_RGB2BGR)
            bridge = CvBridge()
            img_msg = bridge.cv2_to_compressed_imgmsg(cvim=bgr_obs,
                                                      dst_format="jpg")

            # Publish the image in the /image/compressed topic
            self.camera_pub.publish(img_msg)
Example #3
0
#                                                            'static': True},
#                                                   },
#                                     'spawn_obstacles': True
#                                     }
if args.spawn_vehicle_ahead:
    env = ForwardObstacleSpawnnigWrapper(env, {'spawn_forward_obstacle': True})
# env = DtRewardWrapper(env)
# env = DtRewardClipperWrapper(env)
# env = DtRewardWrapperDistanceTravelled(env)
env = DtRewardPosAngle(env)
env = DtRewardVelocity(env)
env = DtRewardCollisionAvoidance(env)

env.reset()
env.reset()
env.render(render_mode)


@env.unwrapped.window.event
def on_key_press(symbol, modifiers):
    """
    This handler processes keyboard commands that
    control the simulation
    """
    if symbol == key.BACKSPACE or symbol == key.SLASH:
        print('RESET')
        env.reset()
        env.render()
    elif symbol == key.PAGEUP:
        env.unwrapped.cam_angle[0] = 0
    elif symbol == key.ESCAPE:
Example #4
0
class DuckiegymRosWrapperNode(DTROS):
    """
    Run a gym-duckietown simulator, redirect the simulator's image to a ros topic, redirect the wheel commands
    to simulator actions.
    """
    def __init__(self, node_name):
        super(DuckiegymRosWrapperNode,
              self).__init__(node_name=node_name, node_type=NodeType.GENERIC)

        self.action = [0, 0]  # wheel commands for simulator
        self.camera_info = self.load_camera_info(
            "/data/config/calibrations/camera_intrinsic/default.yaml")

        self.bridge = CvBridge()

        vehicle_name = os.environ.get("VEHICLE_NAME")
        image_topic = "/" + vehicle_name + "/camera_node/image/compressed"
        self.pub_img = rospy.Publisher(image_topic, CompressedImage)
        camera_info_topic = "/" + vehicle_name + "/camera_node/camera_info"
        self.pub_camera_info = rospy.Publisher(camera_info_topic, CameraInfo)
        wheels_cmd_topic = "/" + vehicle_name + "/wheels_driver_node/wheels_cmd"
        self.sub_wheels_cmd = rospy.Subscriber(wheels_cmd_topic,
                                               WheelsCmdStamped,
                                               self.callback_wheels_cmd,
                                               queue_size=1)

        self.sim = Simulator(
            seed=123,  # random seed
            map_name="loop_empty",
            max_steps=500001,  # we don't want the gym to reset itself
            domain_rand=0,
            camera_width=640,
            camera_height=480,
            accept_start_angle_deg=4,  # start close to straight
            full_transparency=True,
            distortion=False,
        )

    def run_simulator(self):
        """
        Main loop of this node, runs simulation loop and camera publishers.
        """
        while not rospy.is_shutdown():
            observation, reward, done, misc = self.sim.step(self.action)
            self.publish_camera_info()
            self.publish_camera_image(observation)
            self.sim.render()
            if done:
                self.sim.reset()

    def callback_wheels_cmd(self, msg):
        """
        Update action variable (parameter for simulator) on callback.
        """
        self.action = [msg.vel_left, msg.vel_right]

    def publish_camera_image(self, observation):
        """
        Publish simulated camera frame as CompressedImage message.
        """
        img = self.bridge.cv2_to_compressed_imgmsg(cv2.cvtColor(
            observation, cv2.COLOR_RGB2BGR),
                                                   dst_format="jpg")
        img.header.stamp = rospy.Time.now()
        self.pub_img.publish(img)

    def publish_camera_info(self):
        """
        Publish calibration info about camera.
        """
        self.camera_info.header.stamp = rospy.Time.now()
        self.pub_camera_info.publish(self.camera_info)

    def load_camera_info(self, filename):
        """
        Load calibration info about camera from config file.
        """
        with open(filename, 'r') as stream:
            calib_data = yaml.load(stream)
        cam_info = CameraInfo()
        cam_info.width = calib_data['image_width']
        cam_info.height = calib_data['image_height']
        cam_info.K = calib_data['camera_matrix']['data']
        cam_info.D = calib_data['distortion_coefficients']['data']
        cam_info.R = calib_data['rectification_matrix']['data']
        cam_info.P = calib_data['projection_matrix']['data']
        cam_info.distortion_model = calib_data['distortion_model']
        return cam_info