Beispiel #1
0
 def robot(self, robot):
     robot_element = robot.get_urdf_element()
     root = robot_element.get_root()
     robot_element.add_children(root)
     self.xml = XML()
     self.xml.root = root
     self._robot = robot
Beispiel #2
0
    def from_file(cls, source):
        """Parse a URDF file from a file path or file-like object.

        Parameters
        ----------
        source : str or file
            File path or file-like object.

        Examples
        --------
        >>> urdf = URDF.from_file(compas.get("ur5.xacro"))
        """
        return cls(XML.from_file(source))
Beispiel #3
0
    def from_string(cls, text):
        """Parse URDF from a string.

        Parameters
        ----------
        text : :obj:`str`
            XML string.

        Examples
        --------
        >>> urdf = URDF.from_string('<robot name="panda"/>')
        """
        return cls(XML.from_string(text))
Beispiel #4
0
    def from_file(cls, source):
        """Parse a URDF file from a file path or file-like object.

        Parameters
        ----------
        source : str or file
            File path or file-like object.

        Examples
        --------
        >>> from compas.files import URDF
        >>> urdf = URDF.from_file('/urdf/ur5.urdf')
        """
        return cls(XML.from_file(source))
Beispiel #5
0
    def from_file(cls, source):
        """Construct a URDF from a file path or file-like object.

        Parameters
        ----------
        source : str | file
            File path or file-like object.

        Returns
        -------
        :class:`compas.files.URDF`

        Examples
        --------
        >>> urdf = URDF.from_file(compas.get("ur_description/urdf/ur5.urdf"))

        """
        return cls(XML.from_file(source))
Beispiel #6
0
    def from_string(cls, text):
        """Construct a URDF from a string.

        Parameters
        ----------
        text : str
            XML string.

        Returns
        -------
        :class:`compas.files.URDF`

        Examples
        --------
        >>> urdf = URDF.from_string('<robot name="panda"/>')

        """
        return cls(XML.from_string(text))
Beispiel #7
0
class URDF(object):
    """Class for working with URDF files.

    This class abstracts away the underlying XML of the Unified Robot
    Description Format (`URDF`_) and represents its as an object graph.

    Attributes
    ----------
    xml : :class:`XML`
        Instance of the XML reader/parser class.
    robot : object
        Root element of the URDF model, i.e. a robot instance.

    References
    ----------
    A detailed description of the model is available on the `URDF Model wiki`_.
    This package parses URDF v1.0 according to the `URDF XSD Schema`_.

    * `URDF`_
    * `URDF Model wiki`_
    * `URDF XSD Schema`_

    .. _URDF: http://wiki.ros.org/urdf
    .. _URDF Model wiki: http://wiki.ros.org/urdf/XML/model
    .. _URDF XSD Schema: https://github.com/ros/urdfdom/blob/master/xsd/urdf.xsd

    """
    def __init__(self, xml=None):
        self.xml = xml
        self._robot = None

    @property
    def robot(self):
        if self._robot is None:
            default_namespace = self.xml.root.attrib.get('xmlns')
            self._robot = URDFParser.parse_element(
                self.xml.root,
                _tag_without_namespace(self.xml.root, default_namespace),
                default_namespace)
        return self._robot

    @robot.setter
    def robot(self, robot):
        robot_element = robot.get_urdf_element()
        root = robot_element.get_root()
        robot_element.add_children(root)
        self.xml = XML()
        self.xml.root = root
        self._robot = robot

    @classmethod
    def from_robot(cls, robot):
        """Construct a URDF from a robot.

        Parameters
        ----------
        robot : :class:`compas.robots.RobotModel`

        Returns
        -------
        :class:`compas.files.URDF`

        """
        urdf = cls()
        urdf.robot = robot
        return urdf

    @classmethod
    def from_file(cls, source):
        """Construct a URDF from a file path or file-like object.

        Parameters
        ----------
        source : str | file
            File path or file-like object.

        Returns
        -------
        :class:`compas.files.URDF`

        Examples
        --------
        >>> urdf = URDF.from_file(compas.get("ur_description/urdf/ur5.urdf"))

        """
        return cls(XML.from_file(source))

    read = from_file

    @classmethod
    def from_string(cls, text):
        """Construct a URDF from a string.

        Parameters
        ----------
        text : str
            XML string.

        Returns
        -------
        :class:`compas.files.URDF`

        Examples
        --------
        >>> urdf = URDF.from_string('<robot name="panda"/>')

        """
        return cls(XML.from_string(text))

    def to_file(self, destination=None, prettify=False):
        """Writes the string representation of this URDF instance,
        including all sub-elements, to the ``destination``.

        Parameters
        ----------
        destination : str, optional
            Filepath where the URDF should be written.  Defaults to
            the filepath of the associated XML object.
        prettify : bool, optional
            Whether the string should add whitespace for legibility.

        Returns
        -------
        None

        """
        if destination:
            self.xml.filepath = destination
        self.xml.write(prettify=prettify)

    def to_string(self, encoding='utf-8', prettify=False):
        """Generate a string representation of this URDF instance,
        including all sub-elements.

        Parameters
        ----------
        encoding : str, optional
            Output encoding.
        prettify : bool, optional
            Whether the string should add whitespace for legibility.

        Returns
        -------
        str
            String representation of the URDF.

        """
        return self.xml.to_string(encoding=encoding, prettify=prettify)

    def write(self, destination=None, prettify=False):
        """Writes the string representation of this URDF instance,
        including all sub-elements, to the ``destination``.

        Parameters
        ----------
        destination : str, optional
            Filepath where the URDF should be written.
            Defaults to the filepath of the associated XML object.
        prettify : bool, optional
            Whether the string should add whitespace for legibility.

        Returns
        -------
        None

        """
        self.to_file(destination=destination, prettify=prettify)