Skip to content

Instantly share code, notes, and snippets.

@wjwwood
Last active April 26, 2017 19:57
Show Gist options
  • Save wjwwood/d7f5f8e8d5bb8e59261ed0661a152cff to your computer and use it in GitHub Desktop.
Save wjwwood/d7f5f8e8d5bb8e59261ed0661a152cff to your computer and use it in GitHub Desktop.
diff --git a/demo_nodes_cpp/src/topics/listener.cpp b/demo_nodes_cpp/src/topics/listener.cpp
index 454f896..aa81a66 100644
--- a/demo_nodes_cpp/src/topics/listener.cpp
+++ b/demo_nodes_cpp/src/topics/listener.cpp
@@ -14,6 +14,7 @@
#include <iostream>
#include <memory>
+#include <vector>
#include "rclcpp/rclcpp.hpp"
@@ -29,8 +30,13 @@ int main(int argc, char * argv[])
rclcpp::init(argc, argv);
auto node = rclcpp::Node::make_shared("listener");
- auto sub = node->create_subscription<std_msgs::msg::String>(
- "chatter", chatterCallback, rmw_qos_profile_default);
+ std::vector<rclcpp::subscription::SubscriptionBase::SharedPtr> subs;
+
+ for (size_t i = 0; i < 40; ++i) {
+ auto sub = node->create_subscription<std_msgs::msg::String>(
+ "chatter" + std::to_string(i), chatterCallback, rmw_qos_profile_default);
+ subs.push_back(sub);
+ }
rclcpp::spin(node);
diff --git a/demo_nodes_cpp/src/topics/talker.cpp b/demo_nodes_cpp/src/topics/talker.cpp
index c1b73f0..fe19834 100644
--- a/demo_nodes_cpp/src/topics/talker.cpp
+++ b/demo_nodes_cpp/src/topics/talker.cpp
@@ -14,6 +14,7 @@
#include <iostream>
#include <memory>
+#include <vector>
#include "rclcpp/rclcpp.hpp"
@@ -28,7 +29,14 @@ int main(int argc, char * argv[])
rmw_qos_profile_t custom_qos_profile = rmw_qos_profile_default;
custom_qos_profile.depth = 7;
- auto chatter_pub = node->create_publisher<std_msgs::msg::String>("chatter", custom_qos_profile);
+ std::vector<rclcpp::publisher::Publisher<std_msgs::msg::String>::SharedPtr> publishers;
+
+ for (size_t i = 0; i < 40; ++i) {
+ auto chatter_pub = node->create_publisher<std_msgs::msg::String>(
+ "chatter" + std::to_string(i),
+ custom_qos_profile);
+ publishers.push_back(chatter_pub);
+ }
rclcpp::WallRate loop_rate(2);
@@ -38,7 +46,9 @@ int main(int argc, char * argv[])
while (rclcpp::ok()) {
msg->data = "Hello World: " + std::to_string(i++);
std::cout << "Publishing: '" << msg->data << "'" << std::endl;
- chatter_pub->publish(msg);
+ for (auto publisher : publishers) {
+ publisher->publish(msg);
+ }
rclcpp::spin_some(node);
loop_rate.sleep();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment