NXTRTC20_callback.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 
00014 # import NXTBrick class
00015 import NXTBrick20
00016 
00017 
00018 # This module's spesification
00019 # <rtc-template block="module_spec">
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 # </rtc-template>
00034 
00035 # @class CallBackClass
00036 # @brief callback class
00037 #
00038 # when data is written in the buffer of InPort,
00039 # it is called.
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     # set velocity
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     # initialize of configuration-data.
00059     # <rtc-template block="configurations">
00060     self._map = [['A', 'B']]
00061     self._nxtbrick = None
00062     self._mapping = {'A':0,'B':1,'C':2}
00063      
00064   def onInitialize(self):
00065     # DataPorts initialization
00066     # <rtc-template block="data_ports">
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     # Bind variables and configuration variable
00078     # <rtc-template block="bind_config">
00079     self.bindParameter("map", self._map, "A,B")
00080 
00081     # create NXTBrick object
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     # set callback class
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     # reset NXTBrick's position.
00100     self._nxtbrick.resetPosition()
00101 
00102     return RTC.RTC_OK
00103 
00104 
00105   def onDeactivated(self, ec_id):
00106     # reset NXTBrick's position.
00107     self._nxtbrick.resetPosition()
00108 
00109     return RTC.RTC_OK
00110 
00111 
00112   def onExecute(self, ec_id):
00113     # get sensor data.
00114     sensor_   = self._nxtbrick.getSensors()
00115     if sensor_:
00116       self._d_sens.data = sensor_
00117       # write sensor data to outport.
00118       self._sensOut.write()
00119 
00120     # get position data.
00121     position_ = self._nxtbrick.getMotors()
00122     if position_:
00123       self._d_pos.data = [position_[self._mapping[self._map[0][0]]],position_[self._mapping[self._map[0][1]]]]
00124       # write position data to outport.
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   # Create a component
00142   comp = manager.createComponent("NXTRTC")
00143 
00144 
00145 
00146 def main():
00147   mgr = OpenRTM_aist.Manager.init(len(sys.argv), sys.argv)
00148   #mgr = OpenRTM_aist.Manager.init(sys.argv)
00149   mgr.setModuleInitProc(MyModuleInit)
00150   mgr.activateManager()
00151   mgr.runManager()
00152 
00153 if __name__ == "__main__":
00154   main()
00155 
00156 


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