ROS: Robot Operating System
EEL 4930/5934: Autonomous Robots
Spring 2023
Md Jahidul Islam
Lecture 2
ROS: Robot Operating System
⇒ A middleware “OS” for robotics
● Open source software packages
○ Components + Tools + Interfaces
● For general-purpose robot programming + hw/sw interfacing
○ Actuators: things that move
○ Sensors: things that read the world
○ Control system: robots brain (AI functions!)
● Works best with linux distributions
● Visit ros.org for an introduction
EEL 4930/5934: Autonomous Robots
ROS: Getting Started
⇒ Install ROS melodic or noetic (ROS 1)
● Preferred: Linux laptops or Raspberry PI or Jetson Nanos
● Follow the instructions:
○ Getting started: https://www.ros.org/blog/getting-started/
○ Installation: https://wiki.ros.org/ROS/Installation
● Make sure to install the correct distribution for your platform
⇒ ROS2 documentation: https://docs.ros.org/
⇒ Learn basic ROS functionalities
● ROS Noetic tutorials by Robotics Back-End
● ROS Noetic tutorials by Emil Vidmark
● ROS2 Humble tutorials by Robotics Back-End
● Or browse any other resources!
3
EEL 4930/5934: Autonomous Robots
Lecture Outline
⇒ ROS backgrounds ⇒ Bagging: Saving and playing data
● Managing topics and data formats
⇒ Installation (Noetic / Melodic)
● ROS nodes, services, topics, packages ⇒ RViz: ROS visualizer
● ROS topics (how to subscribe and how to publish) ● Simulator packages and interfaces
⇒ Working in Catkin workspaces ⇒ Case study and HH1
● How to create, build, and run. ● Capturing webcam video with ros node
● Examples: listener/talker, turtlesim ● Draw face bounding box
● Running ROS packages in command line ● Publish image topic
○ Using rosrun ● Use image_view or RViz for visualization
○ Using roslaunch ● Use rosrun/roslaunch
EEL 4930/5934: Autonomous Robots
ROS: Structure
https://www.ros.org/
= + + +
EEL 4930/5934: Autonomous Robots
Life Before ROS
⇒ Lack of standards
⇒ Little code reusability
● Keeping reinventing (or rewriting) device drivers
● Inter-process communication protocols
● Standard algorithms
⇒ New robot in the lab (or in the factory)
● Start re-coding (mostly) from scratch
EEL 4930/5934: Autonomous Robots
ROS: History
⇒ Originated by a grad student at Stanford AI Lab in 2007.
⇒ Taken up and developed by Willow Garage
○ A now defunct, but influential, robotics start-up
○ Probably the driving influence behind ROS adoption
⇒ 2013: supported by the Open Source Robotics Foundation (OSRF)
○ https://www.openrobotics.org/
○ Some Caltech Alums work for/with the foundation
⇒ A series of “releases” define different generations of ROS
⇒ Read more details here: https://www.theconstructsim.com/history-ros/
EEL 4930/5934: Autonomous Robots
ROS: Distributions
⇒ A versioned set of ROS Packages:
Grey: unsupported release (End of Life)
Green: supported release
● Like a Linux distribution
● Provide a relatively stable codebase for development
● Primarily for core ROS components
○ User contributed packages must make their own updates
⇒ Which distribution to use:
● Noeitc Ninjemys is the final release of ROS 1 by Open Robotics
● Future ROS releases will all be based on ROS 2
(visit index.ros.org Releases page) 8
EEL 4930/5934: Autonomous Robots
Tools To Know For A Roboticist
EEL 4930/5934: Autonomous Robots
ROS: Architecture
10
EEL 4930/5934: Autonomous Robots
ROS1: Architecture
⇒ Low-level device abstraction
○ Joystick
○ GPS
○ Camera
○ Controllers
○ Laser Scanners
○ …
⇒ Application building blocks
○ Coordinate system transforms
○ Visualization tools
○ Debugging tools
○ Robust navigation stack (SLAM)
○ Arm path planning
○ Object recognition
○ ...
11
EEL 4930/5934: Autonomous Robots
ROS: Philosophy
● Peer to Peer
○ ROS systems consist of many small programs (nodes)
○ Nodes connect to each other and exchange messages
● Tools-based
○ There are many small, generic programs that perform tasks
○ Such as visualization, logging, plotting data streams, etc.
● Multi-lingual
○ ROS software modules can be written in any language
○ Currently client libraries: C++, Python, LISP, Java, JavaScript, MATLAB, Ruby
● Thin
○ The ROS conventions encourage contributors to create stand-alone libraries/packages and then
wrap those libraries so they send and receive messages to/from other ROS modules.
● Free and open source, community-based, repositories
12
EEL 4930/5934: Autonomous Robots
ROS Installation: Linux
⇒ Check your Ubuntu version first:
Open the terminal and type the following command:
$ lsb_release -a
⇒ ROS Noetic:
● Primarily targeted at the Ubuntu 20.04 (Focal)
● Follow the installation instruction and reference
video to install ROS Noetic step by step
⇒ ROS Melodic:
● Primarily targeted at the Ubuntu 18.04 (Bionic)
● Follow the installation instruction to install ROS
Melodic step by step
Example of installing Noetic on ubuntu 20.04
13
EEL 4930/5934: Autonomous Robots
ROS Nodes
● Single purpose, executable program
○ Can contain many functions, can call other nodes
○ Can subscribe and/or publish topics
● Nodes are assembled into a graph (via communication links)
○ Communication via topics or with a service or with a parameter server
● Example: sensor or actuator driver, control loop, motion planning module
● Programming: Nodes are developed with the use of a ROS client library
○ roscpp – C++ programs rospy – python programs
14
EEL 4930/5934: Autonomous Robots
ROS Master
⇒ Master: Matchmaker between nodes
● Nodes make be on different cores, different computers, different robots, even different networks.
● This should be transparent to each node’s code
● The “master” service runs on one machine
○ It provides name registration & lookup of nodes and services
● roscore starts the master server, parameter server, and logging processes (if any)
● Every node connects to the master at start-up to register details of the message streams that it publishes
● Also determine its connectivity with the rest of the computation graph via its subscriptions
15
EEL 4930/5934: Autonomous Robots
ROS Topics
⇒ Topic: A name for a data stream (TCP or UDP)
● A message bus over which nodes exchange messages
● Example: lidar can be the topic that a robot’s on-board LiDAR uses to communicate its sensor data
○ The data could be raw, or it could be preprocessed by the lidar sensor node
○ It can send data once, or repeatedly
● Topics are best for unidirectional, streaming communication.
● A request/response model is handled by a service. Fixed data is handled by a parameter server.
● Topic statistics: age of data, traffic volume, # dropped messages
● Publishing topics: 1-to-N communication model
● Subscribing to topics:
○ Ros Node receives access to the data (bus) published under that topic name
16
EEL 4930/5934: Autonomous Robots
Example: Listener / Talker
Open four terminals, run the following commands in order:
Terminal_1:$ roscore
# roscore start ROS and create the Master so that nodes can communicate
Terminal_2:$ rosrun rospy_tutorials talker
# The rosrun command takes the arguments [package name] [node name]
# The "talker" node will broadcast a message on topic "chatter"
Terminal_3:$ rosrun rospy_tutorials listener
# The "listener" node will receive and print that message
Terminal_4:$ rqt_graph
# rqt_graph provides a GUI plugin for visualizing the ROS computation graph
17
EEL 4930/5934: Autonomous Robots
Example: Listener / Talker
The application can be divided into two nodes:
● Talker node: responsible of creating the
message “Hello World”
● Listener node: subscribes to the talker topic
and thus receive the messages sent it
18
EEL 4930/5934: Autonomous Robots
Example: Turtlesim
Open four terminals, run the following commands in order:
Terminal_1:$ roscore
Terminal_2:$ rosrun turtlesim turtlesim_node
# This node creates the screen image and the turtle
Terminal_3:$ rosrun turtlesim turtle_teleop_key
# This node allows keyboard control of the turtle
Terminal_4:$ rqt_graph
ROS computation graph provided by rqt_graph
19
EEL 4930/5934: Autonomous Robots
Example: Turtlesim
ROS computation graph provided by rqt_graph
20
EEL 4930/5934: Autonomous Robots
ROS Packages
⇒ Packages: Basic organizational unit of ROS
● Contains one or more nodes
● Provides a ROS interface (via messages, services)
● Typically implements a well defined function
○ Example: making a map from sensory data
● Organized into a self-contained directory (specific structure)
○ Contains source code for nodes
○ Message definitions, services, etc
21
EEL 4930/5934: Autonomous Robots
Catkin Workspace
⇒ Catkin workspace:
● A set of directories in which a set of related ROS
code/packages live
○ Catkin ~ ROS build system
○ CMake + Python scripts
● It’s possible to have multiple workspaces
○ Only one-at-a-time can be active
● A ROS package is a directory inside a catkin
workspace that has a package.xml file in it
22
EEL 4930/5934: Autonomous Robots
Catkin Workspace
23
EEL 4930/5934: Autonomous Robots
Setup A Catkin Workspace
⇒ Create and setup a Catkin workspace: ⇒ Catkin workspace folders:
● Follow the CreateWorkspace Tutorial and reference ● Source space: workspace_folder/src
video to create and setup Catkin workspace ● Build space: workspace_folder/build
● Development space: workspace_folder/devel
● Install space: workspace_folder/install
Example of Catkin workspace setup
24
EEL 4930/5934: Autonomous Robots
Create ROS Package In Catkin
⇒ Create a ROS package:
● Follow the CreatingPackage Tutorial and reference
video to create ROS package in Catkin workspace
● Useful command:
$ catkin_create_pkg <package_name> [depend]
The created package
Example of package creation 25
EEL 4930/5934: Autonomous Robots
Most Useful Commands
$ roscore
# roscore command start ROS and create the Master so that nodes can communicate
$ rosrun <package_name> <node_name>
# rosrun command allows you to run an executable in an arbitrary package from anywhere
$ roslaunch <package_name> <file.launch>
# Many ROS packages come with “launch files”, roslaunch command reads the .launch/XML format
$ rqt_graph
# rqt_graph command provides a GUI plugin for visualizing the ROS computation graph
$ rosnode info/kill/list/machine/ping/cleanup
# rosnode command can display debug information about ROS Nodes, including publications, subscriptions and connections
$ rostopic info/list/echo/type/pub/bw/delay/find
# rostopic command can display debug information about ROS Topics, including publishers, subscribers, publishing rate, and ROS Messages
26
EEL 4930/5934: Autonomous Robots
Hands-on Case Study: Camera Interfacing
Screen
USB Camera
Mouse
Raspberry PI
(with ros installed)
⇒ Use your own ROS system (PC, Jetson nano, PIs, etc.) with any USB camera.
⇒ You can also use your laptop’s built-in webcam (device id: 0) for this!
27
EEL 4930/5934: Autonomous Robots
Catkin Workspace And Terminals
28
EEL 4930/5934: Autonomous Robots
Create ROS Package In Catkin
Create a new package and corresponding scripts and launch folder
package name dependencies
Your package folder should look like this (for Python; use roscpp instead of rospy for C++)
29
EEL 4930/5934: Autonomous Robots
Build Package
Install cv-bridge (for OpenCV; if not installed already) ROS version
Dependent package
Build the workspace with your new empty package
Make the workspace visible to the file system (Linux way)
Try to find your package that you just created
30
EEL 4930/5934: Autonomous Robots
Check The USB Camera
Plug and check if camera was recognized by system
Before plugging in the camera
After plugging in the camera
The usb camera
31
EEL 4930/5934: Autonomous Robots
Install usb_cam Node
Install the usb_cam package (ie, camera driver)
Check where the packages get installed!
32
EEL 4930/5934: Autonomous Robots
Check The Launch File
usb_cam package comes with a sample test launch file
33
EEL 4930/5934: Autonomous Robots
Start roscore
Before run the launch file, start roscore on one of the terminal
● Keep roscore running
● Check topics on another terminal before starting usb_cam
Terminal 1
Terminal 3
34
EEL 4930/5934: Autonomous Robots
Initiate Camera
Now start usb_cam with roslaunch on a new terminal
The image view window will be displayed
35
EEL 4930/5934: Autonomous Robots
Get The Image Topics
Keep roslaunch running, check topics after starting usb_cam
Terminal 2
Terminal 3
Before roslaunch
After roslaunch
image_view topics
usb_cam topics
36
EEL 4930/5934: Autonomous Robots
Check The Graph!
Keep roslaunch running, check ROS computational graph
Terminal 2, keep roalaunch running
Terminal 3, check rqt_graph
37
EEL 4930/5934: Autonomous Robots
Copy-Paste Magics
Go to the launch folder of the new package, create a new launch file
Copy the content of the usb_cam node from the usb_cam launch file to the new launch file, and save
38
EEL 4930/5934: Autonomous Robots
Check Image Topics In rqt_image_view
Run the new launch file (notice that we only copied one node, to initiate the camera)
● When you see the image topics (rostopic list), you can view those using rqt_image_view
Terminal window of roslaunch,
Keep it running
Run rqt_image_view in another terminal
Camera data provided by rqt_image_view
39
EEL 4930/5934: Autonomous Robots
Checking Rostopics
Terminal 2, keep roslaunch running
Terminal 3, keep rqt_image_view running
Terminal 4, check rostopic
Before roslaunch
After roslaunch
40
EEL 4930/5934: Autonomous Robots
Check rqt_graph
Terminal 2, keep roalaunch running
Terminal 3, keep rqt_image_view running
Terminal 4, check rqt_graph
41
EEL 4930/5934: Autonomous Robots
HH1: Hands-on Homework #1
42
EEL 4930/5934: Autonomous Robots
HH1 Logistics
Check the HH1 assignment: HH1_AuRo.pdf in Canvas
43
EEL 4930/5934: Autonomous Robots
ROS Message Types
See http://wiki.ros.org/sensor_msgs
● Most commonly used ones
○ Image, CameraInfo, LaserScan, Range
○ Joy, Imu, PointCloud, PointCloud2
Interfacing sensor messages
● Check the data structure syntaxes from ROS wiki
● Conform / adjust (ie, wrap) data for later use
● See example codes!
Use case: how to get image from camera sensor topic
to OpenCV (as Numpy array)?
44
EEL 4930/5934: Autonomous Robots
ROS CVBridge
CvBridge is a ROS library
● Provides an interface between ROS and OpenCV
● Converts ROS image messages to OpenCV images
○ CvBridge().imgmsg_to_cv2
● Also converts ROS image messages to OpenCV images
○ CvBridge().cv2_to_imgmsg
● Various encoding is available
○ read more on the wiki
Subscribe:
imCV = CvBridge() .imgmsg_to_cv2( ros_msg, "bgr8")
Publish:
ros_msg = CvBridge().cv2_to_imgmsg( imCV, encoding="bgr8")
45
EEL 4930/5934: Autonomous Robots
Sample Code!
46
EEL 4930/5934: Autonomous Robots
How TO Detect Face In OpenCV?
● Read an image (grayscale mode) file given the path
○ image = cv2.imread(img_path, 0) #grayscale-mode
● Load the cascade classifier model
○ faceCascade = cv2.CascadeClassifier(cascade_path)
● Detect faces
○ faces = faceCascade.detectMultiScale(image,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
● Detect bounding boxes on the image
○ for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h),
color = (0, 255, 0),
thickness = 2
)
47
EEL 4930/5934: Autonomous Robots
Viola-Jones Concept
The famous Viola-Jones Algorithm
● Works with frontal face images with visible
○ Eyes and eyebrows, nose, and lips.
○ Symmetry and positioning of facial features
● Uses Haar features (see this)
● Calculates pixel features with different window sizes
● Then it finds the best features using Adaptive Boosting
(Adaboost) an ML algorithm. See this for more
information.
● Then uses a cascade of classifiers to identify the
presence of each features.
● The accumulated scores gives the final result.
https://medium.datadriveninvestor.com/how-the-facial-detection-algo
rithms-work-viola-jones-algorithm-and-opencv-bd694936512f
48
EEL 4930/5934: Autonomous Robots
ROS Bagging:
Useful Bag Tools
● rosbag: unified console tool for recording,
playback, and other operations.
● rqt_bag: graphical tool for visualizing bag file data.
● rostopic: the echo and list commands are
compatible with bag files.
Example commands
● rosbag record rosout tf cmd_vel
● rosbag play recorded.bag
See more at
● http://wiki.ros.org/Bags
● http://wiki.ros.org/rosbag/Commandline
49
EEL 4930/5934: Autonomous Robots