igor-krechetov / hsmcpp Goto Github PK
View Code? Open in Web Editor NEWC++ based Hierarchical / Finite State Machine library oriented for embedded and RTOS systems.
Home Page: https://hsmcpp.readthedocs.io
License: MIT License
C++ based Hierarchical / Finite State Machine library oriented for embedded and RTOS systems.
Home Page: https://hsmcpp.readthedocs.io
License: MIT License
Hello, I'm trying to use this library in platformio with my ESP32 WROOM.
I tried to create a minimum repro. Here is my platform.ini
:
[env:sparkfun_thing_bluetooth]
platform = espressif32
board = sparkfun_esp32s2_thing_plus_c
framework = arduino
lib_deps =
igor-krechetov/hsmcpp@^1.0.1
custom_hsm_files=./blink.scxml:BlinkHsm:Base
custom_hsm_gendir=./src
Other than the platform and board, I believe this is identical to the example.
It generated the BlinkHsmBase cpp and header files (very cool!) but fails to compile:
Compiling .pio\build\sparkfun_thing_bluetooth\liba67\hsmcpp\HsmEventDispatcherArduino.cpp.o
Compiling .pio\build\sparkfun_thing_bluetooth\liba67\hsmcpp\HsmEventDispatcherBase.cpp.o
In file included from .pio/libdeps/sparkfun_thing_bluetooth/hsmcpp/include/hsmcpp/HsmEventDispatcherBase.hpp:13,
from .pio/libdeps/sparkfun_thing_bluetooth/hsmcpp/include/hsmcpp/HsmEventDispatcherArduino.hpp:10,
from .pio/libdeps/sparkfun_thing_bluetooth/hsmcpp/include/hsmcpp.hpp:8,
from src/main.cpp:5:
.pio/libdeps/sparkfun_thing_bluetooth/hsmcpp/include/hsmcpp/os/Mutex.hpp:9:11: fatal error: freertos/Mutex.hpp: No such file or directory
#include "freertos/Mutex.hpp"
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
*** [.pio\build\sparkfun_thing_bluetooth\src\main.cpp.o] Error 1
hsmcpp/include/hsmcpp/os/Mutex.hpp
Line 8 in 91b791d
I tried digging into this error and found that INC_FREERTOS_H
is being defined here:
~.platformio\packages\framework-arduinoespressif32\tools\sdk\esp32\include\freertos\include\freertos\FreeRTOS.h
:
Apparentl this board is using freeRTOS under the hood. But like the error message suggests, I'm not able to find any associated file freertos/Mutex.hpp
.
Any ideas what I should try? Thanks!
Disabling the thread safety HSMBUILD_THREAD_SAFETY
option causes build failures.
In HsmImpl.hpp
mIsDispatching
is inside the macro block, but is still used in HsmImpl.cpp
So far this library has been awesome, I'd really like to get this one feature working but for some reason transitioning back to a history state is mangling some of my parallel state machines.
I have four parallel state machines running under the "Main" state machine:
My button state machine is pretty straightforward and looks something like this:
When the button is released, it will transition my "Workout" state machine from "Workout__Active__Running" to "Workout__Active__Paused" and vice versa. "Workout__Active__Running" has some nested states that I'd like to restore when coming back from "Workout__Active__Paused":
Here's my workoutActive.scxml with the irrelevant bits removed (note that I'm using the xi:include
feature so these states all get preprended with "Workout__Active__" in my generated state machine):
<scxml initial="SetUp" version="1.0" xmlns="http://www.w3.org/2005/07/scxml" >
<state id="Running" initial="Running_Squeezing">
<transition event="HW_BUTTON_RELEASED" target="Paused" type="external" />
<state id="Running_Squeezing" >
...
</state>
<state id="Running_Resting" >
...
</state>
<history type="deep" id="RunningHistory" />
</state>
<state id="Paused">
<transition event="HW_BUTTON_RELEASED" target="RunningHistory" type="external" />
<invoke srcexpr="on0301_Paused" />
</state>
</scxml>
Something gets screwed up when trying to transition to "RunningHistory" from the "Paused" state. I've printed out the active states during the transition:
1) Main, Button, Workout, Led, Haptics, Haptics__Off, Workout__Active, Workout__Active__Paused, Led__Blinking, Led__Blinking_Off, Button__Pressed
--- TRANSITION TO HISTORY (event: HW_BUTTON_RELEASED, state: Workout__Active__RunningHistory, previously active: Workout__Active__Running_Squeezing) ---
2) Main, Button, Workout, Led, Haptics, Workout__Active, Led__Blinking, Workout__Active__Running,
3) Main, Workout, Led, Workout__Active, Workout__Active__Running, Workout__Active__Running_Squeezing,
So it seems like it is popping off the leaf states on the parallel state machines for each history state it restores.
My expected final state:
Main, Button, Workout, Led, Haptics, Haptics__Off, Led__Blinking, Led__Blinking_Off, Button__Pressed, Workout__Active, Workout__Active__Running, Workout__Active__Running_Squeezing,
Hopefully this makes sense. Let me know if it doesn't and I'll try to provide more information.
Hello,
I am trying to implement your HSM inside a STM32G4, ARM MCU.
However i need to write a scheduler as i can't use the std::thread posix stuff on a Cortex-M4 CPU.
FreeRTOS can be used as OS to maintain asynchrony with the lib cpp_freertos wrote by Mickael Becker. This keep things very similar to the std::thread POSIX environnement. The major difference is the way threads are created and launched as cpp_freertos use a java like pattern with subclass and virtual run() method for threads.
I have (not so) sucessfully ported the HsmEventDispatcherBase.hpp/cpp files to HsmEventDispatcherBaseFREERTOS.hpp/cpp, also HsmEventDispatcherSTD.hpp/cpp files to HsmEventDispatcherFREERTOS.hpp/cpp with the cpp_freertos threading API.
I am now stuck with the hsm.hpp file here :
template <typename HsmStateEnum, typename HsmEventEnum>
void HierarchicalStateMachine<HsmStateEnum, HsmEventEnum>::PendingEventInfo::wait(const int timeoutMs)
{
if (isSync())
{
std::unique_lock<std::mutex> lck(*cvLock);
if (timeoutMs > 0)
{
syncProcessed->wait_for(lck, std::chrono::milliseconds(timeoutMs),
[=](){return (HsmEventStatus_t::PENDING != *transitionStatus);});
}
else
{
syncProcessed->wait(lck, [=](){return (HsmEventStatus_t::PENDING != *transitionStatus);});
}
}
With cpp_freertos API that could become (i don't need the timeout) :
template <typename HsmStateEnum, typename HsmEventEnum>
void HierarchicalStateMachine<HsmStateEnum, HsmEventEnum>::PendingEventInfo::wait()
{
if (isSync())
{
cpp_freertos::LockGuard lck(cvLock);
Wait(syncProcessed, cvLock);
}
}
You call syncProcessed->wait and that's a std::condition_variable method usable outside a thread (on a bare metal POSIX the main loop is also a thread indeed, not on a MCU).
However, my cpp_freertos::Thread::Wait method need to be called inside the scope of a thread.
So here i'm stuck, i don't know how i can fix it.
Maybe i can subclass HierarchicalStateMachine to run it inside a thread ?
Looking for help on this.
Hello, Igor Krechetov.
Would you like to add a tag to this project??
I will be glad if you do.
Thank you.
Hi @igor-krechetov,
I am trying to build hsmcpp library for Renesas Cortex M33 MCU. As a start I tried building the freertos posix port example/08_freertos. The build fails at linking stage. Its failing to link following libraries.
Where can i find these libraries? They are not available in the FreeRTOS souce directory as well. I have attached the build log. Please have a look.
build-log.txt
Regards,
Harsha
I tried turning debug logging on with HSM_LOGGING_MODE_STRICT_VERBOSE
and it failed to find serialPrintf
(using arduino).
Should this line
Line 4 in 91b791d
be this:
#if defined(PLATFORM_ARDUINO) && !defined(HSM_DISABLE_TRACES)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.