generated from icsl-Jeon/simple-ros2-package
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublisher_node.cc
54 lines (43 loc) · 1.59 KB
/
publisher_node.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <random>
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>
using namespace std::chrono_literals;
class PublisherNode : public rclcpp::Node {
public:
PublisherNode() : Node("publisher_node") {
short_publisher_ = create_publisher<std_msgs::msg::String>(
"/short_topic", rclcpp::SystemDefaultsQoS());
long_publisher_ = create_publisher<std_msgs::msg::String>(
"/long_topic", rclcpp::SystemDefaultsQoS());
short_timer_ =
create_wall_timer(100ms, [this]() { PublishShortMessage(); });
long_timer_ = create_wall_timer(1s, [this]() { PublishLongMessage(); });
}
private:
size_t short_seq_{0};
size_t long_seq_{0};
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr short_publisher_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr long_publisher_;
rclcpp::TimerBase::SharedPtr short_timer_;
rclcpp::TimerBase::SharedPtr long_timer_;
void PublishShortMessage() {
std_msgs::msg::String message;
message.data = "Short message (seq=" + std::to_string(short_seq_++) + ")";
RCLCPP_INFO(get_logger(), message.data, " published.");
short_publisher_->publish(message);
}
void PublishLongMessage() {
std_msgs::msg::String message;
message.data = "Long message with more characters (seq=" +
std::to_string(long_seq_++) + ")";
RCLCPP_INFO(get_logger(), message.data, " published.");
long_publisher_->publish(message);
}
};
int main(int argc, char **argv) {
rclcpp::init(argc, argv);
auto node = std::make_shared<PublisherNode>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}