-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate_machine.cpp
More file actions
91 lines (79 loc) · 2.74 KB
/
state_machine.cpp
File metadata and controls
91 lines (79 loc) · 2.74 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*************************************************
* Publicly released by Rhoban System, August 2012
* www.rhoban-system.fr
*
* Freely usable for non-commercial purposes
*
* Licence Creative Commons *CC BY-NC-SA
* http://creativecommons.org/licenses/by-nc-sa/3.0
*************************************************/
/** ***************************************************************************
* \file state_machine.cpp
* \author Hugo Gimbert - Olivier Ly
* \date 2010-12
*
* \brief State machine infrastructure.
*****************************************************************************/
#include "state_machine.h"
#include <list>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
/*****************************************************************************/
#define DBUG(msg,...) { printf(msg __VA_ARGS__); fflush(stdout); };
/*****************************************************************************/
int StateMachine::reset() {
init();
if (init_state == NULL) return 0;
active_state = init_state;
if (debug) printf("[Machine %s]: starting from state %s.\n", get_machine_id().c_str(), init_state->get_state_id().c_str());
this->entry();
init_state->entry();
return 1;
}
int StateMachine::tic() {
if (next_state != NULL) {
if (transition_delay > 0) {
transition_delay--;
}
else {
if (debug) printf("[Machine %s]: leaving %s\n", get_machine_id().c_str(), active_state->get_state_id().c_str());
active_state->quit();
active_state->intern_set_active(false);
active_state = next_state;
active_state->father = this;
next_state = NULL;
active_state->intern_set_active(true);
if (debug) printf("[Machine %s]: entering %s\n", get_machine_id().c_str(), active_state->get_state_id().c_str());
active_state->entry();
}
}
if (active_state==NULL) return -1;
if (!(active_state->is_sleeping())) {
active_state->activity();
this->activity();
}
/* look for transition */
if (next_state != NULL) return 1;
State * new_state = NULL;
new_state = this->transition(); // First: check for machine global transition
if (new_state == NULL) new_state = active_state->transition(); // Then: one checks the transition of the current state
if (new_state != NULL)
next_state = new_state;
if (active_state->terminal()) {
this->quit();
return 0;
}
else return 1;
}
StateMachine::StateMachine() :
debug(false),
transition_delay(0),
init_state(NULL),
active_state(NULL),
next_state(NULL),
frequency(TICK_FREQUENCY_DEFAULT)
{}
/*****************************************************************************/
/*****************************************************************************/