CodeTheRobot

C++ Advanced · Lesson 23 of 23

Lifecycle nodes

Managed states: unconfigured, inactive, active, and the transition callbacks.

Estimated time · 90–120 min

Learning goals

What you will understand

  • Explain lifecycle nodes in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A lifecycle (managed) node has a defined state machine: unconfigured, inactive, active and finalized. You build it from rclcpp_lifecycle::LifecycleNode and implement transition callbacks: on_configure (allocate resources), on_activate (start publishing), on_deactivate, on_cleanup and on_shutdown, each returning SUCCESS, FAILURE or ERROR.

This gives a system a controlled start-up and shutdown: hardware is opened during configure and only starts moving on activate, and it can be paused without destroying the node. Drive it with ros2 lifecycle set /node configure and ros2 lifecycle get.

Guided lesson

Work through these steps

  1. 1.

    Read the lifecycle nodes example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

#include "rclcpp_lifecycle/lifecycle_node.hpp"
// Header for the lifecycle node base class.
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
// A short name for the return type of the transition callbacks.
class Driver : public rclcpp_lifecycle::LifecycleNode {
// A managed node.
public:
// Public interface.
  Driver() : LifecycleNode("driver") {}
// Start in the unconfigured state.
  CallbackReturn on_configure(const rclcpp_lifecycle::State&) override {
// Called on the configure transition.
    pub_ = create_publisher<std_msgs::msg::String>("status", 10);
// Create resources but do not use them yet.
    return CallbackReturn::SUCCESS;
// Move to the inactive state.
  }
// End of on_configure.
  CallbackReturn on_activate(const rclcpp_lifecycle::State&) override {
// Called on activate.
    pub_->on_activate();
// Lifecycle publishers only send once activated.
    return CallbackReturn::SUCCESS;
// Move to the active state.
  }
// End of on_activate.
private:
// Hidden state.
  rclcpp_lifecycle::LifecyclePublisher<std_msgs::msg::String>::SharedPtr pub_;
// A publisher that obeys the lifecycle.
};
// End of the class.

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.

Watch it run · Messages between nodes

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

requestresponseclientservercalls answered: 0MESSAGES BETWEEN NODES

Demo for Lifecycle nodes. Expected result: ros2 lifecycle set /driver configure -> Transitioning successful

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

// src/driver.cpp
#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "std_msgs/msg/string.hpp"

using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

class Driver : public rclcpp_lifecycle::LifecycleNode {
 public:
  Driver() : LifecycleNode("driver") {}
  // TODO: on_configure creates the publisher; on_activate activates it; both return SUCCESS

 private:
  rclcpp_lifecycle::LifecyclePublisher<std_msgs::msg::String>::SharedPtr pub_;
};

Expected output

ros2 lifecycle set /driver configure  ->  Transitioning successful
Stuck? Show the full solution (try it yourself first)
// src/driver.cpp
#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "std_msgs/msg/string.hpp"

using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

class Driver : public rclcpp_lifecycle::LifecycleNode {
 public:
  Driver() : LifecycleNode("driver") {}
  CallbackReturn on_configure(const rclcpp_lifecycle::State&) override {
    pub_ = create_publisher<std_msgs::msg::String>("status", 10);
    return CallbackReturn::SUCCESS;
  }
  CallbackReturn on_activate(const rclcpp_lifecycle::State&) override {
    pub_->on_activate();
    return CallbackReturn::SUCCESS;
  }

 private:
  rclcpp_lifecycle::LifecyclePublisher<std_msgs::msg::String>::SharedPtr pub_;
};

Expected result

How you know it worked

  • After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
  • Changing the stated input produces the result you predicted.

Practical exercise

Apply the lesson

Turn the Talker into a lifecycle node. Use ros2 lifecycle set to configure, activate, deactivate and clean it up, and confirm with ros2 topic echo that messages flow only while it is active.

Before you continue

  • The program builds with -std=c++17 and no warnings
  • You can explain the key rule without reading the example
  • The normal and edge cases both have recorded results

If something goes wrong

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.