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 import NXTBrick
00016
00017
00018
00019
00020 nxtrtc_spec = ["implementation_id", "NXTRTC",
00021 "type_name", "NXTRTC",
00022 "description", "NXT sample component",
00023 "version", "0.1",
00024 "vendor", "AIST",
00025 "category", "example",
00026 "activity_type", "DataFlowComponent",
00027 "max_instance", "10",
00028 "language", "Python",
00029 "lang_type", "SCRIPT",
00030 "conf.default.map", "A,B",
00031 ""]
00032
00033
00034
00035
00036
00037
00038
00039
00040 class CallBackClass:
00041 def __init__(self, nxtbrick_, map_):
00042 self._nxtbrick = nxtbrick_
00043 self._map = map_
00044 self._mapping = {'A':0,'B':1,'C':2}
00045
00046 def __call__(self, pData):
00047 vel_ = [0,0,0]
00048 vel_[self._mapping[self._map[0][0]]] = pData.data[0]
00049 vel_[self._mapping[self._map[0][1]]] = pData.data[1]
00050
00051 self._nxtbrick.setMotors(vel_)
00052
00053
00054 class NXTRTC(OpenRTM_aist.DataFlowComponentBase):
00055 def __init__(self, manager):
00056 OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
00057
00058
00059
00060 self._map = [['A', 'B']]
00061 self._nxtbrick = None
00062 self._mapping = {'A':0,'B':1,'C':2}
00063
00064 def onInitialize(self):
00065
00066
00067 self._d_vel = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00068 self._velIn = OpenRTM_aist.InPort("vel", self._d_vel)
00069 self.addInPort("vel",self._velIn)
00070 self._d_pos = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00071 self._posOut = OpenRTM_aist.OutPort("pos", self._d_pos)
00072 self.addOutPort("pos",self._posOut)
00073 self._d_sens = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00074 self._sensOut = OpenRTM_aist.OutPort("sens", self._d_sens)
00075 self.addOutPort("sens",self._sensOut)
00076
00077
00078
00079 self.bindParameter("map", self._map, "A,B")
00080
00081
00082 try:
00083 print "Connecting to NXT brick ...."
00084 self._nxtbrick = NXTBrick.NXTBrick()
00085 print "Connection established."
00086 except:
00087 print "NXTBrick connection failed."
00088 return RTC.RTC_ERROR
00089
00090
00091 self._velIn.setOnWrite(CallBackClass(self._ntxbrick,self._map))
00092
00093 return RTC.RTC_OK
00094
00095 def onFinalize(self):
00096 self._nxtbrick.close()
00097
00098 def onActivated(self, ec_id):
00099
00100 self._nxtbrick.resetPosition()
00101
00102 return RTC.RTC_OK
00103
00104
00105 def onDeactivated(self, ec_id):
00106
00107 self._nxtbrick.resetPosition()
00108
00109 return RTC.RTC_OK
00110
00111
00112 def onExecute(self, ec_id):
00113
00114 sensor_ = self._nxtbrick.getSensors()
00115 if sensor_:
00116 self._d_sens.data = [sensor_[3]]
00117
00118 self._sensOut.write()
00119
00120
00121 position_ = self._nxtbrick.getMotors()
00122 if position_:
00123 self._d_pos.data = [position_[self._mapping[self._map[0][0]]][9],position_[self._mapping[self._map[0][1]]][9]]
00124
00125 self._posOut.write()
00126
00127 return RTC.RTC_OK
00128
00129
00130
00131 def NXTRTC_callbackInit(manager):
00132 profile = OpenRTM_aist.Properties(defaults_str=nxtrtc_spec)
00133 manager.registerFactory(profile,
00134 NXTRTC,
00135 OpenRTM_aist.Delete)
00136
00137
00138 def MyModuleInit(manager):
00139 NXTRTC_callbackInit(manager)
00140
00141
00142 comp = manager.createComponent("NXTRTC")
00143
00144
00145
00146 def main():
00147 mgr = OpenRTM_aist.Manager.init(len(sys.argv), sys.argv)
00148
00149 mgr.setModuleInitProc(MyModuleInit)
00150 mgr.activateManager()
00151 mgr.runManager()
00152
00153 if __name__ == "__main__":
00154 main()
00155
00156