层次状态机(HSM)用c语言的实现

2026/1/21 6:40:20

QSTATE s0(QEVENT const *e); QSTATE s1(QEVENT const *e); QSTATE s2(QEVENT const *e); QSTATE s11(QEVENT const *e); QSTATE s21(QEVENT const *e); QSTATE s211(QEVENT const *e); QSTATE Q_Top(QEVENT const *e); static void Initial(QEVENT const *e); static bool bFoo;

enum QSignals { A_SIG = Q_USER_SIG,

B_SIG, C_SIG, D_SIG, E_SIG, F_SIG, G_SIG, H_SIG };

static const QEVENT testQEvt[] = {

{A_SIG, 0, 0}, {B_SIG, 0, 0}, {C_SIG, 0, 0}, {D_SIG, 0, 0}, {E_SIG, 0, 0}, {F_SIG, 0, 0}, {G_SIG, 0, 0}, {H_SIG, 0, 0} };

int main() {

printf(\

Initial(0); // trigger initial transition for (;;) {

char c;

printf(\ c = getc(stdin);

getc(stdin); // discard '\\n' if (c < 'a' || 'h' < c) { return 0;

}

Q_Dispatch(&testQEvt[c - 'a']); // dispatch }

return 0; }

static QSTATE Q_Top(QEVENT const *e) {

return 0; }

void Initial(QEVENT const *e) {

bFoo = false; Q_Init((QSTATE)s0); }

QSTATE s0(QEVENT const *e) { if (e != NULL) {

switch (e->sig) {

case Q_ENTRY_SIG: printf(\return 0; case Q_EXIT_SIG: printf(\return 0;

case Q_INIT_SIG: printf(\return 0; case E_SIG: printf(\return 0; } }

return (QSTATE)Q_Top; }

QSTATE s1(QEVENT const *e) { switch (e->sig) {

case Q_ENTRY_SIG: printf(\return 0; case Q_EXIT_SIG: printf(\return 0;

case Q_INIT_SIG: printf(\return 0; case A_SIG: printf(\return 0; case B_SIG: printf(\return 0; case C_SIG: printf(\return 0; case D_SIG: printf(\return 0; case F_SIG: printf(\return 0; }

return (QSTATE)s0; }

QSTATE s11(QEVENT const *e) { switch (e->sig) {

case Q_ENTRY_SIG: printf(\return 0; case Q_EXIT_SIG: printf(\return 0; case G_SIG: printf(\return 0;

case H_SIG: // internal transition with a guard if (bFoo)

{ // test the guard condition printf(\ bFoo = false; return 0; } break; }

return (QSTATE)s1; }

QSTATE s2( QEVENT const *e) { switch (e->sig) {

case Q_ENTRY_SIG: printf(\return 0; case Q_EXIT_SIG: printf(\return 0;

case Q_INIT_SIG: printf(\return 0; case C_SIG: printf(\return 0; case F_SIG: printf(\return 0; }

return (QSTATE)s0; }

QSTATE s21(QEVENT const *e) { switch (e->sig) {

case Q_ENTRY_SIG: printf(\return 0; case Q_EXIT_SIG: printf(\return 0;

case Q_INIT_SIG:printf(\return 0; case B_SIG: printf(\return 0; case H_SIG: // self transition with a guard if (!bFoo)

{ // test the guard condition printf(\ bFoo = true;

Q_TRAN(s21); // self transition return 0; }

break; //break to return the superstate }

return (QSTATE)s2; }

QSTATE s211(QEVENT const *e) { switch (e->sig) {

case Q_ENTRY_SIG: printf(\return 0; case Q_EXIT_SIG: printf(\return 0; case D_SIG: printf(\return 0; case G_SIG: printf(\return 0; }

return (QSTATE)s21; }

(3) 输出结果:

Hiberarchy state machine testing

Top_Init;s0-ENTRY;s0-INIT;s1-ENTRY;s1-INIT;s11-ENTRY; Signal<-a

s1-A;s11-EXIT;s1-EXIT;s1-ENTRY;s1-INIT;s11-ENTRY; Signal<-e

s0-E;s11-EXIT;s1-EXIT;s2-ENTRY;s21-ENTRY;s211-ENTRY; Signal<-e

s0-E;s211-EXIT;s21-EXIT;s2-EXIT;s2-ENTRY;s21-ENTRY;s211-ENTRY; Signal<-a Signal<-h

s21-H;s211-EXIT;s21-EXIT;s21-ENTRY;s21-INIT;s211-ENTRY; Signal<-h Signal<-x

说明:上面功能都是通过C语言实现的,大家可以将其用C++实现,共享一下。


层次状态机(HSM)用c语言的实现.doc 将本文的Word文档下载到电脑
搜索更多关于: 层次状态机(HSM)用c语言的实现 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219