codegen/python/state.py
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 from threading import Thread
21 import time, sys
22 
23 class State():
24  def __init__(self, id, initial, cycleDuration, parent=None, gui=None):
25  self.id = id
26  self.active = False
27  self.thread = None
28  self.running = False
29  self.parent = parent
30  self.currentState = None
31  self.initial = initial
32  self.displayGui = False
33 
34  self.cycleDuration = cycleDuration
35 
36  self.states = []
37  self.transitions = []
38  self.statesById = {}
39 
40  self.gui = gui
41 
42  # add state to parent
43  if self.parent is not None:
44  self.parent.addState(self)
45 
46  def init(self):
47  for tran in self.transitions:
48  tran.init()
49  if self.gui is not None:
50  self.gui.emitRunningStateById(self.id)
51 
52  # reset the currentState as the initial state
53  for state in self.states:
54  if state.initial:
55  self.currentState = state
56  break
57  if self.currentState is not None:
58  self.currentState.init()
59 
60  def runCode(self):
61  pass
62 
63  def addState(self, state):
64  if state.initial:
65  self.currentState = state
66 
67  self.states.append(state)
68  self.statesById[state.id] = state
69 
70  def addTransition(self, tran):
71  self.transitions.append(tran)
72 
73  def startThread(self):
74  self.running = True
75  self.thread = Thread(target=self.run)
76  self.thread.start()
77 
78  def run(self):
79  initState = True
80  while(self.running):
81  startTime = self.getCurrentTime()
82 
83  runState = False
84  if self.parent is not None:
85  if self.parent.getRunningChild() == self:
86  runState = True
87  elif self.parent is None:
88  runState = True
89 
90  if initState and runState:
91  self.currentState.init()
92  initState = False
93 
94  if runState:
95  # transition evaluations
96  for tran in self.currentState.transitions:
97  if tran.checkCondition():
98  tran.runCode()
99  self.currentState = self.statesById[tran.getDestinationId()]
100  self.currentState.init()
101  initState = False
102  break
103 
104  # print('current state:' + str(self.currentState.id))
105  self.currentState.runCode()
106 
107  # control the cycle running time
108  finishTime = self.getCurrentTime()
109  # calculate elapsed time in milliseconds
110  elapsedTime = (finishTime - startTime)/1000
111 
112  if elapsedTime < self.cycleDuration:
113  elapsedTime = self.cycleDuration - elapsedTime
114  # sleep in seconds
115  time.sleep(elapsedTime / 1000)
116 
117  def stop(self):
118  self.running = False
119 
120  # returns the current time in microseconds
121  def getCurrentTime(self):
122  return time.time() * 1000000
123 
124  def join(self):
125  self.thread.join()
126 
127  def getRunningChild(self):
128  if self.parent is None:
129  return self.currentState
130  else:
131  if self.parent.getRunningChild() == self:
132  return self.currentState
133 
134  return None
135 
def __init__(self, id, initial, cycleDuration, parent=None, gui=None)


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