Ros in 5 Days: Unit 2: Basic Concepts
Ros in 5 Days: Unit 2: Basic Concepts
Simulated robot: Turtlebot
What is ROS?
This is probably the question that has brought you all here. Well, let me tell you that you are still not
prepared to understand the answer to this question, so... let's get some work done first.
Since a previously-made ROS program already exists that allows you to move the robot using the
keyboard, let's launch that ROS program to teleoperate the robot.
**Example 2.1**
Execute the following command in WebShell number #1
Execute in WebShell #1
In [ ]:
roslaunch turtlebot_teleop keyboard_teleop.launch
WebShell #1 Output
In [ ]:
CTRL-C to quit
Now, you can use the keys indicated in the WebShell Output in order to move the robot around. The
basic keys are the following:
Try it!! When you're done, you can press Ctrl+C to stop the execution of the program.
roslaunch is the command used to launch a ROS program. Its structure goes as follows:
In [ ]:
All those files in the package are organized with the following structure:
launch folder: Contains launch files
src folder: Source files (cpp, python)
CMakeLists.txt: List of cmake rules for compilation
package.xml: Package information and dependencies
To go to any ROS package, ROS gives you a command named roscd. When typing:
In [ ]:
roscd <package_name>
It will take you to the path where the package package_name is located.
**Example 2.2**
Go to WebShell #1, navigate to the turtlebot_teleop package, and check that it has that structure.
Execute in WebShell #1
In [ ]:
roscd turtlebot_teleop
ls
In [ ]:
roscd turtlebot_teleop
cd launch
cat keyboard_teleop.launch
WebShell #1 Output
In [ ]:
<launch>
<!-- turtlebot_teleop_key already has its own built in velocity smoother -->
<node pkg="turtlebot_teleop" type="turtlebot_teleop_key.py" name="turtlebot_teleop_keyboard" output="screen">
<param name="scale_linear" value="0.5" type="double"/>
<param name="scale_angular" value="1.5" type="double"/>
<remap from="turtlebot_teleop_keyboard/cmd_vel" to="/cmd_vel"/> <!-- cmd_vel_mux/input/teleop"/-->
</node>
</launch>
In the launch file, you have some extra tags for setting parameters and remaps. For now, don't worry
about those tags and focus on the node tag.
All launch files are contained within a <launch> tag. Inside that tag, you can see a <node> tag,
where we specify the following parameters:
1. pkg="package_name" # Name of the package that contains the code of the ROS program
to execute
2. type="python_file_name.py" # Name of the program file that we want to execute
3. name="node_name" # Name of the ROS node that will launch our Python file
4. output="type_of_output" # Through which channel you will print the output of the Python
file
**END Example 2.3**
Create a package
Until now we've been checking the structure of an already-built package... but now, let's create one
ourselves.
When we want to create packages, we need to work in a very specific ROS workspace, which is
known as the catkin workspace. The catkin workspace is the directory in your hard disk where your
own ROS packages must reside in order to be usable by ROS. Usually, the catkin
workspace directory is called catkin_ws.
**Example 2.4**
In order to do this, type roscd in the shell. You'll see that you are thrown to
a catkin_ws/devel directory. Since you want to go to the workspace, just type cd .. to move up 1
directory. You must end up here in the /home/user/catkin_ws.
Execute in WebShell #1
In [ ]:
roscd
cd ..
pwd
WebShell #1 Output
In [ ]:
user ~ $ pwd
/home/user/catkin_ws
Inside this workspace, there is a directory called src. This folder will contain all the packages
created. Every time you want to create a new package, you have to be in this directory
(catkin_ws/src). Type in your WebShell cd src in order to move to the source directory.
Execute in WebShell #1
In [ ]:
cd src
Now we are ready to create our first package! In order to create a package, type in your WebShell:
Execute in WebShell #1
In [ ]:
In [ ]:
rospack list
rospack list | grep my_package
roscd my_package
rospack list: Gives you a list with all of the packages in your ROS system.
rospack list | grep my_package: Filters, from all of the packages located in the ROS system, the
package named my_package.
roscd my_package: Takes you to the location in the Hard Drive of the package,
named my_package.
You can also see the package created and its contents by just opening it through the IDE (similar
to {Figure 1.1})
#! /usr/bin/env python
import rospy
rospy.init_node('ObiWan')
print "Help me Obi-Wan Kenobi, you're my only hope"
NOTE: If you create your Python file from the shell, it may happen that it's created without execution
permissions. If this happens, ROS won't be able to find it. If this is your case, you can give execution
permissions to the file by typing the next command: chmod +x name_of_the_file.py
**END Python Program {2.1-py}: simple.py**
In [ ]:
roscd my_package
mkdir launch
You can also create it through the IDE.
3- Create a new launch file inside the launch directory.
Execute in WebShell #1
In [ ]:
touch launch/my_package_launch_file.launch
You can also create it through the IDE.
4- Fill this launch file as we've previously seen in this course {Example 2.3}.
HINT: You can copy from the turtlebot_teleop package, the keyboard_teleop.launch file and
modify it. If you do so, remove the param and remap tags and leave only the node tag,
because you don't need those parameters.
The final launch file should be something similar to this my_package_launch_file.launch.
<launch>
<!-- My Package launch file -->
<node pkg="my_package" type="simple.py" name="ObiWan" output="screen">
</node>
</launch>
**END Launch File {2.1-l}: my_package_launch_file.launch**
5- Finally, execute the roslaunch command in the WebShell in order to launch your program.
Execute in WebShell #1
In [ ]:
roslaunch my_package my_package_launch_file.launch
END **Example 2.6**
**Expected Result for Example 2.6**
You should see Leia's quote among the output of the roslaunch command.
WebShell #1 Output
In [ ]:
SUMMARY
========
PARAMETERS
* /rosdistro: kinetic
* /rosversion: 1.11.20
NODES
/
ObiWan (my_package/simple.py)
ROS_MASTER_URI=http://localhost:11311
In [ ]:
rospack profile
**Code Explanation {2.1a-py}: simple.py**
Although it is a very simple Python script, let's explain it line by line, to avoid any confusion:
In [ ]:
#! /usr/bin/env python
# This line will ensure the interpreter used is the first one on your environment's $PATH. Every Python file needs
# to start with this line at the top.
import rospy # Import the rospy, which is a Python library for ROS.
print "Help me Obi-Wan Kenobi, you're my only hope" # A simple Python print
**END Code Explanation {2.1a-py}: simple.py**
Common Issues
From our experience, we've seen that it is a common issue when working with Python scripts in this
Course, that users get an error similar to this one:
This error usually appears to users when they create a Python script from the WebShell. It happens
because when created from the shell, the Python scripts don't have execution permissions. You can
check the permissions of a file using the following command, inside the directory where the file is
located at:
Execute in WebShell #1
In [ ]:
ls -la
The first row in the left indicates the permissions of this file. In this case, we have -rw-rw-r-. So, you
only have read(r) and write(w) permissions on this file, but not execution permissions (which are
represented with an x).
To add execution permissions to a file, you can use the following command:
Execute in WebShell #1
In [ ]:
chmod +x name_of_file.py
Using this command, you will see that execution permissions are added to the file. Also, the file will
appear now in green color.
ROS Nodes
You've initiated a node in the previous code but... what's a node? ROS nodes are basically
programs made in ROS. The ROS command to see what nodes are actually running in a computer
is:
In [ ]:
rosnode list
**Example 2.7**
Type this command in a new shell and look for the node you've just initiated (ObiWan).
Execute in WebShell #1
In [ ]:
rosnode list
You can't find it? I know you can't. That's because the node is killed when the Python program ends.
Let's change that.
#! /usr/bin/env python
import rospy
rospy.init_node("ObiWan")
rate = rospy.Rate(2) # We create a Rate object of 2Hz
while not rospy.is_shutdown(): # Endless loop until Ctrl + C
print "Help me Obi-Wan Kenobi, you're my only hope"
rate.sleep() # We sleep the needed time to maintain the Rate fixed above
# This program creates an endless loop that repeats itself 2 times per second (2Hz) until somebody presses Ctrl + C
# in the Shell
**END Python Program {2.1b-py}: simple_loop.py**
Launch your program again using the roslaunch command.
Execute in WebShell #1
In [ ]:
In [ ]:
rosnode list
Can you now see your node?
WebShell #2 Output
In [ ]:
In [ ]:
Subscriptions:
* /clock [rosgraph_msgs/Clock]
Services:
* /ObiWan/set_logger_level
* /ObiWan/get_loggers
Compile a package
When you create a package, you will usually need to compile it in order to make it work. The
command used by ROS to compile is the next one:
In [ ]:
catkin_make
This command will compile your whole src directory, and it needs to be issued in
your catkin_ws directory in order to work. This is MANDATORY. If you try to compile from
another directory, it won't work.
**Example 2.8**
Go to your catkin_ws directory and compile your source folder. You can do this by typing:
Execute in WebShell #1
In [ ]:
roscd; cd ..
catkin_make
Sometimes (for example, in large projects) you will not want to compile all of your packages, but just
the one(s) where you've made changes. You can do this with the following command:
In [ ]:
In [ ]:
Parameter Server
A Parameter Server is a dictionary that ROS uses to store parameters. These parameters can be
used by nodes at runtime and are normally used for static data, such as configuration parameters.
To get a list of these parameters, you can type:
In [ ]:
rosparam list
To get a value of a particular parameter, you can type:
In [ ]:
In [ ]:
Roscore
In order to have all of this working, we need to have a roscore running. The roscore is the main
process that manages all of the ROS system. You always need to have a roscore running in order
to work with ROS. The command that launches a roscore is:
In [ ]:
roscore
Fig.1.2 - ROS Core Diagram
NOTE: At the platform you are using for this course, when you enter a course it automatically
launches a roscore for you, so you don't need to launch one.
Environment Variables
ROS uses a set of Linux system environment variables in order to work properly. You can check
these variables by typing:
In [ ]:
ROS_MASTER_URI -> Contains the url where the ROS Core is being executed. Usually, your own computer (localhost).
ROS_PACKAGE_PATH -> Contains the paths in your Hard Drive where ROS has packages in it.
NOTE 2: At the platform you are using for this course, we have created an alias to display the
environment variables of ROS. This alias is rosenv. By typing this on your shell, you'll get a list of
ROS environment variables. It is important that you know that this is not an official ROS command,
so you can only use it while working on this platform.
Ros Nodes: http://wiki.ros.org/Nodes
Parameter Server: http://wiki.ros.org/Parameter%20Server
Roscore: http://wiki.ros.org/roscore