uleroboticsgroup / yasmin

YASMIN (Yet Another State MachINe)
GNU General Public License v3.0
130 stars 25 forks source link

Concurrent states #14

Open chrisbitter opened 5 months ago

chrisbitter commented 5 months ago

I am currently exploring YASMIN as alternative to Smach for ROS2. Is it possible to model concurrent states (analogous to https://wiki.ros.org/smach/Tutorials/Concurrent%20States)?

If not, any idea of how this can be integrated? Happy to contribute.

mgonzs13 commented 5 months ago

Hey @chrisbitter, this will be a great feature to add to YASMIN. A primitive version could be this, but the outcomes of the states and the cancelations should be added. In this case, a set of states are executed concurrently and the same outcome is returned in any case. Do you have any suggestions?


from threading import Thread
from typing import List
from yasmin import State
from yasmin.blackboard import Blackboard

class Concurrence(State):

    def __init__(self, outcome: str, states: List[State]) -> None:

        super().__init__([outcome])

        self.states = states

    def execute(self, blackboard: Blackboard) -> str:

        state_threads = []

        for s in self.states:
            state_threads.append(Thread(target=s, args=(blackboard,)))
            state_threads[-1].run()

        for t in state_threads:
            t.join()

        return self._outcomes[0]
mgonzs13 commented 3 months ago

Hey @chrisbitter, how is this going?