[openrtm-commit:03291] r971 - trunk/OpenRTM-aist-Python/OpenRTM_aist

openrtm @ openrtm.org openrtm @ openrtm.org
2018年 3月 28日 (水) 10:17:48 JST


Author: miyamoto
Date: 2018-03-28 10:17:48 +0900 (Wed, 28 Mar 2018)
New Revision: 971

Added:
   trunk/OpenRTM-aist-Python/OpenRTM_aist/EventPort_pyfsm.py
   trunk/OpenRTM-aist-Python/OpenRTM_aist/StaticFSM_pyfsm.py
Log:
[merge] r837,851-858,861 have been merged from FSM4RTC.

Added: trunk/OpenRTM-aist-Python/OpenRTM_aist/EventPort_pyfsm.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/EventPort_pyfsm.py	                        (rev 0)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/EventPort_pyfsm.py	2018-03-28 01:17:48 UTC (rev 971)
@@ -0,0 +1,221 @@
+#!/usr/bin/env python
+# -*- coding: euc-jp -*-
+
+##
+# @file EventPort_pyfsm.py
+# @brief EventInPort template class
+# @date $Date: $
+# @author Nobuhiko Miyamoto <n-miyamoto at aist.go.jp>
+#
+# Copyright (C) 2017
+#     Intelligent Systems Research Institute,
+#     National Institute of
+#         Advanced Industrial Science and Technology (AIST), Japan
+#     All rights reserved.
+
+import OpenRTM_aist
+import OpenRTM_aist.StaticFSM_pyfsm
+import pyfsm
+
+
+class EventBinder0(OpenRTM_aist.ConnectorDataListener):
+  def __init__(self, fsm, event_name, handler, ptask=False):
+    self._fsm = fsm
+    self._eventName = event_name
+    self._handler = handler
+    self._ptask = ptask
+  def __del__(self):
+    pass
+  def __call__(self, info, data):
+    if info.properties.getProperty("fsm_event_name") == self._eventName or info.name == self._eventName:
+      if not self._ptask:
+        self._fsm.dispatch(pyfsm.Event(self._handler))
+      else:
+        task = OpenRTM_aist.Async_tInvoker(self._fsm, pyfsm.Machine.dispatch, pyfsm.Event(self._handler))
+        task.invoke()
+      return OpenRTM_aist.ConnectorListenerStatus.NO_CHANGE
+    return OpenRTM_aist.ConnectorListenerStatus.NO_CHANGE
+
+    
+
+class EventBinder1(OpenRTM_aist.ConnectorDataListenerT):
+  def __init__(self, fsm, event_name, handler, data_type, ptask=False):
+    self._fsm = fsm
+    self._eventName = event_name
+    self._handler = handler
+    self._data_type = data_type
+    self._ptask = ptask
+  def __del__(self):
+    pass
+  def __call__(self, info, data):
+    data_ = OpenRTM_aist.ConnectorDataListenerT.__call__(self, info, data, self._data_type)
+    
+    if info.properties.getProperty("fsm_event_name") == self._eventName or info.name == self._eventName:
+      if not self._ptask:
+        self._fsm.dispatch(pyfsm.Event(self._handler, data_))
+      else:
+        task = OpenRTM_aist.Async_tInvoker(self._fsm, pyfsm.Machine.dispatch, pyfsm.Event(self._handler, data_))
+        task.invoke()
+      return OpenRTM_aist.ConnectorListenerStatus.NO_CHANGE
+    return OpenRTM_aist.ConnectorListenerStatus.NO_CHANGE
+
+
+
+
+
+
+##
+# @if jp
+#
+# @class EventInPort
+#
+# @brief EventInPort ¥Æ¥ó¥×¥ì¡¼¥È¥¯¥é¥¹
+# 
+# EventInPort ¤Î¼ÂÁõ¤Ç¤¢¤ë EventInPort<T> ¤Î¥Æ¥ó¥×¥ì¡¼¥È¥¯¥é¥¹¡£
+# <T> ¤ÏBasicDataType.idl ¤Ë¤ÆÄêµÁ¤µ¤ì¤Æ¤¤¤ë·¿¤Ç¡¢¥á¥ó¥Ð¤È¤·¤Æ
+# Time ·¿¤Î tm , ¤ª¤è¤Ó T·¿¤Î data ¤ò»ý¤Ä¹½Â¤ÂΤǤʤ¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
+# EventInPort ¤ÏÆâÉô¤Ë¥ê¥ó¥°¥Ð¥Ã¥Õ¥¡¤ò»ý¤Á¡¢³°Éô¤«¤éÁ÷¿®¤µ¤ì¤¿¥Ç¡¼¥¿¤ò½ç¼¡
+# ¤³¤Î¥ê¥ó¥°¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤¹¤ë¡£¥ê¥ó¥°¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤Ï¥Ç¥Õ¥©¥ë¥È¤Ç64¤È
+# ¤Ê¤Ã¤Æ¤¤¤ë¤¬¡¢¥³¥ó¥¹¥È¥é¥¯¥¿°ú¿ô¤Ë¤è¤ê¥µ¥¤¥º¤ò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
+# ¥Ç¡¼¥¿¤Ï¥Õ¥é¥°¤Ë¤è¤Ã¤Æ̤ÆÉ¡¢´ûÆɾõÂÖ¤¬´ÉÍý¤µ¤ì¡¢isNew(), write(), read(),
+# isFull(), isEmpty() Åù¤Î¥á¥½¥Ã¥É¤Ë¤è¤ê¥Ï¥ó¥É¥ê¥ó¥°¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
+#   
+# OnRead·Ï¥³¡¼¥ë¥Ð¥Ã¥¯ (Æɤ߽Ф·¤Ëµ¯°ø¤¹¤ë¥¤¥Ù¥ó¥È¤Ë¤è¤ê¥³¡¼¥ë¤µ¤ì¤ë)
+#
+# - void OnRead::operator(): 
+#     EventInPort::read() ¤ò¸Æ¤Ó½Ð¤·Æɤ߽Ф·¤ò¹Ô¤¦ºÝ¤Ë¥³¡¼¥ë¤µ¤ì¤ë¡£
+#
+# - DataType OnReadConvert::operator(DataType): 
+#     EventInPort::read() ¤ò¸Æ¤Ó½Ð¤·¡¢¥Ç¡¼¥¿¤ò¥Ð¥Ã¥Õ¥¡¤«¤éÆɤߤÀ¤¹ºÝ¤Ë¸Æ¤Ð¤ì
+#     ¥Ç¡¼¥¿¤ÎÊÑ´¹¤ò¹Ô¤¦¡£°ú¿ô¤Ë¤Ï¥Ð¥Ã¥Õ¥¡¤«¤éÆɤ߽Фµ¤ì¤¿Ãͤ¬Í¿¤¨¤é¤ì¡¢
+#     ÊÑ´¹¸å¤Î¥Ç¡¼¥¿¤òÌá¤êÃͤȤ·¤ÆÊÖ¤¹¡£¤³¤ÎÃͤ¬read()¤ÎÊÖ¤¹ÃͤȤʤ롣
+#
+# @since 0.2.0
+#
+# @else
+#
+# @class EventInPort
+#
+# @brief EventInPort template class
+#
+# This is a template class that implements EventInPort.  <T> is the type
+# defined in BasicDataType.idl and must be the structure which has
+# both Time type tm and type-T data as a member. EventInPort has a ring
+# buffer internally, and stores the received data externally in
+# this buffer one by one. The size of ring buffer can be specified
+# according to the argument of constructor, though the default size
+# is 64. Unread data and data which is already read are managed
+# with the flag, and the data can be handled by the isNew(),
+# write(), read(), isFull() and isEmpty() method etc.
+#
+# @since 0.2.0
+#
+# @endif
+#
+class EventInPort(OpenRTM_aist.InPortBase):
+  ##
+  # @if jp
+  #
+  # @brief ¥³¥ó¥¹¥È¥é¥¯¥¿
+  #
+  # ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
+  # ¥Ñ¥é¥á¡¼¥¿¤È¤·¤ÆÍ¿¤¨¤é¤ì¤ë T ·¿¤ÎÊÑ¿ô¤Ë¥Ð¥¤¥ó¥É¤µ¤ì¤ë¡£
+  #
+  # @param name EventInPort ̾¡£EventInPortBase:name() ¤Ë¤è¤ê»²¾È¤µ¤ì¤ë¡£
+  # @param value ¤³¤Î EventInPort ¤Ë¥Ð¥¤¥ó¥É¤µ¤ì¤ë T ·¿¤ÎÊÑ¿ô
+  # @param bufsize EventInPort ÆâÉô¤Î¥ê¥ó¥°¥Ð¥Ã¥Õ¥¡¤Î¥Ð¥Ã¥Õ¥¡Ä¹(¥Ç¥Õ¥©¥ë¥ÈÃÍ:64)
+  # @param read_block Æɹþ¥Ö¥í¥Ã¥¯¥Õ¥é¥°¡£
+  #        ¥Ç¡¼¥¿Æɹþ»þ¤Ë̤Æɥǡ¼¥¿¤¬¤Ê¤¤¾ì¹ç¡¢¼¡¤Î¥Ç¡¼¥¿¼õ¿®¤Þ¤Ç¥Ö¥í¥Ã¥¯¤¹¤ë
+  #        ¤«¤É¤¦¤«¤òÀßÄê(¥Ç¥Õ¥©¥ë¥ÈÃÍ:false)
+  # @param write_block ½ñ¹þ¥Ö¥í¥Ã¥¯¥Õ¥é¥°¡£
+  #        ¥Ç¡¼¥¿½ñ¹þ»þ¤Ë¥Ð¥Ã¥Õ¥¡¤¬¥Õ¥ë¤Ç¤¢¤Ã¤¿¾ì¹ç¡¢¥Ð¥Ã¥Õ¥¡¤Ë¶õ¤­¤¬¤Ç¤­¤ë
+  #        ¤Þ¤Ç¥Ö¥í¥Ã¥¯¤¹¤ë¤«¤É¤¦¤«¤òÀßÄê(¥Ç¥Õ¥©¥ë¥ÈÃÍ:false)
+  # @param read_timeout Æɹþ¥Ö¥í¥Ã¥¯¤ò»ØÄꤷ¤Æ¤¤¤Ê¤¤¾ì¹ç¤Î¡¢¥Ç¡¼¥¿Æɼ西¥¤¥à
+  #        ¥¢¥¦¥È»þ´Ö(¥ß¥êÉÃ)(¥Ç¥Õ¥©¥ë¥ÈÃÍ:0)
+  # @param write_timeout ½ñ¹þ¥Ö¥í¥Ã¥¯¤ò»ØÄꤷ¤Æ¤¤¤Ê¤¤¾ì¹ç¤Î¡¢¥Ç¡¼¥¿½ñ¹þ¥¿¥¤¥à
+  #        ¥¢¥¦¥È»þ´Ö(¥ß¥êÉÃ)(¥Ç¥Õ¥©¥ë¥ÈÃÍ:0)
+  #
+  # @else
+  #
+  # @brief A constructor.
+  #
+  # constructor.
+  # This is bound to type-T variable given as a parameter.
+  #
+  # @param name A name of the EventInPort. This name is referred by
+  #             EventInPortBase::name().
+  # @param value type-T variable that is bound to this EventInPort.
+  # @param bufsize Buffer length of internal ring buffer of EventInPort
+  #                (The default value:64)
+  # @param read_block Flag of reading block.
+  #                   When there are not unread data at reading data,
+  #                   set whether to block data until receiving the next 
+  #                   data. (The default value:false)
+  # @param write_block Flag of writing block.
+  #                    If the buffer was full at writing data, set whether 
+  #                    to block data until the buffer has space. 
+  #                    (The default value:false)
+  # @param read_timeout Data reading timeout time (millisecond) 
+  #                     when not specifying read blocking.
+  #                     (The default value:0)
+  # @param write_timeout Data writing timeout time (millisecond)
+  #                      when not specifying writing block.
+  #                      (The default value:0)
+  #
+  # @endif
+  #
+  def __init__(self, name, fsm, bufsize=64, read_block=False, write_block=False, read_timeout=0, write_timeout=0):
+    super(EventInPort, self).__init__(name, "any")
+    self._name = name
+    self._fsm = fsm
+  ##
+  # @if jp
+  #
+  # @brief ¥Ç¥¹¥È¥é¥¯¥¿
+  #
+  # ¥Ç¥¹¥È¥é¥¯¥¿¡£
+  #
+  # @else
+  #
+  # @brief Destructor
+  #
+  # Destructor
+  #
+  # @endif
+  #
+  def __del__(self):
+    pass
+  ##
+  # @if jp
+  #
+  # @brief ¥Ý¡¼¥È̾¾Î¤ò¼èÆÀ¤¹¤ë¡£
+  #
+  # ¥Ý¡¼¥È̾¾Î¤ò¼èÆÀ¤¹¤ë¡£
+  #
+  # @return ¥Ý¡¼¥È̾¾Î
+  #
+  # @else
+  #
+  # @brief Get port name
+  #
+  # Get port name.
+  #
+  # @return The port name
+  #
+  # @endif
+  #
+  def name(self):
+    return self._name
+
+  def bindEvent0(self, name, handler, ptask=False):
+    self.addConnectorDataListener(OpenRTM_aist.ConnectorDataListenerType.ON_RECEIVED,
+                                  EventBinder0(self._fsm, name, handler, ptask))
+    
+  def bindEvent1(self, name, handler, data_type, ptask=False):
+    self.addConnectorDataListener(OpenRTM_aist.ConnectorDataListenerType.ON_RECEIVED,
+                                  EventBinder1(self._fsm, name, handler, data_type, ptask))
+  
+
+
+
+

Added: trunk/OpenRTM-aist-Python/OpenRTM_aist/StaticFSM_pyfsm.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/StaticFSM_pyfsm.py	                        (rev 0)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/StaticFSM_pyfsm.py	2018-03-28 01:17:48 UTC (rev 971)
@@ -0,0 +1,130 @@
+#!/usr/bin/env python
+# -*- coding: euc-jp -*-
+
+##
+# @file StaticFSM_pyfsm.py
+# @brief Static FSM framework based on pyfsm
+# @date $Date: $
+# @author Nobuhiko Miyamoto <n-miyamoto at aist.go.jp>
+#
+# Copyright (C) 2017
+#     Intelligent Systems Research Institute,
+#     National Institute of
+#         Advanced Industrial Science and Technology (AIST), Japan
+#     All rights reserved.
+
+import OpenRTM_aist
+import pyfsm
+import RTC
+
+
+def fsm_topstate(TOP):
+  ret = pyfsm.topstate(TOP)
+  class STATE(ret):
+    def __init__(self, instance):
+      ret.__init__(self, instance)
+    def on_entry(self, *argv):
+      ret.call_entry(self, ret, *argv)
+    def on_exit(self, *argv):
+      ret.call_exit(self, ret, *argv)
+    def on_init(self, *argv):
+      ret.call_init(self, ret, *argv)
+  return STATE
+
+
+def fsm_substate(superstate):
+  def _fsm_substate(cls):
+    ret = pyfsm.substate(superstate)(cls)
+    class STATE(ret):
+      def __init__(self, instance):
+        ret.__init__(self, instance)
+      def on_entry(self, *argv):
+        ret.call_entry(self, cls, *argv)
+      def on_exit(self, *argv):
+        ret.call_exit(self, cls, *argv)
+      def on_init(self, *argv):
+        ret.call_init(self, cls, *argv)
+
+    return STATE
+  return _fsm_substate
+
+
+
+def FSM_TOPSTATE(TOP):
+  return fsm_topstate(TOP)
+
+
+def FSM_SUBSTATE(SUPERSTATE):
+  return fsm_substate(SUPERSTATE)
+
+
+
+class Machine(pyfsm.Machine):
+  def __init__(self, TOP, comp):
+    self._rtComponent = comp
+    super(Machine,self).__init__(TOP)
+    
+    
+    
+  def __del__(self):
+    pass
+  def init_other(self, other):
+    pass
+  def equal(self, snapshot):
+    pass
+  def getComp(self):
+    return self._rtComponent
+
+
+
+class Link(pyfsm.StateDef):
+  def __init__(self):
+    super(Link,self).__init__()
+    self._rtComponent = None
+  def __del__(self):
+    pass
+  def setrtc(self):
+    if self._rtComponent:
+      return
+    machine = self._myStateInstance.machine()
+    if machine:
+      self._rtComponent = machine.getComp()
+
+  def call_entry(self, cls, *argv):
+    self.setrtc()
+    if not self._rtComponent:
+      self.onEntry(*argv)
+    else:
+      self._rtComponent.postOnFsmStateChange(self._state_name(), RTC.RTC_OK)
+      self._rtComponent.preOnFsmEntry(self._state_name())
+      self._rtComponent.postOnFsmEntry(self._state_name(),cls.onEntry(self, *argv))
+
+  def call_init(self, cls, *argv):
+    self.setrtc()
+    if not self._rtComponent:
+      self.onInit(*argv)
+    else:
+      self._rtComponent.preOnFsmInit(self._state_name())
+      self._rtComponent.postOnFsmInit(self._state_name(), cls.onInit(self, *argv))
+
+
+  def call_exit(self, cls, *argv):
+    self.setrtc()
+    if not self._rtComponent:
+      self.onExit(*argv)
+    else:
+      self._rtComponent.preOnFsmExit(self._state_name())
+      self._rtComponent.postOnFsmExit(self._state_name(), cls.onExit(self, *argv))
+      self._rtComponent.preOnFsmStateChange(self._state_name())
+
+  def onEntry(self):
+    return RTC.RTC_OK
+  def onInit(self):
+    return RTC.RTC_OK
+  def onExit(self):
+    return RTC.RTC_OK
+
+
+State = pyfsm.State
+deephistory = pyfsm.deephistory
+Event = pyfsm.Event
\ No newline at end of file



openrtm-commit メーリングリストの案内