Difference between revisions of "ROS Tutorials"

From Lofaro Lab Wiki
Jump to: navigation, search
(Using rqt_consol and roslaunch)
 
(41 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Practicing writing a wiki page- all information below is incomplete until otherwise noted.
+
This tutorial assumes that you will be working in ROS-hydro on Ubuntu 12.04.
  
Aside to editor, to improve:
 
 
This tutorial assumes that you will be working in ROS-hydro on Ubuntu 12.04.
 
 
This wiki is based on the tutorials given [http://wiki.ros.org/ROS/Tutorials here].
 
This wiki is based on the tutorials given [http://wiki.ros.org/ROS/Tutorials here].
  
Line 9: Line 6:
  
 
Open the terminal. To complete each step, type in the following commands in the order shown.
 
Open the terminal. To complete each step, type in the following commands in the order shown.
 +
# [[Installing and configuring your ROS environment]]
 +
# [[Navigating the ROS Filesystem]]
 +
# [[Creating a ROS Package]]
 +
# [[Building a ROS Package]]
 +
# [[Understanding ROS Nodes]]
 +
# [[Understanding ROS Topics]]
 +
# [[Understanding ROS Services and Parameters]]
 +
# [[Using rqt_consol and roslaunch]]
 +
# [[Using rosed to Edit Files in ROS]]
 +
# [[Creating a ROS msg and srv]]
 +
# [[Writing a Simple Publisher and Subscriber (C++)]]
 +
# [[Writing a Simple Publisher and Subscriber (Python)]]
 +
# [[Examining the Simple Publisher and Subscriber]]
 +
# [[Writing a Simpler Service and Client (C++)]]
 +
# [[Writing a Simpler Service and Client (Python)]]
 +
# [[Examining the Simpler Service and Client]]
 +
# [[Recording and Playing Back Data]]
 +
# [[Getting Started with roswtf]]
 +
# [[Navigating the ROS wiki]]
 +
# [[Where Next?]]
  
==Installing and configuring your ROS environment==
+
=Intermediate Level=
  
We recommend ROS-hydro on Ubuntu 12.04, instructions given on this [http://wiki.ros.org/hydro/Installation/Ubuntu link]. The commands are given below. Complete all of these in the same Terminal during the same session.
+
Open the terminal. To complete each step, type in the following commands in the order shown.
 +
# [[Creating a ROS Package by Hand]]
 +
# [[Managing System Dependencies]]
 +
# [[Roslaunch Tips for Large Projects]]
 +
# [[Running ROS Across Multiple Machines]]
 +
# [[Defining Custom Messages]]
  
'''If not using Ubuntu 12.04 and installing hydro:''' please refer to instructions given [http://wiki.ros.org/ROS/Tutorials/InstallingandConfiguringROSEnvironment here].
 
  
1) Setup sources list:
+
=Advanced=
 
+
* [[Setting up RGBDSLAM with ROS in Gazebo]]
    <nowiki>sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu precise main" > /etc/apt/sources.list.d/ros-latest.list'</nowiki>
+
* [[Setting up RGBDSLAM with ROS in the real world]]
 
+
* [[Raspberry_Pi_and_Arducopter_connections_through_ROS]]
You may be prompted for your super-user password. This should be your username password. Type it into the terminal and press ENTER.
+
* [[Ros_on_Raspberry_pi]]
 
+
2) Setup keys:
+
 
+
    <nowiki>wget https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -O - | sudo apt-key add -</nowiki>
+
 
+
3) Update Debian package:
+
 
+
    <nowiki>sudo apt-get update</nowiki>
+
 
+
4) Install Desktop-Full:
+
 
+
  <nowiki>sudo apt-get install ros-hydro-desktop-full</nowiki>
+
 
+
5) Initialize rosdep:
+
 
+
  <nowiki>sudo rosdep init</nowiki>
+
  <nowiki>rosdep update</nowiki>
+
 
+
6) Setup Enviornment:
+
 
+
  <nowiki>echo "source /opt/ros/hydro/setup.bash" >> ~/.bashrc</nowiki>
+
  <nowiki>source ~/.bashrc</nowiki>
+
 
+
7) Install rosinstall:
+
 
+
  <nowiki>sudo apt-get install python-rosinstall</nowiki>
+
 
+
==Navigating the ROS Filesystem==
+
 
+
First, if catkin is not already installed, we need to settle some dependencies and install catkin.
+
 
+
Settle dependencies:
+
 
+
  <nowiki>sudo apt-get install cmake python-catkin-pkg python-empty python-nose python-setuptools libgtest-dev build-essential</nowiki>
+
 
+
Install catkin:
+
 
+
  <nowiki>sudo apt-get install ros-hydro-catkin</nowiki>
+
 
+
Now we can focus on navigation. Here is a list of commands to navigate around the ROS files and folders.
+
 
+
*To find the path of a package: <nowiki>rospack find [package-name]</nowiki>
+
Example:
+
  <nowiki>rospack find roscpp</nowiki>
+
 
+
*To change directory: <nowiki>roscd [locationname[/subdir]]</nowiki>
+
Example:
+
  <nowiki>roscd roscpp</nowiki>
+
 
+
*To see your working (current) directory:
+
  <nowiki>pwd</nowiki>
+
 
+
*To see your ROS environment path:
+
  <nowiki>echo $ROS_PACKAGE_PATH</nowiki>
+
 
+
*To move to the folder ROS stores its logs:
+
  <nowiki>roscd log</nowiki>
+
 
+
*To see contents of a particular package: <nowiki>rosls [locationname[/subdir]]</nowiki>
+
Example:
+
  <nowiki>rosls roscpp_tutorials</nowiki>
+
 
+
*TAB completion is also supported.
+
 
+
==Creating a ROS Package==
+
A workspace is created, then a ROS package within the workspace.
+
 
+
'''Creating a Workspace for Catkin'''
+
 
+
1) We create a src file in the catkin_ws folder, move into it, and create a workspace called catkin_init_workspace:
+
  <nowiki>mkdir -p ~/catkin_ws/src</nowiki>
+
  <nowiki>cd ~/catkin_ws/src</nowiki>
+
  <nowiki>catkin_init_workspace</nowiki>
+
2) Then we build the workspace:
+
  <nowiki>cd ~/catkin_ws/</nowiki>
+
  <nowiki>catkin_make</nowiki>
+
3) And create the setup.*sh file:
+
  <nowiki>source devel/setup.bash</nowiki>
+
 
+
'''Creating a ROS Package'''
+
 
+
4) Make sure you are in the src folder you had created earlier:
+
  <nowiki>cd ~/catkin_ws/src</nowiki>
+
 
+
5) Now we create a new ROS package:  <nowiki>catkin_create_pkg <package_name> [depend1] [depend2] [depend3]</nowiki>
+
  <nowiki>catkin_create_pkg beginner_tutorials std_msgs rospy roscpp</nowiki>
+
This will create a beginner_tutorials folder which contains a package.xml and a CMakeLists.txt
+
5a) The package.xml contains the names of the dependenices. These can be checked by looking at the folder or typing:
+
  <nowiki>rospack depends1 beginner_tutorials</nowiki>
+
5b) To see all dependencies for the package and dependencies given in the xml:
+
  <nowiki>rospack depends beginner_tutorials</nowiki>
+
The xml file has a description (line 5), maintainer tag (lines 7,9, and 10), license tag (line 8), and dependency list (lines 12-20). Below is given a concise example of our beginner_tutorial xml file:
+
  <nowiki> 1 <?xml version="1.0"?>
+
  2 <package>
+
  3  <name>beginner_tutorials</name>
+
  4  <version>0.1.0</version>
+
  5  <description>The beginner_tutorials package</description>
+
  6
+
  7  <maintainer email="you@yourdomain.tld">Your Name</maintainer>
+
  8  <license>BSD</license>
+
  9  <url type="website">http://wiki.ros.org/beginner_tutorials</url>
+
  10  <author email="you@yourdomain.tld">Jane Doe</author>
+
  11
+
  12  <buildtool_depend>catkin</buildtool_depend>
+
  13
+
  14  <build_depend>roscpp</build_depend>
+
  15  <build_depend>rospy</build_depend>
+
  16  <build_depend>std_msgs</build_depend>
+
  17
+
  18  <run_depend>roscpp</run_depend>
+
  19  <run_depend>rospy</run_depend>
+
  20  <run_depend>std_msgs</run_depend>
+
  21
+
  22 </package></nowiki>
+
 
+
==Building a ROS Package==
+
 
+
1) Source your environment (in case you skipped this step above)
+
  <nowiki>source /opt/ros/groovy/setup.bash</nowiki>
+
For information regarding cmake and catkin_make, see this [http://wiki.ros.org/ROS/Tutorials/BuildingPackages#ROS.2BAC8-Tutorials.2BAC8-catkin.2BAC8-BuildingPackages.Using_catkin_make link]
+
 
+
For those who are creating there own code, a change to Cmake.txt may be in order. Please see later tutorials in [http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29 C++] and [http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29 Python] for assistance.
+
 
+
2) Make sure you are in the catkin workspace. Look at the files listed there.
+
  <nowiki>cd ~/catkin_ws/</nowiki>
+
  <nowiki>ls src</nowiki>
+
beginner_tutorials, the folder we have previously made, will be listed there.
+
 
+
3) Let us build the package:
+
  <nowiki>catkin_make</nowiki>
+
 
+
4) After the build, take a look again at the folders listed there:
+
  <nowiki>ls</nowiki>
+
You will see folders build, devel, and src. catkin_make already knows where to place these new folders and automatically takes care of it for you.
+
 
+
==Understanding ROS Nodes==
+
 
+
1) First, please install this simulator:
+
  <nowiki>sudo apt-get install ros-hydro-ros-tutorials</nowiki>
+
 
+
Terminology:
+
*Nodes: A node is an executable that uses ROS to communicate with other nodes.
+
*Messages: ROS data type used when subscribing or publishing to a topic.
+
*Topics: Nodes can publish messages to a topic as well as subscribe to a topic to receive messages.
+
*Master: Name service for ROS (i.e. helps nodes find each other)
+
*rosout: ROS equivalent of stdout/stderr
+
*roscore: Master + rosout + parameter server (parameter server will be introduced later)
+
 
+
2) To use ROS, start by running the core:
+
  <nowiki>roscore</nowiki>
+
It will display a summary. '''If it fails to initialize''', try changing the ownership of the root:
+
  <nowiki>sudo chown -R <your_username> ~/.ros</nowiki>
+
Then attempt to bring up roscore again.
+
 
+
3) '''In a new terminal''' (we will call this terminal 2) see what nodes are running:
+
  <nowiki>rosnode list</nowiki>
+
Likely, you will only see one called /rosout.
+
 
+
4) In terminal 2 try seeing more information about each node:
+
  <nowiki>rosnode info /rosout</nowiki>
+
This gives you information about the currently running nodes.
+
 
+
5) '''In a new terminal''' (terminal 3) we will initialize a new node.
+
 
+
To run a node, use <nowiki>rosrun [package_name] [node_name]:</nowiki>
+
  <nowiki>rosrun turtlesim turtlesim_node</nowiki>
+
A new window with a turtle will show up.
+
 
+
6) In terminal 2 (where we used rosnode list) run rosnode again:
+
  <nowiki>rosnode list</nowiki>
+
The nodes /rosout and /turtlesim now appear.
+
 
+
7) Close the turtle sim. In terminal 3 (where we had started the turtle sim) we will restart it again, but give the node a name:
+
  <nowiki>rosrun turtlesim turtlesim_node __name:=my_turtle</nowiki>
+
 
+
8) In terminal 2, look at the nodes again:
+
  <nowiki>rosnode list</nowiki>
+
You will see /rosout and /my_turtle. If not, try cleaning up the rosnode list:
+
  <nowiki>rosnode cleanup</nowiki>
+
Then try running the rosnode list command again.
+
 
+
9) In terminal 2, let us ping our simulation:
+
  <nowiki>rosnode ping my_turtle</nowiki>
+
This tests the simulation to see if it is in fact running. You will see time of replies printed to the command.
+
 
+
==Understanding ROS Topics==
+
If continuing from earlier, please close down all currently running terminals.
+
 
+
1) In a terminal, run roscore:
+
  <nowiki>roscore</nowiki>
+
 
+
2) '''In a new terminal''' (terminal 2) run the turtle simulation:
+
  <nowiki>rosrun turtlesim turtlesim_node</nowiki>
+
 
+
3) '''In a new terminal''' (terminal 3) run a controller to be able to control the turtle with keystrokes:
+
  <nowiki>rosrun turtlesim turtle_teleop_key</nowiki>
+
You should be able to move the turtle. If not, make sure you have this terminal 3 selected, then try pressing the arrow keys again. (If on a vm, there might be a slight lag to start)
+
 
+
The turtlesim_node and the turtle_teleop_key node are communicating with each other over a ROS Topic. turtle_teleop_key is publishing the key strokes on a topic, while turtlesim subscribes to the same topic to receive the key strokes.
+
 
+
4) Install rqt_graph:
+
  <nowiki>sudo apt-get install ros-hydro-rqt</nowiki>
+
  <nowiki>sudo apt-get install ros-hydro-rqt-common-plugins</nowiki>
+
 
+
5) '''In a new terminal''' (terminal 4) run the rqt_graph to display a graphical representation of the system:
+
  <nowiki>rosrun rqt_graph rqt_graph</nowiki>
+
The nodes are circles, the topics are lines between them.
+
 
+
6) '''In a new terminal''' (terminal 5) use <nowiki>rostopic echo [topic]</nowiki> to view what data is being published to a topic:
+
  <nowiki>rostopic echo /turtle1/cmd_vel</nowiki>
+
Likely nothing happened. To see some data, select terminal 3 (with the teleop_key controller) and press the arrow keys. Terminal 5 should now print some data.
+
 
+
7) Go back to the graph of the nodes and topics. Hit refresh at the top-left. A new node will appear inside the graph.
+
 
+
8) '''In a new terminal''' (terminal 6) view the verbose list of topics:
+
  <nowiki>rostopic list -v</nowiki>
+
 
+
9) In terminal 6, use <nowiki>rostopic type [topic]</nowiki> to return the message type of any topic being published:
+
  <nowiki>rostopic type /turtle1/cmd_vel</nowiki>
+
 
+
10) In terminal 6, use <nowiki>rosmsg show geometry_msgs/Twist</nowiki> to see details of the message:
+
  <nowiki>rosmsg show geometry_msgs/Twist</nowiki>
+
 
+
11) In terminal 6, use <nowiki>rostopic pub [topic] [msg_type] [args]</nowiki> to publish data to a topic:
+
  <nowiki>rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'</nowiki>
+
The turtle sim should now show a turtle going around in an arc for a time.
+
 
+
Break down of previous command:
+
*rostopic pub        : published the information
+
*-1                  : has the publisher send one command then exit
+
*/turtle1/cmd_vel    : name of topic to publish to
+
*geometry_msgs/Twist : message type to use
+
*--                  : lets parser know following informations are not an option (important for negative numbers)
+
*'[x, y, z] (2x)    : YAML velocity vectores. For more on YAML syntax, please see this [http://wiki.ros.org/ROS/YAMLCommandLine link].
+
 
+
12) Likely the turtle has stopped. The simulation needs constant commands to keep going.
+
 
+
In terminal 6, use rostopic pub with the -r argument to send continuous commands:
+
  <nowiki>rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'</nowiki>
+
The turtle should now be driving around in a circle. You can look at teh rqt_graph (hit refresh) to see the current hierarchy.
+
 
+
13) In terminal 6, use  <nowiki>rostopic hz [topic]</nowiki> to see how fast the data is published to the topic:
+
  <nowiki>rostopic hz /turtle1/pose</nowiki>
+
Intervals of time with some information should print to the terminal.
+
 
+
14) In terminal 6, view in depth information on the topic:
+
  <nowiki>rostopic type /turtle1/cmd_vel | rosmsg show</nowiki>
+
 
+
15) In terminal 6, view a graph of the data being sent on the topic:
+
  <nowiki>rosrun rqt_plot rqt_plot</nowiki>
+
 
+
==Understanding ROS Services and Parameters==
+
If continuing from earlier, please close down all currently running terminals.
+
*Services: allow nodes to send a request and receive a response.
+
 
+
1) In a terminal, run roscore:
+
  <nowiki>roscore</nowiki>
+
 
+
2) '''In a new terminal''' (terminal 2) run the turtle simulation:
+
  <nowiki>rosrun turtlesim turtlesim_node</nowiki>
+
 
+
3) '''In a new terminal''' (terminal 3) run a controller to be able to control the turtle with keystrokes:
+
  <nowiki>rosrun turtlesim turtle_teleop_key</nowiki>
+
You should be able to move the turtle. If not, make sure you have this terminal 3 selected, then try pressing the arrow keys again. (If on a vm, there might be a slight lag to start)
+
 
+
4)'''In a new terminal''' (terminal 4) list the services the turtle sim can provide:
+
  <nowiki>rosservice list</nowiki>
+
You will see a list reset, clear, spawn, kill, turtle1/set_pen, /turtle1/teleport_absolute, /turtle1/teleport_relative, turtlesim/get_loggers, and turtlesim/set_logger_level.
+
 
+
5) In terminal 4, use rosservice type [service] to display information on a service that was found earlier with rosservice list:
+
  <nowiki>rosservice type clear</nowiki>
+
 
+
6) In terminal 4, use rosservice call [service] [args] to use a service found earlier with rosservice list:
+
  <nowiki>rosservice call clear</nowiki>
+
 
+
7) In terminal 4, view the information on the service spawn:
+
  <nowiki>rosservice type spawn| rossrv show</nowiki>
+
The information tells us that we can spawn a turtle in the same window at a specified location and orientation.
+
 
+
8) In terminal 4, spawn the new turtle with:
+
  <nowiki>rosservice call spawn 2 2 0.2 ""</nowiki>
+
 
+
9) In terminal 4, list the parameters the node (our simulation) has on the param server:
+
  <nowiki>rosparam list</nowiki>
+
 
+
10) In terminal 4, use <nowiki>rosparam set [param_name]</nowiki> to set parameters:
+
  <nowiki>rosparam set background_r 150</nowiki>
+
The background color parameter has been changed.
+
 
+
11) In terminal 4, to see the change made to the parameters:
+
  <nowiki>rosservice call clear</nowiki>
+
 
+
12) In terminal 4, use <nowiki>rosparam get [param_name]</nowiki> to retrieve parameters:
+
  <nowiki>rosparam get background_g </nowiki>
+
or
+
  <nowiki>rosparam get /</nowiki>
+
To see all parameters.
+
 
+
13) In terminal 4, use <nowiki>rosparam dump [file_name] [namespace]</nowiki> to dump parameters to a file:
+
  <nowiki>rosparam dump params.yaml</nowiki>
+
 
+
14) In terminal 4, use <nowiki>rosparam load [file_name] [namespace]</nowiki> to retrieve the newly created file contents in the same or different workspaces:
+
  <nowiki>rosparam load params.yaml copy</nowiki>
+
 
+
==Using rqt_consol and roslaunch==
+
 
+
If you have not installed the turtle sim and rqt packages do so before continuing:
+
  <nowiki>sudo apt-get install ros-hydro-rqt ros-<distro>-rqt-common-plugins ros-hydro-turtlesim</nowiki>
+
 
+
1) In a terminal, start the ros_console:
+
  <nowiki>rosrun rqt_console rqt_console</nowiki>
+
A window will pop up.
+
 
+
2) '''In a new terminal''' (terminal 2) begin the logger:
+
  <nowiki>rosrun rqt_logger_level rqt_logger_level</nowiki>
+
A window will pop up.
+
 
+
3) '''In a new terminal''' (terminal 3) begin the turtle simulation:
+
  <nowiki>rosrun turtlesim turtlesim_node</nowiki>
+
 
+
4) '''In a new terminal''' (terminal 4) send a command to the sim:
+
  <nowiki>rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 0.0]'</nowiki>
+
 
+
5) Change the logger level to Warn by refreshing the nodes in the rqt_logger_level window and selecting Warn.
+
  [[File:http://wiki.ros.org/ROS/Tutorials/UsingRqtconsoleRoslaunch?action=AttachFile&do=get&target=rqt_logger_level%28error%29.png]]
+
 
+
==Using rosed to Edit Files in ROS==
+
==Creating a ROS msg and srv==
+
==Writing a Simple Publisher and Subscriber (C++)==
+
==Writing a Simple Publisher and Subscriber (Python)==
+
==Examining the Simple Publisher and Subscriber==
+
==Writing a Simpler Service and Client (C++)==
+
==Writing a Simpler Service and Client (Python)==
+
==Examining the Simpler Service and Client==
+
==Recording and Playing Back Data==
+
==Getting Started with roswtf==
+
==Navigating the ROS wiki==
+
==Where Next?==
+
=Intermediate Level=
+

Latest revision as of 17:23, 30 October 2014

This tutorial assumes that you will be working in ROS-hydro on Ubuntu 12.04.

This wiki is based on the tutorials given here.

Beginner Level

Open the terminal. To complete each step, type in the following commands in the order shown.

  1. Installing and configuring your ROS environment
  2. Navigating the ROS Filesystem
  3. Creating a ROS Package
  4. Building a ROS Package
  5. Understanding ROS Nodes
  6. Understanding ROS Topics
  7. Understanding ROS Services and Parameters
  8. Using rqt_consol and roslaunch
  9. Using rosed to Edit Files in ROS
  10. Creating a ROS msg and srv
  11. Writing a Simple Publisher and Subscriber (C++)
  12. Writing a Simple Publisher and Subscriber (Python)
  13. Examining the Simple Publisher and Subscriber
  14. Writing a Simpler Service and Client (C++)
  15. Writing a Simpler Service and Client (Python)
  16. Examining the Simpler Service and Client
  17. Recording and Playing Back Data
  18. Getting Started with roswtf
  19. Navigating the ROS wiki
  20. Where Next?

Intermediate Level

Open the terminal. To complete each step, type in the following commands in the order shown.

  1. Creating a ROS Package by Hand
  2. Managing System Dependencies
  3. Roslaunch Tips for Large Projects
  4. Running ROS Across Multiple Machines
  5. Defining Custom Messages


Advanced