Ask Your Question
0

how does one index array in rospy?

asked 2020-08-18 10:46:36 -0500

gbohus gravatar image

I need the size of a custom array msg type. (The type is geometry_msgs/PoseStamped[] PoseStampedArray.) I need to iterate on the msg if it is not empty and I would very much like to get the last element without iterating on the whole thing.

I must be missing something because these look like basic things one should be able to do.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2020-08-18 11:53:02 -0500

gvdhoorn gravatar image

updated 2020-08-19 03:04:06 -0500

I need to iterate on the msg if it is not empty and I would very much like to get the last element without iterating on the whole thing.

You ask about rospy, which would make this Python.

As you can see on wiki/msg (which documents the ROS 1 msg IDL and how it gets mapped to various programming languages), a field of type geometry_msgs/PoseStamped[] will be mapped to a Python tuple (ie: a tuple of geometry_msgs/PoseStamped instances, as many as were put in the array byt the sender).

For your use you can just consider that a list, and accessing the last element of a list can be done as follows in Python (my_msg is an instance of a message with pose_list being a field of type geometry_msgs/PoseStamped[]):

last_element = my_msg.pose_list[-1]

Iteration would be as you would normally do this with Python:

for elem in my_msg.pose_list:
    # do something

Note: this is not ROS specific at all. It's all just Python.

I must be missing something because these look like basic things one should be able to do.

The reason you cannot find any specific documentation about this is because it isn't something ROS changes.

edit flag offensive delete link more

Comments

Thank you @gvdhoorn.

My mistake was not to look _inside_ the message, but trying to iterate on/access the elements of the message itself.

gbohus gravatar image gbohus  ( 2020-08-19 03:39:17 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-08-18 10:46:36 -0500

Seen: 513 times

Last updated: Aug 19 '20