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 NXTBrick20
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 = NXTBrick20.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 self._nxtbrick.setMotors(vel_)
00101 else:
00102 print "buffer empty"
00103
00104
00105 sensor_ = self._nxtbrick.getSensors()
00106 if sensor_:
00107 self._d_sens.data = sensor_
00108
00109 self._sensOut.write()
00110
00111
00112 position_ = self._nxtbrick.getMotors()
00113 if position_:
00114 self._d_pos.data = \
00115 [position_[self._mapping[self._map[0][0]]], \
00116 position_[self._mapping[self._map[0][1]]]]
00117
00118 self._posOut.write()
00119 except:
00120 print sys.exc_info()[1]
00121
00122 return RTC.RTC_OK
00123
00124
00125
00126 def NXTRTCInit(manager):
00127 profile = OpenRTM_aist.Properties(defaults_str=nxtrtc_spec)
00128 manager.registerFactory(profile,
00129 NXTRTC,
00130 OpenRTM_aist.Delete)
00131
00132
00133 def MyModuleInit(manager):
00134 NXTRTCInit(manager)
00135
00136
00137 comp = manager.createComponent("NXTRTC")
00138
00139
00140
00141 def main():
00142 mgr = OpenRTM_aist.Manager.init(sys.argv)
00143 mgr.setModuleInitProc(MyModuleInit)
00144 mgr.activateManager()
00145 mgr.runManager()
00146
00147 if __name__ == "__main__":
00148 main()
00149
00150