state.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19 */
20 #include <visualstates/state.h>
21 #include <unistd.h>
22 #include <sys/time.h>
23 #include <iostream>
24 
25 State::State(int id, bool initial, int cycleDuration, State* parent, RunTimeGui* gui) {
26  this->id = id;
27  this->initial = initial;
28  this->cycleDuration = cycleDuration;
29  this->parent = parent;
30  this->gui = gui;
31  running = true;
32  currentState = NULL;
33 
34  if (parent != NULL) {
35  parent->addState(this);
36  }
37 }
38 
39 void State::init() {
40  for (unsigned int i = 0; i < transitions.size(); i++) {
41  transitions.at(i)->init();
42  }
43 
44  if (gui != NULL) {
46  }
47 
48  // re-set the initial state as the current state
49  for (unsigned int i = 0; i < states.size(); i++) {
50  if (states.at(i)->initial) {
51  currentState = states.at(i);
52  break;
53  }
54  }
55 
56  if (currentState != NULL) {
57  currentState->init();
58  }
59 }
60 
61 void State::addState(State* state) {
62  if (state->initial) {
63  currentState = state;
64  }
65 
66  states.push_back(state);
67  statesById.insert(std::pair<int, State*>(state->id, state));
68 }
69 
71  transitions.push_back(tran);
72 }
73 
74 void* State::threadRunner(void* owner) {
75  ((State*)owner)->run();
76 }
77 
79  pthread_create(&thread, NULL, &State::threadRunner, this);
80 }
81 
82 void State::run() {
83  bool initState = true;
84  while (running) {
85  long startTime = getCurrentTime();
86 
87  bool runState = false;
88  if (parent != NULL) {
89  if (parent->currentState == this) {
90  runState = true;
91  }
92  } else if (parent == NULL) {
93  runState = true;
94  }
95 
96  if (initState) {
97  currentState->init();
98  initState = false;
99  }
100 
101  if (runState) {
102  // transition evaluations
103  for (unsigned int i = 0; i < currentState->transitions.size(); i++) {
104  Transition* tran = currentState->transitions.at(i);
105  if (tran->checkCondition()) {
106  tran->runCode();
108  currentState->init();
109  }
110  }
111  // user code of the state
113  }
114 
115  long finishTime = getCurrentTime();
116  long elapsedTime = (finishTime - startTime) / 1000;
117  if (elapsedTime < cycleDuration) {
118  elapsedTime = cycleDuration - elapsedTime;
119  // convert milliseconds to microseconds
120  usleep(elapsedTime*1000);
121  }
122  }
123 }
124 
125 void State::stop() {
126  running = false;
127 }
128 
130  struct timeval a;
131  gettimeofday(&a, NULL);
132  return (a.tv_sec * 1000000 + a.tv_usec)/1000;
133 }
134 
135 void State::join() {
136  pthread_join(thread, NULL);
137 }
void init()
Definition: state.cpp:39
virtual void runCode()=0
long getCurrentTime()
Definition: state.cpp:129
static void * threadRunner(void *)
Definition: state.cpp:74
bool initial
Definition: state.h:35
int getDestinationId()
Definition: transition.cpp:27
int id
Definition: state.h:30
virtual void runCode()
Definition: state.h:58
State(int id, bool initial, int cycleDuration, State *parent, RunTimeGui *gui)
Definition: state.cpp:25
RunTimeGui * gui
Definition: state.h:43
State * parent
Definition: state.h:33
Definition: state.h:28
void addTransition(Transition *transition)
Definition: state.cpp:70
std::vector< State * > states
Definition: state.h:39
void join()
Definition: state.cpp:135
void run()
Definition: state.cpp:82
void stop()
Definition: state.cpp:125
void addState(State *state)
Definition: state.cpp:61
State * currentState
Definition: state.h:34
virtual bool checkCondition()=0
std::map< int, State * > statesById
Definition: state.h:41
void emitRunningStateById(int id)
Definition: runtimegui.cpp:28
std::vector< Transition * > transitions
Definition: state.h:40
pthread_t thread
Definition: state.h:44
int cycleDuration
Definition: state.h:37
void startThread()
Definition: state.cpp:78
bool running
Definition: state.h:32


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20