Ask Your Question

Revision history [back]

Hello,

I had formatted your code got something like this:

#!/usr/bin/env python

import rospy
from std_msgs.msg import String
from std_msgs.msg import Int16
from myworkcell.msg import message1
from myworkcell.srv import LocalizePart
from fake_ar_publisher.msg import ARMarker
import random

received_pose=ARMarker()

def callback(data):
    #print "receiving data"
    received_pose.pose=data.pose
    # # print "received_pose"

def handle_service2(req):
    print "in the response"
    #req.base_frame=received_pose
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", received_pose.pose)
    print "Im here"
    return LocalizePartResponse(received_pose.pose)

def vision_node_service_server_2():
    rospy.init_node('myworkcell_node', anonymous=True)
    #ROS_INFO("ScanNPlan node has been initialized");
    #print "about to subscribe"
    rospy.Subscriber('ar_pose_marker',ARMarker, callback)
    print "hereeeee"
    #print "subscribed"
    #ROS_INFO("I'm subscribed to fake_ar_publisher");
    #s=rospy.Service('localize_part',LocalizePart,handle_service2)
    s=rospy.Service('Localize_Part',LocalizePart,handle_service2)
    #print"I'm here already"
    rospy.spin()

if __name__ == '__main__':
    try:
        vision_node_service_server_2()
    except rospy.ROSInterruptException:
        pass

Finally, I've reproduced your problem and concluded that to solve it you only have to import the server response library.

ROS creates 3 classes for each service message: Service definition, Request message and Response message. You should take a look here: http://wiki.ros.org/rospy/Overview/Services

Following the code I've posted, I had just to replace this:

from myworkcell.srv import LocalizePart

with this:

from myworkcell.srv import LocalizePart, LocalizePartResponse

I hope it can help you!

Hello,

I had formatted your code got something like this:

#!/usr/bin/env python

import rospy
from std_msgs.msg import String
from std_msgs.msg import Int16
from myworkcell.msg import message1
from myworkcell.srv import LocalizePart
from fake_ar_publisher.msg import ARMarker
import random

received_pose=ARMarker()

def callback(data):
    #print "receiving data"
    received_pose.pose=data.pose
    # # print "received_pose"

def handle_service2(req):
    print "in the response"
    #req.base_frame=received_pose
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", received_pose.pose)
    print "Im here"
    return LocalizePartResponse(received_pose.pose)

def vision_node_service_server_2():
    rospy.init_node('myworkcell_node', anonymous=True)
    #ROS_INFO("ScanNPlan node has been initialized");
    #print "about to subscribe"
    rospy.Subscriber('ar_pose_marker',ARMarker, callback)
    print "hereeeee"
    #print "subscribed"
    #ROS_INFO("I'm subscribed to fake_ar_publisher");
    #s=rospy.Service('localize_part',LocalizePart,handle_service2)
    s=rospy.Service('Localize_Part',LocalizePart,handle_service2)
    #print"I'm here already"
    rospy.spin()

if __name__ == '__main__':
    try:
        vision_node_service_server_2()
    except rospy.ROSInterruptException:
        pass

Finally, I've reproduced your problem (with some simplifications, because I don't have all your message definitions and the publisher) and concluded that to solve it you only have to import the server response library.library (you can check my steps in this video: https://youtu.be/A8cK3Ns0W4M).

ROS creates 3 classes for each service message: Service definition, Request message and Response message. You should take a look here: http://wiki.ros.org/rospy/Overview/Services

Following the code I've posted, I had just to replace this:

from myworkcell.srv import LocalizePart

with this:

from myworkcell.srv import LocalizePart, LocalizePartResponse

I hope it can help you!