-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROSpymlabServer.py
executable file
·154 lines (130 loc) · 5.21 KB
/
ROSpymlabServer.py
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
#!/usr/bin/env python
import rospy
import pymlab
from pymlab import config
import sys
import arom
from std_msgs.msg import String
from std_msgs.msg import Float32
import std_msgs
import time
from arom.srv import *
from arom.msg import *
from drivers.__init__ import AromNode
def server(req):
print req
print "Returning [%s + %s]"%(req.name, req.data)
return GetSensValResponse( 10 )
class pymlab_server(AromNode):
node_name = "pymlab_node"
node_type = "pymlab"
node_pymlab = True
def __init__(self):
self.err_count = 0
self.pymlab_read = False # slouzi k ochrane pymlabu pred pokusem o dve cteni zaroven ...
self.devices = {}
self.i2c = []
self.bus = []
rospy.Subscriber("pymlab_server", msg_pymlabInit, self.status)
s1 = rospy.Service('pymlab_init', PymlabInit, self.init)
s2 = rospy.Service('pymlab_server', PymlabServerStatus, self.status)
s3 = rospy.Service('pymlab_drive', PymlabDrive, self.drive)
AromNode.__init__(self)
self.set_feature('pymlab_structure',{'i2c': "", 'bus': ""})
def init(self, cfg=None):
try:
self.status = False
self.init = cfg
self.cfg_i2c = eval(cfg.i2c)
self.i2c.append(self.cfg_i2c)
self.cfg_bus = eval(cfg.bus)
self.bus.append(self.cfg_bus)
rospy.loginfo("konfiguracni data: \n\r %s" %str(cfg))
self.init_bus(i2c = self.cfg_i2c, bus = self.cfg_bus)
return True
except Exception, e:
rospy.logerr("#03:"+repr(e))
return False
def init_bus(self, i2c, bus):
self.pymlab_config = config.Config(i2c = i2c, bus = bus)
self.pymlab_config.initialize()
for x in bus:
try:
self.devices[x['name']] = self.pymlab_config.get_device(x['name'])
except Exception, e:
rospy.logerr("#02:"+repr(e))
print x,
self.devices[x['name']] = self.pymlab_config.get_device(x['name'])
print self.devices[x['name']]
def getvalue(self, cfg=None):
val = int(float(self.lts_sen.get_temp()))
return GetSensValResponse(val)
def status(self, cfg=None):
self.rate = 1
rospy.loginfo("prijaty cfg: %s" %(str(cfg)))
try: # pokud je servis s GetSensVal, tak se pouzije toto,
ecfg = eval(cfg.data)
except Exception, e: # toto je pro zpravu 'pymlab_server'
ecfg = eval(cfg)
print ecfg
if "start" in ecfg:
rospy.loginfo("Starting PyMLAB ROS server with: %s" %(repr(self.devices)))
self.status = True
rate = rospy.Rate(0.1)
#AutoInputs = self.AutoInputs
devices = self.devices
#sender = rospy.Publisher('pymlab_data', SensorValues, queue_size=20)
#values = {}
#for x in AutoInputs:
# for y in AutoInputs[x]:
# #print "AutoInputs >>", x, y,
# #print str(x)+"/"+str(y), str(x)+"/"+str(y)
# values[str(x)+"/"+str(y)] = str(x)+"/"+str(y)
#rospy.set_param("values", values)
# print "\n run \n\n"
while True:
if self.err_count > 100:
rospy.logwarn("restarting pymlab")
self.err_count = 0
for i, bus in enumerate(self.bus):
try:
rospy.loginfo("reloading pymlab bus:" + repr(bus))
self.init_bus(i2c = self.i2c[i], bus = bus)
except Exception, e:
rospy.logerr(e)
rate.sleep()
return True
def drive(self, cfg): # zpracovava requesty ostatatich nodu
parameters = cfg.parameters
method = cfg.method
device = cfg.device
print "parameters '%s', method '%s', device '%s'" %(cfg.parameters, cfg.method, cfg.device)
while self.pymlab_read: time.sleep(0.01)
self.pymlab_read = True
try:
if parameters == "" or parameters == None:
reval = getattr(self.devices[cfg.device], cfg.method)()
elif isinstance(eval(parameters), tuple):
print "#Tuple:", eval(parameters)
reval = getattr(self.devices[cfg.device], cfg.method)(*eval(parameters))
elif isinstance(eval(parameters), dict):
print "#Dictionary:", eval(parameters)
reval = getattr(self.devices[cfg.device], cfg.method)(**eval(parameters))
else:
reval = getattr(self.devices[cfg.device], cfg.method)(eval(parameters))
print "#None"
self.pymlab_read = False
return str(reval)
except Exception, e:
self.err_count += 1
rospy.logerr("#01:"+repr(e))
self.pymlab_read = False
return str(False)
def main():
ps = pymlab_server()
#rospy.init_node('pymlab_node')
# slouzi k prijmuti requestu
rospy.loginfo("Ready to get work.")
rospy.spin()
if __name__ == "__main__":
main()