TkJoyStickComp.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # -*- Python -*-
4 
5 import sys
6 import time
7 sys.path.append(".")
8 
9 # Import RTM module
10 import RTC
11 import OpenRTM_aist
12 
13 # for convert()
14 import math
15 
16 # This module's spesification
17 # <rtc-template block="module_spec">
18 tkjoystick_spec = ["implementation_id", "TkJoyStick",
19  "type_name", "TkJoyStick",
20  "description", "Sample component for MobileRobotCanvas component",
21  "version", "1.0",
22  "vendor", "Noriaki Ando and Shinji Kurihara",
23  "category", "example",
24  "activity_type", "DataFlowComponent",
25  "max_instance", "10",
26  "language", "Python",
27  "lang_type", "SCRIPT",
28  ""]
29 # </rtc-template>
30 
31 import tkjoystick
32 
33 class Position:
34  def __init__(self, x = 0.0, y = 0.0, r = 0.0, th = 0.0):
35  self.x = x
36  self.y = y
37  self.r = r
38  self.th = th
39 
40 position = Position()
41 #tkJoyCanvas = tkjoystick.TkJoystick()
42 
43 
45  def __init__(self, manager):
46  OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
47 
48  self._k = 1.0
49  self.x = 0.0
50  self.y = 0.0
51 
52 
53  def onInitialize(self):
54  self._d_pos = RTC.TimedFloatSeq(RTC.Time(0,0),[])
55  self._posOut = OpenRTM_aist.OutPort("pos", self._d_pos)
56  self._d_vel = RTC.TimedFloatSeq(RTC.Time(0,0),[])
57  self._velOut = OpenRTM_aist.OutPort("vel", self._d_vel)
58 
59  # Set OutPort buffers
60  self.addOutPort("pos",self._posOut)
61  self.addOutPort("vel",self._velOut)
62 
63  return RTC.RTC_OK
64 
65 
66  def onShutdown(self, ec_id):
67  return RTC.RTC_OK
68 
69 
70  def onExecute(self, ec_id):
71  self._d_pos.data = [self.x, self.y]
72  self._d_vel.data = self.convert(self.x, self.y)
73  self._posOut.write()
74  self._velOut.write()
75 
76  return RTC.RTC_OK
77 
78  """
79  \brief Converting from canvas data to MobileRobotCanvas data
80  """
81  def convert(self, x, y):
82  _th = math.atan2(y,x)
83  _v = self._k * math.hypot(x, y)
84  _vl = _v * math.cos(_th - (math.pi/4.0))
85  _vr = _v * math.sin(_th - (math.pi/4.0))
86  print x, y, _vl, _vr
87  return [_vl, _vr]
88 
89  def set_pos(self, pos, pol):
90  self.x = pos[0]
91  self.y = pos[1]
92  self.r = pol[0]
93  self.th = pol[1]
94 
95 
96 
97 #def MyModuleInit(manager):
98 # profile = OpenRTM_aist.Properties(defaults_str=tkjoystick_spec)
99 # manager.registerFactory(profile,
100 # TkJoyStick,
101 # OpenRTM_aist.Delete)
102 #
103 # # Create a component
104 # comp = manager.createComponent("TkJoyStick")
105 
106 
107 
108 def main():
109  tkJoyCanvas = tkjoystick.TkJoystick()
110  tkJoyCanvas.master.title("TkJoystick")
111  mgr = OpenRTM_aist.Manager.init(sys.argv)
112  mgr.activateManager()
113 
114  # Register component
115  profile = OpenRTM_aist.Properties(defaults_str=tkjoystick_spec)
116  mgr.registerFactory(profile,
117  TkJoyStick,
118  OpenRTM_aist.Delete)
119  # Create a component
120  comp = mgr.createComponent("TkJoyStick")
121 
122  tkJoyCanvas.set_on_update(comp.set_pos)
123  mgr.runManager(True)
124  tkJoyCanvas.mainloop()
125 
126 if __name__ == "__main__":
127  main()
128 
The Properties class represents a persistent set of properties.
Definition: Properties.py:83
def __init__(self, x=0.0, y=0.0, r=0.0, th=0.0)
def addOutPort(self, name, outport)
Definition: RTObject.py:2765


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:07