ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

looping through a custom message in ROS Kinetic

asked 2020-02-07 19:43:04 -0600

mustafa404 gravatar image

updated 2020-02-08 04:02:38 -0600

gvdhoorn gravatar image

I created a custom message and it has string array(can be called a list) As I need to iterate through the string array, there is no built in dir that helps with it. Whenever I try to run a for loop on it, the error says "Custom_Message object is not iterable. Also I can not use any split or iter stuff on it since its not iterable. Any idea how to iterate through a custom message.

My Custom_Message file is like this:

string[] custom_msg

The coding environment is python.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-02-08 04:05:54 -0600

gvdhoorn gravatar image

updated 2020-02-08 04:12:40 -0600

Whenever I try to run a for loop on it, the error says "Custom_Message object is not iterable.

Plain message objects are indeed not iterable.

Also I can not use any split or iter stuff on it since its not iterable.

Again, expected.

Any idea how to iterate through a custom message.

My Custom_Message file is like this:

string[] custom_msg

The iterable here is custom_msg, not the message object itself (it's unfortunate you've given your message type the same name as the only field it contains, that makes this less obvious).

So to iterate over the list contained in custom_msg (note: the field, not the message object itself), you'd do something like this (in a callback called my_callback(..) and the only argument it gets is a Custom_Message instance called msg):

def my_callback(msg):
    for the_msg in msg.custom_msg:
        # do something with 'the_msg' here

Finally: I'd recommend giving a better name to your message type. Custom_Message does not convey any information as to what sort of information it contains, and also does not give any clues as to how to interprete the data in it. custom_msg as a field name is of course also not really a good idea.

Try to use the field name to give information on what sort of messages are in there.

edit flag offensive delete link more

Comments

Note that this is not a ROS "thing" really, more likely a misunderstanding how to deal with message data and especially in Python.

What could be confusing is the fact that your calback receives a message instance, not just the custom_msg field. The message contains the field, but it's not the same entity.

gvdhoorn gravatar image gvdhoorn  ( 2020-02-08 04:06:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-02-07 19:43:04 -0600

Seen: 2,278 times

Last updated: Feb 08 '20