[Documentation] [TitleIndex] [WordIndex

Topics

topics

The topics object provides you access to all active ROS topics in your system. This object follows the general ROSH convention for converting ROS names, i.e. to access the topic /foo/bar/laser, you would use:

You can also use mapping-style lookups on topics, which is useful if you are dealing with string names:

TopicNS Instances

info(topics.foo)

str(topics.foo)

rostype(topics.foo)

rostype(obj, type)

topics.foo(msg)

topics.foo(key2=value2, key2=value2)

topics.foo(arg1, arg2...argN)

topics.foo[0]

topics.foo[1]

topics.foo[N]

topics.foo[:]

topics.foo[M:N]

topics.foo[M:N:step]

topics.foo = topics.bar

TopicInfo instances

topic.name

topic_info.pub_nodes

topic_info.sub_nodes

list(topic_info.pub_nodes)

list(topic_info.sub_nodes)

Examples

rostopic echo

Print every message (using standard Python slice syntax):

for m in topics.chatter[:]:
     print m

data: hello world 1275964384.53
data: hello world 1275964384.63
data: hello world 1275964384.73

Print every 10th message (using standard Python slice syntax):

for m in topics.yo[::10]:
     print m

data: hello world 1275964128.88
data: hello world 1275964350.23
data: hello world 1275964351.23

rostopic type/rosmsg show

Typing the name of the topic and hitting enter will show you the msg definition of the topic:

In [1]: topics.rosout
Out[1]: 
byte DEBUG=1
byte INFO=2
byte WARN=4
byte ERROR=8
byte FATAL=16
Header header
  uint32 seq
  time stamp
  string frame_id
byte level
string name
string msg
string file
string function
uint32 line
string[] topics

If you want to access the underlying message class, you can access the ._type or ._type_name fields, e.g.

In [5]: topics.rosout._type_name
Out[5]: 'roslib/Log'

In [6]: topics.rosout._type
Out[6]: <class 'roslib.msg._Log.Log'>

In [7]: topics.rosout._type()
Out[7]: 
header: 
  seq: 0
  stamp: 
    secs: 0
    nsecs: 0
  frame_id: ''
level: 0
name: ''
msg: ''
file: ''
function: ''
line: 0
topics: []

rostopic pub

Calling a method on a topic is equivalent to publishing to that topic. A common pattern is to print the msg definition of the topic first to familiarize yourself with the fields, then publishing your desired fields.

In [1]: topics.rosout
Out[1]: 
byte DEBUG=1
byte INFO=2
byte WARN=4
byte ERROR=8
byte FATAL=16
Header header
  uint32 seq
  time stamp
  string frame_id
byte level
string name
string msg
string file
string function
uint32 line
string[] topics

Followed by

In [2]: topics.rosout(msg='Hello There')

Republishing

for m in topics.chatter[:]:
     topics.chatter_echo(m)

Piping/Muxing

You can easily pipe from one topic to another. This creates a mux that can then be used for future reassignment.

topics.foo = topics.bar
topics.foo = topics.car

2024-11-30 17:52