1#include "ros/ros.h"
2#include "std_msgs/String.h"
3
4/**
5 * This tutorial demonstrates simple receipt of messages over the ROS system.
6 */
7void chatterCallback(const std_msgs::String::ConstPtr& msg)
8{
9 ROS_INFO("I heard: [%s]", msg->data.c_str());
10}
11
12int main(int argc, char **argv)
13{
14 ros::init(argc, argv, "listener");
15
16 ros::NodeHandle n;
17
18 ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
19
20 ros::spin();
21
22 return 0;
23}
1#include "ros/ros.h"
2#include "std_msgs/String.h"
3
4#include <sstream>
5
6/**
7 * This tutorial demonstrates simple sending of messages over the ROS system.
8 */
9int main(int argc, char **argv)
10{
11 ros::init(argc, argv, "talker");
12
13 ros::NodeHandle n;
14
15 ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
16
17 ros::Rate loop_rate(10);
18
19 int count = 0;
20 while (ros::ok())
21 {
22 std_msgs::String msg;
23
24 std::stringstream ss;
25 ss << "hello world " << count;
26 msg.data = ss.str();
27
28 ROS_INFO("%s", msg.data.c_str());
29
30 chatter_pub.publish(msg);
31
32 ros::spinOnce();
33
34 loop_rate.sleep();
35 ++count;
36 }
37
38 return 0;
39}
40