AutoControl.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # -*- Python -*-
00004 
00005 import sys
00006 import time
00007 sys.path.append(".")
00008 
00009 # Import RTM module
00010 import RTC
00011 import OpenRTM_aist
00012 
00013 # Import Service implementation class
00014 # <rtc-template block="service_impl">
00015 
00016 # </rtc-template>
00017 
00018 # Import Service stub modules
00019 # <rtc-template block="consumer_import">
00020 # </rtc-template>
00021 
00022 
00023 # This module's spesification
00024 # <rtc-template block="module_spec">
00025 AutoControl_spec = ["implementation_id", "AutoControl", 
00026                     "type_name",         "AutoControl", 
00027                     "description",       "Auto controller component for MobileRobot", 
00028                     "version",           "1.0.0", 
00029                     "vendor",            "AIST", 
00030                     "category",          "example", 
00031                     "activity_type",     "DataFlowComponent", 
00032                     "max_instance",      "1", 
00033                     "language",          "Python", 
00034                     "lang_type",         "SCRIPT",
00035                     "conf.default.velocity", "80.0",
00036                     "conf.default.turn_velocity", "80.0",
00037                     "conf.default.distance_to_env", "40.0",
00038                     ""]
00039 # </rtc-template>
00040 
00041 class AutoControl(OpenRTM_aist.DataFlowComponentBase):
00042   def __init__(self, manager):
00043     OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
00044     # initialize of configuration-data.
00045     # <rtc-template block="init_conf_param">
00046     self._velocity = [80.0]
00047     self._turn_velocity = [80.0]
00048     self._distance_to_env = [40.0]
00049 
00050     return
00051 
00052      
00053   def onInitialize(self):
00054     # Bind variables and configuration variable
00055     self.bindParameter("velocity", self._velocity, "80.0")
00056     self.bindParameter("turn_velocity", self._turn_velocity, "80.0")
00057     self.bindParameter("distance_to_env", self._distance_to_env, "40.0")
00058     
00059     self._d_sens = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00060     self._sensIn = OpenRTM_aist.InPort("sens", self._d_sens)
00061     self._d_vel = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00062     self._velOut = OpenRTM_aist.OutPort("vel", self._d_vel)
00063     
00064     # Set InPort buffers
00065     self.addInPort("sens",self._sensIn)
00066     
00067     # Set OutPort buffers
00068     self.addOutPort("vel",self._velOut)
00069 
00070     return RTC.RTC_OK
00071 
00072 
00073   
00074   def onActivated(self, ec_id):
00075     self._d_vel.data  = [0.0, 0.0]
00076     self._velOut.write()
00077     return RTC.RTC_OK
00078   
00079   def onDeactivated(self, ec_id):
00080     self._d_vel.data  = [0.0, 0.0]
00081     self._velOut.write()
00082     return RTC.RTC_OK
00083   
00084   def onExecute(self, ec_id):
00085     if self._sensIn.isNew():
00086       self._d_sens = self._sensIn.read()
00087       self._d_vel.data  = self.calcVel()
00088       self._velOut.write()
00089       time.sleep(0.1)
00090       return RTC.RTC_OK
00091 
00092     time.sleep(0.1)
00093     return RTC.RTC_OK
00094     
00095   
00096 
00097   def calcVel(self):
00098     if self._d_sens.data[3] <= self._distance_to_env[0]:
00099       return [self._turn_velocity[0], -(self._turn_velocity[0])]
00100 
00101     elif self._d_sens.data[3] > self._distance_to_env[0]:
00102       return [self._velocity[0] for i in range(2)]
00103     else:
00104       return [0.0 for i in range(2)]
00105     
00106     
00107 
00108 def AutoControlInit(manager):
00109   profile = OpenRTM_aist.Properties(defaults_str=AutoControl_spec)
00110   manager.registerFactory(profile,
00111                           AutoControl,
00112                           OpenRTM_aist.Delete)
00113 
00114 def MyModuleInit(manager):
00115   AutoControlInit(manager)
00116 
00117   # Create a component
00118   comp = manager.createComponent("AutoControl")
00119 
00120 
00121 
00122 def main():
00123   mgr = OpenRTM_aist.Manager.init(sys.argv)
00124   mgr.setModuleInitProc(MyModuleInit)
00125   mgr.activateManager()
00126   mgr.runManager()
00127 
00128 if __name__ == "__main__":
00129   main()
00130 


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