ROS-Lab Tutorial
ROS-Lab Tutorial
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)
$ 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.
$ 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 list
You can ping your turtle to see if it is still alive by this command:
$ 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:
$ roscd
2. Create our package:
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:
$ roscore
Run the Talker in a new terminal:
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:
$ rosnode list
The see the dynamic graph, run:
$ rxgraph
To see the messages being passed, run:
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