Creating a C++ Extension#
For performance-critical hardware drivers or integration with C++ libraries, you can create a C++ extension. This involves writing C++ code and exposing it to Python using pybind11.
Structure#
A C++ extension typically looks like this:
rcs_mycppext/
├── CMakeLists.txt
├── pyproject.toml
├── src/
│ ├── MyDevice.cpp
│ ├── MyDevice.h
│ └── bindings.cpp
└── ...
Steps#
CMake Configuration: Use
CMakeLists.txtto configure your build. You’ll need to link againstrcs(if it exposes C++ headers) andpybind11.Implement C++ Class: Write your device driver in C++.
#include <rcs/Robot.h> class MyRobot : public rcs::Robot { public: void setJointPosition(const Eigen::VectorXd& q) override { // ... implementation ... } // ... other methods ... };
Create Bindings: Use
pybind11to expose your class to Python.#include <pybind11/pybind11.h> #include "MyDevice.h" namespace py = pybind11; PYBIND11_MODULE(rcs_mycppext, m) { py::class_<MyRobot, rcs::Robot>(m, "MyRobot") .def(py::init<>()) .def("set_joint_position", &MyRobot::setJointPosition); }
Build System: Use
scikit-buildor similar tools inpyproject.tomlto compile the C++ extension during installation.
Examples#
rcs_fr3: Implements the driver for the Franka Research 3 robot in C++ using
libfranka.rcs_robotics_library: Wraps the Robotics Library (RL) for kinematics and path planning.