Example #1
0
    def deserialize(self, turbine, directory):
        """ Method deserializes data in OpenRave format.
        Args:
            turbine: (@ref Turbine) is the turbine object.
            directory: (str) is the relative path to the folder where to load.
        Examples:
            >>> path.deserialize(turbine,'my_folder')
        """

        ind = str()
        for i in range(turbine.robot.GetDOF()):
            ind += str(i) + ' '

        cs = ConfigurationSpecification()
        _ = cs.AddGroup('joint_values ' + turbine.robot.GetName() + ' ' + ind,
                        len(turbine.robot.GetActiveDOFIndices()), 'cubic')
        cs.AddDerivativeGroups(1, False)
        cs.AddDerivativeGroups(2, False)
        _ = cs.AddDeltaTimeGroup()

        directory = realpath(directory)
        TRAJ = []
        onlyfiles = [
            f for f in listdir(directory) if isfile(join(directory, f))
        ]
        onlyfiles.sort(key=int)
        for afile in onlyfiles:
            traj = RaveCreateTrajectory(turbine.env, '')
            traj.Init(cs)
            xml = ET.parse(open(join(directory, afile)))
            traj.deserialize(ET.tostring(xml.getroot()))
            TRAJ.append(traj)
        self.data = TRAJ
        return
Example #2
0
def _deserialize_internal(env, data, data_type):
    from numpy import array, ndarray
    from openravepy import (Environment, KinBody, Robot, Trajectory,
                            RaveCreateTrajectory)
    from prpy.tsr import TSR, TSRChain
    from .exceptions import UnsupportedTypeDeserializationException

    if data_type == dict.__name__:
        return {
            deserialize(env, k): deserialize(env, v)
            for k, v in data.iteritems()
            if k != TYPE_KEY
        }
    elif data_type == ndarray.__name__:
        return array(data['data'])
    elif data_type in [ KinBody.__name__, Robot.__name__ ]:
        body = env.GetKinBody(data['name'])
        if body is None:
            raise ValueError('There is no body with name "{:s}".'.format(
                data['name']))

        return body
    elif data_type == KinBody.Link.__name__:
        body = env.GetKinBody(data['parent_name'])
        if body is None:
            raise ValueError('There is no body with name "{:s}".'.format(
                data['parent_name']))

        link = body.GetLink(data['name'])
        if link is None:
            raise ValueError('Body "{:s}" has no link named "{:s}".'.format(
                data['parent_name'], data['name']))

        return link
    elif data_type == KinBody.Joint.__name__:
        body = env.GetKinBody(data['parent_name'])
        if body is None:
            raise ValueError('There is no body with name "{:s}".'.format(
                data['parent_name']))

        joint = body.GetJoint(data['name'])
        if joint is None:
            raise ValueError('Body "{:s}" has no joint named "{:s}".'.format(
                data['parent_name'], data['name']))

        return joint
    elif data_type == Robot.Manipulator.__name__:
        body = env.GetKinBody(data['parent_name'])
        if body is None:
            raise ValueError('There is no robot with name "{:s}".'.format(
                data['parent_name']))
        elif not body.IsRobot():
            raise ValueError('Body "{:s}" is not a robot.'.format(
                data['parent_name']))

        manip = body.GetJoint(data['name'])
        if manip is None:
            raise ValueError('Robot "{:s}" has no manipulator named "{:s}".'.format(
                data['parent_name'], data['name']))

        return manip
    elif data_type == Trajectory.__name__:
        traj = RaveCreateTrajectory(env, '')
        traj.deserialize(data['data'])
        return traj
    elif data_type == TSR.__name__:
        return TSR.from_dict(data['data'])
    elif data_type == TSRChain.__name__:
        return TSRChain.from_dict(data['data'])
    else:
        raise UnsupportedTypeDeserializationException(data_type)