Node Spinning

Why Spinning Matters

Spinning is how a ROS 2 node processes incoming data. Without it, subscriptions, timers, and services will never be triggered.

Spinning Models

The simplest pattern. Processes callbacks one at a time.

rclcpp::spin(node);
  • ✅ Easy to reason about and debug.

  • ✅ Suitable for many small nodes.

For handling multiple callbacks (high-rate sensors/services) concurrently.

rclcpp::executors::MultiThreadedExecutor executor;
executor.add_node(node);
executor.spin();
  • ✅ Callbacks run concurrently.

  • ⚠️ Requires thread safety for shared data.

Best Practices

🛡️ Lifecycle

Always call rclcpp::shutdown() before exiting. Use RAII and smart pointers.

🧩 Composition

Prefer encapsulating functionality in classes rather than writing monolithic node code.

⚡ Performance

Keep callbacks small and focused. Offload heavy work to worker threads.