StateMachine.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
16 
17 
18 import threading
19 
20 import OpenRTM_aist
21 import RTC
22 
23 
24 
40  def __init__(self):
41  self.curr = None
42  self.prev = None
43  self.next = None
44 
45 
46 
135  """
136  """
137 
138  state_array = (RTC.CREATED_STATE,
139  RTC.INACTIVE_STATE,
140  RTC.ACTIVE_STATE,
141  RTC.ERROR_STATE)
142 
143 
155  def __init__(self, num_of_state):
156  self._num = num_of_state
157  self._entry = {}
158  self._predo = {}
159  self._do = {}
160  self._postdo = {}
161  self._exit = {}
162 
163  self.setNullFunc(self._entry, None)
164  self.setNullFunc(self._do, None)
165  self.setNullFunc(self._exit, None)
166  self.setNullFunc(self._predo, None)
167  self.setNullFunc(self._postdo, None)
168  self._transit = None
169  self._mutex = threading.RLock()
170 
171 
172 
184  def setNOP(self, call_back):
185  self.setNullFunc(self._entry, call_back)
186  self.setNullFunc(self._do, call_back)
187  self.setNullFunc(self._exit, call_back)
188  self.setNullFunc(self._predo, call_back)
189  self.setNullFunc(self._postdo, call_back)
190  self._transit = call_back
191 
192 
193 
205  def setListener(self, listener):
206  self._listener = listener
207 
208 
209 
224  def setEntryAction(self, state, call_back):
225  if self._entry.has_key(state):
226  self._entry[state] = call_back
227  else:
228  self._entry.setdefault(state, call_back)
229  return True
230 
231 
232 
247  def setPreDoAction(self, state, call_back):
248  if self._predo.has_key(state):
249  self._predo[state] = call_back
250  else:
251  self._predo.setdefault(state, call_back)
252  return True
253 
254 
255 
270  def setDoAction(self, state, call_back):
271  if self._do.has_key(state):
272  self._do[state] = call_back
273  else:
274  self._do.setdefault(state, call_back)
275  return True
276 
277 
278 
293  def setPostDoAction(self, state, call_back):
294  if self._postdo.has_key(state):
295  self._postdo[state] = call_back
296  else:
297  self._postdo.setdefault(state, call_back)
298  return True
299 
300 
301 
316  def setExitAction(self, state, call_back):
317  if self._exit.has_key(state):
318  self._exit[state] = call_back
319  else:
320  self._exit.setdefault(state, call_back)
321  return True
322 
323 
324 
339  def setTransitionAction(self, call_back):
340  self._transit = call_back
341  return True
342 
343 
344 
356  def setStartState(self, states):
358  self._states.curr = states.curr
359  self._states.prev = states.prev
360  self._states.next = states.next
361 
362 
363 
377  def getStates(self):
378  guard = OpenRTM_aist.ScopedLock(self._mutex)
379  return self._states
380 
381 
382 
395  def getState(self):
396  guard = OpenRTM_aist.ScopedLock(self._mutex)
397  return self._states.curr
398 
399 
400 
414  def isIn(self, state):
415  guard = OpenRTM_aist.ScopedLock(self._mutex)
416  if self._states.curr == state:
417  return True
418  else:
419  return False
420 
421 
422 
438  def goTo(self, state):
439  guard = OpenRTM_aist.ScopedLock(self._mutex)
440  self._states.next = state
441 
442 
443 
455  def worker(self):
456  states = StateHolder()
457  self.sync(states)
458 
459  # If no state transition required, execute set of do-actions
460  if states.curr == states.next:
461  # pre-do
462  if self._predo[states.curr]:
463  self._predo[states.curr](states)
464  if self.need_trans():
465  return
466 
467  # do
468  if self._do[states.curr]:
469  self._do[states.curr](states)
470  if self.need_trans():
471  return
472 
473  # post-do
474  if self._postdo[states.curr]:
475  self._postdo[states.curr](states)
476  # If state transition required, exit current state and enter next state
477  else:
478  if self._exit[states.curr]:
479  self._exit[states.curr](states)
480  self.sync(states)
481 
482  # If state transition still required, move to the next state
483  if states.curr != states.next:
484  states.curr = states.next
485  if self._entry[states.curr]:
486  self._entry[states.curr](states)
487  self.update_curr(states.curr)
488 
489 
490 
503  def setNullFunc(self, s, nullfunc):
504  for i in range(self._num):
505  if s.has_key(StateMachine.state_array[i]):
506  s[StateMachine.state_array[i]] = nullfunc
507  else:
508  s.setdefault(StateMachine.state_array[i], nullfunc)
509 
510 
511 
520  def sync(self, states):
521  guard = OpenRTM_aist.ScopedLock(self._mutex)
522  states.prev = self._states.prev
523  states.curr = self._states.curr
524  states.next = self._states.next
525 
526 
527 
528 
538  def need_trans(self):
539  guard = OpenRTM_aist.ScopedLock(self._mutex)
540  return (self._states.curr != self._states.next)
541 
542 
543 
552  def update_curr(self, curr):
553  guard = OpenRTM_aist.ScopedLock(self._mutex)
554  self._states.curr = curr
def __init__(self, num_of_state)
Constructor.
def setTransitionAction(self, call_back)
Set state transition action function.
def setPreDoAction(self, state, call_back)
Set PreDo action function.
def setNullFunc(self, s, nullfunc)
Worker function.
def setNOP(self, call_back)
Set NOP function.
def getStates(self)
Get state machine's status.
def setPostDoAction(self, state, call_back)
Set PostDo action function.
def setStartState(self, states)
Set Exit action function.
def isIn(self, state)
Evaluate current status.
def getState(self)
Get current state.
def worker(self)
Worker function.
def setEntryAction(self, state, call_back)
Set Entry action function.
def setExitAction(self, state, call_back)
Set Exit action function.
def setDoAction(self, state, call_back)
Set Do action function.
def goTo(self, state)
Change status.
def setListener(self, listener)
Set Listener Object.


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:06