7 ms·
If you're fond of state machines as an abstraction for system design, I highly recommend reading the Harel statechart paper. It's well-written and understandabl
by ttd 1y ago
If you're fond of state machines as an abstraction for system design, I highly recommend reading the Harel statechart paper. It's well-written and understandable. And, it's truly a useful extension of the type of state machine diagrams software engineers typically produce.
- all2 1y agoI discovered the above implementation about the time I finished reading through the paper. I need to go back through and finish taking notes. I think there might be room to add `any` and `all` guards that take many guards as arguments. The other thing I've been pondering is the apparent similarity between ASTs and the sort of graph a statechart forms. If we compress state and chart into a single object, now you have states that can have parents and children (the semantics of what is an active state would vary with your node type, IE compound or parallel). Then your edges would the transitions between nodes. The semantics of the whole thing starts to look vaguely like there's a programming language that could emerge if the intermediate representation is hammered out well enough.
- deleted 1y ago[deleted]
- TFortunato 1y agoAt Amazon Robotics we built a robot control system that executed state charts (described in an extended version of SCXML) in real-time for managing the core behaviors of the robot. To help people not have write the charts by hand, we built a DSL (originally in python, then in Kotlin), to author robot behaviors that then compiled down to SCXML. Conditions on guards were written in a tiny expression language we wrote, enabling you to look at various signals from the hardware and software at runtime and make decisions based off them. The nice part of this setup was that it opened up a path for doing more formal analysis on the behavior. E.g. you could easily find "terminal" states that you could enter but not leave. You can also imagine things like checking things like which states in a parallel or in concurrently running machines, aren't allowed to be active, and verifying there is no path in the state graph that allowed that to occur. There were other nice properties as well, like getting a graphical visualization of your program state "for free" It was definitely a more constrained model than a "full featured" programming language, but for our use case, controlling machines in a reliable way, it worked out very well!
- chermi 1y agoIs there a modern reference implementation of statecharts? My understanding is that there's tons of offspring and they all kind of disagree. Same with regular FSM at least from limited understanding from this book https://ptolemy.berkeley.edu/books/leeseshia/ https://ptolemy.berkeley.edu/books/leeseshia/. I'm a little worried the author (Lee) may have a particular PoV and perhaps this wasn't the best resource to learn from? I'm building an automated environmentally monitored +. controlled mushroom fruiting chamber. V1 I got by just with very basic state machines but I'm trying to take it up a notch in sophistication and do it the "right " way.
- all2 1y agoThere is for JavaScript called XState. I think it is SCXML compliant .You might also take a look at qmuntal/stateless (not SCXML compliant), a golang module. It has rudimentary sub-state functionality. I'm in the process of writing a golang library, if you're interested in seeing some raw development (IE, I'm in the middle of designing dev UX, and the guts of the lib have yet to be implemented). You may also consider n8n for your uses. As workflow management goes, it is quite robust. For embedded work, I honestly don't know. I do know that golang can be transpiled into C (https://tinygo.org/ https://tinygo.org/) and tinygo + qmuntal/stateless might work really well for your use-case.
- chermi 1y agoThank you so much! For context, this for a "driver" running on a pi that controls actuators on an esp32 via MQTT. So the the most basics states just clones of the states (On/Off) on the esp32, while more complicated states would combine actions like fan + humidifier. I've written everything on the pi in python so far. But that was all V1 and I'm starting over so maybe I'll try something new.
- all2 1y agoYou might map out your functionality and see what you actually need in terms of programmatic functionality. LLMs are actually decent at creating the boilerplate if you know what you need. Here's an example I had Claude throw together for states/substates for a door with a lock https://mermaidchart.com/play?utm_source=mermaid_live_editor&utm_medium=share#pako:eNp1Uk1TgzAQ_SuZPanTVr5bcvBgPTrjQb0oPSBsISMkTBJstdP_bggg9SuHkN197-3ykgNkIkegoHSq8YalhUzr-ZuXcGLW88WGzOdXZF0JhXmf63cLH_Lk0OdOGbciex0Z3epjW3rklQ0oae2JXJKcKeRFWuC1qPTZ-UQbsSealAyk34zj6YTDbB3xrkFOiTA7eW4HxY1RUDvGi5HcgU5-lpKs-xpUVmL2eo9cCalGcL9zYTyQrCg1EdtvJtkBRN0IxQykN2vHdDlV9U4Q1b7YkurTyHMr-atFhVvboRtxUrhndVMN4v8LjKYh1_L9T9em-xgxf15Hb-IIMV2lfmA1yh_1PdO2LJqhCjMoJMuBatniDEyuTrsQ7KtJQJdYYwLUHHPcpm2lE0j40dCalD8JUY9MKdqiBLpNK2WitsmnB_uVlcYClGvRcg3U9WLHqgA9wB6oHy3CwF_F4dJzAz8Mwxm8A_X8RRz7ThSsVo6_dJ04OM7gw_Z1FrEbRJ4frZw4cpeeHx4_AbEn-Bk https://mermaidchart.com/play?utm_source=mermaid_live_editor... Wow. That's a lot of link. Here's the raw chart: stateDiagram-v2 [*] --> Closed state Closed { [*] --> Locked Locked --> Unlocked: unlock / disengageBolt() Unlocked --> Locked: lock / engageBolt() } Closed --> Open: open [unlocked] / swing() Open --> Closed: close / checkSensors() note right of Closed Composite state with two substates end note note left of Open Simple state end note Locked: entry / engageBolt() Unlocked: entry / disengageBolt() Open: entry / startTimer() Open: exit / stopTimer() The syntax in the text labels is written the same way the Harel paper proposes: <State>: <entry/exit> / <action>()