项目作者: igor-krechetov

项目描述 :
C++ based Hierarchical / Finite State Machine library oriented for embedded and RTOS systems.
高级语言: C++
项目地址: git://github.com/igor-krechetov/hsmcpp.git
创建时间: 2021-02-15T10:26:44Z
项目社区:https://github.com/igor-krechetov/hsmcpp

开源协议:MIT License

下载


MIT License
Changelog
Documentation Status

Releases

Latest Release
PlatformIO Registry
arduino-library-badge

Quality Status

Build Status

Static Code Analysis

SCA: MISRA
SCA: CodeQL
SCA: Coverity

Unit Tests

Coverage Status

Tests: STD
Tests: Glib
Tests: GLibmm
Tests: Qt

Overview

HSMCPP is a C++ library providing implementation of state machine design pattern (also known as statecharts). It allows you to easily add hierarchical (HSM) or finite state machine (FSM) to your project. The main motivation behind creating it was the lack of suitable non-commercial alternatives which do not require the usage of large frameworks. And even existing commercial solutions couldn’t satisfy all project needs that I usually have to deal with. This is in no way a “silver bullet” library, but it might be useful for you when dealing with RTOS systems, multi-threading or event-driven applications.

It’s also applicable for single-threaded and synchronous applications, but it might not be the most efficient option.

If you are not familiar with HSM/FSM design concept and which problems it helps you solve, I recommend reading:

And if you just want to know if state machines are for you or not, here is a quick list (taken from statecharts.dev)

Why should you use Statecharts?

Statecharts offer a surprising array of benefits

It’s worth noting that you’re already coding state machines, except that they’re hidden in the code.

Why should you not use statecharts?

There are a few downsides to using statecharts that you should be aware of.

Why are they not used?

What are the main arguments against statecharts?

There are a few common arguments against statecharts in addition to the ones listed above:

The benefits outlined above should make it clear that the introduction of statecharts is generally a net positive.

Key Features

Generic

Documentation

Documentation is available online.

HSM GUI Editors

Check out documentation to learn more about available editors.

Editing HSM in Qt Creator

Editing HSM in scxmlgui

hsmdebugger

Read documentation for details on how to use debugger.

hsmdebugger demo

Installation

  1. git clone https://github.com/igor-krechetov/hsmcpp.git
  2. cd ./hsmcpp
  3. ./build.sh
  4. cd ./build
  5. make install

By default, it will build all included components, tests and examples. You can disable any of them using cmake build flags. For example you probably will not have glib or glibmm libraries available on Windows so you might want to exclude them.

See detailed instructions in documentation.

Dependencies

  • For library:
    • C++11 or newer
    • glib (optional, for dispatcher)
    • glibmm (optional, for dispatcher)
    • Qt (optional, for dispatcher)
  • For build:
    • cmake 3.16+
    • Visual Studio 2015+ (for Windows build)
  • For code generator:
    • Python 3
  • For hsmdebugger:
    • Python 3
    • PyYaml (pip3 install PyYaml)
    • PySide6 (pip3 install PySide6)
    • plantuml (minimal version: V1.2020.11)

Creating a simple State Machine

HSM structure:

Hello Wolrd HSM

Implementation using HsmEventDispatcherSTD:

  1. #include <chrono>
  2. #include <thread>
  3. #include <hsmcpp/hsm.hpp>
  4. #include <hsmcpp/HsmEventDispatcherSTD.hpp>
  5. enum class States
  6. {
  7. OFF,
  8. ON
  9. };
  10. enum class Events
  11. {
  12. SWITCH
  13. };
  14. int main(const int argc, const char**argv)
  15. {
  16. std::shared_ptr<hsmcpp::HsmEventDispatcherSTD> dispatcher = hsmcpp::HsmEventDispatcherSTD::create();
  17. hsmcpp::HierarchicalStateMachine<States, Events> hsm(States::OFF);
  18. hsm.initialize(dispatcher);
  19. hsm.registerState(States::OFF, [&hsm](const VariantList_t& args)
  20. {
  21. printf("Off\n");
  22. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  23. hsm.transition(Events::SWITCH);
  24. });
  25. hsm.registerState(States::ON, [&hsm](const VariantList_t& args)
  26. {
  27. printf("On\n");
  28. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  29. hsm.transition(Events::SWITCH);
  30. });
  31. hsm.registerTransition(States::OFF, States::ON, Events::SWITCH);
  32. hsm.registerTransition(States::ON, States::OFF, Events::SWITCH);
  33. hsm.transition(Events::SWITCH);
  34. dispatcher->join();
  35. return 0;
  36. }

See /examples/07_build for CMake configuration examples.

For other examples see Getting Started guide or /examples.

Notable FSM/HSM libraries

There is no one-for-all library, so if hsmcpp doesn’t fully suit your needs you can check out one of these alternatives: