Sie sind auf Seite 1von 3

ros Topics

Integrating ROS and MATLAB


By Peter Corke

T
he Robot Operating System has three main areas of functionality: rospublisher(’/MyTopic’,
(ROS) has gained wide curren- ROS integration, support for pose repre- msg);
cy for the creation of working sented as special Euclidean group (3) Alternatively, we could create a pub-
robotic systems, initially in the homogeneous transformations, and lisher object and optionally specify the
laboratory but now also in industry. probabilistic-road-map-based path message type
Despite ongoing evolution, the funda- planning. The remainder of this article
pub = r o s p u b l i s h e r ( ’ /
mental principles of publishing and will introduce the ROS functionality in a
M y T o p i c ’, ’ s t d _
subscribing on topics, application-spe- tutorial manner. Note that this function-
msgs/String’);
cific messages, invoking services, and ality is an evolution of Mathworks’ own
sharing parameters have remained con- ROS input/output package introduced and then invoke its send method
stant. The primary programming envi- in 2014, and the RST ROS application
pub.send(msg);
ronment for those working with ROS is program interface has some changes
C++ and Python, though using Java is with respect to this earlier package. Various options can be configured
also possible. Assuming the presence of a running at construction time for the publish-
MATLAB is a powerful tool for ROS system with an ROS master, we er object.
prototyping and simulating control initialize the MATLAB ROS subsystem Receiving a topic is just as easy. We
systems and robotics [1], [2], but, until with the IP address and port number of first create a subscriber object for the
very recently, it has not been easy to in- the ROS master, e.g., particular topic and optionally specify
tegrate with ROS. The need for such the message type
rosinit(’192.168.1.10’,
integration is evidenced by many
11311). sub = r ossubscriber(’/
solutions that have been developed, in-
M y T o p i c ’, ’ s t d _
cluding the JavaScript Object Nota- If no arguments are provided, then
msgs/String’).
tion-based rosbridge (http://wiki.ros. MATLAB will create an ROS master
org/rosbridge_suite), the Java-based and display its URI so that it can be The constructor has various options to
ROS-MATLAB bridge package used by other nodes. control buffer size and whether only the
(https://code.google.com/p/mplab-ros- Next, we want to publish on a topic; most recent message should be returned.
pkg/wiki/java_matlab_bridge), and the so let us take a simple example from the We read the next message on the topic by
ROSlab-IPC bridge (https://alliance. ROS tutorial. We first create a message
msg = sub.receive(),
seas.upenn.edu/meam620/wiki/index. object of the standard string type
php?n=Roslab.IpcBridge), among oth- which blocks until a message is re-
msg = r osmessage(’std_
ers. However, none of these have ceived, but we could also specify a time-
msgs/String’);
caught on in a big way, perhaps due to out interval in seconds
installation and usability concerns. and then set its value
With the recent release of MATLAB msg = sub.receive(5)
msg.Data = ’hello world’;
2015a, there is a better option available
through the newly introduced Robotics The message is an object, and its prop- An alternative to polling for mes-
Systems Toolbox Q (RST). This toolbox erties are hierarchical and match the sages is to establish a callback
fields of the message. We can read or sub = r
 ossubscriber(’/
write the properties directly without M y T o p i c ’, ’ s t d _
Digital Object Identifier 10.1109/MRA.2015.2418513
having to use setter or getter methods. m s g s / S t r i n g ’,
Date of publication: 18 June 2015 All that remains now is to publish it. @rxcallback)

18 • IEEE ROBOTICS & AUTOMATION MAGAZINE • june 2015


to the function and the service function is and then invoking the service
function rxcallback(src, f
unction resp = sumresp = call(sumclient,
msg) SumCallback(˜,req,resp) s u m r e q , ’ T i m e o u t ’, 3 )
disp([char(msg. r
esp.Sum = req.A + >> sumresp.Sum ans = 3.
Data()), sprintf(’\n req.B;
There is also the capability to
Message received: %s’,
and this can now be invoked from any read and write ROS bag files. First,
datestr(now))]);
ROS node we open the bag file and list the
which is invoked on every message available topics:
$ rosservice call /sum2 1
receipt.
2 sum: 3 bag = 
rosbag (’quad-2014-
Next, let us look at a more com-
06-13.bag’)bag.
plex message: the velocity twist with or from inside MATLAB by first creat-
AvailableTopics.
time stamp ing a service client
To extract all the images on the topic
msg = sumclient = 
rossvcclient
/ p r e v i e w , we use the s e l e c t
rosmessage(’geometry_ (’/sum’),
method to choose the particular
msgs/TwistStamped’),
creating a message with the numbers to topic, use readMessages to ex-
and we can view its definition be added tract a cell array of 100 messages
that happen to be of type s e n -
>> definition(msg) sumreq = r o s m e s s a g e
sormsgs/Image, and then convert
% A Twist with reference ( s u m c l i e n t );
coordinate frame and
this to a cell array of images that can
sumreq.A = 2;
timestamp std_msgs/Head-
be displayed
sumreq.B = 1,
er Header Twist Twist

or access one of its fields


msg.Twist.Linear.X = 0;
Custom messages are also possible
but beyond the scope of this article.
(See http://www.mathworks.com/
matlabcentral/fileexchange/49810 for
details.) The ROS parameters can be
accessed via a ParameterTree ob-
ject returned by
ptree = rosparam,
and we can use it to set, get, create, or
delete parameters in the ROS parame-
ter server.

ptree.get(’rosversion’)
ptree.set(’myparameter’, 23)

and parameters can have integer, log-


ical, char, double, or cell array types.
We can also access and create ser-
vices in MATLAB code. Inspired by
the TwoInts example given in the
ROS tutorial, we can easily create a
service to add two integers

sumserver = 
rossvcserver(’/
sum’, rostype.
roscpp_tutori-
als_TwoInts, @
SumCallback),

june 2015 • IEEE ROBOTICS & AUTOMATION MAGAZINE • 19


?

The Desired (X, Y) Position of the Robot


(Colored Blocks Indicate Tunable Parameters)
ROS Scope
IsNew
Subscribe IsNew
Msg
/odom
[-6 8] Desired Position
Subscribe Linear v
Desired Position Position X Velocity (v)
<X>
Position Y w
<Y> Angular
x Orientation (i) Velocity (w)
<X> Command Velocity
y Publisher
<Y> Yaw Proportional Controller
z
<Z> quat2eul
w The /odom Topic Represents Orientation as a
<W> Quarternion, So Convert It to a Yaw Angle
Conversion
Copyright 2014 The MathWorks, Inc.

Figure 1. A robot controller implemented in Simulink with ROS. (Image used with permission from MathWorks.)

preview = b
ag.select >> rosmsg list lished only after a message is received.
(’Topic’, ’pre- >> rosmsg show geometry_ With the appropriate Simulink settings,
view’) msgs/TwistStamped this controller can run in “real time” (see
subset = p
review.readMes- >> rosnode list the aforementioned caveats), and the
sages(500:599) >> rostopic list Scope blocks allow us to conveniently
images = c
ellfun(@readIm- >> rosparam list see what is happening. We can, of course,
age, all, ’Uni- >> rosservice list. log signals to workspace variables for later
formOutput’, This provides all the programmatic analysis and graphical display.
false); tools required to write code in MAT- Finally, and most significantly, we can
LAB that can fully participate in an export this diagram as code. Simulink
We could also extract inertial mea- ROS-based robot control system. A generates a .tgz archive file that con-
surement unit (IMU) data and place powerful advantage of MATLAB ROS is tains all the code necessary to build a
the x-axis translational and z-axis an- its platform independence—this code standalone real-time ROS node on a
gular velocity, for example into a will work on a Mac, Windows, or Linux Linux system.
timeseries object, which automati- system. The real-time update rate is, of In this short article, we can only skim
cally picks up the message time stamps course, going to depend on the size of the surface of the new MATLAB capa-
and has various methods for analysis the messages, the complexity and effi- bility for ROS integration. More details
and display. ciency of your MATLAB code, and the can be found at mathworks.com/ros.
performance of your computer, but tens Over time, the RST will gain additional
imu = b
 ag.select(’Topic’,
of hertz is feasible. functionality that will allow students, en-
’fcu/imu’);
An alternative to programmatic im- gineers, and researchers to create robotic
ts = i
mu.timeseries(’Linea
plementation is to use the Simulink block systems more quickly and leverage the
r A c c e l e r a t i o n . X ’,
diagram modeling environment, as large and mature ROS code base.
’AngularVelocity.Z’);
shown in Figure 1. An RST provides a
ts.plot
palette of blocks that includes Publish References
A number of interactive com- and Subscribe. The Msg output of the [1] P. I. Corke, Robotics, Vision and Control: Fun-
mands are available in the MATLAB Subscribe block is a bus type, and we damental Algorithms in MATLAB. Berlin Heidel-
environment, which mimics the ROS can use a Simulink Bus Selector to pull berg, Germany: Springer-Verlag, 2011.
command line utilities to find mes- out the particular message fields in which [2] P. Corke. Robotics toolbox for MATLAB. [On-
sages, nodes, topics, parameters, or we are interested. We use a triggered sub- line]. Available: http://www.petercorke.com/robot
services, e.g., system to ensure that a message is pub- 

20 • IEEE ROBOTICS & AUTOMATION MAGAZINE • june 2015

Das könnte Ihnen auch gefallen