结合ros的boost sml状态机实践

有限状态机

有限状态机

State Machine design pattern prevents you from creating and maintaining spaghetti code.

简介

State Machine Language/Lite/Library

实践

代码结构

├── CMakeLists.txt
├── launch
│   └── sml_test_node.launch
├── package.xml
├── param
│   └── sml_test_node.yaml
├── README.md
└── src
    ├── boost
    │   ├── sml
    │   └── sml.hpp
    ├── fsm.h
    ├── main.cc
    ├── node.cc
    ├── node.h
    ├── processor.cc
    ├── processor.h
    └── util
        ├── guardian_fsm.h
        └── log_helper.h

代码实践

  • fsm.h
#ifndef FSM_H_
#define FSM_H_

namespace ericeric {

class FSM {
 public:
  virtual ~FSM() = default;

  bool on_start() {return true;};
  bool on_error() {return false;};
  bool on_lock() {return false;};
  bool on_unlock() {return false;};
  bool on_resume() {return false;};
};

}  // namespace ericeric

#endif  // FSM_H_

  • util/guardian_fsm.h
#ifndef GUARDIAN_FSM_H_
#define GUARDIAN_FSM_H_
#include "../boost/sml.hpp"
#include "fsm.h"

namespace ericeric {

namespace sml = boost::sml;

// state
struct WaitActive {};
struct Active {};
struct Inactive {};
struct Error {};

// event、transition
struct ev_start {};

struct ev_error {};
struct ev_resume {};

struct ev_lock {};
struct ev_unlock {};

// guards、transition condition
const auto onStart = [](FSM* fsm) { return fsm->on_start(); };
const auto onError = [](FSM* fsm) { return fsm->on_error(); };
const auto onLock = [](FSM* fsm) { return fsm->on_lock(); };
const auto onUnlock = [](FSM* fsm) { return fsm->on_unlock(); };
const auto onResume = [](FSM* fsm) { return fsm->on_resume(); };

// 定义状态机
struct FsmGuardian {
  auto operator()() const {
    using namespace sml;
    // clang-format off
    return make_transition_table(
      * state<WaitActive>                + event<ev_start>             [onStart]        = state<Active>
      , state<Active>                    + event<ev_error>             / onError        = state<Error>
      , state<Active>                    + event<ev_lock>              / onLock         = state<Inactive>
      , state<Inactive>                  + event<ev_unlock>            / onUnlock       = state<Active>
      , state<Error>                     + event<ev_resume>            / onResume       = state<Active>

    );
    // clang-format on
  }
};

}  // namespace ericeric
#endif // GUARDIAN_FSM_H_

参考

[Boost::ext].SML文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值