ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Why is this simple service not returning anything ?

asked 2011-12-19 08:47:16 -0500

madmax gravatar image

updated 2012-02-27 07:35:39 -0500

kwc gravatar image

Hi,

I am new to ROS and have a little problem regarding services.
Right now I am sending a request to the server, triggering the handle_speech function.
In this function I subscribe to the pocketsphinx recognizer/output node.
When I return an integer value from here, the client receives it and shows it to me.

But when I return a value from the speechCB callback function, the client shows nothing.
What am I doing wrong here?

Edit: Now it is working, but this code is not my best coding ;)

#!/usr/bin/env python
import roslib; roslib.load_manifest('kmr01')

from kmr01.srv import *
import rospy

from std_msgs.msg import String

command = 0

def handle_speech(req):
  print "handling speech input"
  rospy.Subscriber('recognizer/output', String, speechCb)

  r = rospy.Rate(10.0)
  while not rospy.is_shutdown():
    r.sleep()
    print "sleeping"
    if command != 0:
      return RecognizeSpeechResponse(command)

def speechCb(msg):
  global command
  print "speechCB"

  if msg.data.find("forward") > -1:    
    command = 2  

  elif msg.data.find("stop") > -1 or msg.data.find("halt") > -1:          
    command = 1    

  print "nothing recognized"    
  command = 99    


def recognize_speech_server():
  rospy.init_node('recognize_speech_server')
  s = rospy.Service('recognize_speech', RecognizeSpeech, handle_speech)
  print "Ears wide open ..."
  rospy.spin()

if __name__ == "__main__":
  recognize_speech_server()

Regards Max

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2011-12-19 19:17:30 -0500

raahlb gravatar image

But, you're never returning a value from the service callback now? The subscribe callback function is completely unrelated to the service callback function.

If you want the service to return a value received on the topic, you have to let the topic CB store the value in some shared variable. As your service function already has a wait loop, you could poll this value there to see if it's been set. You probably want to have some global boolean also to make sure the value has been updated since the service was called (unless returning an old value is fine).

Don't know if I'd call this solution a good design, though...

edit flag offensive delete link more

Comments

BTW, by definition a topic callback can never return a value. Also you probably won't want to start a new subscription for each call to the service. Save the handle somewhere and each call check if it's set. If not, start the subscription, otherwise do nothing.
raahlb gravatar image raahlb  ( 2011-12-19 19:21:20 -0500 )edit
I know that it is not a good design, but I put basically the service client tutorial and the voice_cmd_vel.py pocketsphinx tutorial together. I aso don't know python really well. Actually, my service should only listen for one command and then stop again.
madmax gravatar image madmax  ( 2011-12-19 19:57:01 -0500 )edit
1

answered 2011-12-19 10:27:55 -0500

fergs gravatar image

What is your definition of RecognizedSpeech.srv? I presume it probably wants to return an integer, and not a string....

edit flag offensive delete link more

Comments

Yes, it wants to return an integer. When I use this "RecognizeSpeechResponse(1)", the client receives an integer.
madmax gravatar image madmax  ( 2011-12-19 19:58:22 -0500 )edit

Question Tools

Stats

Asked: 2011-12-19 08:47:16 -0500

Seen: 1,759 times

Last updated: Dec 19 '11