Anda di halaman 1dari 7

AT90.

9027: Selected Topic: Cloud Robotics Asian Institute of Technology Handout: ROS Tutorial for Beginners

July 11, 2012 Computer Science and Information Management Instructor: Kan Ouivirach (kan@ieee.org)

ROS Tutorial for Beginners


Instructions: This tutorial will guide you through the ROS environment and the basic command-line tools. We will also write a simple ROS application and get it up and running. Before we start, please note that this tutorial is intended for using with Ubuntu. Presentation: http://goo.gl/WAVkn Credits: Thanks to Jednipat for his valuable guidance.

Congure Your ROS Environment


If you do not have ROS installed on your machine yet, please complete the installation instructions rst at http://www.ros.org/wiki/ROS/Installation/Ubuntu. Messing with the ROS base installation is not recommended unless you have some specic reasons. Since we are still a ROS noob, it is better to create a new path (e.g., /home/YOUR USER NAME/ros workspace) and add it to ROS PACKAGE PATH. Lets follow the steps below. 1. Create a ROS workspace:

$ mkdir

/ros

workspace

2. It is convenient to set up the ROS workspace automatically every time we open a new terminal. Run the following commands: (a) $ echo "export ROS PACKAGE PATH=/ros workspace:$ROS PACKAGE PATH" >> (b) $ echo "export ROS WORKSPACE=/ros workspace" >> (c) $ .
/.bashrc /.bashrc /.bashrc

3. Just to conrm you have done it correctly. Run this command: echo $ROS PACKAGE PATH, and you should see something similar to: /home/YOUR USER NAME/ros workspace:/opt/ros/fuerte/share:/opt/ros/fuerte/stacks If you are using the dierent version of ROS, please change the environment accordingly. 4. Now if you run: roscd, you will go to your workspace directly.

Understand ROS with Turtle


In this section, a turtle will help you to understand how ROS messaging mechanism works. 1. We get started with building a ROS package turtlesim. First we need to install system dependencies required for turtlesim:

$ rosdep install turtlesim


If you see an error saying that rosdep has not been initialized. To x this, run:

$ sudo rosdep init $ rosdep update


2. Then to build the package, run:

$ rosmake turtlesim
3. In order for ROS nodes to communicate, you must keep the ROS server running. Open a new terminal and run:

$ roscore
By default, the server will be run on your local machine. If you want to run nodes across multiple machines, please see http://www.ros.org/wiki/ROS/NetworkSetup for more information. To see the information about the ROS nodes that are currently running, run:

$ rosnode list
You will see a node rosout that is always running as it collects and logs nodes debugging output. You may use the following command to see the information about a specic running node:

$ rosnode info <node-name>


4. Lets summon a turtle. Open a new terminal and run:

$ rosrun turtlesim turtlesim node


This means that you are running the executable turtlesim node in the package turtlesim. OK, by now you should have your little turtle! Check again (in a new terminal) what nodes are running:

$ rosnode list
You can ping your turtle to see if it is still alive by this command:

$ rosnode ping turtlesim


If you want to stop the node, just press Ctrl-C. 5. Lets control your turtle around. Run an another new node in a new terminal to enable your keyboard to control:

$ rosrun turtlesim turtle teleop key


Congratulations! You can control your little turtle now. :-) 6. To understand how messaging mechanism works, run the command below in a new terminal:

$ rxgraph
You should see a graph similar to Figure 1. In this gure, you can see the ROS nodes and topics. The turtlesim and the teleop turtle nodes are communicating on the topic named /turtle1/command velocity. 7. To see the data published on this topic, run:

Figure 1: Dynamic graph created by rxgraph.

$ rostopic echo /turtle1/command velocity


If you check the graph created by rxgraph again, you will see a new node has subscribed to the /turtle1/command velocity topic.

Write a Simple ROS Application


Say good bye to your turtle. In this section, we will write our own application, Hello ROS World, to get an idea how to write a program with ROS in Python! Therefore, you should have Python installed on your machine. If not, run: sudo apt-get install python. 1. Lets do it in our workspace so that ROS can nd our new package. Simply do:

$ roscd
2. Create our package:

$ roscreate-pkg beginner tutorials std msgs rospy roscpp


This means that our package beginner tutorials will depend on std msgs, rospy, and roscpp. std msgs contains common message types, rospy is the client library that allows Python to communicate through ROS, and roscpp is the client library for C++. 3. Make sure ROS can nd your package. Run:

$ rospack find beginner tutorials


You should see the output like this: YOUR PACKAGE PATH/beginner tutorials. 4. To build our package, run:

$ rosmake beginner tutorials


5. Change directory to our package:

$ roscd beginner tutorials


6. Create a le talker.py under /src. (a) Every node will have this declaration at the top. #!/usr/bin/env python import roslib; roslib.load_manifest('beginner_tutorials') This tells roslib to read in the manifest.xml le and add all the dependencies listed there to the PYTHONPATH. roslib.load manifest() also enables us to nd both rospy and std msgs, which are declared as dependencies in the manifest.xml. (b) We can import other libraries as follows.

import rospy from std_msgs.msg import String (c) We then dene the talkers interface to the rest of ROS as follows. def talker(): pub = rospy.Publisher('chatter', String) rospy.init_node('talker') We declare that our node will publish to the chatter topic using the message type String, and we also tell rospy the name of the node. (d) We will let it run forever until we press Ctrl-C or otherwise. See the code below. while not rospy.is_shutdown(): # substitute the time to the message str = 'Hello ROS World %s' % rospy.get_time() # loginfo() # gets the message printed to screen # gets it written to the Node's log file # gets it written to rosout rospy.loginfo(str) pub.publish(String(str)) rospy.sleep(1.0) Here the real work is pub.publish(String(str)) that keeps publishing the message to the chatter topic. (e) Here is the entire code. #!/usr/bin/env python import roslib; roslib.load_manifest('beginner_tutorials') import rospy from std_msgs.msg import String def talker(): pub = rospy.Publisher('chatter', String) rospy.init_node('talker') while not rospy.is_shutdown(): str = 'Hello ROS World %s' % rospy.get_time() rospy.loginfo(str) pub.publish(String(str)) rospy.sleep(1.0) if __name__ == '__main__': talker() (f) We need to make the node executable. Run:

$ chmod +x src/talker.py
7. We also need to write a node to receive the messages. Create a le listener.py under /src. (a) The code will look similar to talker.py, except we need to rst write a callback function for handling the messages as follows. def callback(data): rospy.loginfo(rospy.get_name() + 'I heard %s', data.data) (b) We then subscribe to the chatter topic as follows.

def listener(): rospy.init_node('listener', anonymous=True) rospy.Subscriber('chatter', String, callback) rospy.spin() When new messages are received, callback is invoked with the message as the rst argument. The anonymous=True ag tells rospy to generate a unique name for the node so that you can have multiple listener.py nodes run easily. In the last line of this function, rospy.spin() simply keeps the node from exiting until the node has been shutdown. (c) Here is the entire code. #!/usr/bin/env python import roslib; roslib.load_manifest('beginner_tutorials') import rospy from std_msgs.msg import String def callback(data): rospy.loginfo(rospy.get_name() + "I heard %s", data.data) def listener(): rospy.init_node('listener', anonymous=True) rospy.Subscriber("chatter", String, callback) rospy.spin() if __name__ == '__main__': listener() (d) Again, we need to make the node executable. Run:

$ chmod +x src/listener.py
8. Even you write a program in Python, you still have to build it to make sure that the ROS creates the auto-generated Python code for messages and services. Run:

$ rosmake beginner tutorials


9. After that, open a new terminal and then start the ROS server:

$ roscore
Run the Talker in a new terminal:

$ rosrun beginner tutorials talker.py


Also, run the Listener in a new terminal:

$ rosrun beginner tutorials listener.py


Your task now is to try to understand the results. 10. After we understand how it works, please exit all of the current running program, except the ROS server. We will try another command roslaunch to start nodes. In the real projects, we usually execute this command to start all nodes we want rather than start one of them one by one. Your life will be easier. 11. Lets create a subfolder launch under our package so that roslaunch can automatically nd our launch le.

12. Create a simple launch le hello.launch and paste the following: <launch> <node name="listener" pkg="beginner_tutorials" type="listener.py" /> <node name="talkerPy" pkg="beginner_tutorials" type="talker.py" /> </launch> This means we will create two nodes: listener and talkerPy by running listener.py and talker.py, respectively. Please see http://ros.org/wiki/roslaunch/XML for more information about the XML format used for roslaunch. Now we start the ROS server and then run the following command in a new terminal:

$ roslaunch beginner tutorial hello.launch


13. Run the following command the see which nodes are currently running:

$ rosnode list
The see the dynamic graph, run:

$ rxgraph
To see the messages being passed, run:

$ rostopic echo chatter


Congratulations again! We have studied the frequently used command-line tools, we understand the communication between nodes, and we can implement a simple publisher and subscriber with ROS. For more advanced information, I would encourage you to see the references provided at the end. [Optional] What if Another Talker is Implemented in C++? In this section, you may download a modied version of Talker from http://se.cs.ait.ac.th/~kan/ cloud-robotics/talker.cpp or follow this instructions: http://www.ros.org/wiki/ROS/Tutorials/WritingPublisherSubscriber(c++) Lets follow these steps below. 1. Put talker.cpp under /src. 2. Change directory to your package:

$ roscd beginner tutorials


3. Before we build the package, we need to add the following line at the bottom of CMakeLists.txt. rosbuild add executable(talker src/talker.cpp) 4. Run make then you can see that the executable le talker is created and goes into the /bin directory by default. 5. Finally, run the ROS server, Listener, Talker (Python), and Talker (C++) in the dierent terminals using rosrun. See the results. You are expected to see an error at Talker (Python). Why? :-) 6. Fix it. Good luck! (Hint: roslaunch)

References
ROS.org http://www.ros.org/wiki/ ROS Cheat Sheet http://www.ros.org/wiki/Documentation?action=AttachFile&do=get&target= ROScheatsheet.pdf ROS HOWTO by AIRWiki http://www.airlab.elet.polimi.it/index.php/ROS_HOWTO

Anda mungkin juga menyukai