Robotics, Vision & Control 3 in Python

repository·main·Indexed 20 days ago

https://github.com/petercorke/rvc3-python

A comprehensive Python environment and support package (rvc3python v0.9.2) for the textbook 'Robotics, Vision & Control: 3rd edition'. It includes the Robotics, Machine Vision, Spatial Maths, and Block Diagram Simulation toolboxes. The repository provides Jupyter notebooks, figure generation scripts using Matplotlib and PyVista, 3D point cloud data in PCD/PLY/glTF formats, and the rvctool interactive workbench for MATLAB-like experimentation.

Tokens
29.5K
Snippets
123
Records
145
Agent score
71%

What's inside rvc3-python

  1. Locate and use figures from the book

    main

    Figures from the Robotics, Vision & Control: 3rd edition in Python are organized by type and chapter. Depending on your needs, you can access:

    • Line drawings: Manually drawn EPS files.
    • Code-generated figures: PDF files generated via individual Python scripts (one script per figure).
    • 3D figures: GLTF format files generated using PyVista.
    • Point clouds: Colored PCD or PLY format files generated using the Machine Vision Toolbox for Python and Open3D.
  2. Access Block Diagram models via `bdsim`

    main

    The repository includes 25 block diagram models used for pedagogy in the RVC3 book. These models are powered by the bdsim package.

    • Running Models: You can invoke these models directly from rvctool using the %run magic command.
    • Locating Models: Use the bdsim_path command-line tool to find the exact installation directory of the models within your Python package tree.
    # Run a specific block diagram model from within rvctool
    %run -m vloop_test
  3. Understand the bdsim model file structure

    main

    Each bdsim model in this repository typically consists of a set of related files that define the system, its visual representation, and its execution logic:

    • model.py: A pure Python implementation using bdsim classes to define blocks and interconnections. This is the primary way to implement a system model in code.
    • model.bd: A JSON file representing a block diagram drawn with bdedit.
    • model-main.py: A script used for complex models containing a "MAIN" block. It sets up necessary Python objects and then imports/executes the model.bd file.
  4. Distortion Model and Parameters

    main

    The eucsbademo program uses the Bouguet distortion model. Distortion is specified by a five-element vector kc:

    • kc[0]: 2nd order radial distortion coefficient
    • kc[1]: 4th order radial distortion coefficient
    • kc[2]: Tangential distortion coefficient
    • kc[3]: Tangential distortion coefficient
    • kc[4]: 6th order radial distortion coefficient

    Mathematical Model

    Given a pinhole projection $x_n = (x, y) = (X/Z, Y/Z)$ and $r^2 = x^2 + y^2$, the distorted projection $x_d$ is:

    $x_d = (1 + kc[0]r^2 + kc[1]r^4 + kc[4]r^6)x_n + dx$

    Where the tangential component $dx$ is:

    • $dx_{sub} = 2 imes kc[2] imes x imes y + kc[3] imes (r^2 + 2x^2)$
    • $dy_{sub} = kc[2] imes (r^2 + 2y^2) + 2 imes kc[3] imes x imes y$

    Note: The implementation uses zero-based indexing for kc coefficients.

  5. Run figure generation scripts

    main

    The repository contains Python scripts used to generate the figures found in the Robotics, Vision & Control: 3rd edition book. These scripts use Matplotlib and the RVC3.tools.rvcprint utility to produce high-quality plots.

    Script Organization

    Scripts are organized by chapter in folders named chapterN:

    • figN_M.py: Generates Figure N.M.
    • box_NAME.py: Generates a figure for an excurse box.

    Prerequisites

    The rvcpython package must be installed in your environment for these scripts to function, as they rely on RVC3.tools.rvcprint for saving files.

    Execution

    You can run the scripts directly from the terminal using either of the following commands:

    ./figN_M.py
    # OR
    python figN_M.py

    Output

    By default, figures are saved in the current working directory in PDF format. The filename follows the pattern figN_M.pdf. Some scripts may generate multiple sub-figures using lowercase letter suffixes (e.g., figN_M_a.pdf).

    python fig10_20.py
    # Output example:
    # saving -->  fig10_20.pdf
  6. Use the `rvctool` interactive workbench

    main

    The rvctool command provides a highly capable, "MATLAB-like" interactive IPython session. It is designed for rapid experimentation and is the primary environment used for examples in the Robotics, Vision & Control book.

    Key Features:

    • Automatic Imports: All Toolboxes (Robotics, Machine Vision, Spatial Math, etc.) and supporting packages are imported using import *, allowing you to access functions and classes without package prefixes.
    • MATLAB-like Display: Results are displayed by default. Use a trailing semicolon (;) to suppress output.
    • Forgiving REPL: It automatically strips >>> prompt characters and ignores indentation, making it easy to copy and paste code blocks from the book.
    • Script Execution: You can run a Python script and then be dropped into an interactive session: $ rvctool script.py.
    $ rvctool
  7. Generate 3D figures using PyVista

    main
    For 3D figures from chapters 2-3, 7-9, and other specific sections, the repository provides GLTF format files and the corresponding Python scripts used to generate them. These scripts utilize PyVista rather than Matplotlib to ensure better 3D visualization quality. You can find these in the 3d/ directory.
  8. Install rvc3python into a Conda environment

    main

    To avoid dependency conflicts with other projects, it is recommended to install rvc3python into a dedicated Conda virtual environment. The following steps create an environment named RVC3 using Python 3.10 and install the package:

    conda create -n RVC3 python=3.10
    conda activate RVC3
    pip install rvc3python
  9. Run Euclidean Bundle Adjustment with eucsbademo

    main

    The eucsbademo program implements three variants of Euclidean Bundle Adjustment (BA) using the sba library. Depending on the variant chosen, the number and type of command-line arguments change.

    Variant A: Fixed Intrinsics

    Use this when camera intrinsics are assumed to be fixed and identical for all cameras. Arguments:

    1. camera_motion_file: Initial estimates for camera pose (rotation and translation).
    2. structure_file: Initial estimates for 3D points and their image projections.
    3. calibration_file: The camera intrinsic calibration matrix.

    Command Example:

    eucsbademo 7cams.txt 7pts.txt calib.txt

    Variant B: Varying Intrinsics

    Use this when camera intrinsics vary among cameras. Arguments:

    1. camera_params_file: Initial estimates for both camera intrinsics and motion parameters.
    2. structure_file: Initial estimates for 3D points and their image projections.

    Command Example:

    eucsbademo 7camsvarK.txt 7pts.txt

    Variant C: Varying Intrinsics and Distortion

    Use this when both intrinsics and distortion parameters vary among cameras. Arguments:

    1. camera_params_file: Initial estimates for camera intrinsics, distortion, and motion parameters.
    2. structure_file: Initial estimates for 3D points and their image projections.

    Command Example:

    eucsbademo 54camsvarKD.txt 54pts.txt
  10. Visualize 3D point clouds from the book

    main

    The 3D point cloud figures in the book are provided in PCD and PLY formats to avoid the ambiguity of 2D representations. To view these files locally, you can use the following tools:

    • MeshLab: A general-purpose tool for processing and visualizing 3D meshes and point clouds.
    • VSCode Extension: Use the 3D Viewer Light extension to view files directly within Visual Studio Code.

    Additionally, hotlinks within the book lead to a web-based online viewer (based on Three.js) that utilizes the glTF version of the point cloud files.

  11. Install deep learning tools for PyTorch examples

    main

    If you intend to run the deep learning examples found in Chapter 11, you must install the pytorch extra. This ensures that torch and other necessary deep learning dependencies are included in your environment.

    pip install rvc3python[pytorch]
  12. Run RVC3 Python notebooks in Google Colab

    main

    The code for each chapter of the book is provided as Jupyter notebooks (.ipynb). You can run these locally or directly in Google Colab by using the Colab badge links provided in the notebook directory.

    Note on Matplotlib in Colab: As of early 2023, Colab provides Matplotlib 3.2.2, which may cause issues with multi-stage plots (they may appear as a stack of individual plots rather than overlays). To fix this, the first code cell in most notebooks installs a newer version of Matplotlib. You will need to restart the kernel and run that cell again after the installation completes.