示例#1
0
    def testInitWithCommunicator(self):
        env = RTRLBaseEnv({'generic': {'Communicator': MockCommunicator, 'kwargs': {}}}, 2, 3)

        self.assertFalse(env._running)
        self.assertEqual(len(env._all_comms), 1)
        self.assertEqual(env._action_buffer.array_len, 2)
        self.assertEqual(env._sensation_buffer.array_len, 5)
示例#2
0
 def testStartWithCommunicator(self):
     env = RTRLBaseEnv({'generic': {'Communicator': MockCommunicator, 'kwargs': {}}}, 2, 3, run_mode='singlethread')
     env.start()
     time.sleep(0.5)
     self.assertTrue(env._running)
     self.assertTrue(env._all_comms['generic'].is_alive())
     env.close()
     self.assertFalse(env._running)
     self.assertFalse(env._all_comms['generic'].is_alive())
示例#3
0
 def testStartMultiprocess(self):
     env = RTRLBaseEnv({}, 2, 3, run_mode='multiprocess')
     env.start()
     self.assertTrue(env._running)
     time.sleep(0.5)
     self.assertTrue(env._polling_loop.is_alive())
     env.close()
     self.assertFalse(env._running)
     self.assertFalse(env._polling_loop.is_alive())
示例#4
0
 def testNotImplementedError(self):
     env = RTRLBaseEnv({}, 2, 3, run_mode='singlethread')
     env.start()
     with self.assertRaises(NotImplementedError):
         env.step(0)
     env.close()
示例#5
0
 def testStartSingalthread(self):
     env = RTRLBaseEnv({}, 2, 3, run_mode='singlethread')
     env.start()
     self.assertTrue(env._running)
     env.close()
     self.assertFalse(env._running)
示例#6
0
    def testInit(self):
        env = RTRLBaseEnv({}, 2, 3)

        self.assertFalse(env._running)
        self.assertEqual(env._action_buffer.array_len, 2)
        self.assertEqual(env._sensation_buffer.array_len, 5)
示例#7
0
    def __init__(
            self,
            setup,
            camera_hosts=('localhost', ),
            camera_ports=(5000, ),
            camera_res=(3, 480, 640),
            host=None,
            q_start_queue=None,
            q_target=None,
            dof=6,
            control_type='position',
            derivative_type='none',
            target_type='position',
            reset_type='random',
            reward_type='linear',
            deriv_action_max=10,
            first_deriv_max=10,  # used only with second derivative control
            vel_penalty=0,
            obs_history=1,
            actuation_sync_period=1,
            episode_length_time=None,
            episode_length_step=None,
            rllab_box=False,
            servoj_t=ur_utils.COMMANDS['SERVOJ']['default']['t'],
            servoj_gain=ur_utils.COMMANDS['SERVOJ']['default']['gain'],
            speedj_a=ur_utils.COMMANDS['SPEEDJ']['default']['a'],
            speedj_t_min=ur_utils.COMMANDS['SPEEDJ']['default']['t_min'],
            movej_t=2,  # used for resetting
            accel_max=None,
            speed_max=None,
            dt=0.008,
            delay=0.0,  # to simulate extra delay in the system
            **kwargs):
        assert len(camera_hosts) == len(camera_ports)

        self.num_cameras = len(camera_hosts)
        self.camera_res = camera_res
        self.camera_dim = int(np.product(camera_res))
        self.buffer_len = obs_history
        self.q_start_queue = q_start_queue
        self.q_target = q_target

        # Setup camera communicators and buffer
        communicator_setups = {}
        self._camera_images_ = {}

        for idx, (camera_host,
                  camera_port) in enumerate(zip(camera_hosts, camera_ports)):
            communicator_setups['Camera_{}'.format(idx)] = {
                'Communicator': RealSenseCommunicator,
                # have to read in this number of packets everytime to support
                # all operations
                'num_sensor_packets': self.buffer_len,
                'kwargs': {
                    'host': camera_host,
                    'port': camera_port,
                    'num_channels': camera_res[0],
                    'height': camera_res[1],
                    'width': camera_res[2]
                }
            }

            self._camera_images_['Camera_{}'.format(idx)] = np.frombuffer(
                Array('f', self.camera_dim).get_obj(), dtype='float32')

        print("Setup Reacher Environment")
        # Setup UR environment
        super(ReacherEnvWithRealSense, self).__init__(
            setup,
            host=host,
            dof=dof,
            control_type=control_type,
            derivative_type=derivative_type,
            target_type=target_type,
            reset_type=reset_type,
            reward_type=reward_type,
            deriv_action_max=deriv_action_max,
            first_deriv_max=
            first_deriv_max,  # used only with second derivative control
            vel_penalty=vel_penalty,
            obs_history=obs_history,
            actuation_sync_period=actuation_sync_period,
            episode_length_time=episode_length_time,
            episode_length_step=episode_length_step,
            rllab_box=rllab_box,
            servoj_t=servoj_t,
            servoj_gain=servoj_gain,
            speedj_a=speedj_a,
            speedj_t_min=speedj_t_min,
            movej_t=movej_t,  # used for resetting
            accel_max=accel_max,
            speed_max=speed_max,
            dt=dt,
            delay=delay,  # to simulate extra delay in the system
            communicator_setups=communicator_setups,
            **kwargs)

        # Update the observation space from ReacherEnv to include camera
        self.joint_dim = int(np.product(self._observation_space.shape))
        self.input_dim = self.joint_dim + self.num_cameras * self.camera_dim

        if rllab_box:
            from rllab.spaces import Box as RlBox  # use this for rllab TRPO
            Box = RlBox
        else:
            from gym.spaces import Box as GymBox  # use this for baselines algos
            Box = GymBox

        self._observation_space = Box(low=-np.concatenate((np.zeros(
            self.num_cameras * self.camera_dim), self._observation_space.low)),
                                      high=np.concatenate(
                                          (np.ones(self.num_cameras *
                                                   self.camera_dim),
                                           self._observation_space.high)))

        print("Communicators Setup")
        RTRLBaseEnv.__init__(self,
                             communicator_setups=communicator_setups,
                             action_dim=len(self.action_space.low),
                             observation_dim=len(self.observation_space.low),
                             dt=dt,
                             **kwargs)

        self._obs_ = np.zeros(self.input_dim)