finite-state-automata
finite-state-automata
net/publication/327389755
CITATIONS READS
19 6,463
2 authors, including:
Richard Balogh
Slovak University of Technology in Bratislava
71 PUBLICATIONS 280 CITATIONS
SEE PROFILE
All content following this page was uploaded by Richard Balogh on 14 April 2020.
Abstract. In this paper, we describe the use of the finite state machines
(FSM, state automata) in robotics, especially for implementing entry-
level control systems. The FSM is defined and couple of explanatory
tasks are described using it. Further, several examples of real tasks are
presented and implementation sketch which uses FSM is shown. The
paper aims to show FSM can be a good tool for implementing control
system of a robot, powerful as well as easy to be used.
1 Introduction
Finite state machines (FSM) are one of the many types of automata. In their
hierarchy, they lie on a low level, i.e. their relative power is not big in comparison
with other types (e.g. Push-down automata or Turing machines). However, they
are well sufficient for many tasks in various applications and areas (including
robotics) where they can significantly help to efficiently implement repetitive
tasks. Moreover, they can contribute to implementation readability and main-
tainability as they represent a methodical way of using simple construction parts
for forming a powerful system. Therefore we believe FSMs are of a great use also
for introductory robotics, for example in education or for self-reliant beginners.
However, a very typical development process in such cases is creation of a sim-
ple dedicated system which gets improved over time to cover more functionality
and resolve special cases and specific details. This often leads to a system which
grows well beyond understandability and maintainability because of its nature
of a monolith system with numerous patches and exceptions. In our paper, we
would like to show that using FSMs and their enhanced versions is not difficult
at all and can bring the aforementioned advantages which in final effect helps
the users to focus more on problem solving than on its low level implementation.
The paper is structured as follows: In Section 2 we give theoretical back-
ground and definition of Finite State Machine. Section 3 shows examples of
FSM used for implementation of two simple automata, a power switch and a
code lock. Section 4 shows the setup for a typical introductory level robot task –
a robot that follows a line and a more advanced task – a robot which manipulates
items in its working space.
2 Finite State Machines
S2
T1a T2
T1b
start S1 S3
T3
Reader may find also other, equivalent definitions of a FSM in various text-
books. This basic definition can be further enhanced in many ways. For our use,
we can add triggers which allow changing the state only based on a condition or
attach an action to be performed when a transition changes a state or when a
state is entered/left3 . It can be proven that such additions do not affect the theo-
retical properties (or power) of a FSM, yet they make the FSM to be practically
usable.
The notion of state Intuitively, the state of a system covers its condition at
a particular point in time. In general, the state affects how the system reacts to
inputs. Formally, we define the state to be an encoding of everything about the
past that has an effect on the system’s reaction to current or future inputs.
T1a T1b T3 T4
S1
→ S1 S2 S3
S2 S3 T1a T1b
S3 S1
S2 S3
T2 T3
S3 S1
3
Adding an output associated with a state creates a Moore machine, adding an output
associated with a state and input (therefore effectively with a transition) creates a
Mealy machine.
Only a relevant part of the code is presented; the inputs are implemented
as calls to a respective function, returning a boolean value corresponding to the
input check.
This coding idea could be very similarly used in other languages like C++,
C#, Java, Python and others. In the following text, we will show even the imple-
mentation in graphical programming language Microsoft Blocks4 . Obviously, it is
far beyond the scope of this paper to show all the wide varieties of representation
and implementation.
Why to use FSM? Finite State Automaton it is a powerful tool for describing
sequential behavior of a control program and is easy to understand. It is often
used as an communication language between the programmer and process engi-
neer. Using the FSM helps to partition a control problem into a smaller units
(partitioning) while at the same time it provides clear and global overview of the
process. It also makes it easier for later changes and diagnostics of the system in
comparison to monolithic definitions or programming. Last, but not least, when
properly implemented, it offers self-documenting features to its users.
Further advantages include: if the program is to be modified, usually it is done
at single point only. Modifications to the control section do not affect settings
of the outputs. The program is also easy to test. For example, if it is stuck on
a certain point, it is easy to determine which transition is not working or which
condition is missing. If the real device (a robot) fails, it is usually easy to detect
in which state condition it occurred. And last, but not least, it forces an user
to structure the programs which in turn minimizes the chance of programming
errors (as mentioned e.g. in [3]).
General advice for implementing FSM is to split the code into the two parts – one
with the transitions only and the second with the execution of outputs. Modified
algorithm of the C-code from above is now “rewritten” in Blocks for micro:bit
programming language. Please note, that one of the transition conditions was
implemented as a timer. This is quite common case, that transition from one
state to another is required after certain amount of time.
Typical implementation of the FSM is formed by the nested if statements,
often replaced by the switch statement when available (C, C++). It is per-
haps the most popular technique. Signals and states are typically represented as
enumerations.
The nested switch statement method is simple and understandable and usu-
ally has a small (RAM) memory footprint, which is advantageous for embedded
systems used in robotics. In the following paragraphs, we show couple of easiest
examples.
4
https://makecode.microbit.org/
Fig. 3. Generalized implementation of the FSM from the Fig.1.
start Off On
1
the states and triggered events. If the combination of states and triggered event
match, the event handler for such service is executed and the state is updated
to the next state as per the transition function definition. It depends on the
requirements whether the states or the events are checked first. In the sample
code below, the state is verified first and if the transition condition is met, the
state changes. After those checks the event triggered by the states are handled
based on the current active state. However, it is possible to swap the procedure:
check the event first and only then check the state5 .
To show how effective is this coding scheme, just imagine that it is necessary
to change the behaviour of the system such that the device is controlled by two
buttons: one for ON, second for OFF state. In this case, it is necessary to change
only a single line: in the condition at line 15, the buttonA has to be changed to
buttonB. No other place in the code needs to be changed.
5
It can be proven that such change does not affect the theoretical expressivity of the
FSM. For practical reasons, this might be of a good use, however.
3.2 Code Lock – Implementation using the transition table matrix
Following diagram is model of the code lock with opening sequence B-A-B.
Wrong combination leads back to the initial state Lock1. Note, that the OPEN
state is final, marked with double line circle, meaning that this state is no more
changed.
B A B
Fig. 5. FSM diagram for simple code lock. Opening sequence is B-A-B.
A B
Lock1 Lock1 Lock2
Lock2 Lock3 Lock1
Lock3 Lock1 Open
Open Open Open
However, the tables used for implementation are usually sparse (see Fig.
2) as many combinations of state/transition are not used or invalid. Then this
approach leads to memory wasting. Also, memory requirements increases when
the number of states and/or events grows [4].
4 Advanced examples
The robot behavior is often easy to model as a state machine. That is, the
outputs of the state machine (e.g. motor feed) depend not only on the inputs
(e.g. the bump sensor state) alone6 , but also on the current state of the machine
or plan execution phase (e.g. whether the robot is going forward or backward).
In Fig. 6, we show a basic line following robot control as this task is a typical
first exercise in robotics. For simplicity, we show an example of a very simple
robot with only 2 line sensors working as follows. When a sensor detects the
line (black), it outputs logical 1, otherwise it outputs 0. With two sensors only,
we have 4 different states with four possible output actions. Note that we can
specify also the required output action directly with each state (e.g. goLeft in
case the robot is too far right in respect to the line). For simplicity, we don’t
consider any activity after the robot is lost. Implementation was tested on the
micro:bot robot kit7 and programmed in Block language (see Fig. 7).
6
Such control is usually called reactive control.
7
https://www.sparkfun.com/products/14216
10 11
01
???
I am lost...
11 01
Lost
STOP
Fig. 6. Line following robot. Sensors detecting line at different phases (left) and cor-
responding FSM diagram (right).
Fig. 7. Implementation of the Line Follower code according the Fig. 6 in Microsoft
Make Code Block language.
The FSM has been successfully used also for implementing the core control of the
“MART Friday Bot”. This robot was prepared for the “Ketchup House” compe-
tition at Istrobot event [5, 6]. Thanks to the FSM, it was possible to adapt the
original program from a choreography-based robot performance “Robot Dance”
to the Ketchup can collecting task in just a few hours8 . The robot fulfilled the
competition rules and (to quite a surprise even of its authors) placed third in
2016 and with a small enhancement for opponent collision avoidance it placed
second in 2017. As relieved from talks to other robot builders in this competi-
tion, their software implementations differed most notably in lack of global code
structure and contained typically lots of code functionalities merged into a single
big program which was hard to debug and close to impossible to adapt. On the
other hand, the MART Friday Bot code was composed of simpler independent
functions which served the FSM in performing individual actions, copying the
FSM graph structure. When a problem arose during the preparation and testing,
it was simple to find the responsible code and correct it.
The main control state machine of MART Friday Bot is shown in Fig. 8. The
robot itself is in Fig. 9.
5 Conclusions
In this paper, we have presented the Finite State Automata as a possible tool
for implementing the core of a robot control. We have shown several examples
which should give the reader basic understanding of FSM usage in this area. Our
experiences show there are numerous task types where a FSM can significantly
help at the implementation phase even if the task seems to be very simple at
the first glance. Using the FSM can make the controlling code be readable, un-
derstandable and maintainable which is very important especially in education.
Let us informally cite one student: “I have daubed everything somehow for the
first exercise and it somehow more or less worked. Then I learned about FSMs
and implemented the last exercise using it. Had I known it earlier, I would have
used it straight on, it was so easy!”
8
Namely, it was adapted during the 300km car journey from Prague to Bratislava.
Fig. 9. MART Friday Bot
Advantages of using FSM: The learning curve for using FSM is minor; it is
quite likely the reader already knows about FSMs from math or logic courses.
Secondly, the integration itself is almost painless, especially when one takes the
FSM into account from early stages of the design. On contrary, the transition
from spaghetti code to FSM, especially in case there is a lot of stages, may be
cumbersome.
Another advantage is that it is possible to test the state machine even before
the real hardware testing. There exists simulators, or even the state machine
audit trail (let’s name e.g. MathWorks Satteflow9 as an example) so one can
test a lot before even obtaining the real hardware. The experiences show that it
is possible to remove an important portion of repeating and unnecessary code
compared to classical programming approach.
For those who are looking for more sophisticated examples of using FSMs in
robotics we can mention e.g. [7] where the authors propose this style of sequential
control is a very effective method of implementing complex robot behaviors.
Another example which combines the outputs of an artificial neural network for
visual navigation with FSM which identifies the robot’s current state and defines
which action the robot should take according to the processed image frame is
given in [8]. Even an example of a humanoid robot behavior coordinated by a
hierarchical and asynchronous finite state machine can be found; see [9]. It is
also worth mentioning that FSM was one of the approaches successfully tested
over five years by the Canadian Space Agency as one of the various autonomy
techniques on typical autonomous robotics scenarios [10].
Those who want to implement the above presented techniques do not have to
implement everything from scratch. The good news is there are several existing
9
https://www.mathworks.com/products/stateflow.html
libraries implementing the structure, e.g. for Javascript10 or even for Arduino11
which is otherwise infamously know for being used as a “first shoot then look”
kind of tools where basic functionality is rapidly made using whichever style and
then continuously upgraded and patched resulting in a total mess.
On contrary, it should be also noted that FSM is not a universal magic tool
capable of solving everything. There are many areas where using FSM is not
appropriate or where it can not be used at all. If nothing else, one should be al-
ways aware of its theoretical properties: taking into consideration the Automata
theory and Formal grammars, the FSM is able to handle only regular languages
which might not be sufficient for solving real world tasks.
References
1. Turing, A. M.: On Computable Numbers with an Application to the Entschei-
dungsproblem. Proceedings of the London Mathematical Society, Ser. 2, Vol. 42,
230–265 (1937)
2. IEC 61131-3 International Standard. Programmable controllers – Part 3: Program-
ming languages. International Electrotechnical Commission, Geneva, Switzerland.
ISBN 978-2-83220-661-4. (2013)
3. MicroSystem Simatic S7-200. Two Hour Primer. Siemens AG, Automation and
Drives, Nuremberg. (2000)
4. Amlendra: How to implement finite state machine in C. Available on-line:
https://aticleworld.com/state-machine-using-c/ (2017)
5. Balogh, R.: Ketchup House – A Promising Robotic Contest. In 3rd Conference on
Robotics in Education 2012. Praha. Volume 3, pp. 41-45. Matfyzpress. (2012)
6. Robotika.SK: Istrobot 2017 Ketchup House Competition. (2017)
http://www.robotika.sk/contest/2017/indexEN.php?page=rules&type=ketchup
7. Blank, D., Kumar, D., Meeden, L. and Yanco, H.: The Pyro toolkit for AI and
robotics. AI magazine, 27(1), 39. (2006)
8. Sales, D. O., Shinzato, P., Pessin, G., Wolf, D. F. and Osorio, F. S.: Vision-based au-
tonomous navigation system using ANN and FSM control. In Proceedings of 2010
Latin American Robotics Symposium and Intelligent Robotic Meeting (LARS),
pp. 85-90, IEEE. (2010)
9. Tellez, R., Ferro, F., Garcia, S., Gomez, E., Jorge, E., Mora, D. and Faconti, D.:
Reem-B: An autonomous lightweight human-size humanoid robot. In Proceedings
of the 8th IEEE-RAS International Conference on Humanoid Robots, pp. 462-468,
IEEE. (2008)
10. Dupuis, E., Allard, P. and L’Archevêque, R.: Autonomous robotics toolbox. In
Proceedings of International Symposium on Artificial Intelligence, Robotics and
Automation in Space (iSAIRAS), Munich, Germany. (2005)
10
https://github.com/jakesgordon/javascript-state-machine
11
http://playground.arduino.cc/Code/FiniteStateMachine