urdfpy

repository·master·Indexed 43 days ago

https://github.com/mmatl/urdfpy

A Python library for loading, manipulating, saving, and visualizing Unified Robot Description Format (URDF) files. It provides a programmatic interface for robot model management, including forward kinematics (FK) for links and trimeshes, and built-in visualization and animation tools.

Tokens
2K
Snippets
10
Records
13
Agent score
54%

What's inside urdfpy

  1. Overview of urdfpy capabilities

    master

    Urdfpy is a Python library designed for working with Unified Robot Description Format (URDF) files. It provides a simple interface for the following tasks:

    • Loading: Reading URDF files into Python objects.
    • Manipulating: Modifying the structure or properties of a robot model.
    • Saving: Exporting modified models back to URDF files.
    • Visualizing: Viewing robot models through a built-in visualizer.
  2. Overview of Urdfpy

    master
    Urdfpy is a pure-Python library designed for loading, manipulating, and exporting Unified Robot Description Format (URDF) files and robot specifications. It provides a programmatic interface to interact with robot models within Python environments.
  3. Set up urdfpy for development

    master

    To develop on urdfpy, install the package in editable mode with development and documentation dependencies. It is also recommended to install git pre-commit hooks to ensure code style compliance via flake8 before commits.

    git clone https://github.com/mmatl/urdfpy.git
    cd urdfpy
    pip install -e .[dev,docs]
    pre-commit install
  4. Load and save URDF files

    master

    You can load a URDF model using URDF.load(). Note that you must ensure all links use relative or absolute paths rather than ROS resource URLs (e.g., package://).

    You can export a URDF model to a file using robot.save(). Any meshes and images will be dumped using their original names. If the names are relative, they will be saved relative to the new URDF file location.

    from urdfpy import URDF
    
    # Loading
    robot = URDF.load('tests/data/ur5/ur5.urdf')
    
    # Saving
    robot.save('/tmp/ur5/ur5.urdf')
  5. Run the urdfpy test suite with pytest

    master

    The project uses pytest for testing. Tests are located in the tests directory. You can run the entire suite, specific files, or individual test functions.

    # Run all tests
    pytest -v tests
    
    # Run tests in a specific file
    pytest -v tests/unit/utils/test_transforms.py
    
    # Run a single test function
    pytest -v tests/unit/utils/test_images.py::test_rgbd_image
  6. Generate code coverage reports

    master

    To check how much of the urdfpy codebase is covered by tests, run pytest with the --cov flag, then use the coverage tool to generate an HTML report.

    ```bash
    # Generate coverage data
    pytest --cov=urdfpy tests
    
    # Generate HTML report
    coverage html

    The HTML report will be available at htmlcov/index.html.

  7. Run code style checks with pre-commit or flake8

    master

    You can run style checks on all files managed by version control using pre-commit. Alternatively, you can run flake8 directly on specific files or directories to check for issues like unused imports or undefined variables.

    # Run on all files under version control
    pre-commit run --all-files
    
    # Run flake8 selectively on a directory or file
    flake8 --ignore=E231,W504 /path/to/files
  8. Perform Forward Kinematics (FK)

    master

    Forward kinematics can be performed using several methods, returning poses as 4x4 homogenous transform matrices relative to the robot's base link.

    robot.link_fk(cfg=None) returns a map from Link objects to their 4x4 transform matrices.

    • If cfg is provided, it should be a dictionary mapping joint names (or joint objects) to their configuration values.

    Trimesh FK

    To get the poses of the actual geometries, use:

    • robot.visual_trimesh_fk(): Returns a map from trimesh.base.Trimesh objects to their poses.
    • robot.collision_trimesh_fk(): Returns a map from trimesh.base.Trimesh objects to their poses.
    # Get link poses at default configuration
    fk = robot.link_fk()
    print(fk[robot.links[1]])
    
    # Get link poses with a specific joint configuration
    # cfg is a dict: {joint_name: value}
    fk = robot.link_fk(cfg={'shoulder_pan_joint': 1.0})
    print(fk[robot.links[1]])
    
    # Get poses for visual/collision trimeshes
    visual_fk = robot.visual_trimesh_fk()
    collision_fk = robot.collision_trimesh_fk()
  9. Access links, joints, and actuated joints

    master

    The URDF object provides direct access to the robot's structure:

    • robot.links: A collection of all links in the robot.
    • robot.joints: A collection of all joints. Each joint object has name, parent, and child attributes.
    • robot.actuated_joints: A collection of joints that can be articulated (non-fixed).
    • robot.base_link: The link designated as the robot's base.
    # Accessing links
    for link in robot.links:
        print(link.name)
    
    # Accessing joints
    for joint in robot.joints:
        print(f'{joint.name} connects {joint.parent} to {joint.child}')
    
    # Accessing actuated joints
    for joint in robot.actuated_joints:
        print(joint.name)
    
    # Accessing base link
    print(robot.base_link.name)
  10. Visualize a robot

    master

    Urdfpy provides built-in methods for quick visualization:

    • robot.show(cfg=None): Visualizes the robot in a static configuration. cfg is a dictionary mapping joint names to specific values.
    • robot.animate(cfg_trajectory=None): Animates the robot over a trajectory. cfg_trajectory is a dictionary where each key is a joint name and each value is a list of configuration values representing the trajectory over time.
    # Static visualization
    robot.show(cfg={
        'shoulder_lift_joint': -2.0,
        'elbow_joint': 2.0
    })
    
    # Trajectory animation
    robot.animate(cfg_trajectory={
        'shoulder_pan_joint': [-np.pi / 4, np.pi / 4],
        'shoulder_lift_joint': [0.0, -np.pi / 2.0],
        'elbow_joint': [0.0, np.pi / 2.0]
    })