ECE 5463 Introduction to Robotics
Spring 2018
ROS
TUTORIAL 1
Guillermo Castillo (Wei Zhang)
Department of Electrical and Computer Engineering
Ohio State University
01/2018
ECE5463 (Sp18) Previous Steps
Outline
• Previous Steps
• Installing VM, Ubuntu 16.04.3, ROS Kinetic Distribution
• Possible problems
• ROS
• Understanding ROS Basics
• ROS Packages
• Creating ROS workspace and ROS Package
• Understanding ROS Filesystem
• ROS Program
• First ROS Program – Hello world
• Python scripts, launch files.
• Turtlesim simulation
2
ECE5463 (Sp18) Previous Steps
Possible problems
• The ROS ENVIRONMENT VARIABLE is not defined properly.
• Solution
Run the following commands:
echo "source ~/catkin_ws/devel/[Link]" >> ~/.bashrc
source ~/.bashrc
3
ECE5463 (Sp18) Previous Steps
Possible problems
• Installation of Anaconda changes the default path for python.
• ROS programs may not run.
• Solution
Open the bashrc file with the following command:
gedit ~/.bashrc
Erase or comment the line
export PATH="/home/guillermo/anaconda3/bin:$PATH“
• Alternative solution
Every time you open a new terminal run the following command:
export $PATH="/usr/bin:$PATH„
4
ECE5463 (Sp18) ROS Basics
General Overview
5
ECE5463 (Sp18) ROS Packages
What is a package?
• All the files that a specific ROS program contains; all its cpp files,
python files, configuration files, compilation files, launch files, and
parameters files.
• Generally all those files in the package are organized with the
following structure:
launch folder: Contains launch files
Not always
src folder: Source files (cpp, python)
[Link]: List of cmake rules for compilation
[Link]: Package information and dependencies
6
ECE5463 (Sp18) ROS Packages
Creating ROS package
• Every new project should be organized in packages
• We need to work in a very specific ROS workspace, which is known as the
catkin workspace. (catkin_ws)
• Associate the name of the package with its functionality.
CREATING ROS WORKSPACE (Only one time)
• The default directory for ROS packages is the path: /opt/ros/kinetic/share/
• Verify it with the command: printenv | grep ROS
• Directory path for new ROS projects: ~/catkin_ws/src (~ = home)
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make --- Build the files in the workspace
7
ECE5463 (Sp18) ROS Packages
Creating ROS package
• Always create new packages inside the SRC folder of the catkin workspace.
cd ~/catkin_ws/src
catkin_create_pkg <package_name> <package_dependecies>
• The package_name is the name of the package you want to create, and
the package_dependencies are the names of other ROS packages that your
package depends on.
catkin_create_pkg tutorial rospy
• The launch and src folder are not always created automatically, but we can
create them manually.
mkdir ~/catkin_ws/src/tutorial/launch
• Package commands:
rospack list: Gives a list of all the packages in your ROS system.
rospack list | grep [my_package]: Filters packages that contain [my_package]
roscd [my_package]: Takes you to the location of [my_package]
8
ECE5463 (Sp18) ROS Packages
Compile a package
• When you create a package, you will usually need to compile it in order to
make it work.
• This command will compile your whole src directory, and it needs to be called
in your catkin_ws directory. If you try to compile from another directory, it won't
work.
cd ~/catkin_ws/
catkin_make
Do not forget!
• Build the package when you install or create a new one, and
• Source the workspace so that ROS can find the new package.
9
ECE5463 (Sp18) ROS Program
First ROS program – Hello world
• Goal: Create the simplest ROS program, to print “HELLO WORLD”
• Therefore, we need some “object/element” that performs the “action” of
printing the desired message on the screen.
• Answer: Node = small program that performs an action.
• Then, we will create a ROS node that print the message on the screen.
Usually this nodes are initiated by a python or cpp script.
• Good practice: name the script according its function (node/action).
cd ~/catkin_ws/src/tutorial/src
gedit [Link]
10
ECE5463 (Sp18) ROS Program
Million dollar question
• What is the structure of the program?
• If you can visualize the structure, you can create it!
11
ECE5463 (Sp18) ROS Program
Python script
• NOTE: If you create your Python file from the shell, it may happen that it is
created without execution permissions. If this happens, ROS will not 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
chmod +x [Link]
12
ECE5463 (Sp18) ROS Program
Running the script
• Initiate ROS MASTER
roscore
• Open another terminal’s window (CTRL + SHIFT + T)
• Run the python script using the command: rosrun [package_name]
[python_file_name]
rosrun tutorial [Link]
• Stop the program (CTRL + C)
13
ECE5463 (Sp18) ROS Program
Launch file
• 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
2. type="python_file_name.py" # Name of the program file to execute
3. name="node_name" # Name of the ROS node that will launch our .py file
4. output="type_of_output" # Through which channel you will print the
output of the .py file
cd ~/catkin_ws/src/tutorial/launch
gedit node_launcher.launch
<launch>
<node pkg="tutorial" type="[Link]" name="printer_node"
output="screen">
</node>
</launch>
14
ECE5463 (Sp18) ROS Program
Running the launch file
• Use ROS command: roslaunch [package_name] [launch_file.launch]
roslaunch tutorial node_launcher.launch
• Now, let’s check what nodes are actually running using the command:
rosnode list
• The node is killed when the Python program ends.
15
ECE5463 (Sp18) ROS Program
Bigger ROS program structure
• Same program with bigger structure.
• GOAL: Create a ROS program
with two nodes. One of them must
publish a series of messages to a
defined topic. The second node
must listen to those messages
(subscribe to the topic) and print
the messages on the screen.
16
ECE5463 (Sp18) ROS Program
Publisher and Topics
• A publisher is a node that keeps publishing a message into a topic.
• A topic is a channel that acts as an information high way, where other ROS
nodes can either publish or read information.
CREATE A PUBLISHER
cd ~/catkin_ws/src/tutorial/src
gedit [Link]
17
ECE5463 (Sp18) ROS Program
Useful ROS commands related to Topics
• To verify if the topic was actually created
rostopic list
• To “listen” what the publisher is “talking”. Namely, to observe the content of
the topic: rostopic echo [name_of_the_topic]
rostopic echo /phrases
• To get information about the topic such as the message type, or the topic’s
publishers and subscribers: rostopic echo [name_of_the_topic]
rostopic info /phrases
18
ECE5463 (Sp18) ROS Program
Messages
• Topics handle information through messages.
• In the case of the code we executed, the message type was an
std_msgs/String, but ROS provides a lot of different messages.
•You can even create your own messages, but it is recommended to use ROS
default messages when possible.
• To get information about a message, use the next command: rosmsg show
[message_type]
rosmsg show std_msgs/String
• In this case, the String message has only one variable named data of type
string. This String message comes from the package std_msgs.
19
ECE5463 (Sp18) ROS Program
Subscriber
• A subscriber is a node that reads information from a topic.
CREATE A SUBSCRIBER
cd ~/catkin_ws/src/tutorial/src
gedit [Link]
20
ECE5463 (Sp18) ROS Program
Running the ROS program
• Recall: To run the scripts you have two ways. Using the rosrun command
(Python file), or using the roslaunch command (launch file).
• Using the rosrun command:
rosrun tutorial [Link]
• Using launch file
roslaunch tutorial subscriber_launcher.launch
<launch>
<node pkg="tutorial" type="[Link]"
name="topic_subscriber" output="screen">
</node>
</launch>
• Notice that the subscriber will print nothing if there is no information published
in the topic “phrases”
21
ECE5463 (Sp18) ROS Program
Alternative ways to publish and launch
• To publish directly in any topic: rostopic pub [topic] [msg_type] [args]
rostopic pub /phrases std_msgs/String “HELLO ROS”
• You can also launch more than one package at the same time from the same
launch file.
<launch>
<node pkg="tutorial" type="[Link]" name="topic_publisher"
output="screen">
</node>
<node pkg="tutorial" type="[Link]"
name="topic_subscriber" output="screen">
</node>
</launch>
roslaunch tutorial publisher_subscriber_launcher.launch
22
ECE5463 (Sp18) ROS Program
Important
• RECALL: rosrun is used to execute Python, cpp files, which initiate the
nodes and perform actions, while roslaunch is used to execute the .launch
file that can run one or more nodes at the same time. (This automate the
above process)
• Important commands summary
• rosnode list
• rostopic list
• rostopic info [package_name]
• rostopic echo [package_name]
• rosmsg show [message_type]
23
ECE5463 (Sp18) ROS Program
Turtlesim Simulation
• Start simulation by running nodes (this package uses cpp files to initiate the
nodes). Run the following commands in different terminal’s windows.
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
• Use the command learned in this tutorial to get information about the nodes,
topics, messages.
rosnode list
rostopic list
rostopic info /turtle1/pose
rosmsg show /turtlesim/pose
rostopic echo /turtle1/cmd_vel
•Publish manually in cmd_vel topic
rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -- '[0.0, 0.0,
0.0]' '[0.0, 0.0, 1.6]'
24
ECE5463 (Sp18) ROS Tutorial
References
• [Link]
• [Link]
• [Link]
on%29
• [Link]
• [Link]
• O’Kane, Jason. A Gentle Introduction to ROS
25
ECE5463 (Sp18)
Thanks for your
attention
26