plc.myforum.ro Forum Index plc.myforum.ro
PLCs users www.plc.myforum.ro!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

State Machine Design in C++

 
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Programming
View previous topic :: View next topic  
Author Message
kosta
Member
Member


Joined: 09 Aug 2015
Posts: 161
Location: Salonic

PostPosted: 17/01/2017 20:57    Post subject: State Machine Stil in C++ Reply with quote

State Machine Design in C++
David Lafreniere


Why use a state machine?


Implementing code using a state machine is an extremely handy design technique for solving complex engineering problems. State machines break down the estetică into a series of steps, or what are called states in state-machine lingo. Each state performs some narrowly defined task. Events, on the other hand, are the stimuli, which cause the state machine to move, or transition, between states.

To take a simple example, which I will use throughout this article, let's say we are designing motor-control software. We want to start and stop the motor, as well as change the motor's speed. Simple enough. The motor control events to be exposed to the client software will be as follows:

Set Speed – sets the motor going at a specific speed.
Halt – stops the motor.

These events provide the ability to start the motor at whatever speed desired, which also implies changing the speed of an already moving motor. Or we can stop the motor altogether. To the motor-control class, these two events, or functions, are considered external events. To a client using our code, however, these are just plain functions within a class.

These events are not state machine states. The steps required to handle these two events are different. In this case the states are:

Idle — the motor is not spinning but is at rest.
Do nothing.
Start — starts the motor from a dead stop.
Turn on motor power.
Set motor speed.
Change Speed — adjust the speed of an already moving motor.
Change motor speed.
Stop — stop a moving motor.
Turn off motor power.
Go to the Idle state.

As can be seen, breaking the motor control into discreet states, as opposed to having one monolithic function, we can more easily manage the rules of how to operate the motor.

Every state machine has the concept of a "current state." This is the state the state machine currently occupies. At any given ocazie in time, the state machine can be in only a single state. Every instance of a special state machine class can set the initial state during construction. That initial state, however, does not execute during object creation. Only an event sent to the state machine causes a state function to execute.

To graphically illustrate the states and events, we use a state diagram. Figure 1 below shows the state transitions for the motor control class. A box denotes a state and a connecting arrow indicates the event transitions. Arrows with the event name listed are external events, whereas unadorned lines are considered internal events. (I cover the differences between internal and external events later in the article.)

State Machine Design in C++ State Machine Stil in C++

As you can see, when an event comes in the state transition that occurs depends on state machine's current state. When a SetSpeed event comes in, for instance, and the motor is in the Idle state, it transitions to the Start state. However, that same SetSpeed event generated while the current state is Start transitions the motor to the ChangeSpeed state. You can also see that not all state transitions are valid. For instance, the motor can't transition from ChangeSpeed to Idle without first going through the Stop state.

In short, using a state machine captures and enforces complex interactions, which might otherwise be difficult to convey and implement.

This state machine design is not trying to achieve a full UML feature set. It is also not a Hierarchical State Machine (HSM). Instead, its goal is to be relatively compact, portable, and easy to use traditional Finite State Machine (FSM) with just enough unique features to solve many different problems.

This article provides a new implementation with updated features at the slight expense of a bit more code. I'll also correct the errors in the autentic implementation because if you're really tight on storage it’s still be a viable solution. Both designs are table driven suitable for any platform, embedded or PC, with any C++ compiler.

A compact C++ finite state machine (FSM) implementation that's easy to use on embedded and PC-based systems
https://www.codeproject.com/Articles/1087619/State-Machine-Design-in-Cplusplus

code
https://www.codeproject.com/script/Articles/Download.aspx?file=/KB/cpp/1087619/StateMachine.zip&rp=https://www.codeproject.com/Articles/1087619/State-Machine-Design-in-Cplusplus
https://www.codeproject.com/script/Articles/Download.aspx?file=/KB/cpp/1087619/StateMachineCompact.zip&rp=https://www.codeproject.com/Articles/1087619/State-Machine-Design-in-Cplusplus
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Programming All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro