00001
00002
00003
00004
00005 import sys
00006 import time
00007 sys.path.append(".")
00008
00009
00010 import RTC
00011 import OpenRTM_aist
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00040
00041 class AutoControl(OpenRTM_aist.DataFlowComponentBase):
00042 def __init__(self, manager):
00043 OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
00044
00045
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
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
00065 self.addInPort("sens",self._sensIn)
00066
00067
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
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