FSM-like nested timers #910
-
hi all - I'm trying to track a mouse and trigger a series of events when it is not moving. I think there's about a billion ways to do this in Bonsai, but I'm struggling to find something robust that handles all of the conditions with my limited knowledge of Rx/Bonsai. Any and all suggestions welcome, thanks! There are three main conditions:
I attempted this by taking the movement (int) and thresholding by Less Than, followed by a DistinctUntilChanged node. This way I know when movement starts or stops. One example of something I tried is to make a SelectMany node that takes the source (bool from DistinctUntilChanged) and if TRUE (means animal stopped moving) I delay, trigger output SerialStringWrite, branch from delay1 and introduce a delay2 and then trigger output SerialStringWrite and then Repeat. I think this should repeat the branch with delay2 and SerialStringWrite. In parallel, inside the SelectMany node, I take the same original DistinctUntilChanged boolean, use a condition for false, and then connect to workflow output. This successfully waits until the movement stops, but then repeats forever instead of exiting. I am pretty stuck on how to stop the loop once it starts -- I also tried some gates and throttles along the way, as well as triggered buffer and triggered window, but couldn't get them working well and am not able to find enough examples like this to hack away at it. Thanks for any/all advice! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Heya, Check the attached workflow for a possible solution (i think....) |
Beta Was this translation helpful? Give feedback.
Heya,
This sounds like a perfect case to use the
Switch
combinator!Basically, the
Switch
closes the current observable once a new one is created. You can leverage this behavior to alternate between the conditions (i.e. Moving -> not moving and vice-versa).Check the attached workflow for a possible solution (i think....)
Let me know if it works !
example_fsm.zip