MPlib Motion Planning Library

repository·main·Indexed 43 days ago

https://github.com/haosulab/mplib

A lightweight Python library for robot manipulation motion planning, decoupled from ROS. It provides functionalities for collision-free trajectory planning, inverse kinematics, and point cloud environment modeling. Key features include the mplib.Planner and mplib.PlanningWorld classes, support for constrained planning with projection-based methods, and collision detection via check_for_self_collision and check_for_env_collision.

Tokens
8.5K
Snippets
31
Records
39
Agent score
54%

What's inside MPlib

  1. Provide a Jacobian for the constrained function

    main

    Because MPlib uses a projection-based planning method, you must provide the Jacobian of the constraint function. This Jacobian allows the planner to efficiently project configurations onto the constraint manifold. For performance, the Jacobian calculation should be optimized (e.g., by manipulating the forward kinematics Jacobian).

    # Example: Jacobian of the constraint function
    def constraint_jacobian(q):
        # ... implementation logic using single link jacobian ...
        return jacobian_vector
  2. Define a constrained function for motion planning

    main

    To perform constrained planning, you must define a constraint function $R^d \rightarrow R$ that evaluates to zero when a configuration is valid and non-zero otherwise. In MPlib, this function is used by a projection-based planner to map sampled configurations into the valid constraint manifold.

    # Example: Constraint function that evaluates to zero when the 
    # end-effector z-axis points downwards
    def constraint_function(q):
        # ... implementation logic ...
        return error_value
  3. Generate pybind11 docstrings from C++ comments

    main

    MPlib uses a custom tool to automatically generate pybind11 docstrings from C++ code comments. This allows C++ documentation to flow into the Python interface.

    Requirements:

    • C++ comments must follow the doxygen format.
    • You must install libclang matching your local LLVM version: python3 -m pip install libclang=={clang-version}

    Mechanism:

    • The tool ./dev/mkdoc.py processes header files.
    • The generated docstrings are in Sphinx reStructuredText (reST) format and are stored in ./pybind/docstring/.
  4. Plan motion with fixed joints

    main

    MPlib allows you to temporarily fix specific joints during a planning cycle. This is useful for multi-stage motions where you want to move one part of a robot (e.g., a mobile base) while keeping another part (e.g., a manipulator arm) stationary, or vice versa.

    To achieve this, you can specify which joints to ignore during the planning process. By ignoring a subset of joints, they are treated as fixed in their current configuration, and the planner only computes trajectories for the remaining 'active' joints.

    # Example logic for ignoring specific joints
    # To move only joints 0 and 1, ignore joints 2 through 9
    # To move joints 2 through 9, ignore joints 0 and 1
    
    # The specific mechanism involves passing the list of joints to ignore 
    # to the planning function (as demonstrated in the two-stage motion example).
  5. Generate Python stubs and documentation

    main

    To automate the generation of Python type stubs and documentation, run the generate_stubs.sh script. This script uses a Docker container to build the wheels and then employs pybind11-stubgen for stubs and pdoc for documentation.

    ./dev/generate_stubs.sh

    Workflow details:

    • Builds a Python wheel using ./dev/build_wheels.sh.
    • Uses pybind11-stubgen to create stubs, which are then copied into mplib/.
    • Uses pdoc to generate documentation, which is then copied into docs/.
  6. Follow a planned path

    main

    Once plan_pose() or plan_screw() returns a successful path, you must drive the robot to follow the generated waypoints. The output dictionary provides the necessary time-parameterized data (position, time, velocity, acceleration) to execute the motion.

    In simulation environments like sapien, you use these waypoints to update the robot's state over time. If the robot fails to follow the path despite a successful plan, verify your controller's parameters.

  7. Build Python wheels using Docker

    main

    To build Python wheels for specific Python versions, use the provided build script which utilizes the kolinguo/mplib-build Docker image. This ensures a consistent build environment with necessary dependencies pre-installed.

    Run the following command to build a wheel for a specific Python version (e.g., 3.10):

    ./dev/build_wheels.sh --py 310

    Generated wheels will be located in the wheelhouse/ directory, and the pybind11 docstrings will be in ./pybind/docstring/.

  8. Compile MPlib locally (without Docker)

    main

    To compile the C++ dynamic library manually on your local machine, follow these steps:

    1. Install Dependencies: Ensure you have the following installed (Ubuntu commands provided):

      • Eigen3: sudo apt install libeigen3-dev
      • Boost: sudo apt install libboost-all-dev
      • libccd: sudo apt install libccd-dev
      • ompl: sudo apt install libompl-dev
      • octomap: sudo apt install liboctomap-dev
      • urdfdom: sudo apt install liburdfdom-dev
      • orocos_kdl: sudo apt install liborocos-kdl-dev
      • Other required libraries: fcl (fork), assimp, and pinocchio (recommended to install from source).
    2. Build with CMake:

      mkdir build && cd build
      cmake .. && make -j8
    3. Python Integration:

      • The compilation produces a shared object file (e.g., pymp.cpython-310-x86_64-linux-gnu.so). You can import this directly in Python using import pymp.
      • To install the full package including Python glue code, run from the project root:
      python3.[version] -m pip install .
    mkdir build && cd build
    cmake .. && make -j8
  9. Get started with MPlib

    main

    MPlib is a lightweight Python package for motion planning that is decoupled from ROS. It is designed to provide most robot manipulation motion planning functionalities with minimal code. For detailed usage instructions and comprehensive examples, refer to the official tutorial.

    https://motion-planning-lib.readthedocs.io/latest/tutorials/getting_started.html
  10. Use the constrained planner in MPlib

    main

    To execute constrained planning, pass the constraint function and its Jacobian into the planning function. You can optionally specify a tolerance parameter to control the precision of the projection onto the constraint manifold.

    # Planning with a constraint
    plan = planner.plan(
        start_q, 
        goal_q, 
        constraint_function=constraint_function, 
        constraint_jacobian=constraint_jacobian, 
        tolerance=1e-3
    )
    
    # Planning without a constraint
    plan = planner.plan(start_q, goal_q)
  11. Configure VS Code for MPlib development

    main

    To improve the development experience in Visual Studio Code, install the C/C++ and CMake extensions.

    To enable automatic parsing of include directories from CMake results, add the following to your VS Code settings:

    "compileCommands": "${workspaceFolder}/build/compile_commands.json"

    If you prefer other editors like Vim, use clangd. You can generate the required compile_commands.json file by running:

    CMAKE_EXPORT_COMPILE_COMMANDS=ON cmake ..