• +4917626969472
  • info@ur-techpartner.de

ROS Basics (01/17) – First ROS Program

Robot Operating System(ROS1/ROS2) ROS Programming

ROS Basics

First ROS Program

Finally, we are starting to learn Robot Operating System (ROS) programming. Onwards, we will not only learn new things, but we will also apply ROS and Linux commands, that we have already gone through in previous blogs.

Create a ROS Package

As we already know, when we want to create a new package, we need to work in catkin workspace. The ROS packages that we want to use must reside in catkin workspace. There can be more workspaces, however at one time we can work in only one workspace. Most of the time if we have created single workspace directory it would be names as “catkin_ws”.

We assume that you have already created workspace.

Go the “src” folder of “catkin_ws” and create a new package.

Open the terminal and run the command

roscore

Open another terminal and run following command to go to the “src” directory

cd ~/catkin_ws/src

Now we use the command “catkin_create_pkg” to create a package.

The structure of the command is as follows

catkin_create_pkg <package_name> <package_dependecies>

Run in terminal

catkin_create_pkg my_first_package rospy

The newly created package “my_first_package” depends on another package which is called “rospy”.

The package “rospy” provides the basic functionality that we need to use while working in python.

We can check using “rospack list” command to see if the package is created and available.

Run in terminal

rospack list | grep my_first

The commands that we have run can be viewed in figure below

Go to the package “my_first_package” and see the contents of the package.

Run in terminal

roscd my_first_package

you will go to the “my_first_package” folder.

Run “ls” command to see the contents

ls

if “roscd” does not detect a newly created package, you can run following command to refresh the packages list, as follows

rospack profile

This will update the list of ROS packages in the system.

Create a ROS Program in Python

Now we create a python file in “src” folder of “my_first_package” in order to write first ROS program.

First go to src folder

if you are already in “my_first_package” directory, simply run the command

cd src

Otherwise, you can run the command

roscd my_first_package/src

Now as you are in src folder, create a python file using following command

touch my_simple_code_file.py

The extension “.py” is used with python files.

Now assign execution rights to the file “my_simple_code_file.py”, using “chmod” command (we have already learn this command in Linux Commands blogs)

chmod +x my_simple_code_file.py

Now open the file in any editor (if you have already installed any). Or we will open it using gedit command

Run in terminal

gedit my_simple_code_file.py

File will be opened as show in figure.

Write following code in the file “my_simple_code_file.py”

#! /usr/bin/env python
import rospy
rospy.init_node("First_node")
print ("Hi! This is my fisrt program to initiate ROS Node.")

Save and close the file.

Create a Launch File

First create a launch folder in package “my_first_package”

Go the the package directory

Run in terminal

roscd my_first_package

Create a launch folder (Run following command)

mkdir launch

Create a launch file with extension “.launch” within the launch directory

Run in terminal

touch launch/my_first_package_launch_file.launch

The launch file named “my_first_package_launch_file.launch” will be created.

Note: you can create/ access the file by going into the launch folder, or can access it by giving the path (which includes folder name) as we did in above command.

Assign execution rights to the file as

chmod +x launch/my_first_package_launch_file.launch

Open the file “my_first_package_launch_file.launch” and write following code in it.

Run in terminal to open the file

gedit launch/my_first_package_launch_file.launch

Write the code

<launch>
<!-- My First Package launch file -->
<node pkg="my_first_package" type="my_simple_code_file.py" name="First_node" output="screen">
</node>
</launch>

Save and close the file

Execute the Program

Now run the “roslaunch” command in terminal to execute the launch file. By executing the launch file, the python program file will also be executed. Because the purpose of launch file is actually to execute the ROS program files (i.e. python file etc)

The structure of the roslaunch command is

roslaunch <Package_Name > <Launch_File_Name>

Run in terminal

roslaunch my_first_package my_first_package_launch_file.launch

You will see the output as follows

You can see in the output that the node “First_node” is initiated and the message is printed on screen.

Now we go through the code of both python and launch files. The code is very simple but we explain it line by line to avoid any confusion.

First we see python file which is “my_simple_code_file.py”

Code and its explanation is as follows

#! /usr/bin/env python
#The above line is always first line in python code file. It is to tell interpreter that this is a python code file. This line should be written as it is at the top of the code.
import rospy
#This line is written to import the “rospy” library. The “rospy” is the python library for ROS.
rospy.init_node("First_node")
# The above line initiates a node. The node is called “First_node”
print ("Hi! This is my fisrt program to initiate ROS Node.")
# This command simply print the message.

Now we explain the launch file which is “my_first_package_launch_file.launch”

HTML like tags are used to write the launch file.

The code and its explanation is as follows

<launch>
<!-- My First Package launch file -->
<node pkg="my_first_package" type="my_simple_code_file.py" name="First_node" output="screen">
</node>
</launch>

The launch file starts and ends with the <launch> </launch> tags.

Second tag is of <node></node>

With the node tag, there are four arguments as explained below

pkg = This is the name of package that contains the ROS code to execute.
Type = This is the name of the program file that we want to execute. For example, python file or c++ (cpp) file.
name = Name of the ROS node.
output = channel to print the output of the python file. Here it is screen.

Search the Node “First_node”

As we have executed the program and initiated the node “first_node”. Let’s see if the node is running

Run in terminal

rosnode list

The output on our screen is as follows (which means only /rosout node is running)

/rosout

We can see that the “First_node” is not listed. Why it is so? Where is the node?

It is because the node “First_node” killed when python program ends.

Let’s update the program to run the code continuously, and see if the node exists.

Open the file “my_simple_code_file.py” and modify it as follows

#! /usr/bin/env python
import rospy
rospy.init_node("First_node")
print ("Hi! This is my fisrt program to initiate ROS Node.")
rate = rospy.Rate(2)
while not rospy.is_shutdown():
print ("Hi! This is my fisrt program to initiate ROS Node.")
rate.sleep()

Save and close the file.

In modified code, we created a Rate object of 2 Hz and then created an endless loop. It means that the “while” loop will continue printing the message 2 times per second until someone press Ctrl + C on command terminal.

The modified file is shown in image below

Now we execute the launch file again

Run in terminal

roslaunch my_first_package my_first_package_launch_file.launch

We can see that the message is printing continuously at the rate of 2 time per second.

Now we check again if the “First_node” is running or not

Run in terminal

rosnode list

Great! We can see the node “First_Node” that we have initiated.

Since, the python program is still running, therefore the node still exists in the running list as shown in the figure below.

To end the python program, press Ctrl + c in the terminal where roslaunch command is executed, and the program will be stopped. Subsequently the node “First_node” will be killed automatically

No comments