第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2025/10/21 11:15:56 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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语言的实现 的文档
层次状态机(HSM)用c语言的实现.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1vkvd80d8e06i7k4fy47_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top