CN111026467B - Control method of finite-state machine and finite-state machine - Google Patents

Control method of finite-state machine and finite-state machine Download PDF

Info

Publication number
CN111026467B
CN111026467B CN201911244086.1A CN201911244086A CN111026467B CN 111026467 B CN111026467 B CN 111026467B CN 201911244086 A CN201911244086 A CN 201911244086A CN 111026467 B CN111026467 B CN 111026467B
Authority
CN
China
Prior art keywords
state
state machine
machine
states
father
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201911244086.1A
Other languages
Chinese (zh)
Other versions
CN111026467A (en
Inventor
宋强
白俊锋
王庆祥
许志瑜
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hefei Technological University Intelligent Robot Technology Co ltd
Original Assignee
Hefei Technological University Intelligent Robot Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Hefei Technological University Intelligent Robot Technology Co ltd filed Critical Hefei Technological University Intelligent Robot Technology Co ltd
Priority to CN201911244086.1A priority Critical patent/CN111026467B/en
Publication of CN111026467A publication Critical patent/CN111026467A/en
Application granted granted Critical
Publication of CN111026467B publication Critical patent/CN111026467B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/448Execution paradigms, e.g. implementations of programming paradigms
    • G06F9/4498Finite state machines
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code

Abstract

The invention discloses a control method of a finite-state machine and the finite-state machine, belonging to the technical field of computer application, comprising the steps of establishing each state of the state machine according to the name and the memory address of each state of the state machine, wherein the state comprises two father states and a final state, and each father state comprises three child states; after the current state of the state machine is executed, the next state to be jumped is designated by a postEvent function, wherein a first parameter of the postEvent function is the name of the next state to be jumped to, and a second parameter is a parameter to be transferred to the next state; the father state or StateMachine looks up the corresponding memory address from the registered state list according to the name of the next state, and executes the interface function of the next state, and each sub-state has 3 execution functions, namely oneentry, handleTimeout and oneexit functions. The invention provides rich state machine functions by adopting simple language and has strong transportability.

Description

Control method of finite-state machine and finite-state machine
Technical Field
The present invention relates to the field of computer application technologies, and in particular, to a finite-state machine and a control method thereof.
Background
The finite state machine can realize simple functions, namely, the mutual conversion among various states is realized. However, the state machine for practical application may need more functions, such as passing parameters during state switching, and the inclusion relationship between the parent state and the child state.
The state machine which is relatively mature at present is a finite state machine algorithm implemented in QT, and fig. 1 shows a typical QT state machine process, which can implement switching between states through a signal driving mechanism, and support functions of starting, stopping, suspending, and the like of the state machine, and also support functions of parent states including child states, switching between parent states, and the like. In fig. 1, the S1 state is a parent state, and it contains 3 child states: s11, S12, and S13, when a button.
Although the QT state machine is powerful, its use requires programming with QT, and some libraries that contain QT cannot be programmed in pure C/C + + code. While other state machines implementing algorithms, such as the finite state machine implemented in C expert programming or state machines in other company code, such as the task state machine in TI official code, implement simpler functions that use function pointer jump tables to implement the switching between states.
Disclosure of Invention
The invention aims to overcome the defects in the prior art and provides a finite-state machine which can be realized by adopting C + + coding and comprises a plurality of functions.
In order to achieve the above object, the present invention adopts a control method of a finite-state machine, comprising:
creating each state of the state machine according to the name and the memory address of each state of the state machine, wherein the state comprises two father states and a final state, and each father state comprises three son states;
after the current state of the state machine is executed, a postEvent function specifies a next state to be jumped, wherein a first parameter of the postEvent function is the name of the next state to be jumped to, and a second parameter is a parameter to be transferred to the next state;
the father state or the StatemMechine searches the corresponding memory address from the registered state list according to the name of the next state, and executes the interface function of the next state;
when jumping between sub-states, firstly executing an oneentry function, and executing a handleTimeout function when the next jumping state is the same as the current state; and when the state of the next jump is different from the current state, executing the onExit function of the current state and then executing the onEntry function.
Further, the creating each state of the state machine according to the name and the memory address of each state of the state machine includes:
creating two father states and a final State of the State machine, wherein the final State is inherited from a State class;
creating three respective child states of the two parent states, wherein each child State is inherited from a State class;
adding three child states of each parent state to the corresponding parent state and setting an initial execution state;
adding the two parent states and the final state into StatemMechine, and setting the initial execution state of the StatemMechine;
the father state registers to the StatemMAC and sends the name and the memory address of the father state;
the child state registers with the parent state and sends its name and memory address.
Furthermore, when the state machine is started, the state machine firstly enters a father state and is switched to another father state according to a corresponding switching signal;
and when the state machine receives the stop machine signal, the state machine is switched to final state from the current state.
In another aspect, a finite state machine is used, and the control method of the finite state machine is used.
Compared with the prior art, the invention has the following technical effects: the state machine in the invention can adopt pure C + + coding, can be embedded into various platforms for use, and can also provide multiple functions, including child state switching, parent state switching, child state circulation, state-to-state parameter transfer and the like.
Drawings
The following detailed description of embodiments of the invention refers to the accompanying drawings in which:
FIG. 1 is a QT state machine flow diagram;
FIG. 2 is a flow chart illustrating a control method of a finite state machine;
FIG. 3 is a flow chart of the state machine of the present invention;
FIG. 4 is a schematic diagram of the core algorithm of the state machine of the present invention.
Detailed Description
To further illustrate the features of the present invention, refer to the following detailed description of the invention and the accompanying drawings. The drawings are for reference and illustration purposes only and are not intended to limit the scope of the present disclosure.
As shown in fig. 2, the present embodiment discloses a method for controlling a finite-state machine, which includes the following steps S1 to S4:
s1, establishing each state of the state machine according to the name and the memory address of each state of the state machine, wherein the state comprises two father states and a final state, and each father state comprises three child states;
s2, after the current state of the state machine is executed, the next state to be jumped is designated through a postEvent function, wherein a first parameter of the postEvent function is the name of the next state to be jumped to, and a second parameter of the postEvent function is a parameter to be transferred to the next state;
s3, finding a corresponding memory address from the registered state list according to the name of the next state by the father state or the StatemAchine, and executing an interface function of the next state;
s4, when jumping among the sub-states, firstly executing an oneentry function, and executing a handleTimeout function when the next jumping state is the same as the current state; and when the state of the next jump is different from the current state, executing the oneexit function of the current state and then executing the oneentry function.
It should be noted that the state machine adopts simple language to provide rich state machine functions, is simple to use, has strong transportability, can effectively simplify code logic, more reasonably organizes program structure, and has strong maintainability.
Further, the step S1: creating each state of the state machine according to the name and the memory address of each state of the state machine, and specifically comprising the following subdivision steps:
creating two father states and a final State of the State machine, wherein the final State is inherited from a State class;
creating three respective child states of the two parent states, wherein each child State is inherited from a State class;
adding three sub-states of each parent state to the corresponding parent state and setting an initial execution state;
adding the two parent states and the final state into StatemMechine, and setting the initial execution state of the StatemMechine;
the father state registers to the StatemMAC and sends the name and the memory address of the father state;
the child state registers with the parent state and sends its name and memory address.
The specific use of this state machine is listed below:
the code for this use in the threadmachine. Cpp file is as follows:
first, 2 parent states and a final State are created, wherein the final State is inherited from a State class
State*pWorkState=new State(pStateMachine);
State*pChargeState=new State(pStateMachine);
StateFinal*pStateFinal=new StateFinal(pStateMachine);
Three sub-states of the pWorkState State are then created, each inherited from the State class
StateGetJob*pStateGetJob=new StateGetJob(pWorkState);
StateGotoPoint*pStateGotoPoint=new StateGotoPoint(pWorkState);
StateFinish*pStateFinish=new StateFinish(pWorkState);
Three sub-states of the pChargeState State are then created, each inherited from the State class
StateBeginCharge*pStateBeginCharge=newStateBeginCharge(pChargeState);
StateOpenDoor*pStateOpenDoor=new StateOpenDoor(pChargeState);
StateCloseDoor*pCloseDoor=new StateCloseDoor(pChargeState);
Adding three child states of the pWorkState state into the pWorkState parent state and setting the initial execution state
pWorkState->addState("get job",pStateGetJob);
pWorkState->addState("goto point",pStateGotoPoint);
pWorkState->addState("finished",pStateFinish);
pWorkState->setInitState(pStateGetJob);
Adding three child states of the pChargeState state into the pWorkState parent state and setting the initial execution state
pChargeState->addState("begin charge",pStateBeginCharge);
pChargeState->addState("open door",pStateOpenDoor);
pChargeState->addState("close door",pCloseDoor);
pChargeState->setInitState(pStateBeginCharge);
Adding two parent states and final state to the pStatEMhine
pStateMachine->addState("work state",pWorkState);
pStateMachine->addState("charge state",pChargeState);
pStateMachine->addFinalState(pStateFinal);
Setting initial execution state of pStatEMAINE
pStateMachine->setInitState(pWorkState);
Starting state machine
pStateMachine->start();
Specifically, after the state machine is started, the state machine executes the pStateGetJob state in the pWorkState first, when the state execution is finished, other states in the pWorkState state are continuously executed, when the pStateMachine receives a parent state switching signal, the state machine exits from the pWorkState state, then executes the pStateBeginCharge state of the pChargeState, and when the state machine receives a stop signal, executes the interface function of the pStateFinal state.
It should be noted that, as shown in fig. 4, state. Cpp and statemachine. Cpp are base classes of the State machine, and a method of each State of the State machine is implemented, where the State machine. Cpp inherits from the State class and is responsible for starting and stopping the State machine and managing switching between parent states, and switching between child states is completed by the parent State. Each state has its own name, when a father state is created, the father state needs to register with StateMachine to provide its own name and memory address, and after a son state is created, it needs to register with the father state to provide its own name and memory address. When a state is executed and jumps to the next state, the next state to be executed is specified through a postEvent function, the first parameter of the postEvent function is the name of the next state to jump to, the second parameter is the parameter to be transferred to the next state, the postEvent function can be null, the father state or StateMachine finds its memory address from the registered state list according to the name of the next state, and then the interface function of the state is executed.
Each sub-state has 3 execution functions, which are onEntry, handleTimeout and onExit functions, when the state is entered for the first time, the onEntry function is executed to execute some initialization work, when the state of the next jump is the same as the current state, the handleTimeout function is executed, when the state of the next jump is different from the current state, the onExit function of the current state is executed first to execute some cleaning work, and then the onEntry function of the next state is executed.
Further, the state machine in this embodiment adopts pure C + + coding, and can be embedded into various platforms for use. The state machine provides a plurality of functions, including child state switching, parent state switching, child state cycling, transferring parameters between states, etc., as shown in fig. 3: before the state machine is started, initializing a parent state of the state machine to determine which child state is executed when the state machine enters a do job state or a goto charge state; and initializing the initial state of the whole state machine to determine which parent state to enter when the state machine is started. The state machine in fig. 3 will enter into the do job state first when starting up, and will switch to the goto charge state when the state machine receives the switch to goto charge signal, and will switch to the fine state when the state machine receives the stop machine signal no matter what state the state machine is in.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents, improvements and the like that fall within the spirit and principle of the present invention are intended to be included therein.

Claims (4)

1. A method for controlling a finite state machine, comprising:
creating each state of the state machine according to the name and the memory address of each state of the state machine, wherein the state comprises two father states and a final state, and each father state comprises three son states;
after the current state of the state machine is executed, a postEvent function specifies a next state to be jumped, wherein a first parameter of the postEvent function is the name of the next state to be jumped to, and a second parameter is a parameter to be transferred to the next state;
the father state or the StatemMechine searches the corresponding memory address from the registered state list according to the name of the next state, and executes the interface function of the next state;
when jumping between sub-states, firstly executing an oneentry function, and executing a handleTimeout function when the next jumping state is the same as the current state; and when the state of the next jump is different from the current state, executing the onExit function of the current state and then executing the onEntry function.
2. The finite state machine control method according to claim 1, wherein the creating each state of the state machine according to the name and the memory address of each state of the state machine comprises:
creating two father states and a final State of the State machine, wherein the final State is inherited from a State class;
creating three respective child states of the two parent states, wherein each child State is inherited from a State class;
adding three child states of each parent state to the corresponding parent state and setting an initial execution state;
adding the two parent states and the final state into StatemMechine, and setting the initial execution state of the StatemMechine;
the father state registers to the StatemMechine and sends the name and the memory address of the father state;
the child state registers with the parent state and sends its name and memory address.
3. The finite-state machine control method according to claim 1, wherein when the state machine is started, it first enters a father state and switches to another father state according to a corresponding switching signal;
and when the state machine receives the stop machine signal, the state machine is switched from the current state to the final state.
4. A finite state machine, characterized in that a control method of a finite state machine according to any of claims 1-3 is employed.
CN201911244086.1A 2019-12-06 2019-12-06 Control method of finite-state machine and finite-state machine Active CN111026467B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201911244086.1A CN111026467B (en) 2019-12-06 2019-12-06 Control method of finite-state machine and finite-state machine

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201911244086.1A CN111026467B (en) 2019-12-06 2019-12-06 Control method of finite-state machine and finite-state machine

Publications (2)

Publication Number Publication Date
CN111026467A CN111026467A (en) 2020-04-17
CN111026467B true CN111026467B (en) 2022-12-20

Family

ID=70204601

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201911244086.1A Active CN111026467B (en) 2019-12-06 2019-12-06 Control method of finite-state machine and finite-state machine

Country Status (1)

Country Link
CN (1) CN111026467B (en)

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103049603A (en) * 2012-12-13 2013-04-17 南京大学 Method for converting state machine diagram with concurrency to automatons
CN104239013A (en) * 2013-06-17 2014-12-24 上海联影医疗科技有限公司 Delegation based distributed state machine and processing method thereof
CN105244042A (en) * 2015-08-26 2016-01-13 安徽建筑大学 FSA (Finite State Automaton) based voice emotion interaction device and method
CN105845137A (en) * 2016-03-18 2016-08-10 中国科学院声学研究所 Voice communication management system
CN106997293A (en) * 2016-01-26 2017-08-01 上海大唐移动通信设备有限公司 The implementation method and device of finite state machine
CN108491238A (en) * 2018-01-22 2018-09-04 中国电子科技集团公司第十五研究所 A kind of state machine and design method for harmful influence electronic transaction order

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9218169B2 (en) * 2013-11-19 2015-12-22 Google Inc. Callpath finder
CN106502770B (en) * 2015-09-07 2021-09-21 上海畅星软件有限公司 HMI state transition method based on finite state machine
US10678562B2 (en) * 2016-11-17 2020-06-09 K&M Systems, Inc. Systems and methods for facilitating real-time analytics

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103049603A (en) * 2012-12-13 2013-04-17 南京大学 Method for converting state machine diagram with concurrency to automatons
CN104239013A (en) * 2013-06-17 2014-12-24 上海联影医疗科技有限公司 Delegation based distributed state machine and processing method thereof
CN105244042A (en) * 2015-08-26 2016-01-13 安徽建筑大学 FSA (Finite State Automaton) based voice emotion interaction device and method
CN106997293A (en) * 2016-01-26 2017-08-01 上海大唐移动通信设备有限公司 The implementation method and device of finite state machine
CN105845137A (en) * 2016-03-18 2016-08-10 中国科学院声学研究所 Voice communication management system
CN108491238A (en) * 2018-01-22 2018-09-04 中国电子科技集团公司第十五研究所 A kind of state machine and design method for harmful influence electronic transaction order

Non-Patent Citations (4)

* Cited by examiner, † Cited by third party
Title
19.QT-事件发送函数sendEvent()、postEvent();诺谦;《博客园 https://www.cnblogs.com/lifexy/p/9042862.html》;20180515;全文 *
A Lightweight Finite State Machine C++ Library aimed at Seamless Integration with Robotic Middlewares;Victores J G et al.;《Towards Humanoid Robots OS Workshop at HUMANOIDS 2016》;20161130;全文 *
一个有限状态机的 C++ 实现;刘伟;《InfoQ https://www.infoq.cn/article/ft3J9SxtC0UgDbYdlzxP?utm_source=related_read_bottom》;20191120;全文 *
基于有限状态机的C++重载唯一性确定;牟永敏 等;《计算机应用研究》;20140430;第31卷(第04期);全文 *

Also Published As

Publication number Publication date
CN111026467A (en) 2020-04-17

Similar Documents

Publication Publication Date Title
CA3125705C (en) Controlling tasks performed by a computing system
JP2011501323A5 (en)
RU2005137695A (en) Predictive command in the data processing system
EP2755138B1 (en) Testing implementation parameters of a computer program in a distributed environment
JP2017531375A5 (en)
CN111026467B (en) Control method of finite-state machine and finite-state machine
CN104391754A (en) Method and device for processing task exception
US9152454B2 (en) Method for enabling sequential, non-blocking processing of statements in concurrent tasks in a control device
KR102212352B1 (en) Method and apparatus for controlling robots
KR101235330B1 (en) The Apparatus and Method for Integrated Management of Embedded Software Development Tools
CN106874072B (en) Embedded operating system partition virtual interrupt processing method based on PowerPC processor
CN104597820A (en) Method for extending single chip microcomputer hardware timer
CN100498714C (en) Method for constituting television software system using mode machine
CN111399813A (en) State switching system and method for object
CN109932997A (en) A kind of programmable logic controller (PLC) core system
CN107291038B (en) A kind of PLC software instruction parsing execution method
TW201810048A (en) Method of operating embedded system and control chip thereof
KR100557518B1 (en) System large scale integrated circuitlsi, method of designing the same, and program therefor
CN111371816A (en) Communication method, system and communication equipment based on condition triggering
CN111371820A (en) Communication method, system and communication equipment based on timer triggering
CN102535096B (en) Cleaning product, and cleaning control method and cleaning controller for cleaning product
KR101767418B1 (en) Method and apparatus for guaranteeing priority of logic in an embedded operating system
Ware et al. Supervisory control with progressive events
Basu et al. Interface synthesis for embedded applications in a codesign environment
CN109518410B (en) Control method of washing machine and washing machine

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
TA01 Transfer of patent application right

Effective date of registration: 20201020

Address after: 230088 No. 5111 Wangjiang West Road, hi tech Zone, Anhui, Hefei

Applicant after: Hefei Technological University Intelligent Robot Technology Co.,Ltd.

Address before: 230088 No. 5111 Wangjiang West Road, hi tech Zone, Anhui, Hefei

Applicant before: Hefei Technological University Intelligent Robot Technology Co.,Ltd.

Applicant before: CSG SMART SCIENCE & TECHNOLOGY Co.,Ltd.

Applicant before: CSG SMART ELECTRIC TECHNOLOGY Co.,Ltd.

TA01 Transfer of patent application right
GR01 Patent grant
GR01 Patent grant