Robotics StackExchange | Archived questions

Ordered List of Models in World

Hello Guys,

i use gazebo 7.0 with Ros kinetic.

First of all i want a ordered List of the models how i add them to the list.

Here is what my code looks like:

class Block:
    def __init__(self, name, relative_entity_name):
        self._name = name
        self._relative_entity_name = relative_entity_name

class Tutorial:

    _blockListDict = {
        'a': Block('cardboard_box', 'link'),
        'b': Block('cabinet', 'link'),
        'c': Block('bookshelf_0', 'link'),
        'e': Block('jersey_barrier', 'link'),
        'd': Block('Dumpster_0', 'link'),
     }

My problem actual is that my order when i print information for the objects is:

cardboard, bookshelf0 , cabinet, jersey barrier, Dumpster0

What i am expected was cardboard, cabinet, bookshelf0 , Dumpster0, Jersey_barrier.

Any ideas what i am doing wrong?

Thats how i iterate the list:

 def update_gazebo_models(self):
        try:
        i = 0
            model_coordinates = rospy.ServiceProxy('/gazebo/get_model_state', GetModelState)
        vlist = []

            for block in self._blockListDict.itervalues():
                blockName = str(block._name)
                 resp_coordinates = model_coordinates(blockName, block._relative_entity_name)
             value = resp_coordinates.pose.position.x + resp_coordinates.pose.position.y
             vlist.append(round(value,2))
             i = i +1
        return vlist
        except rospy.ServiceException as e:
            rospy.loginfo("Get Model State service call failed:  {0}".format(e))

Asked by Baumboon on 2018-01-22 08:23:53 UTC

Comments

Answers

So this is not a Gazebo specific problem as it could happen to any dictionary in Python.

The reason for your problem is that in Python dictionaries are not ordered.

I suggest you read more on the provided link and try a different datatype for storing your block objects.

https://stackoverflow.com/questions/11784860/why-does-this-python-dictionary-get-created-out-of-order-using-setdefault

Asked by Raskkii on 2018-01-22 09:56:14 UTC

Comments

Thank u, i messed up that i did´t use a orderedList Thank u!

Asked by Baumboon on 2018-01-22 10:20:48 UTC