TkMotorComp.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", "TkMotorComp", 
00016             "type_name", "TkMotorComp", 
00017             "description", "Tk Motor component (velocity 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 TkMotorComp(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("vel", self._tk_data)
00042 
00043     self.addInPort("vel", self._tkIn)
00044     return RTC.RTC_OK
00045 
00046   def onActivated(self, ec_id):
00047     val = [self._cnt] * self._num
00048     tkm.set_angle(val)
00049     time.sleep(0.01)
00050     self._cnt += 1
00051     self._v = [0] * 6
00052     return RTC.RTC_OK
00053 
00054 
00055   def onExecute(self, ec_id):
00056     try:
00057       indata = self._tkIn.read()
00058       val = indata.data
00059       print val
00060       if len(val) == 6:
00061         for i in range(6):
00062           self._v[i] += val[i] / 2
00063         tkm.set_angle(self._v)
00064     except:
00065       print "Exception cought in onExecute()"
00066 
00067     time.sleep(0.01)
00068     return RTC.RTC_OK
00069 
00070 
00071   def onShutdown(self, ec_id):
00072     tkm.quit()
00073     return RTC.RTC_OK
00074 
00075 
00076 
00077 def TkMotorCompInit(manager):
00078   profile = OpenRTM_aist.Properties(defaults_str=mod_spec)
00079   manager.registerFactory(profile,
00080                           TkMotorComp,
00081                           OpenRTM_aist.Delete)
00082 
00083 
00084 def MyModuleInit(manager):
00085   TkMotorCompInit(manager)
00086 
00087   # Create a component
00088   comp = manager.createComponent("TkMotorComp")
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