RP6 ROBOT SYSTEM - 4. Programming the RP6
Example 9: Excursion – Finite state machines
Directory: <RP6Examples>\RP6BaseExamples\Example_05_Move_04_FSM\
File: RP6Base_Move_04_FSM.c
This program will output messages on the serial interface
The robot does not move in this example program!
For more complex programs, the simple methods we discussed so far may not be suf-
ficient.
For instance, behaviour controlled robots require so-called Finite State Machines
(shortcut FSMs) – this would not be that easy with simple methods. This example pro-
gram demonstrates a simple FSM changing its state if a bumper is hit. To understand
the program's operation, just try it out and press the bumpers twice while you care-
fully observe the terminal's display and the status-LEDs. Press and then release the
bumpers slowly!
In C, most Finite State Machines will be designed by using switch-case constructs, or
alternatively a bunch of conditional statements by using “if-else-if-else”-constructs...
however switch/case will result in more clearly arranged source code.
Let's have a look at a simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "RP6RobotBaseLib.h"
#define STATE_START 0
#define STATE_1 1
#define STATE_2 2
#define STATE_3 3
uint8_t
state
=
STATE_START
;
void
simple_stateMachine
(
void
)
{
switch
(
state
)
{
case
STATE_START
:
writeString
(
"\nSTART\n"
);
state = STATE_1;
break
;
case
STATE_1
:
writeString
(
"State 1\n"
);
state = STATE_2;
break
;
case
STATE_2
:
writeString
(
"State 2\n"
);
state = STATE_3;
break
;
case
STATE_3
:
writeString
(
"State 3\n"
);
state = STATE_START;
break
;
}
}
int
main
(
void
)
{
initRobotBase
();
while
(
true
)
{
simple_stateMachine
();
mSleep
(
500
);
}
return
0
;
}
- 115 -