This repository shows minimal pybind11 examples using catkin. This project depends on pybind11_catkin. It is a pybind11 catkin package. Having a catkin package is very convenient if you are using catkin as build system.
- Install the package pybind11_catkin with
sudo apt-get install ros-kinetic-pybind11-catkin
- Create a catkin workspace or use an existing workspace.
- Clone this repository into the
src/
subdirectory of the workspace:git clone https://github.com/arturmiller/pybind11_examples.git
. - Compile the code
catkin build
. - Source the config file:
source path_to_workspace/devel/setup.bash
.
The first example is C++ print function, which is call in Python.
You run this example with rosrun function_call hello_world.py
. As output you should see hello world!
.
The second example shows a Python class inheriting from a C++ class. An in Python overriden method is called from the C++ base class.
Run it with rosrun inheritance dog.py
. You should see something similar to this:
<__main__.Dog object at 0x7f90143954d0>
Fluffy dog Molly!
Fluffy dog Charly!
The last two examples have shown, how to call C++ code from Python. With Pybind11 you can do the inverse as well. This is needed for example if you want to write a plugin written in Python for a C++ library. There are multiple advantages of this approach. For example you can rerun the plugin without recompiling and running a debugger is much simpler.
The following classes are part of the plugin system:
- PluginRunner: The plugins are stored and called in this module.
- BasePlugin: All plugins inherit from BasePlugin.
- CustomPlugin: A custom plugin implemented in Python.
- PluginWrapper: The PluginWrapper calls the Python Plugin.
- PyBasePlugin: This module makes the base plugin accessible in Python.
Run this example with rosrun plugin plugin_runner
. The Python plugin is called, with the resulting output hello world!
. Now you can change the string of the print statement and rerun it without recompiling the source code.