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 import math
00015
00016
00017
00018 tkjoystick_spec = ["implementation_id", "TkJoyStick",
00019 "type_name", "TkJoyStick",
00020 "description", "Sample component for MobileRobotCanvas component",
00021 "version", "1.0",
00022 "vendor", "Noriaki Ando and Shinji Kurihara",
00023 "category", "example",
00024 "activity_type", "DataFlowComponent",
00025 "max_instance", "10",
00026 "language", "Python",
00027 "lang_type", "SCRIPT",
00028 ""]
00029
00030
00031 import tkjoystick
00032
00033 class Position:
00034 def __init__(self, x = 0.0, y = 0.0, r = 0.0, th = 0.0):
00035 self.x = x
00036 self.y = y
00037 self.r = r
00038 self.th = th
00039
00040 position = Position()
00041
00042
00043
00044 class TkJoyStick(OpenRTM_aist.DataFlowComponentBase):
00045 def __init__(self, manager):
00046 OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
00047
00048 self._k = 1.0
00049 self.x = 0.0
00050 self.y = 0.0
00051
00052
00053 def onInitialize(self):
00054 self._d_pos = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00055 self._posOut = OpenRTM_aist.OutPort("pos", self._d_pos)
00056 self._d_vel = RTC.TimedFloatSeq(RTC.Time(0,0),[])
00057 self._velOut = OpenRTM_aist.OutPort("vel", self._d_vel)
00058
00059
00060 self.addOutPort("pos",self._posOut)
00061 self.addOutPort("vel",self._velOut)
00062
00063 return RTC.RTC_OK
00064
00065
00066 def onShutdown(self, ec_id):
00067 return RTC.RTC_OK
00068
00069
00070 def onExecute(self, ec_id):
00071 self._d_pos.data = [self.x, self.y]
00072 self._d_vel.data = self.convert(self.x, self.y)
00073 self._posOut.write()
00074 self._velOut.write()
00075
00076 return RTC.RTC_OK
00077
00078 """
00079 \brief Converting from canvas data to MobileRobotCanvas data
00080 """
00081 def convert(self, x, y):
00082 _th = math.atan2(y,x)
00083 _v = self._k * math.hypot(x, y)
00084 _vl = _v * math.cos(_th - (math.pi/4.0))
00085 _vr = _v * math.sin(_th - (math.pi/4.0))
00086 print x, y, _vl, _vr
00087 return [_vl, _vr]
00088
00089 def set_pos(self, pos, pol):
00090 self.x = pos[0]
00091 self.y = pos[1]
00092 self.r = pol[0]
00093 self.th = pol[1]
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 def main():
00109 tkJoyCanvas = tkjoystick.TkJoystick()
00110 tkJoyCanvas.master.title("TkJoystick")
00111 mgr = OpenRTM_aist.Manager.init(sys.argv)
00112 mgr.activateManager()
00113
00114
00115 profile = OpenRTM_aist.Properties(defaults_str=tkjoystick_spec)
00116 mgr.registerFactory(profile,
00117 TkJoyStick,
00118 OpenRTM_aist.Delete)
00119
00120 comp = mgr.createComponent("TkJoyStick")
00121
00122 tkJoyCanvas.set_on_update(comp.set_pos)
00123 mgr.runManager(True)
00124 tkJoyCanvas.mainloop()
00125
00126 if __name__ == "__main__":
00127 main()
00128