Skip to content

rclcpp_kit

rclcpp_kit lets Python use ROS 2's real C++ objects and APIs through cppyy. It provides the rclcpp namespace, C++ message proxies, familiar node and pub/sub calls, and access to C++ libraries such as tf2 and rosbag2. Use it when you want to work with ROS 2 C++ APIs directly from Python. The separate rclcppyy product is a drop-in rclpy accelerator that re-exports rclcpp_kit.

Getting started

For published package use or development from a checkout, follow the Getting Started guide. The published package is named ros-jazzy-rclcpp-kit.

From this repository checkout, run the existing TF example with:

pixi run -e rclcpp demo-tf-lookup

For a verified rclpy-style pub/sub round-trip that publishes Python messages and receives C++ message proxies in the subscription callback, see the example used by the tests.

What's here

Module Surface
bringup_rclcpp bringup_rclcpp() (JIT rclcpp/rclcpp.hpp + load core libs), add_ros2_include_paths(), shutdown_rclcpp(), the rclpy-style rclcpp.Node adapters (create_publisher / create_subscription / create_timer / destroy_node), C++ message resolution + the shared recursive convert_python_msg_to_cpp
direct_entities Strict typed C++ publishers/subscriptions/timers plus creation-time lowering of non-negative integer depth or explicit Jazzy rclpy.qos.QoSProfile values to rclcpp::QoS; no message conversion
native Managed custom Context, real Node/NodeOptions, single- and multi-threaded executors, callback groups and callback-group entity options, intra-process selection, per-publisher loaning capability queries, deterministic shutdown, and the raw rclcpp namespace
native_pipeline Content-addressed editable C++ subscription callbacks and fused subscription-transform-publisher objects; zero Python callback crossings, structured counters, explicit every/latest/bounded-batch delivery, and fresh/reused/loaned output memory
python_callback_entities Compiled C++ callback trampolines for typed direct subscriptions and services; owning generated-message proxies, callback-group support, optional subscription message info, and deferred GIL-safe callback reference release
type_adapter Value-only extension contract for domain-kit ROS/native conversions, including copy semantics, owner retention, alias mutability, and limitations
native_service Content-addressed editable C++ service callbacks with stock and AOT-client interoperability, counters, zero Python request crossings, and managed teardown
borrowed_set_bool_service Explicit callback-scoped SetBool request/response views over rclcpp-owned generated C++ messages; no adapter message copies, in-place response field writes, fail-closed return contract, and managed teardown
native_client Cached typed C++ clients with C++-owned async futures, stock and AOT-server interoperability, cancellation/counters, raw-client access, and managed teardown
native_action Cached typed C++ action clients with C++-owned goal/result/cancel state, stock and AOT-server interoperability, bounded feedback, raw handles, counters, and managed teardown
native_action_server Cached typed C++ action servers with synchronous Python decisions, polled accepted goals, exact generated C++ payloads, contained native errors, counters, and managed teardown
native_component Real rclcpp_components::ComponentManager containers on the managed context, stock composition services, AOT component loading, raw-manager access, and ordered teardown
native_lifecycle Managed real rclcpp_lifecycle::LifecycleNode objects, executor attachment, stock and AOT-client lifecycle services, raw-node access, and ordered teardown
serialization CDR serialize/deserialize of C++ messages, byte-compatible with rclpy.serialization; bytes ⇄ rclcpp::SerializedMessage
rosbag2_cpp the C++ rosbag2_cpp reader/writer (open_reader / open_writer / iterate)
rosbag2_py_compat a rosbag2_py-compatible shim (SequentialReader/Writer, StorageOptions, …) backed by rosbag2_cpp
tf the tf2 C++ transform stack: a tf2_ros::TransformListener ingesting /tf wholly in C++ on its own thread (TransformListener.lookup_transform / can_transform / set_transform)

Native lowering stays inside the managed ownership boundary:

with rclcpp_kit.native(["pipeline"]) as ros:
    node = ros.create_node("pipeline")
    relay = ros.create_fused_pipeline(
        node, String, String, "input", "output",
        'output.data = input.data + ":native";',
        delivery="latest",
        output_memory="loaned",
    )
    stats = relay.stats()
    assert stats.python_boundary_crossings == 0
    assert stats.middleware_loaned_messages + stats.allocator_fallbacks \
        == stats.output_instances

output_memory="fresh" constructs an output for every transform and is the default. "reuse" keeps one output behind a mutex, so the transform must fully overwrite it and concurrent transforms are serialized. "loaned" uses rclcpp::LoanedMessage RAII; the counters distinguish middleware loans from the publisher allocator fallback. A loan is not by itself a zero-copy guarantee.

Direct entity QoS lowering is control-plane only. qos_from_depth() implements the rclpy integer shorthand, while qos_from_profile() preserves explicit history/depth, reliability, durability, deadline, lifespan, liveliness, lease duration, and ROS namespace-convention selection. System-default and best-available policies map to their exact Jazzy rclcpp counterparts; unknown policies are rejected. The stock qos_profile_sensor_data and qos_profile_system_default presets are supported directly.

Managed clients keep template use and future lifetime in a small adapter. They accept only owning generated C++ requests, return opaque call tokens, and leave the original typed client exposed:

client = ros.create_native_client(node, SetBool, "set_bool")
request = client.make_request()
request.data = True
token = client.send(request)
if client.ready(token):
    response = client.take(token)  # the C++ response object
raw_client = client.raw_client

Calls submitted through the adapter must be taken or canceled through it. Session teardown cancels any calls still pending before releasing the client. send_cpp_value() is the explicit alternative for an existing generated C++ request value; it takes the one native ownership copy required by the async call. Generated Python requests are rejected rather than converted.

Cold native service and client glue is compiled to a content-addressed DSO before its declarations are loaded into Cling. This permits both facilities to coexist in one interpreter without conflicting std::call_once TLS state. If no runtime compiler is available, an individual adapter retains the original Cling fallback, but cold same-interpreter multi-glue coexistence is not guaranteed; query ros.capabilities.native_service_client_coexistence.

The SetBool-only borrowed service is a separate opt-in API. It does not change the safe owning semantics of create_python_service:

def handle(request, response):
    response.success = request.data
    response.message = "enabled" if request.data else "disabled"
    # None is the only valid return; replacement responses fail closed.

service = ros.create_borrowed_set_bool_service(node, "set_bool", handle)

Both callback arguments are guarded views of the actual request and response owned by the synchronous rclcpp callback. The response properties write its native generated C++ response in place. The adapter constructs no extra C++ request or response message and performs no whole-response assignment. Views expire when the callback returns or raises; retaining one is unsupported, and any later field access raises RuntimeError. Coroutine callbacks, runtime awaitables, replacement return values, and unsupported field types fail closed. The specialized flat SetBool surface is intentional: it makes the lifetime guard complete instead of exposing nested C++ references that could outlive the callback.

Managed action clients apply the same narrow rule to rclcpp_action template and future state. They expose opaque goal tokens plus the original typed client and accepted goal handles. Feedback is an explicit bounded drop-oldest queue, and forget(token) releases local state without canceling the remote goal. The first use of an action type synchronously builds content-addressed glue because this toolchain cannot safely instantiate its type support through Cling alone. Construct an owning C++ goal with make_goal() before calling send_goal(), or use send_cpp_value() for an existing generated C++ goal value. Generated Python goals are rejected rather than converted.

Lifecycle nodes keep only ownership and executor attachment behind the adapter; transitions and lifecycle-specific facilities remain the real C++ API:

with rclcpp_kit.native(["lifecycle"]) as ros:
    lifecycle = ros.create_native_lifecycle_node("worker")
    executor = ros.create_executor()
    lifecycle.attach_executor(executor)
    thread = ros.start_executor(executor)
    raw_node = lifecycle.raw_node

The raw node is a shared-pointer escape hatch. Retaining it after close() extends the C++ node lifetime by design, so deterministic managed teardown assumes callers do not retain an extra raw owner.

Managed component containers load ordinary AOT C++ components registered in the ament resource index and keep the standard composition_interfaces services:

with rclcpp_kit.native(["container"]) as ros:
    executor = ros.create_executor("multi_threaded", threads=2)
    container = ros.create_native_component_manager(
        executor, name="native_container")
    ros.start_executor(executor)
    raw_manager = container.raw_manager

Component packages remain separate runtime dependencies. The adapter deliberately does not turn Python classes into components, and explicit close must not race an active load or unload request.

import rclcpp_kit
rclcpp = rclcpp_kit.bringup_rclcpp()             # rclcpp up under cppyy

from rclcpp_kit import tf
listener = tf.TransformListener()                # own node + own C++ spin thread
ts = listener.lookup_transform("world", "sensor", timeout=1.0)
x = ts.transform.translation.x                   # the real geometry_msgs message

from rclcpp_kit import serialization as ser
blob = ser.serialized_message_to_bytes(ser.serialize_message(cpp_msg))

The surface keeps the names used by the rclcppyy product, which can re-export this package. The TF report records one shared-host run. Measure the target workload on its deployment host before choosing a backend for performance.

Running it

rclcpp_kit needs only the ROS core (rclcpp + tf2, both in the default ros-base env); it carries no opt-in C++ dependency of its own. Its env is rclcpp:

pixi run -e rclcpp test-rclcpp     # full suite: bringup, pub/sub, serialization, tf
pixi run -e rclcpp test-tf         # the tf gate (8 tests)
pixi run -e rclcpp demo-tf-lookup  # C++ listener ingests /tf; Python looks it up
pixi run -e rclcpp demo-tf-storm   # synthetic TF storm publisher
pixi run -e rclcpp bench-tf        # stock rclpy listener vs rclcpp_kit C++ listener
pixi run -e rclcpp bench-native-choices-smoke  # verified eight-case raw smoke
pixi run -e rclcpp bench-native-choices        # repeated raw characterization

Unlike the domain kits, rclcpp_kit's tests bring up rclcpp + DDS (they do not auto-skip in the default env), so they run in the rclcpp env rather than the default pixi run test collect-and-skip smoke.

Docs

  • SKILL.md: usage patterns and notes for coding agents.
  • WHY.md: reasons to use rclcpp_kit.
  • REPORT.md: TF implementation details and benchmark results.
  • NATIVE_CHOICES_BENCHMARK.md: correctness contract, reproduction commands, raw native-choice measurements, and limits.