-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdummy_test.py
More file actions
executable file
·180 lines (132 loc) · 4.65 KB
/
Copy pathdummy_test.py
File metadata and controls
executable file
·180 lines (132 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python2
import sys, os, io
import collections, struct, string, re, random
import time, threading, Queue, subprocess
import serial
import logging
from helpers import *
import r2p
mw = r2p.Middleware.instance()
#==============================================================================
class TestMsg(r2p.Message):
def __init__(self, id=0, value=0):
self.id = int(id)
self.value = int(value)
def __repr__(self):
return '%s(id=%d, value=%d)' % (type(self).__name__, self.id, self.value)
def marshal(self):
return struct.pack('<LL', self.id, self.value)
def unmarshal(self, data, offset=0):
self.key, self.value = struct.unpack_from('<LL', data, offset)
#==============================================================================
def node1_threadf():
node = r2p.Node('Node1')
node.begin()
pub = r2p.LocalPublisher()
node.advertise(pub, 'test', r2p.Time.ms(500), TestMsg)
msg = TestMsg(33, 77)
while r2p.ok():
#logging.debug('Tick: node1_threadf()')
try:
node.spin(pub.topic.publish_timeout)
except r2p.TimeoutError:
pass
try:
logging.debug('pub1 <<< %s', repr(msg))
pub.publish(msg)
except IndexError:
logging.warning('Publisher of "%s/%s/%s" overpublished' % \
(mw.module_name, node.name, pub.topic.name))
node.end()
#==============================================================================
def sub2_cb(msg):
logging.debug('sub2 >>> %s', repr(msg))
def node2_threadf():
node = r2p.Node('Node2')
node.begin()
sub = r2p.LocalSubscriber(5, sub2_cb)
node.subscribe(sub, 'test', TestMsg)
timeout = r2p.Time.ms(100)
while r2p.ok():
#logging.debug('Tick: node2_threadf()')
try:
node.spin(timeout)
except r2p.TimeoutError:
pass
node.end()
#==============================================================================
def sub3_cb(msg):
logging.debug('sub3 >>> %s', repr(msg))
def node3_threadf():
node = r2p.Node('Node3')
node.begin()
sub = r2p.LocalSubscriber(5, sub3_cb)
node.subscribe(sub, 'asdf', TestMsg)
timeout = r2p.Time.ms(100)
while r2p.ok():
#logging.debug('Tick: node3_threadf()')
try:
node.spin(timeout)
except r2p.TimeoutError:
pass
node.end()
#==============================================================================
def node4_threadf():
node = r2p.Node('Node4')
node.begin()
pub = r2p.LocalPublisher()
node.advertise(pub, 'asdf', r2p.Time.ms(500), TestMsg)
msg = TestMsg(0xB00B1337, 0xDEADBEEF)
while r2p.ok():
#logging.debug('Tick: node4_threadf()')
try:
node.spin(pub.topic.publish_timeout)
except r2p.TimeoutError:
pass
try:
logging.debug('pub4 <<< %s', repr(msg))
pub.publish(msg)
except Queue.Full:
logging.warning('Publisher of "%s/%s/%s" overpublished' % \
(mw.module_name, node.name, pub.topic.name))
node.end()
#==============================================================================
def _main():
logging.basicConfig(stream=sys.stderr, level=verbosity2level(int(4)))
logging.debug('sys.argv = ' + repr(sys.argv))
#dbgtra = r2p.DebugTransport('ttyUSB0', r2p.StdLineIO())
dbgtra = r2p.DebugTransport('ttyUSB0', r2p.SerialLineIO('/dev/ttyUSB0', 115200))
node1_thread = threading.Thread(name='Node1', target=node1_threadf)
node2_thread = threading.Thread(name='Node2', target=node2_threadf)
node3_thread = threading.Thread(name='Node3', target=node3_threadf)
node4_thread = threading.Thread(name='Node4', target=node4_threadf)
try:
mw.initialize('R2PY')
dbgtra.open()
#node1_thread.start()
node2_thread.start()
#node3_thread.start()
#node4_thread.start()
while r2p.ok():
time.sleep(1)
except Exception as e:
logging.exception(str(e))
raise
finally:
mw._stop()
time.sleep(1)
#node1_thread.join()
node2_thread.join()
#node3_thread.join()
#node4_thread.join()
mw.uninitialize()
dbgtra.close()
#==============================================================================
if __name__ == '__main__':
try:
_main()
except KeyboardInterrupt:
print
pass
except:
raise