ros2 / examples

Example packages for ROS 2
Apache License 2.0
681 stars 308 forks source link

How to get latest msg #345

Closed Amazingmum closed 2 years ago

Amazingmum commented 2 years ago

I defined the subscription as follows to get msg when other node publishes to the "mytopic" topics: subscription = this->create_subscription( "mytopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));

And I wanna copy the above msg from the topic_callback when it called to the function of Process() to do something else,so how can i get the latest msg ,and how can idesign the node,like following to handle : the function of Process() should be executed onece the topic_callback is called.

class MyNode : public rclcpp::Node { public: MyNode(); ~MyNode(); bool Init(); bool Start();

private: bool Process(); std::string timing_string(); void on_topic1(const std_msgs::msg::String::SharedPtr msg); void on_topic2(const std_msgs::msg::String::SharedPtr msg);

rclcpp::Subscription::SharedPtr topic1sub; rclcpp::Subscription::SharedPtr topic2sub;

rclcpp::callback_group::CallbackGroup::SharedPtr cb_group_sub_topic1; rclcpp::callback_group::CallbackGroup::SharedPtr cb_group_sub_topic2; //local msg copied from sub and to be process std_msgs::msg::String local_msg1; std_msgs::msg::String local_msg2; } And need define something else...?

clalancette commented 2 years ago

Please open questions like this on https://answers.ros.org, which is our central Question and Answer site. You'll get a better answer there, and it will be searchable for the future.