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

Revision history [back]

click to hide/show revision 1
initial version

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.

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.

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.