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 class NXTRTC(OpenRTM_aist.DataFlowComponentBase):
00036 def __init__(self, manager):
00037 OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
00038
00039
00040
00041 self._map = [['A', 'B']]
00042 self._nxtbrick = None
00043 self._mapping = {'A':0,'B':1,'C':2}
00044
00045 def onInitialize(self):
00046
00047
00048 self._d_vel = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00049 self._velIn = OpenRTM_aist.InPort("vel", self._d_vel)
00050 self.addInPort("vel",self._velIn)
00051 self._d_pos = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00052 self._posOut = OpenRTM_aist.OutPort("pos", self._d_pos)
00053 self.addOutPort("pos",self._posOut)
00054 self._d_sens = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00055 self._sensOut = OpenRTM_aist.OutPort("sens", self._d_sens)
00056 self.addOutPort("sens",self._sensOut)
00057
00058
00059
00060 self.bindParameter("map", self._map, "A,B")
00061
00062
00063 try:
00064 print "Connecting to NXT brick ...."
00065 self._nxtbrick = NXTBrick.NXTBrick()
00066 print "Connection established."
00067 except:
00068 print "NXTBrick connection failed."
00069 return RTC.RTC_ERROR
00070
00071 return RTC.RTC_OK
00072
00073 def onFinalize(self):
00074 self._nxtbrick.close()
00075
00076 def onActivated(self, ec_id):
00077 self._nxtbrick.setMotors([0,0,0])
00078
00079 self._nxtbrick.resetPosition()
00080 return RTC.RTC_OK
00081
00082 def onDeactivated(self, ec_id):
00083 self._nxtbrick.setMotors([0,0,0])
00084
00085 self._nxtbrick.resetPosition()
00086
00087 return RTC.RTC_OK
00088
00089 def onExecute(self, ec_id):
00090 try:
00091
00092 if self._velIn.isNew():
00093
00094 self._d_vel = self._velIn.read()
00095
00096 vel_ = [0,0,0]
00097 vel_[self._mapping[self._map[0][0]]] = self._d_vel.data[0]
00098 vel_[self._mapping[self._map[0][1]]] = self._d_vel.data[1]
00099
00100
00101 self._nxtbrick.setMotors(vel_)
00102 else:
00103 print "buffer empty"
00104
00105
00106 sensor_ = self._nxtbrick.getSensors()
00107 if sensor_:
00108 self._d_sens.data = sensor_
00109
00110 self._sensOut.write()
00111
00112
00113 position_ = self._nxtbrick.getMotors()
00114 if position_:
00115 self._d_pos.data = \
00116 [position_[self._mapping[self._map[0][0]]][9], \
00117 position_[self._mapping[self._map[0][1]]][9]]
00118
00119 self._posOut.write()
00120 except:
00121 print sys.exc_info()[1]
00122
00123 return RTC.RTC_OK
00124
00125
00126
00127 def NXTRTCInit(manager):
00128 profile = OpenRTM_aist.Properties(defaults_str=nxtrtc_spec)
00129 manager.registerFactory(profile,
00130 NXTRTC,
00131 OpenRTM_aist.Delete)
00132
00133
00134 def MyModuleInit(manager):
00135 NXTRTCInit(manager)
00136
00137
00138 comp = manager.createComponent("NXTRTC")
00139
00140
00141
00142 def main():
00143 mgr = OpenRTM_aist.Manager.init(sys.argv)
00144 mgr.setModuleInitProc(MyModuleInit)
00145 mgr.activateManager()
00146 mgr.runManager()
00147
00148 if __name__ == "__main__":
00149 main()
00150
00151