A simple “Hello ROS” program

From Lofaro Lab Wiki
Revision as of 07:00, 13 November 2014 by Kanik (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

We are going to write a simple program using ros, and will explore the codes and all other procedures to run your first program. By now you should be familiar with packages. Before wiring your first program you need to create a ros workspace and make a package where your code will reside. To create a workspace you can make a directory in your convenient place in the machine. If your linux is named km, the directory might be in home/km/ros_ws. You can create it using mkdir command or using linux gui system. After creating your directory make a folder name src. Your manifest and cmake file will reside within the folder. Go to your workspace source directory and create the package using catkin command.

catkin_create_package  “package name” 

If you want to have your package name robot. Let’s write

catkin_create_package  robot

Your robot package will be created with the manifest.xml and Cmakelists.txt file.

Now we have created the package and we can for writing our first ros program. We will make the program using cpp, and lets name it hello.cpp. the sample code for hello.cpp is given below. We will explore all parts of the code momentarily,

# include <ros/ros.h> 
Int main(int argc, char **argv){
//initialize the ros system
ros::init ( argc, argv, “hello ROS”);
//establish this program as ros node 
ros:: NodeHandla nh;
// send some output as a log message 
ROS_INFO_STREAM (“Hello, ROS!”);
}

The header file ros/ros.h includes standard ros classes that you might be needed to run your program. You need to include it in every program you write.

The ros::init function starts the ros client library. You need to call it at the beginning of your program.

The ros::NodeHandle object is the main mechanism that your program will use to communicate with the ros system. When you create this object, it registers your program as a ros node with the ros master.

The ROS_INFO_STREAM generated any informational message which can be sent to console screen. Before compiling your program you need to declare dependencies on your manifest and cmake file. In cmake file, find the line

find_package (catkin REQUIRD) 

and modify it like

find_package (catkin REQUIRD roscpp) , 

since you need the package to compile your program. Also edit your manifest file and add

<build_depend>roscpp</build_depend>
<run_depend> roscpp<run_depend> 

Now add two lines to the CMakelists.txt file to declare the program as executable.

add_executable(hello hello.cpp)
target_link_libraries (hello $catkin_LIBRARIES) 

Now build the workspace using

Catkin_make

The last step is to source your bash file which is created by the catkin_make command within devel subdirectory of the workspace

source devel/setup.bash 

Now execute your program using rosrun command. The ususal command is

rosrun package-name program-name

In this case it would be

rosrun robot hello

The program should generate an outpu in the console which is Hello, ROS.