ROS Indigo/Rospy: AttributeError: 'function' object has no attribute '_request_class'
ROS Indigo/Rospy: AttributeError: 'function' object has no attribute '_request_class'
我一直在尝试建立一个ROS服务来帮助程序之间传输图像,但没有成功。不管使用与该程序的旧版本相同的过程——它完全有效——我得到的只是同样令人沮丧的模糊错误消息:
Traceback (most recent call last):
File "/catkin_ws/src/package/scripts/rc.py", line 110, in <module>
main_function()
File "/catkin_ws/src/package/scripts/rc.py", line 105, in main_function
incoming = sessionstart(f, t, x1i, y1i, x2i, y2i)
File "/catkin_ws/src/package/scripts/rc.py", line 40, in sessionstart
requestimage = rospy.ServiceProxy('StartSession', sessionstart)
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/tcpros_service.py", line 404, in __init__
super(ServiceProxy, self).__init__(name, service_class)
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/service.py", line 59, in __init__
self.request_class = service_class._request_class
AttributeError: 'function' object has no attribute '_request_class'
这个错误是什么意思??我如何解决它??我尝试过的任何方法都不起作用,包括更改服务、重试 catkin_make、重写代码以确保没有多余的空格 - 什么都没有。
服务请求如下:
global sid, f, x1i, y1i, x2i, y2i
rospy.init_node('name', log_level = rospy.INFO, anonymous = True)
bridge = CvBridge()
refPt = []
sid = 0; t = 0; x1i = 0; y1i = 0; x2i = 0; y2i = 0
def sessionstart(t, x1i, y1i, x2i, y2i):
rospy.wait_for_service('StartSession')
print '------------------------ service found'
try:
requestimage = rospy.ServiceProxy('StartSession', sessionstart)
print "------------------------ service proxy set"
response = requestimage(t, x1i, y1i, x2i, y2i)
print "------------------------ service accessed"
return response
except rospy.ServiceException, e:
print "Service call failed: %s"%e
def imagecallback(data):
global sid, f, x1i, y1i, x2i, y2i
b, t, sid = data.split()
print '------------------------ imagecallback started'
t = float(t); sid = int(sid)
timestr = time.strftime("%H:%M:%S", time.getgmtime(t))
print "Loaded image from %s."%timestr
imagedata = bridge.imgmsg_to_cv2(b, "bgr8")
npimg = cv2.imencode('.jpg', imagedata)
cv2.imwrite('tempimage.jpg', imagedata)
img = cv2.imread('tempimage.jpg')
clone = img.copy()
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow("image", img)
cv2.waitKey(1)
def main_function():
global sid, f, x1i, y1i, x2i, y2i
f = 0
t = time.time()
print '------------------------ time set, frame set'
sid = 0; x1i = 0; y1i = 0; x2i = 0; y2i = 0
print "sid %s, x1i %s, y1i %s, x2i %s, y2i %s"%(sid, x1i, y1i, x2i, y2i)
print '------------------------ everything else set'
incoming = sessionstart(t, x1i, y1i, x2i, y2i)
print "------------------------ service assigned"
imagecallback(incoming)
print "------------------------ service called"
if __name__ == '__main__':
main_function()
此服务由以下代码处理(在单独的 rospy 脚本中):
def handle_sessionstart(req):
global assignsid
sid = assignsid
print "Assigning SID %s to this client."%sid
assignsid = assignsid + 1
t = req.t
x1i = req.x1i
y1i = req.y1i
x2i = req.x2i
y2i = req.y2i
requestinfo = "%s %s %s %s %s %s"%(sid, t, x1i, y1i, x2i, y2i)
print "Requesting: " + requestinfo
client.publish('/imagerequest', requestinfo, 0)
rosimg = imgq.get()
imgq.join()
return sessionstartResponse(sid, rosimg, t, error)
def handle_sessionend(req):
sid = req.sid
requestinfo = "%s"%(sid)
client.publish('/endsession', requestinfo, 0)
closed = endq.get()
return sessionendResponse(closed)
def handle_imgreq(req):
# global x1v, y1v, x2v, y2v
sid = req.sid; df = req.f; t = req.t # session id, frame increment, abs time
xli = req.xli; y1i = req.yli; x2i = req.x2i; y2i = req.y2i
requestinfo = "%s %s %s %s %s %s %s"%(sid, f, t, x1i, y1i, x2i, y2i)
client.publish('/imagerequest', requestinfo, 0)
rosimg = imgq.get()
return imgreqResponse(rosimg, t, error)
def RDtoLDDServiceHandler():
s = rospy.Service('StartSession', sessionstart, handle_sessionstart)
print "Services ready..."
rospy.spin()
if __name__ == '__main__':
imgq = Queue.Queue(maxsize=1)
endq = Queue.Queue(maxsize = 1)
imgpub = rospy.Publisher('/ros_image', Image, queue_size = 10)
datapub = rospy.Publisher('/ros_data', String, queue_size = 10)
client = mqtt.Client()
rospy.init_node('RD')
client.connect('192.168.1.7', 1883, 60)
RDtoLDDServiceHandler()
如有任何帮助,我们将不胜感激。谢谢!
嗯,我是个白痴。我找到了解决方案,在发布此消息后不久就想了一个上午。
我处理图像请求的函数与我尝试调用的服务同名。所以我想我混淆了 rospy/python。第二个我将函数更改为 "sessionstart2",它起作用了。
但是...我想现在已经解决了。哈哈
我一直在尝试建立一个ROS服务来帮助程序之间传输图像,但没有成功。不管使用与该程序的旧版本相同的过程——它完全有效——我得到的只是同样令人沮丧的模糊错误消息:
Traceback (most recent call last):
File "/catkin_ws/src/package/scripts/rc.py", line 110, in <module>
main_function()
File "/catkin_ws/src/package/scripts/rc.py", line 105, in main_function
incoming = sessionstart(f, t, x1i, y1i, x2i, y2i)
File "/catkin_ws/src/package/scripts/rc.py", line 40, in sessionstart
requestimage = rospy.ServiceProxy('StartSession', sessionstart)
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/tcpros_service.py", line 404, in __init__
super(ServiceProxy, self).__init__(name, service_class)
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/service.py", line 59, in __init__
self.request_class = service_class._request_class
AttributeError: 'function' object has no attribute '_request_class'
这个错误是什么意思??我如何解决它??我尝试过的任何方法都不起作用,包括更改服务、重试 catkin_make、重写代码以确保没有多余的空格 - 什么都没有。
服务请求如下:
global sid, f, x1i, y1i, x2i, y2i
rospy.init_node('name', log_level = rospy.INFO, anonymous = True)
bridge = CvBridge()
refPt = []
sid = 0; t = 0; x1i = 0; y1i = 0; x2i = 0; y2i = 0
def sessionstart(t, x1i, y1i, x2i, y2i):
rospy.wait_for_service('StartSession')
print '------------------------ service found'
try:
requestimage = rospy.ServiceProxy('StartSession', sessionstart)
print "------------------------ service proxy set"
response = requestimage(t, x1i, y1i, x2i, y2i)
print "------------------------ service accessed"
return response
except rospy.ServiceException, e:
print "Service call failed: %s"%e
def imagecallback(data):
global sid, f, x1i, y1i, x2i, y2i
b, t, sid = data.split()
print '------------------------ imagecallback started'
t = float(t); sid = int(sid)
timestr = time.strftime("%H:%M:%S", time.getgmtime(t))
print "Loaded image from %s."%timestr
imagedata = bridge.imgmsg_to_cv2(b, "bgr8")
npimg = cv2.imencode('.jpg', imagedata)
cv2.imwrite('tempimage.jpg', imagedata)
img = cv2.imread('tempimage.jpg')
clone = img.copy()
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow("image", img)
cv2.waitKey(1)
def main_function():
global sid, f, x1i, y1i, x2i, y2i
f = 0
t = time.time()
print '------------------------ time set, frame set'
sid = 0; x1i = 0; y1i = 0; x2i = 0; y2i = 0
print "sid %s, x1i %s, y1i %s, x2i %s, y2i %s"%(sid, x1i, y1i, x2i, y2i)
print '------------------------ everything else set'
incoming = sessionstart(t, x1i, y1i, x2i, y2i)
print "------------------------ service assigned"
imagecallback(incoming)
print "------------------------ service called"
if __name__ == '__main__':
main_function()
此服务由以下代码处理(在单独的 rospy 脚本中):
def handle_sessionstart(req):
global assignsid
sid = assignsid
print "Assigning SID %s to this client."%sid
assignsid = assignsid + 1
t = req.t
x1i = req.x1i
y1i = req.y1i
x2i = req.x2i
y2i = req.y2i
requestinfo = "%s %s %s %s %s %s"%(sid, t, x1i, y1i, x2i, y2i)
print "Requesting: " + requestinfo
client.publish('/imagerequest', requestinfo, 0)
rosimg = imgq.get()
imgq.join()
return sessionstartResponse(sid, rosimg, t, error)
def handle_sessionend(req):
sid = req.sid
requestinfo = "%s"%(sid)
client.publish('/endsession', requestinfo, 0)
closed = endq.get()
return sessionendResponse(closed)
def handle_imgreq(req):
# global x1v, y1v, x2v, y2v
sid = req.sid; df = req.f; t = req.t # session id, frame increment, abs time
xli = req.xli; y1i = req.yli; x2i = req.x2i; y2i = req.y2i
requestinfo = "%s %s %s %s %s %s %s"%(sid, f, t, x1i, y1i, x2i, y2i)
client.publish('/imagerequest', requestinfo, 0)
rosimg = imgq.get()
return imgreqResponse(rosimg, t, error)
def RDtoLDDServiceHandler():
s = rospy.Service('StartSession', sessionstart, handle_sessionstart)
print "Services ready..."
rospy.spin()
if __name__ == '__main__':
imgq = Queue.Queue(maxsize=1)
endq = Queue.Queue(maxsize = 1)
imgpub = rospy.Publisher('/ros_image', Image, queue_size = 10)
datapub = rospy.Publisher('/ros_data', String, queue_size = 10)
client = mqtt.Client()
rospy.init_node('RD')
client.connect('192.168.1.7', 1883, 60)
RDtoLDDServiceHandler()
如有任何帮助,我们将不胜感激。谢谢!
嗯,我是个白痴。我找到了解决方案,在发布此消息后不久就想了一个上午。
我处理图像请求的函数与我尝试调用的服务同名。所以我想我混淆了 rospy/python。第二个我将函数更改为 "sessionstart2",它起作用了。
但是...我想现在已经解决了。哈哈