TkMotorPosComp.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # -*- Python -*-
00004 
00005 import sys
00006 sys.path.append(".")
00007 
00008 import RTC
00009 import OpenRTM_aist
00010 
00011 import tkmotor
00012 import time
00013 
00014 
00015 mod_spec = ["implementation_id", "TkMotorPosComp", 
00016             "type_name", "TkMotorPosComp", 
00017             "description", "Tk Motor component (position control)", 
00018             "version", "1.0", 
00019             "vendor", "Noriaki Ando and Shinji Kurihara", 
00020             "category", "Generic", 
00021             "activity_type", "DataFlowComponent", 
00022             "max_instance", "10", 
00023             "language", "Python", 
00024             "lang_type""SCRIPT",
00025             ""]
00026 
00027 
00028 tkm = tkmotor.TkMotor(6, 40)
00029 #thread.start_new_thread(tkm.mainloop, ())
00030 
00031 class TkMotorPosComp(OpenRTM_aist.DataFlowComponentBase):
00032   def __init__(self, manager):
00033     OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
00034     self._cnt = 0
00035     self._num = 6
00036     return
00037 
00038 
00039   def onInitialize(self):
00040     self._tk_data = RTC.TimedFloatSeq(RTC.Time(0,0), [])
00041     self._tkIn = OpenRTM_aist.InPort("pos", self._tk_data)
00042 
00043     self.addInPort("pos", self._tkIn)
00044     return RTC.RTC_OK
00045 
00046 
00047   def onActivated(self, ec_id):
00048     val = [self._cnt] * self._num
00049     tkm.set_angle(val)
00050     time.sleep(0.01)
00051     self._cnt += 1
00052     self._v = [0] * 6
00053     return RTC.RTC_OK
00054 
00055 
00056   def onExecute(self, ec_id):
00057     try:
00058       indata = self._tkIn.read()
00059       val = indata.data
00060       print val
00061       if len(val) == 6:
00062         for i in range(6):
00063           self._v[i] = val[i] * 10
00064         tkm.set_angle(self._v)
00065     except:
00066       print "Exception cought in onExecute()"
00067 
00068     time.sleep(0.01)
00069     return RTC.RTC_OK
00070 
00071 
00072   def onShutdown(self, ec_id):
00073     tkm.quit()
00074     return RTC.RTC_OK
00075 
00076 
00077 
00078 def TkMotorPosCompInit(manager):
00079   profile = OpenRTM_aist.Properties(defaults_str=mod_spec)
00080   manager.registerFactory(profile,
00081                           TkMotorPosComp,
00082                           OpenRTM_aist.Delete)
00083 
00084 def MyModuleInit(manager):
00085   TkMotorPosCompInit(manager)
00086 
00087   # Create a component
00088   comp = manager.createComponent("TkMotorPosComp")
00089 
00090   print "Componet created"
00091 
00092 
00093 def main():
00094   # Initialize manager
00095   mgr = OpenRTM_aist.Manager.init(sys.argv)
00096 
00097   # Set module initialization proceduer
00098   # This procedure will be invoked in activateManager() function.
00099   mgr.setModuleInitProc(MyModuleInit)
00100 
00101   # Activate manager and register to naming service
00102   mgr.activateManager()
00103 
00104   # run the manager in blocking mode
00105   # runManager(False) is the default
00106   #mgr.runManager()
00107 
00108   # If you want to run the manager in non-blocking mode, do like this
00109   mgr.runManager(True)
00110   tkm.mainloop()
00111 
00112 if __name__ == "__main__":
00113   main()


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28