You are on page 1of 15

EE50237 – Robotics

Software
Dr Rob Wortham

Lecture 4b - Developing ROS Nodes #2

Note: This lecture is being recorded using University of Bath Panopto

22/01/2024 EE50237 Robotics Software - Lecture 4b 1


Today’s Lecture

• ROS Message Types


• ROS Launch Files

22/01/2024 EE50237 Robotics Software - Lecture 4b 2


ROS Message Types
• ROS provides the infrastructure to simply create you own message types.
• However, ROS already ships with many common message types.
• Number 1 Rule: Don’t create a new message if you can use an existing one.
• ROS common message documentation: http://wiki.ros.org/common_msgs
• diagnostic_msgs (5)
• geometry_msgs (29)
• nav_msgs (9)
• sensor_msgs (28)
• Common messages are a vital mechanism to support interoperability and
code reuse.
• The common message structures provide valuable design input for system
architecture and node design.

22/01/2024 EE50237 Robotics Software - Lecture 4b 3


ROS Common Messages: An Example
File: sensor_msgs/Range.msg
Raw Message Definition
# Single range reading from an active ranger that emits energy and reports
# one range reading that is valid along an arc at the distance measured.
# This message is not appropriate for laser scanners. See the LaserScan
# message if you are working with a laser scanner.

#
#
This message also can represent a fixed-distance (binary) ranger. This
sensor will have min_range===max_range===distance of detection.
Compact Message Definition:
# These sensors follow REP 117 and will output -Inf if the object is detected
# and +Inf if the object is outside of the detection range.
uint8 ULTRASOUND=0
uint8 INFRARED=1
Header header # timestamp in the header is the time the ranger
# returned the distance reading

# Radiation type enums


# If you want a value added to this list, send an email to the ros-users list
uint8 ULTRASOUND=0
std_msgs/Header header
uint8 INFRARED=1 uint8 radiation_type
uint8 radiation_type # the type of radiation used by the sensor float32 field_of_view
# (sound, IR, etc) [enum]
float32 min_range
float32 field_of_view #
#
the size of the arc that the distance reading is
valid for [rad]
float32 max_range
# the object causing the range reading may have float32 range
# been anywhere within -field_of_view/2 and
# field_of_view/2 at the measured range.
# 0 angle corresponds to the x-axis of the sensor.

float32 min_range # minimum range value [m]


float32 max_range # maximum range value [m]
# Fixed distance rangers require min_range==max_range

float32 range # range data [m]


# (Note: values < range_min or > range_max
# should be discarded)
# Fixed distance rangers only output -Inf or +Inf.
# -Inf represents a detection within fixed distance.
# (Detection too close to the sensor to quantify)
# +Inf represents no detection within the fixed distance. http://docs.ros.org/api/sensor_msgs/html/msg/Range.html
# (Object out of range)
22/01/2024 EE50237 Robotics Software - Lecture 4b 4
Defining and Using ROS Messages - Primitives

http://wiki.ros.org/msg

22/01/2024 EE50237 Robotics Software - Lecture 4b 5


Defining and Using ROS Messages - Field Types
Field types can be:
• A built-in type, such as "float32 pan" or "string name"
• Names of Message descriptions defined on their own, such as "geometry_msgs/PoseStamped"
• Fixed or variable length arrays (lists) of the above, such as "float32[] ranges" or "Point32[10] points"
• The special Header type, which maps to std_msgs/Header - Provides a general mechanism for setting
frame IDs for libraries like tf. Commonly used and has special semantics, see http://wiki.ros.org/msg

An Example
File name, put this file in
{package_directory}/msg Complex.msg

float32 real
float32 imaginary

See: http://wiki.ros.org/msg

22/01/2024 EE50237 Robotics Software - Lecture 4b 6


Defining and Using ROS Messages – Build and Execution

1. package.xml - Tell ROS about dependencies for this package, specifically that we are defining our own message types.

Show all lines in package.xml that


contain ‘message_’

• build_depend – dependencies when code is built i.e. what does catkin need to be able to build the package?
• exec_depend – dependencies when code is executed i.e. what must be present in the package, or externally within ROS
• build_export_depend – advanced use, to build libraries (.so) from your ROS code. Not needed for simple ROS nodes.

2. CMakeLists.txt - Tell the catkin build system to generate code to represent, marshal and un-marshal the message.
This requires several changes within CMakeLists.txt – see next slide
O’Reilly Ch3 P41
22/01/2024 EE50237 Robotics Software - Lecture 4b 7
Defining and Using ROS Messages – CMakeLists.txt
Tell catkin to look for message find_package(catkin REQUIRED COMPONENTS
generation files (*.msg) roscpp
rospy
std_msgs
Tell catkin that at runtime we will be message_generation
using our own messages. There will be )
other things in this section catkin_package(
CATKIN_DEPENDS message_runtime
)
Tell catkin which message files to add_message_files(
compile FILES
Complex.msg
)
Tell catkin about the dependency on generate_messages(
std_msgs DEPENDENCIES
std_msgs
)

22/01/2024 EE50237 Robotics Software - Lecture 4b 8


Defining and Using ROS Messages – Publisher Node in Python

Import our new message type from basics.msg import Complex

from random import random

When we create the publisher, tell it rospy.init_node('message_publisher')


what message type it will publish
pub = rospy.Publisher('complex', Complex)

Create an instance of a Complex rate = rospy.Rate(2)


message
while not rospy.is_shutdown():
msg = Complex()
Set the elements in the message msg.real = random()
msg.imaginary = random()

pub.publish(msg)
rate.sleep()

22/01/2024 EE50237 Robotics Software - Lecture 4b 9


ROS Launch Files
• rosrun only starts one node – like this: rosrun package_name node_executable_name [{remap args}]
• Sometimes we have many nodes in a system
• We want to start them all together, perhaps with other programs, like rqt_graph
• Why?
• We can ensure we start the system in the same way each time i.e. consistently
• Other users can start the system easily
• We need to do this a lot! i.e. frequently
?
• roslaunch starts a whole bunch of things, specified in a launch file

like this: roslaunch package_name launch_file_name

Launch files are in


{package_name}/launch

22/01/2024 EE50237 Robotics Software - Lecture 4b 10


ROS Launch Files – XML File Format
Note: roslaunch will automatically start roscore if it is not already running.
A simple example:

<launch>
<node name="talker" pkg="basics" type="message_publisher.py" />
<node name="listener" pkg="basics" type="message_subscriber.py"
output="screen"/>
</launch>

22/01/2024 EE50237 Robotics Software - Lecture 4b 11


ROS Launch Files – Another Example
A more complex example:
<launch>
<node name="talker" pkg="basics" type="message_publisher.py">
<remap from="complex" to="complex_number"/>
</node>
<node name="listener1" pkg="basics" type="message_subscriber.py" output="screen">
<remap from="complex" to="complex_number"/>
</node>
<node name="listener2" pkg="basics" type="message_subscriber.py" output="screen">
<remap from="complex" to="complex_number"/>
</node>
</launch>

22/01/2024 EE50237 Robotics Software - Lecture 4b 12


ROS Launch Files – Further Reading & Assessment

• Launch files can contain many options - see


• http://wiki.ros.org/roslaunch/XML
• http://wiki.ros.org/roslaunch/XML/node

• Your assessment must include a launch file to launch all the ROS components needed
• You must start your robot with a single command -> launch from a launch file

22/01/2024 EE50237 Robotics Software - Lecture 4b 13


Reading
Essential Reading from Programming Robots with ROS,
Quigley, 2015:

• Chapter 2: Preliminaries : roslaunch

• Chapter 3: Topics : Defining your own message types

22/01/2024 EE50237 Robotics Software - Lecture 4b 14


Today’s topics were:

• User defined message types


• Using a launch file

Questions?
Moodle!

22/01/2024 EE50237 Robotics Software 15

You might also like