• Stars
    star
    191
  • Rank 202,877 (Top 4 %)
  • Language
    Jupyter Notebook
  • License
    MIT License
  • Created over 3 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code examples for Robotics, Vision & Control 3rd edition in Python

Robotics, Vision & Control: 3rd edition in Python (2023)

A Python Robotics Package QUT Centre for Robotics Open Source License: MIT

PyPI version PyPI - Python Version PyPI - Downloads

Front cover 978-3-031-06468-5_5208 Welcome to the online hub for the book:
  • Robotics, Vision & Control: fundamental algorithms in Python (3rd edition)
  • Peter Corke, published by Springer-Nature 2023.
  • ISBN 978-3-031-06468-5 (hardcopy), 978-3-031-06469-2 (eBook)
  • DOI 10.1007/978-3-031-06469-2


Report an issue with the book or its supporting code here.

Known errata for the book can be viewed here.

This book uses many examples based on the following open-source Python packages

Robotics Toolbox for Python Machine Vision Toolbox for Python Spatial Maths Toolbox for Python Block diagram simulation for Python

Robotics Toolbox for Python, Machine Vision Toolbox for Python, Spatial Maths Toolbox for Python, Block Diagram Simulation for Python. These in turn have dependencies on other packages created by the author and third parties.

Installing the package

This package provides a simple one-step installation of all the required Toolboxes

pip install rvc3python

or

conda install rvc3python

There are a lot of dependencies and this might take a minute or so. You now have a very powerful computing environment for robotics and computer vision.

Python version

Given the rapid rate of language additions, particularly around type hinting, use at least Python 3.8. Python 3.7 goes end of life in June 2023.

Not all package dependencies will work with the latest release of Python. In particular, check:

  • PyTorch used for segmentation examples in Chapter 12
  • Open3D, used for point cloud examples in Chapter 14.

Installing into a Conda environment

It's probably a good idea to create a virtual environment to keep this package and its dependencies separated from your other Python code and projects. If you've never used virtual environments before this might be a good time to start, and it is really easy using Conda:

conda create -n RVC3 python=3.10
conda activate RVC3
pip install rvc3python

Installing deep learning tools

Chapter 11 has some deep learning examples based on PyTorch. If you don't have PyTorch installed you can use the pytorch install option

pip install rvc3python[pytorch]

or

conda install rvc3python[pytorch]

Using the Toolboxes

The simplest way to get going is to use the command line tool

$ rvctool
 ____       _           _   _             __     ___     _                ___      ____            _             _   _____ 
|  _ \ ___ | |__   ___ | |_(_) ___ ___    \ \   / (_)___(_) ___  _ __    ( _ )    / ___|___  _ __ | |_ _ __ ___ | | |___ / 
| |_) / _ \| '_ \ / _ \| __| |/ __/ __|    \ \ / /| / __| |/ _ \| '_ \   / _ \/\ | |   / _ \| '_ \| __| '__/ _ \| |   |_ \ 
|  _ < (_) | |_) | (_) | |_| | (__\__ \_    \ V / | \__ \ | (_) | | | | | (_>  < | |__| (_) | | | | |_| | | (_) | |  ___) |
|_| \_\___/|_.__/ \___/ \__|_|\___|___( )    \_/  |_|___/_|\___/|_| |_|  \___/\/  \____\___/|_| |_|\__|_|  \___/|_| |____/ 
                                      |/                                                                                   
                                                                                 
for Python (RTB==1.1.0, MVTB==0.9.5, SG==1.1.7, SMTB==1.1.7, NumPy==1.24.2, SciPy==1.10.1, Matplotlib==3.7.1)

    import math
    import numpy as np
    from scipy import linalg, optimize
    import matplotlib.pyplot as plt
    from spatialmath import *
    from spatialmath.base import *
    from spatialmath.base import sym
    from spatialgeometry import *
    from roboticstoolbox import *
    from machinevisiontoolbox import *
    import machinevisiontoolbox.base as mvb
    
    # useful variables
    from math import pi
    puma = models.DH.Puma560()
    panda = models.DH.Panda()

    func/object?       - show brief help
    help(func/object)  - show detailed help
    func/object??      - show source code

Results of assignments will be displayed, use trailing ; to suppress
    
Python 3.10.9 | packaged by conda-forge | (main, Feb  2 2023, 20:24:27) [Clang 14.0.6 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.11.0 -- An enhanced Interactive Python. Type '?' for help.


>>> 

This provides an interactive Python (IPython) session with all the Toolboxes and supporting packages imported, and ready to go. It's a highly capable, convenient, and "MATLAB-like" workbench environment for robotics and computer vision.

For example to load an ETS model of a Panda robot, solve a forward kinematics and inverse kinematics problem, and an interactive graphical display is simply:

>>> panda = models.ETS.Panda()
ERobot: Panda (by Franka Emika), 7 joints (RRRRRRR)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚link โ”‚ link  โ”‚ joint โ”‚ parent โ”‚             ETS: parent to link             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚   0 โ”‚ link0 โ”‚     0 โ”‚ BASE   โ”‚ tz(0.333) โŠ• Rz(q0)                          โ”‚
โ”‚   1 โ”‚ link1 โ”‚     1 โ”‚ link0  โ”‚ Rx(-90ยฐ) โŠ• Rz(q1)                           โ”‚
โ”‚   2 โ”‚ link2 โ”‚     2 โ”‚ link1  โ”‚ Rx(90ยฐ) โŠ• tz(0.316) โŠ• Rz(q2)                โ”‚
โ”‚   3 โ”‚ link3 โ”‚     3 โ”‚ link2  โ”‚ tx(0.0825) โŠ• Rx(90ยฐ) โŠ• Rz(q3)               โ”‚
โ”‚   4 โ”‚ link4 โ”‚     4 โ”‚ link3  โ”‚ tx(-0.0825) โŠ• Rx(-90ยฐ) โŠ• tz(0.384) โŠ• Rz(q4) โ”‚
โ”‚   5 โ”‚ link5 โ”‚     5 โ”‚ link4  โ”‚ Rx(90ยฐ) โŠ• Rz(q5)                            โ”‚
โ”‚   6 โ”‚ link6 โ”‚     6 โ”‚ link5  โ”‚ tx(0.088) โŠ• Rx(90ยฐ) โŠ• tz(0.107) โŠ• Rz(q6)    โ”‚
โ”‚   7 โ”‚ @ee   โ”‚       โ”‚ link6  โ”‚ tz(0.103) โŠ• Rz(-45ยฐ)                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚name โ”‚ q0  โ”‚ q1     โ”‚ q2  โ”‚ q3    โ”‚ q4  โ”‚ q5    โ”‚ q6   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  qr โ”‚  0ยฐ โ”‚ -17.2ยฐ โ”‚  0ยฐ โ”‚ -126ยฐ โ”‚  0ยฐ โ”‚  115ยฐ โ”‚  45ยฐ โ”‚
โ”‚  qz โ”‚  0ยฐ โ”‚  0ยฐ    โ”‚  0ยฐ โ”‚  0ยฐ   โ”‚  0ยฐ โ”‚  0ยฐ   โ”‚  0ยฐ  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”˜

>>> panda.fkine(panda.qz)
   0.7071    0.7071    0         0.088     
   0.7071   -0.7071    0         0         
   0         0        -1         0.823     
   0         0         0         1      
>>> panda.ikine_LM(SE3.Trans(0.4, 0.5, 0.2) * SE3.Ry(pi/2))
IKSolution(q=array([  -1.849,   -2.576,   -2.914,     1.22,   -1.587,    2.056,   -1.013]), success=True, iterations=13, searches=1, residual=3.3549072615799585e-10, reason='Success')
>>> panda.teach(panda.qz)

Computer vision is just as easy. For example, we can import an image, blur it and display it alongside the original

>>> mona = Image.Read("monalisa.png")
>>> Image.Hstack([mona, mona.smooth(sigma=5)]).disp()

or load two images of the same scene, compute SIFT features and display putative matches

>>> sf1 = Image.Read("eiffel-1.png", mono=True).SIFT()
>>> sf2 = Image.Read("eiffel-2.png", mono=True).SIFT()
>>> matches = sf1.match(sf2)
>>> matches.subset(100).plot("w")

rvctool is a wrapper around IPython where:

  • robotics and vision functions and classes can be accessed without needing package prefixes
  • results are displayed by default like MATLAB does, and like MATLAB you need to put a semicolon on the end of the line to prevent this
  • the prompt is the standard Python REPL prompt >>> rather than the IPython prompt, this can be overridden by a command-line switch
  • allows cutting and pasting in lines from the book, and prompt characters are ignored

The Robotics, Vision & Control book uses rvctool for all the included examples.

rvctool imports the all the above mentioned packages using import * which is not considered best Python practice. It is very convenient for interactive experimentation, but in your own code you can handle the imports as you see fit.

Cutting and pasting

IPython is very forgiving when it comes to cutting and pasting in blocks of Python code. It will strip off the >>> prompt character and ignore indentation. The normal python REPL is not so forgiving. IPython also allows maintains a command history and allows command editing.

Simple scripting

You can write very simple scripts, for example test.py is

T = puma.fkine(puma.qn)
sol = puma.ikine_LM(T)
sol.q
puma.plot(sol.q);

then

$ rvctool test.py
   0         0         1         0.5963    
   0         1         0        -0.1501    
  -1         0         0         0.6575    
   0         0         0         1         

IKSolution(q=array([7.235e-08,  -0.8335,  0.09396,    3.142,   0.8312,   -3.142]), success=True, iterations=15, searches=1, residual=1.406125546650288e-07, reason='Success')
array([7.235e-08,  -0.8335,  0.09396,    3.142,   0.8312,   -3.142])
PyPlot3D backend, t = 0.05, scene:
  robot: Text(0.0, 0.0, 'Puma 560')
>>>

and you are dropped into an IPython session after the script has run.

Issues running on Apple Silicon

Check out the wiki page.

Using Jupyter and Colab

Graphics and animations are problematic in these environments, some things work well, some don't. As much as possible I've tweaked the Jupyter notebooks to work as best they can in these environments.

For local use the Jupyter plugin for Visual Studio Code is pretty decent. Colab suffers from old versions of major packages (though they are getting better at keeping up to date) and animations can suffer from slow update over the network.

Other command line tools

Additional command line tools available (from the Robotics Toolbox) include:

  • eigdemo, animation showing linear transformation of a rotating unit vector which demonstrates eigenvalues and eigenvectors.
  • tripleangledemo, Swift visualization that lets you experiment with various triple-angle sequences.
  • twistdemo, Swift visualization that lets you experiment with 3D twists. The screw axis is the blue rod and you can position and orient it using the sliders, and adjust its pitch. Then apply a rotation about the screw using the bottom slider.

Block diagram models

bdsim logo

Block diagram models are key to the pedagogy of the RVC3 book and 25 models are included. To simulate these models we use the Python package bdsim which can run models:

  • written in Python using bdsim blocks and wiring.
  • created graphically using bdedit and saved as a .bd (JSON format) file.

The models are included in the RVC3 package when it is installed and rvctool adds them to the module search path. This means you can invoke them from rvctool by

>>> %run -m vloop_test

If you want to directly access the folder containing the models, the command line tool

bdsim_path

will display the full path to where they have been installed in the Python package tree.

Additional book resources

Front cover 978-3-031-06468-5_5208

This GitHub repo provides additional resources for readers including:

  • Jupyter notebooks containing all code lines from each chapter, see the notebooks folder
  • The code to produce every Python/Matplotlib (2D) figure in the book, see the figures folder
  • 3D points clouds from chapter 14, and the code to create them, see the pointclouds folder.
  • 3D figures from chapters 2-3, 7-9, and the code to create them, see the 3dfigures folder.
  • All example scripts, see the examples folder.
  • To run the visual odometry example in Sect. 14.8.3 you need to download two image sequence, each over 100MB, see the instructions here.

To get that material you must clone the repo

git clone https://github.com/petercorke/RVC3-python.git

More Repositories

1

robotics-toolbox-python

Robotics Toolbox for Python
Python
2,041
star
2

robotics-toolbox-matlab

Robotics Toolbox for MATLAB
MATLAB
1,259
star
3

spatialmath-python

Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
Python
294
star
4

machinevision-toolbox-matlab

Machine Vision Toolbox for MATLAB
MATLAB
202
star
5

spatialmath-matlab

Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
MATLAB
197
star
6

bdsim

Simulate dynamic systems expressed in block diagram form using Python
HTML
174
star
7

machinevision-toolbox-python

Machine vision toolbox for Python
Jupyter Notebook
148
star
8

RVC3-MATLAB

New toolbox, data and examples for Robotics, Vision & Control: 3rd edition in MATLAB
MATLAB
82
star
9

toolbox-common-matlab

Common code for MATLAB toolboxes for Robotics, Machine Vision
MATLAB
57
star
10

rvc-notation

RVC notation
TeX
19
star
11

ansitable

Quick, easy and pretty display of tabular data or matrices, with optional ANSI color and borders
Python
17
star
12

pgraph-python

Simple graph classes
Python
15
star
13

simple-threads-coder

A simple POSIX threading library for MATLAB Coder
C
9
star
14

help2doc

Convert MATLAB source files to various documentation formats
Python
4
star
15

petercorke.github.io

Static web pages
3
star
16

pybib

Python class to parse/sort/filter bibtex files
Python
2
star
17

tkbibtex

Simple python/tk graphical browser/editor for BibTeX files
Shell
2
star
18

RVC3-test

For testing MATLAB Online access to GH based toolboxes and scripts
MATLAB
2
star
19

robotics-toolbox-julia

Julia port of robotics toolbox
1
star
20

arduIO

Simple and efficient Arduino I/O server and Python client
Python
1
star
21

plane-catcher

Log aircraft flying low and loud over Brisbane's Western suburbs
Python
1
star