-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic_publisher_template.cpp
37 lines (32 loc) · 1.11 KB
/
topic_publisher_template.cpp
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
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <iostream>
namespace ros_template_programs {
class TopicPublisher {
private:
ros::NodeHandle nh_;
ros::NodeHandle pnh_;
ros::Publisher pub_msg_;
ros::Timer timer_;
std_msgs::String msg_;
void callbackTimer(const ros::TimerEvent&);
public:
TopicPublisher( );
};
}
void ros_template_programs::TopicPublisher::callbackTimer(const ros::TimerEvent&) {
pub_msg_.publish(msg_);
ROS_INFO("\n[%s] Message Published\n%s\n", ros::this_node::getName().c_str(), msg_.data.c_str() );
return;
}
ros_template_programs::TopicPublisher::TopicPublisher( ) : nh_(), pnh_("~") {
msg_.data = pnh_.param<std::string>("message", "Hello, World!");
pub_msg_ = nh_.advertise<std_msgs::String>("topic", 1);
timer_ = nh_.createTimer(ros::Duration(0.5), &TopicPublisher::callbackTimer, this);
}
int main(int argc, char *argv[]) {
ros::init(argc, argv, "topic_publisher_template");
ros_template_programs::TopicPublisher topic_pub;
ros::spin();
return 0;
}