tue-robotics-graveyard / robot_smach_states

A library of smach states and state machine using the robot_skills API
2 stars 1 forks source link

Iteration state using designators #38

Closed LoyVanBeek closed 9 years ago

LoyVanBeek commented 9 years ago

We should have an easy iteration mechanism, like a for loop in smach states. Instead of

for i in iterable:
    do_something(i)

The can be

iterable = EdEntityCollectionDesignator(type="human") #Set up the collection we want to iterate over
i = VariableDesignator(resolve_type=ed.msg.EntityInfo) #iterable is a collection of ed.msg.EntityInfo's

iterator_state = IteratorState(iterable, i)
do_something_state = Greet(i)

iterator_state.execute() #Assigns the next value of iterable to i
do_something_state() #Use the designator that now resolves to the (next) human.

iterator_state.execute() #Assigns the next value of iterable to i
do_something_state() #Use the designator that now resolves to the (next) human.

iterator_state.execute() #If there are no more items in iterable, just return a different outcome

As implementation, we have

class IteratorState(smach.State):
    def __init__(self, iterable, element):
        self.iterable = iterable
        self.element = element

    def execute(self, userdata=None):
        elements = self.iterable.resolve()
        if elements:
            self.element.current = elements.pop(0) #Take the first item.
            return "next"
        else:
            return "stop_iteration"

With the pop(0), I really need to make sure that the references and Python naming/references work that way.