Ask Your Question
0

What is the calling sequence of publish and callback functions in ROS2 ?

asked 2018-05-18 04:14:32 -0600

aks gravatar image

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 :

  1. Enters the main function.
  2. Initialises the rclcpp library. rclcpp::init(argc, argv)
  3. Name the topic. auto topic = std::string("chatter");
  4. Create a node which basically intantiates the class(creating an object) and thus the constructor is called. auto node = std::make_shared<ClassName>(topic);
  5. In the constructor, the auto_publish function isnt called and it executes pub_ = this->create_publisher<std_msgs::msg::Float32>(topic_name, custom_qos_profile);
  6. At this point it seems that the auto node = std::make_shared<ClassName>(topic); has completely been executed as i goes again to the main (why?) and jumps to the rclcpp::spin(node); funtion.
  7. Now it goes again to the constructor and executes the auto_publish function until i terminate and then it goes to shutdown.
  8. Does that mean that the auto_publish function in the constructor is called by spin ?
  9. What exactly does the spin function do ? I thought it is used for callbacks.
  10. 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 ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-05-18 09:34:11 -0600

sloretz gravatar image

What exactly does the spin function do ? I thought it is used for callbacks.

It does call callbacks.

At this point it seems that the auto node = std::make_shared<ClassName>(topic); has completely been executed as i goes again to the main (why?)

If I understand correctly the question is why the lambda auto_publish was not executed in the constructor? It's because it was never called in the constructor. It was defined, but not called.

Does that mean that the auto_publish function in the constructor is called by spin?

Yes. This line in the constructor created a timer: timer_ = this->create_wall_timer(2s, publish_message);. It calls the callback publish_message (a lambda) every 2 seconds.

More info about lambdas here http://en.cppreference.com/w/cpp/lang...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-05-18 04:14:32 -0600

Seen: 167 times

Last updated: May 18 '18