AA 274
Principles of Robotic Autonomy
The Robot Operating System (ROS)
Writing Software for Robotics
l Robotics requires very
complex software
l The software you will
deal with in AA274A
has way more moving
parts than what you’ve
dealt with in most
other classes…
9/23/21 AA 274A | Lecture 2 2
Writing Software for Robotics
l We deal with the complexity through modularity
l We enable modularity by following the right design pattern: “a
general, reusable solution to a commonly occurring problem within a given
context in software design” – Wikipedia
9/23/21 AA 274A | Lecture 2 3
The Pub/Sub Design Pattern
l We divide our software into individual components
l We define “topics” (think chat rooms) where components can
broadcast information to anyone listening
l Each component can:
l Publish: send messages to a topic regardless of whether someone is listening or not
l Subscribe: receive messages on a topic if anyone is sending them regardless of who
9/23/21 AA 274A | Lecture 2 4
The Pub/Sub Design Pattern
Note: there are countless ways to IMPLEMENT pub/sub!
9/23/21 AA 274A | Lecture 2 5
The Pub/Sub Design Pattern
You already use
Note: there are countless ways to IMPLEMENT pub/sub!
Pub/Sub every day!
Where???
9/23/21 AA 274A | Lecture 2 6
Alternatives to Pub/Sub
l Request/Reply (RPC)
l Push/Pull
l Data binding (e.g. shared data members)
l Observers
9/23/21 AA 274A | Lecture 2 7
What is ROS?
Depending on who you are talking to…
l An implementation of pub/sub geared towards robotic applications
and that is network-aware
l Lots of open-source software shared by the community:
n SLAM (gmapping, amcl)
n Vision (OpenCV, PCL, OpenNI)
n Arm Navigation (MoveIt)
n Simulation (Gazebo)
9/23/21 AA 274A | Lecture 2 8
Are there “Alternatives” to ROS?
l LCM
l Drake
l Player
l YARP
l Orocos
l MRPT
l And many others!
9/23/21 AA 274A | Lecture 2 9
Why is ROS popular in industry?
l Not reinventing the wheel is generally good
l Robotics is hard! It’s great to offload some of the work to smart
people
l ROS is now 12 years
old and still going
strong
9/23/21 AA 274A | Lecture 2 10
Why are we using ROS in AA274?
l The closest thing we have to an “industry standard”
l It’s an insurance policy for you (stability, online teaching resources)
9/23/21 AA 274A | Lecture 2 11
Why not ROS 2?
• A major overhaul of ROS
• Ecosystem/documentation still not quite as
complete
• Fundamental design pattern (if not
implementation mechanics) still the same
• Keep an eye on it!
http://design.ros2.org/articles/why_ros2.html
9/23/21 AA 274A | Lecture 2 12
ROS – Robot Operating System
• 2007-Today
l Stanford AI Robot (STAIR)
l Willow Garage founded by Scott Hassan (eGroups, Google,
Stanford Digital Libraries)
l Willow awards 11 $400k PR2 robots to Universities
l OSRF (Open Source Robotics Foundation) created to maintain ROS
and Gazebo
l ROS is everywhere!
9/23/21 AA 274A | Lecture 2 13
ROS Integrates Existing Projects
l OpenCV (computer vision)
l Stage, Gazebo (simulation)
l OpenSLAM (navigation)
l Orocos KDL (arm navigation)
l Many ROS “wrappers” to existing software
9/23/21 AA 274A | Lecture 2 14
The Main Software Components
1) Master
2) Nodes
l Nodes talk to each other over topics (think chat rooms). Master
coordinates the whole thing
l Message types: abstraction away from specific hardware
l Camera image
l Laser scan data
l Motion control
9/23/21 AA 274A | Lecture 2 15
ROS Node
l A process (typically Python or C++) that runs some computation
l The “fundamental” building block
l Can act as a subscriber, publisher or both
l Nodes talk to each other over “topics”
l Run them using rosrun <package> <node>
l Initialize using rospy.init_node()
Note: nodelets are different. They are not individual processes, they share memory
9/23/21 AA 274A | Lecture 2 16
Node Examples
Sensors and actuators are
wrapped in self-contained,
reusable software containers
called “nodes”
9/23/21 AA 274A | Lecture 2 17
Node Examples
Higher level operations also
become nodes in the ROS
computational architecture
9/23/21 AA 274A | Lecture 2 18
More Concrete Node Examples
l LiDAR node publishes laser scan arrays
l Camera node publishes RGB images (+depth if RGBD) and camera info
(resolution, distortion coefficients)
l Mobile robot controller publishes odometry values (e.g. x-y
coordinates and velocities, +z for UAVs or underwater vehicles)
l Navigation node subscribes to LiDAR and odometry messages,
publishes motion control messages
9/23/21 AA 274A | Lecture 2 19
ROS Master
l A process that is in charge of coordinating nodes, publishers and
subscribers
l Also provides a global parameter server
l Exactly one of them running at any time
l Messages do NOT go through Master (i.e. peer-to-peer)
l Nodes will not be able to find each other without Master
9/23/21 AA 274A | Lecture 2 20
Sending Messages
l pub = rospy.Publisher()
l msg = ...
l pub.publish(msg)
9/23/21 AA 274A | Lecture 2 21
ROS Node - Publisher
9/23/21 AA 274A | Lecture 2 22
Monitoring Messages
l You can check if you are sending messages using the rostopic
command line tool:
l rostopic list – lists all the active topics
l rostopic echo <topic> – prints messages received on <topic>
l rostopic hz <topic> – measures topic publishing rate
9/23/21 AA 274A | Lecture 2 23
Receiving Messages
l rospy.Subscriber("chatter", String, callback)
l def callback(msg): …
(in C++ need to call spinOnce(), not in Python)
9/23/21 AA 274A | Lecture 2 24
ROS Node - Subscriber
9/23/21 AA 274A | Lecture 2 25
ROS Launch Files
l Simple XML files that allow you to
n Launch multiple nodes at once
n Set parameters for those nodes
n Start Master
l roslaunch <package> <file>.launch
9/23/21 AA 274A | Lecture 2 26
ROS Launch File Example
<launch>
<!-- Start the talker node -->
<node name="talker" pkg="aa274" type="talker.py" output="screen">
<param name="rate" value="5"/>
</node>
</launch>
9/23/21 AA 274A | Lecture 2 27
A Case Study
• Edge detection in camera images
Node 1 – Camera Driver Node 3 – image_view
Subscribes to: Nothing Subscribes to: Camera images
Publishes: Camera images Publishes: Nothing
Node 2 – Edge Detection Node 4 – image_view
Subscribes to: Camera images Subscribes to: Image with edges
Publishes: Image with edges Publishes: Nothing
9/23/21 AA 274A | Lecture 2 28
A Case Study
l Edge detection in camera image
l rqt_graph
9/23/21 AA 274A | Lecture 2 29
ROS Launch File for Edge Detection
<launch>
<arg name="video_device" default="/dev/video0" />
<include file="$(find aa274)/launch/usbcam_driver.launch">
<arg name="video_device" value="$(arg video_device)" />
</include>
<node name="image_view_1" pkg="image_view" type="image_view">
<remap from="image" to="/camera/image_color" />
<param name="autosize" value="true"/>
</node>
<node name="image_view_2" pkg="image_view" type="image_view">
<remap from="image" to="/edge_detection/image" />
<param name="autosize" value="true" />
</node>
<node name="edge_detection" pkg="opencv_apps" type="edge_detection">
<remap from="image" to="/camera/image_color" />
<param name="debug_view" value="false" />
</node>
</launch>
9/23/21 AA 274A | Lecture 2 30
Developing with ROS
l Catkin workspace: a directory that contains all your ROS development
l It sets the right environment variables
l It knows how to compile your nodes (using cmake which in turn uses
a compiler)
The commands you need to know:
l mkdir -p ~/catkin_ws/src
l cd ~/catkin_ws
l catkin_make
9/23/21 AA 274A | Lecture 2 31
ROS Packages
l The basic organization structure for your nodes
l Usually corresponds to a “functionality” (e.g. a SLAM package)
l Can contain code for multiple nodes
l Directory structure:
The command you need to know:
catkin_create_pkg <name> roscpp rospy std_msgs
9/23/21 AA 274A | Lecture 2 32
Debugging
l rospy.loginfo()
l rqt_console
l rosbag record <topic>
l rosbag play file.bag
l pdb – Python Debugger
l import pdb
l pdb.set_trace()
9/23/21 AA 274A | Lecture 2 33
Creating Custom Messages
l Write message definitions (.msg) that are language agnostic
l ROS generates the right files so that roscpp and rospy can use your
message
l rosmsg show student
[aa274/Student]:
string name_first
string name_last
uint8 age
uint32 grade
9/23/21 AA 274A | Lecture 2 34
ROS Services
l A different way for nodes to pass messages to each other
l Request/Response scheme (not Pub/Sub!)
l Examples:
l Turn a light or LED on or off
l Assign a name to a face and retrain face recognizer
l Spawn a new model in the Gazebo simulator
9/23/21 AA 274A | Lecture 2 35
The Parameter Server
l Parameters are stored under namespaces; e.g.
n /move_base/local_costmap/height
n /usb_cam/framerate
n /gazebo/time_step
l Setting and getting parameters:
l rosparam set param_name param_value
l param_value = rospy.get_param("param_name")
l NOTE: Setting a parameter does not affect a running node!
9/23/21 AA 274A | Lecture 2 36
Dynamic Reconfigure
l Some nodes provide dynamically changeable parameters
n rosrun rqt_reconfigure rqt_reconfigure
9/23/21 AA 274A | Lecture 2 37
URDF
l Universal Robot Description Format
l An XML file that describes the kinematic chain of your robot
9/23/21 AA 274A | Lecture 2 38
Gazebo
l Same code that will run in production
l Physics is mostly accurate
9/23/21 AA 274A | Lecture 2 39
Some more libraries you will hear about...
l TF: coordinate frame transform library
l Actionlib: processes with goals and feedback
l dynamic_reconfigure: making nodes configurable on the fly
9/23/21 AA 274A | Lecture 2 40
Getting help
l ROS wiki (http://wiki.ros.org/)
l Github
l Stack Overflow
l The Construct / Robot Ignite Academy
l Google :)
9/23/21 AA 274A | Lecture 2 41
Next time
• Motion control
9/23/21 AA 274A | Lecture 2 42