Lab 1.2: First example in ROS using C++ – Create a ROS Node that “publishes” data


This lab is to write your own script in c++ to implement the ROS node which publishes the data.

Create an ROS Package

We will create the new package called my_ex1 in the Workspace folder, type following command to create folder and package:

cd ~/dev/CIS623

roscreate-pkg my_ex1 std_msgs rospy roscpp

Create a ROS Node that Publishes Data

In this section, we are going to learn how to create nodes and also compile them, these nodes are executable programs and these executables are in the bin folder under your package folder (/dev/CIS622/my_ex1/bin). Let us create script that publishes data first.

First create .cpp files in package folder named IamPub.cpp using following commands:

roscd my_ex1/src

gedit IamPub.cpp

Use following code in IamPub.cpp file:

#include “ros/ros.h”
#include “std_msgs/String.h”
#include <sstream>
int main(int argc, char **argv)
ros::init(argc, argv, “IamPub”);
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>(“message“, 1000);
ros::Rate loop_rate(10);
while (ros::ok())
std_msgs::String msg;
std::stringstream ss;
ss << ” I am the pub node “; = ss.str();
return 0;

In ROS node, the published data is called a topic, when you create a node and publish the data, you have to named the topic which can be seen in above script in green text. In the script, we called the published topic as message.

Then save the file. And open the CMakeLists.txt in package (my_ex1) folder using following commands:

roscd my_ex1

gedit CMakeLists.txt

Add following line to the last line of the CMakeLists.txt file:

rosbuild_add_executable(IamPub src/IamPub.cpp)

Save the CMakeLists.txt, then type following command in the terminal to compile the package and create the node in bin folder.

rosmake my_ex1

If every thing goes well, you will see something like:

[ rosmake ] Results:
[ rosmake ] Built 25 packages with 0 failures.
[ rosmake ] Summary output to directory
[ rosmake ] /home/stevas/.ros/rosmake/rosmake_output-20150810-155408

If it shows 0 failures, it means the compiling is succeed. You can now execute the node you just create.

To use the nodes in the ROS, you have to run the core of ROS which tells the computer that you are using the ROS. Type following command to run the core.


If everything goes well, you will see:

setting /run_id to d8770408-3f37-11e5-b738-000e8e4dff4f
process[rosout-1]: started with pid [4917]
started core service [/rosout]

Above message means the roscore is running, noted that the roscore should keep running until you do not want to use ROS.

Now open a new terminal to run IamPub node using following command:

rosrun my_ex1 IamPub

To check the publisher node is working or not, type following command in a new terminal:

rostopic list

Then you will see:


Where the /message is the data sent from IamPub node. You can use following command to see what is inside /message:

rostopic echo /message

Then you can see:

data:  I am the pub node

data:  I am the pub node

data:  I am the pub node

Which are the topic called messages and the topic is published from the created ROS node.

Now you are able to create a ROS node to publish the data, later on we are going to create another node that subscribe the published data.