What is the calling sequence of publish and callback functions in ROS2 ?
I am currently trying to understand the exact functionality and the calling sequence of the callback and the publish function. I have debugged the code but a bit confused.
Normally, for a publisher
(say in ROS2), the flow is as follows :
- Enters the main function.
- Initialises the rclcpp library.
rclcpp::init(argc, argv)
- Name the topic.
auto topic = std::string("chatter");
- Create a node which basically intantiates the class(creating an object) and thus the constructor is called.
auto node = std::make_shared<ClassName>(topic);
- In the constructor, the
auto_publish
function isnt called and it executespub_ = this->create_publisher<std_msgs::msg::Float32>(topic_name, custom_qos_profile);
- At this point it seems that the
auto node = std::make_shared<ClassName>(topic);
has completely been executed as i goes again to themain
(why?) and jumps to therclcpp::spin(node);
funtion. - Now it goes again to the constructor and executes the
auto_publish
function until i terminate and then it goes to shutdown. - Does that mean that the
auto_publish
function in the constructor is called byspin
? - What exactly does the spin function do ? I thought it is used for
callbacks
. This is the class definition : `
class Source : public rclcpp::Node { public: explicit Source(const std::string & topic_name) : Node("source") { msg_ = std::make_shared<std_msgs::msg::Float32>(); // Create a function for when messages are to be sent. printf("Entered the constructor \n"); auto publish_message = [this]() -> void { printf("Entering the publish function\n"); count_++; if (count_ <= 50){ msg_->data = 100;} if (count_ > 50) { msg_->data = 0.0;} printf("pushed the value into a pointer to be published\n"); RCLCPP_INFO(this->get_logger(), "Publishing: '%f'", msg_->data) printf("After the logger info and before publishing\n"); // Put the message into a queue to be processed by the middleware. // This call is non-blocking. pub_->publish(msg_); printf("After publishing in the constructor\n"); }; // Create a publisher with a custom Quality of Service profile. printf("Now I come out of the auto_publish function to set the qos ?\n"); rmw_qos_profile_t custom_qos_profile = rmw_qos_profile_default; custom_qos_profile.depth = 7; pub_ = this->create_publisher<std_msgs::msg::Float32>(topic_name, custom_qos_profile); printf("After create_publisher and before timer\n"); // Use a timer to schedule periodic message publishing. timer_ = this->create_wall_timer(2s, publish_message); printf("After the timer and before the constructor ends\n"); } private: size_t count_ = 1; std::shared_ptr<std_msgs::msg::Float32> msg_; rclcpp::Publisher<std_msgs::msg::Float32>::SharedPtr pub_; rclcpp::TimerBase::SharedPtr timer_; };
`
11. Why does the auto_publish
function has a class like definition auto publish = {}**;**
Is this lambda
?