PROGRAMMING FOR ROBOTICS
ROS
Contents
• ROS Package structure
• Integration and programming
• ROS C++ client library
• ROS subscriber and publishers
• ROS parameter server
• Rviz visualization
Robot Operating System
ROS Packages
• ROS software is organized into
packages, which can contain source
code, launch files, message
definitions, data, and documentation
• A package that builds up on/requires
other packages
To create a new package, use
> catkin_create_pkg package_name
{dependencies}
http://wiki.ros.org/Packages
Robot Operating System
ROS Packages (package.xml)
• The package.xml file defines
the properties of the package
• Package name
• Version number
• Authors
• Dependencies on other
packages
• ….
http://wiki.ros.org/catkin/package.xml
Robot Operating System
ROS Packages (CMakeLists.txt)
• The CMakeLists.txt is input to the Cmakebuild system
• Required Cmake Version (cmake_minimun_required)
• Package Name (project())
• Find other Cmake/Catkin packages needed for build (find_package())
• Message/Service/Action Generators (add_message_files(), add_service_files(),
add_action_files())
• ….
http://wiki.ros.org/catkin/CMakeLists.txt
Robot Operating System
ROS Packages (CMakeLists.txt)
• Use the same name as in the package.xml
• List the packages that your package
requires to build (have to be listed in
package.xml
• Specify locations of of header files
http://wiki.ros.org/catkin/CMakeLists.txt
Robot Operating System
Setup a Project in Visual Code
Robot Operating System
ROS C++
#include <ros/ros.h> • ROS main header file include
int main(int argc, char** argv) • ros::init(…) has to be called before calling other ROS
{ functions
ros::init(argc, argv, "hello_world"); • The node handle is the access point for
ros::NodeHandle nodeHandle; communications with thenROS system (topics,
ros::Rate loopRate(10); services, parameters)
unsigned int count = 0; • ros::Rate is a helper class to run loops at a desired
while (ros::ok()) { frequency
ROS_INFO_STREAM("Hello World " << count); • ros::ok() checks if a node should continue running.
ros::spinOnce(); Returns false if SIGINT is received (Ctrl + C) or
loopRate.sleep(); ros::shutdown() has been called
count++; • ROS_INFO() logs messages to the filesystem
} • ros::spinOnce() processes incoming messages via
return 0; callbacks
}
http://wiki.ros.org/roscpp
http://wiki.ros.org/roscpp/Overview
Robot Operating System
ROS C++ (Node Handle)
• There are four main types of node handles
1. Default (public) node handle:
nh_ = ros::NodeHandle();
2. Private node handle:
nh_private_ = ros::NodeHandle(“~”);
3. Namespaced node handle:
nh_robot_ = ros::NodeHandle(“robot”);
4. Global node handle:
nh_global = ros::NodeHandle(“/”);
http://wiki.ros.org/roscpp/Overview/NodeHandles
Robot Operating System
ROS C++ (Subscriber)
#include "ros/ros.h"
• Start listening to a topic by calling the method #include "std_msgs/String.h"
subscribe() of the node handle void chatterCallback(const
std_msgs::String::ConstPtr& msg)
> ros::Subscriber subscriber = {
ROS_INFO("I heard: [%s]", msg-
nodeHandle.subscribe(topic, queue_size,
>data.c_str());
callback_function); }
int main(int argc, char **argv)
• When a message is received, callback function is
{
called with the contents of the message as argument ros::init(argc, argv, "listener");
• Hold on to the subscriber object until you want to ros::NodeHandle node;
unsubscribe ros::Subscriber subscriber =
• ros::spin() processes callbacks and will not return node.subscribe("chatter",
until the node has been shutdown 10, chatterCallback);
ros::spin();
return 0;
}
http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers
Robot Operating System
ROS C++ (Publisher)
#include <ros/ros.h>
• Create a publisher with help of the node handle #include <std_msgs/String.h>
int main(int argc, char **argv) {
ros::init(argc, argv, "talker");
ros::NodeHandle nh;
> ros::Publisher publisher = ros::Publisher chatterPublisher =
nodeHandle.advertise<message_type>(topic, nh.advertise<std_msgs::String>("chatter"
queue_size); , 1);
ros::Rate loopRate(10);
unsigned int count = 0;
while (ros::ok()) {
std_msgs::String message;
• Publish the contents with message.data = "hello world " +
std::to_string(count);
ROS_INFO_STREAM(message.data);
chatterPublisher.publish(message);
> publisher.publish(message); ros::spinOnce();
loopRate.sleep();
count++;
}
return 0;
}
http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers
Robot Operating System
ROS Parameter server
• Nodes use the parameter server to store and retrieve camera:
parameters at runtime left:
• Best used for static data such as configuration name: left_camera
parameters exposure: 1
right:
• Parameters can be defined in launch files or separate
name: right_camera
YAML files exposure: 1.1
List all parameters
> rosparam list
Get the value of a parameter
<launch>
<node name="name" pkg="package" type="node_type">
> rosparam get param_name
<rosparam command="load"
file="$(find package)/config/config.yaml" />
Set the value of a parameter </node>
</launch>
> rosparam set param_name value
http://wiki.ros.org/rosparam
Robot Operating System
ROS Parameter server (C++ API)
• Get a parameter in C++ with
> nodeHandle.getParam(para_name, variable)
• Method returns true if parameter was found, false ros::NodeHandle nodeHandle("~");
otherwise std::string topic;
if (!nodeHandle.getParam("topic", topic)) {
ROS_ERROR("Could not find topic
parameter!");
}
• For parameters, typically use the private node handle
ros::NodeHandle("~")
http://wiki.ros.org/roscpp/Overview/NodeHandles
Robot Operating System
RViz
• 3D visualization tool for ROS
• Subscribes to topics and visualizes the
message contents
• Different camera views (orthographic, topdown, etc.)
• Interactive tools to publish user information
• Save and load setup as RViz configuration
• Extensible with plugins
> rosrun rviz rviz
http://wiki.ros.org/rviz
Robot Operating System
RViz
Display Plugins
Robot Operating System
RViz
Visualizing Point Clouds Example
Robot Operating System
Further References
• Ros Wiki • ROS Cheat Sheet
• http://wiki.ros.org/ • https://github.com/ros/cheatsheet/r
• Installation eleases/dow
• http://wiki.ros.org/ROS/Installa nload/0.0.1/ROScheatsheet_catkin.
tion pdf
• Tutorials • ROS Best Practices
• http://wiki.ros.org/ROS/Tutoria • https://github.com/ethzasl/ros_best
ls _practices/wiki
• Available packages • ROS Package Template
• http://www.ros.org/browse/ • https://github.com/ethzasl/ros_best
_practices/tree/master/ros_packag
e_template
Robot Operating System
Q&A
Robot Operating System