talker.cpp
Go to the documentation of this file.
00001 /*
00002  * Author: Anis Koubaa for Gaitech EDU
00003  * Year: 2016
00004  *
00005  */
00006 
00007 
00008 #include "ros/ros.h"
00009 #include "std_msgs/String.h"
00010 #include <sstream>
00011 
00012 int main(int argc, char **argv)
00013 {
00014         // Initiate new ROS node named "talker"
00015         ros::init(argc, argv, "talker_node");
00016 
00017         //create a node handle: it is reference assigned to a new node
00018         ros::NodeHandle n;
00019         //create a publisher with a topic "chatter" that will send a String message
00020         ros::Publisher chatter_publisher = n.advertise<std_msgs::String>("chatter", 1000);
00021         //Rate is a class the is used to define frequency for a loop. Here we send a message each two seconds.
00022         ros::Rate loop_rate(0.5); //1 message per second
00023 
00024    int count = 0;
00025    while (ros::ok()) // Keep spinning loop until user presses Ctrl+C
00026    {
00027        //create a new String ROS message.
00028            //Message definition in this link http://docs.ros.org/api/std_msgs/html/msg/String.html
00029            std_msgs::String msg;
00030 
00031        //create a string for the data
00032            std::stringstream ss;
00033            ss << "Hello World " << count;
00034            //assign the string data to ROS message data field
00035        msg.data = ss.str();
00036 
00037        //print the content of the message in the terminal
00038        ROS_INFO("[Talker] I published %s\n", msg.data.c_str());
00039 
00040        //Publish the message
00041        chatter_publisher.publish(msg);
00042 
00043        ros::spinOnce(); // Need to call this function often to allow ROS to process incoming messages
00044 
00045       loop_rate.sleep(); // Sleep for the rest of the cycle, to enforce the loop rate
00046        count++;
00047    }
00048    return 0;
00049 }
00050 
00051 


gapter
Author(s):
autogenerated on Thu Jun 6 2019 22:05:13