ROS Tutorials

From Lofaro Lab Wiki
Revision as of 18:23, 17 October 2014 by Dlofaro (Talk | contribs)

Jump to: navigation, search

Practicing writing a wiki page- all information below is incomplete until otherwise noted.

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 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

Understanding ROS Topics

If continuing from earlier, please close down all currently running terminals.

1) In a terminal, run roscore:

  roscore

2) In a new terminal (terminal 2) run the turtle simulation:

  rosrun turtlesim turtlesim_node

3) In a new terminal (terminal 3) run a controller to be able to control the turtle with keystrokes:

  rosrun turtlesim turtle_teleop_key

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:

  sudo apt-get install ros-hydro-rqt
  sudo apt-get install ros-hydro-rqt-common-plugins

5) In a new terminal (terminal 4) run the rqt_graph to display a graphical representation of the system:

  rosrun rqt_graph rqt_graph

The nodes are circles, the topics are lines between them.

6) In a new terminal (terminal 5) use rostopic echo [topic] to view what data is being published to a topic:

  rostopic echo /turtle1/cmd_vel

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:

  rostopic list -v

9) In terminal 6, use rostopic type [topic] to return the message type of any topic being published:

  rostopic type /turtle1/cmd_vel

10) In terminal 6, use rosmsg show geometry_msgs/Twist to see details of the message:

  rosmsg show geometry_msgs/Twist

11) In terminal 6, use rostopic pub [topic] [msg_type] [args] to publish data to a topic:

  rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

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 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:

  rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

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 rostopic hz [topic] to see how fast the data is published to the topic:

  rostopic hz /turtle1/pose

Intervals of time with some information should print to the terminal.

14) In terminal 6, view in depth information on the topic:

  rostopic type /turtle1/cmd_vel | rosmsg show

15) In terminal 6, view a graph of the data being sent on the topic:

  rosrun rqt_plot rqt_plot

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:

  roscore

2) In a new terminal (terminal 2) run the turtle simulation:

  rosrun turtlesim turtlesim_node

3) In a new terminal (terminal 3) run a controller to be able to control the turtle with keystrokes:

  rosrun turtlesim turtle_teleop_key

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:

  rosservice list

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:

  rosservice type clear

6) In terminal 4, use rosservice call [service] [args] to use a service found earlier with rosservice list:

  rosservice call clear

7) In terminal 4, view the information on the service spawn:

  rosservice type spawn| rossrv show

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:

  rosservice call spawn 2 2 0.2 ""

9) In terminal 4, list the parameters the node (our simulation) has on the param server:

  rosparam list

10) In terminal 4, use rosparam set [param_name] to set parameters:

  rosparam set background_r 150

The background color parameter has been changed.

11) In terminal 4, to see the change made to the parameters:

  rosservice call clear

12) In terminal 4, use rosparam get [param_name] to retrieve parameters:

  rosparam get background_g 

or

  rosparam get /

To see all parameters.

13) In terminal 4, use rosparam dump [file_name] [namespace] to dump parameters to a file:

  rosparam dump params.yaml

14) In terminal 4, use rosparam load [file_name] [namespace] to retrieve the newly created file contents in the same or different workspaces:

  rosparam load params.yaml copy

Using rqt_consol and roslaunch

If you have not installed the turtle sim and rqt packages do so before continuing:

  sudo apt-get install ros-hydro-rqt ros-hydro-rqt-common-plugins ros-hydro-turtlesim

1) In a terminal, start roscore:

  roscore

1) In a new terminal (terminal 2), start the ros_console:

  rosrun rqt_console rqt_console

A window will pop up.

2) In a new terminal (terminal 3) begin the logger:

  rosrun rqt_logger_level rqt_logger_level

A window will pop up.

3) In a new terminal (terminal 4) begin the turtle simulation:

  rosrun turtlesim turtlesim_node

4) Change the logger level to Warn by refreshing the nodes in the rqt_logger_level window and selecting Warn.

5) In a new terminal (terminal 5) send a command to the sim:

  rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 0.0]'

The logger will begin to print several warnings as expected.

6) Kill the simulation. Close terminal 5. Navigate to the beginners_tutorial package:

  roscd beginner_tutorials

6b) If roscd says similar to roscd: No such package/stack 'beginner_tutorials' , you will need to source the environment

  cd ~/catkin_ws
   source devel/setup.bash
   roscd beginner_tutorials

This must be done for each terminal you use

7) Make a launch directory:

  mkdir launch
   cd launch

8) Create a newe file titled turtlemimic.launch and paste in the following:

  <launch>
   
     <group ns="turtlesim1">
       <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
     </group>
   
     <group ns="turtlesim2">
       <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
     </group>
   
     <node pkg="turtlesim" name="mimic" type="mimic">
       <remap from="input" to="turtlesim1/turtle1"/>
       <remap from="output" to="turtlesim2/turtle1"/>
     </node>
   
   </launch>

9) In terminal 4, use roslaunch [package] [filename.launch] to run launch files that contain nodes:

  roslaunch beginner_tutorials turtlemimic.launch

Two turtlesims will appear.

10) In a new terminal (terminal 5) send a command to the first simulation:

  rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

The command sent to the first turtle is mimicked by the second.

Using rosed to Edit Files in ROS

1) Close all current terminals. In a new terminal, edit your bash file to use nano (an editor):

  export EDITOR='nano -w'

Use

  echo $EDITOR

And see a line print to the terminal to make sure the bash file change was successful.

2) Use rosed [package_name] [filename] to edit a file within a package:

  rosed roscpp Logger.msg

This works similarly to other editors in a terminal. If running into trouble, try gedit.

Creating a ROS msg and srv

  • msg: msg files are simple text files that describe the fields of a ROS message. They are used to generate source code for messages in different languages.
  • srv: an srv file describes a service. It is composed of two parts: a request and a response.

Creating a msg

1) Move into the beginner_tutorials folder, create a msg directory, and create a new msg file:

  cd ~/catkin_ws/src/beginner_tutorials
   mkdir msg
   echo "int64 num" > msg/Num.msg

1b) Use rosmsg show [message type] to make sure ROS sees the new msg:

  rosmsg show beginner_tutorials/Num

Or

  rosmsg show Num

The message int64 num will display. The second option is if you do not have the name of the package. If int64 num does not display, see step 6b in Using rqt_console and roslaunch and try again.

Creating a srv 1) Move into your beginner_tutorials folder and create a src folder:

  roscd beginner_tutorials
   mkdir srv

2) Use roscp [package_name] [file_to_copy_path] [copy_path] to copy files from one folder to another:

  roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

This will copy a premade srv file into the srv folder.

2b) Use rossrv show <service type> to make sure ROS sees the new srv:

  rossrv show beginner_tutorials/AddTwoInts

The message with two int64 a and b will show, then a '---' line, followed by an int64 sum.

Farther Notes

1) Once you have made the msg and srv, you will need to rebuild the package:

  rosmake beginner_tutorials

Writing a Simple Publisher and Subscriber (C++)

Writing the Publisher Node

1) In a terminal, move to your beginner_tutorials folder:

  cd ~/catkin_ws/src/beginner_tutorials 

2) Create the file talker.cpp:

  gedit src/talker.cpp

3) In the gedit editor, paste all from this link. Save the file and close the editor. For information on what is inside this file, see this link. In a nutshell, it initializes the ROS system, advertise what we are doing, and finally loop what we are doing.

Writing the Subscriber Node

1) In a terminal, move to your beginner_tutorials folder:

  cd ~/catkin_ws/src/beginner_tutorials 

2) Create the file listener.cpp:

  gedit src/listener.cpp

3) In the gedit editor, paste all from this link. Save the file and close the editor. For information on what is inside this file, see this link. In a nutshell, it initializes the ROS system, subscribes to the topic 'chatter', spin (wait for messages), when message arrives call the chatterCallback() function.

Building Nodes

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