SeqOut.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # -*- Python -*-
4 
5 import sys
6 
7 import RTC
8 import OpenRTM_aist
9 
10 import random
11 import time
12 
13 seqout_spec = ["implementation_id", "SeqOut",
14  "type_name", "SequenceOutComponent",
15  "description", "Sequence OutPort component",
16  "version", "1.0",
17  "vendor", "Shinji Kurihara",
18  "category", "example",
19  "activity_type", "DataFlowComponent",
20  "max_instance", "10",
21  "language", "Python",
22  "lang_type", "script",
23  ""]
24 
25 
26 
28  def __init__(self, manager):
29  OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
30  return
31 
32  def onInitialize(self):
33  self._octet = RTC.TimedOctet(RTC.Time(0,0),0)
34  self._short = RTC.TimedShort(RTC.Time(0,0),0)
35  self._long = RTC.TimedLong(RTC.Time(0,0),0)
36  self._float = RTC.TimedFloat(RTC.Time(0,0),0)
37  self._double = RTC.TimedDouble(RTC.Time(0,0),0)
38  self._octetSeq = RTC.TimedOctetSeq(RTC.Time(0,0),[])
39  self._shortSeq = RTC.TimedShortSeq(RTC.Time(0,0),[])
40  self._longSeq = RTC.TimedLongSeq(RTC.Time(0,0),[])
41  self._floatSeq = RTC.TimedFloatSeq(RTC.Time(0,0),[])
42  self._doubleSeq = RTC.TimedDoubleSeq(RTC.Time(0,0),[])
43 
44  self._octetOut = OpenRTM_aist.OutPort("Octet", self._octet)
45  self._shortOut = OpenRTM_aist.OutPort("Short", self._short)
46  self._longOut = OpenRTM_aist.OutPort("Long", self._long)
47  self._floatOut = OpenRTM_aist.OutPort("Float", self._float)
48  self._doubleOut = OpenRTM_aist.OutPort("Double", self._double)
49  self._octetSeqOut = OpenRTM_aist.OutPort("OctetSeq", self._octetSeq)
50  self._shortSeqOut = OpenRTM_aist.OutPort("ShortSeq", self._shortSeq)
51  self._longSeqOut = OpenRTM_aist.OutPort("LongSeq", self._longSeq)
52  self._floatSeqOut = OpenRTM_aist.OutPort("FloatSeq", self._floatSeq)
53  self._doubleSeqOut = OpenRTM_aist.OutPort("DoubleSeq", self._doubleSeq)
54 
55 
56  # Set OutPort buffer
57  self.addOutPort("Octet", self._octetOut)
58  self.addOutPort("Short", self._shortOut)
59  self.addOutPort("Long", self._longOut)
60  self.addOutPort("Float", self._floatOut)
61  self.addOutPort("Double", self._doubleOut)
62 
63  self.addOutPort("OctetSeq", self._octetSeqOut)
64  self.addOutPort("ShortSeq", self._shortSeqOut)
65  self.addOutPort("LongSeq", self._longSeqOut)
66  self.addOutPort("FloatSeq", self._floatSeqOut)
67  self.addOutPort("DoubleSeq", self._doubleSeqOut)
68  return RTC.RTC_OK
69 
70 
71  def onExecute(self, ec_id):
72  octetSeq = ""
73  shortSeq = []
74  longSeq = []
75  floatSeq = []
76  doubleSeq = []
77 
78  self._octet.data = int(random.uniform(0x41, 0x4a))
79  self._short.data = int(random.uniform(0, 10))
80  self._long.data = long(random.uniform(0, 10))
81  self._float.data = float(random.uniform(0.0, 10.0))
82  self._double.data = float(random.uniform(0.0, 10.0))
83 
84  print '%3.2s %10.8s %10.8s %10.8s %10.8s %10.8s' \
85  % (' ', 'octet', 'short', 'long', 'float', 'double')
86  print '%3.2s %7s[%s] %10.8s %10.8s %10.8s %10.8s' \
87  % (' ', self._octet.data, chr(self._octet.data), self._short.data, self._long.data, self._float.data, self._double.data)
88  print "-------------------------------------------------------------"
89  print " Sequence Data "
90  print "-------------------------------------------------------------"
91  for i in range(10):
92  octetSeq = octetSeq + chr(int(random.uniform(0x41, 0x4a)))
93  shortSeq.append(int(random.uniform(0, 10)))
94  longSeq.append(long(random.uniform(0, 10)))
95  floatSeq.append(float(random.uniform(0.0, 10.0)))
96  doubleSeq.append(float(random.uniform(0.0, 10.0)))
97  print '%3.2s : %7s[%s] %10.8s %10.8s %10.8s %10.8s' \
98  % (str(i), ord(octetSeq[i]), octetSeq[i], shortSeq[i], longSeq[i], floatSeq[i], doubleSeq[i])
99 
100  # Moving cursor (^[[nA : n lines upward)
101  print "\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r"
102 
103  self._octetSeq.data = octetSeq
104  self._shortSeq.data = shortSeq
105  self._longSeq.data = longSeq
106  self._floatSeq.data = floatSeq
107  self._doubleSeq.data = doubleSeq
108 
109  self._octetOut.write()
110  self._shortOut.write()
111  self._longOut.write()
112  self._floatOut.write()
113  self._doubleOut.write()
114  self._octetSeqOut.write()
115  self._shortSeqOut.write()
116  self._longSeqOut.write()
117  self._floatSeqOut.write()
118  self._doubleSeqOut.write()
119 
120  time.sleep(1)
121 
122  return RTC.RTC_OK
123 
124 
125 def SeqOutInit(manager):
126  profile = OpenRTM_aist.Properties(defaults_str=seqout_spec)
127  manager.registerFactory(profile,
128  SeqOut,
129  OpenRTM_aist.Delete)
130  return
131 
132 
133 def MyModuleInit(manager):
134  SeqOutInit(manager)
135 
136  # Create a component
137  comp = manager.createComponent("SeqOut")
138 
139  print "Component created"
140 
141 
142 def main():
143  # Initialize manager
144  mgr = OpenRTM_aist.Manager.init(sys.argv)
145 
146  # Set module initialization proceduer
147  # This procedure will be invoked in activateManager() function.
148  mgr.setModuleInitProc(MyModuleInit)
149 
150  # Activate manager and register to naming service
151  mgr.activateManager()
152 
153  # run the manager in blocking mode
154  # runManager(False) is the default
155  mgr.runManager()
156 
157  # If you want to run the manager in non-blocking mode, do like this
158  # mgr.runManager(True)
159 
160 if __name__ == "__main__":
161  main()
The Properties class represents a persistent set of properties.
Definition: Properties.py:83
def addOutPort(self, name, outport)
Definition: RTObject.py:2765


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:34