void callback(const std_msgs::String::ConstPtr & msg){ROS_INFO("I heard: [%s]", msg->data.c_str());}int main(int argc, char * argv[]){ros::init(argc, argv, "listener");ros::NodeHandle node;ros::Subscriber sub = node.subscribe("chatter", 10, callback);ros::spin();}return 0;// void callback(const std_msgs::String::ConstPtr & msg){// ROS_INFO("I heard: [%s]", msg->data.c_str());}int main(int argc, char * argv[]){// ros::init(argc, argv, "listener");// ros::NodeHandle node;// ros::Subscriber sub = node.subscribe("chatter", 10, callback);// ros::spin();}return 0;// void callback(const std_msgs::String::ConstPtr & msg)void callback(std_msgs::msg::String::ConstSharedPtr msg){// ROS_INFO("I heard: [%s]", msg->data.c_str());
printf("I heard: [%s]\n", msg->data.c_str());}int main(int argc, char * argv[]){// ros::init(argc, argv, "listener");rclcpp::init(argc, argv);// ros::NodeHandle node;auto node = rclcpp::Node::make_shared("listener");// ros::Subscriber sub = node.subscribe("chatter", 10, callback);auto sub = node->create_subscription<std_msgs::msg::String>("chatter", rmw_qos_profile_default, callback);// ros::spin();rclcpp::spin(node);}return 0;// void callback(const std_msgs::String::ConstPtr & msg)void callback(std_msgs::msg::String::ConstSharedPtr msg){// ROS_INFO("I heard: [%s]", msg->data.c_str());
printf("I heard: [%s]\n", msg->data.c_str());}int main(int argc, char * argv[]){// ros::init(argc, argv, "listener");rclcpp::init(argc, argv);// ros::NodeHandle node;auto node = rclcpp::Node::make_shared("listener");// ros::Subscriber sub = node.subscribe("chatter", 10, callback);auto sub = node->create_subscription<std_msgs::msg::String>("chatter", rmw_qos_profile_default, callback);// ros::spin();rclcpp::spin(node);}return 0;// void callback(const std_msgs::String::ConstPtr & msg)void callback(std_msgs::msg::String::ConstSharedPtr msg){// ROS_INFO("I heard: [%s]", msg->data.c_str());
printf("I heard: [%s]\n", msg->data.c_str());}int main(int argc, char * argv[]){// ros::init(argc, argv, "listener");rclcpp::init(argc, argv);// ros::NodeHandle node;auto node = rclcpp::Node::make_shared("listener");// ros::Subscriber sub = node.subscribe("chatter", 10, callback);auto sub = node->create_subscription<std_msgs::msg::String>("chatter", rmw_qos_profile_default, callback);// ros::spin();rclcpp::spin(node);}return 0;// void callback(const std_msgs::String::ConstPtr & msg)void callback(std_msgs::msg::String::ConstSharedPtr msg){// ROS_INFO("I heard: [%s]", msg->data.c_str());
printf("I heard: [%s]\n", msg->data.c_str());}int main(int argc, char * argv[]){// ros::init(argc, argv, "listener");rclcpp::init(argc, argv);// ros::NodeHandle node;auto node = rclcpp::Node::make_shared("listener");// ros::Subscriber sub = node.subscribe("chatter", 10, callback);auto sub = node->create_subscription<std_msgs::msg::String>("chatter", rmw_qos_profile_default, callback);// ros::spin();rclcpp::spin(node);}return 0;spin()shared_ptrstd::shared_ptr<std_msgs::msg::String> msg(new std_msgs::msg::String());
msg->data = "testing";publisher->publish(msg);// The user still has ownership at this point, could do something like this:msg->data = "testing2";"testing""testing""testing2""testing2"publish(...)

unique_ptrunique_ptrunique_ptrstd::unique_ptr<A> a, b;a.reset(new A());// a is valid.
// b is a nullptr.
b = a; // Ownership of the pointer returned by `new A()` transfered.
// a is now nullptr.
// b is now valid.

unique_ptrstd::unique_ptr<std_msgs::msg::String> msg(new std_msgs::msg::String());
msg->data = "testing";publisher->publish(msg); // This is non-blocking, the message goes into a queue.
// The user no longer has access to the message created above.
// Instead the middleware now owns it, and this is no longer valid:// msg->data = "testing2"; // <-- access nullptr, will cause segmentation fault.void callback(std_msgs::msg::String::ConstSharedPtr msg){// msg->data = "new value"; This is illegal; the user doesn't own it.
std_msgs::msg::String msg_copy(*msg); // Must make a copy that the user owns.
msg_copy = "new value";outgoing_publisher->publish(msg_copy);}unique_ptrvoid callback(std_msgs::msg::String::UniquePtr msg){msg->data = "new value"; // Edit directly; middleware relinquished ownership.
outgoing_publisher->publish(msg);}struct IncrementerPipe : public rclcpp::Node{IncrementerPipe(const std::string & name, const std::string & in, const std::string & out)// ...
[this](std_msgs::msg::Int32::UniquePtr & msg) {printf("Received message with value:%d, and address: %p\n",msg->data, msg.get());
printf(" sleeping for 1 second...\n");if (!rclcpp::sleep_for(1_s)) {return; // Return if the sleep failed (e.g. on ctrl-c).
}printf(" done.\n");msg->data++; // Increment the message's data.
printf("Incrementing and sending with value: %d, and address: %p\n",msg->data, msg.get());
this->pub->publish(msg); // Send the message along to the output topic.
});}
// ..
};int main(int argc, char * argv[]){rclcpp::init(argc, argv);rclcpp::executors::SingleThreadedExecutor executor;auto pipe1 = std::make_shared<IncrementerPipe>("pipe1", "topic1", "topic2");auto pipe2 = std::make_shared<IncrementerPipe>("pipe2", "topic2", "topic1");// ..
// Publish the first message (kicking off the cycle).
std::unique_ptr<std_msgs::msg::Int32> msg(new std_msgs::msg::Int32());
msg->data = 42;printf("Published first message with value: %d, and address: %p\n",msg->data, msg.get());
pipe1->pub->publish(msg);}executor.add_node(pipe1);executor.add_node(pipe2);executor.spin();return 0;shared_ptr

memcpyBest effort
Reliable
Keep last
Keep allVolatileTransient localtypedef struct RMW_PUBLIC_TYPE rmw_qos_profile_t{enum rmw_qos_history_policy_t history;size_t depth;enum rmw_qos_reliability_policy_t reliability;enum rmw_qos_durability_policy_t durability;} rmw_qos_profile_t;rosmsg☹
ament resource index.yaml∀↔
roscpprclcppChallengepkg-configCMake find_package()Alpha 1

Alpha 1

Alpha 1

Alpha 2class_loaderpluginlib