ros service that receives a pose command, gives float feedback in python

Here's an example of how to write a ROS service using Python that receives a pose message and returns a float feedback:

main.py
#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Pose
from std_srvs.srv import SetBool, SetBoolResponse

def feedback_service(req):
    # Do something with the received pose message
    # and compute the feedback value.
    feedback_value = 1.0 # Replace with your own implementation
    return SetBoolResponse(True, str(feedback_value))

def feedback_server():
    rospy.init_node('feedback_server')
    s = rospy.Service('/feedback_service', SetBool, feedback_service)
    print("Ready to receive pose commands.")
    rospy.spin()

if __name__ == "__main__":
    feedback_server()
600 chars
21 lines

Explanation:

  1. The first line of the file simply tells the shell that this file should be interpreted with Python.

  2. Next, we import the necessary packages. We need rospy to use the ROS Python API, Pose to represent the pose message, and SetBool and SetBoolResponse to create a service that returns a feedback value.

  3. We define the feedback_service function, which takes a SetBool request as input and returns a SetBoolResponse. The request includes a Boolean value that we can set to True to indicate success and a feedback message that we'll set to the feedback value that we compute. For now, we simply return a fixed feedback value of 1.0.

  4. We define the feedback_server function, which initializes the ROS node, creates a new service with the name /feedback_service, and tells the node to keep running until it receives a SIGINT signal.

  5. Finally, we call feedback_server to start the service. When run, the service will be ready to receive initial SetBool requests with a True value and return the computed feedback message.

gistlibby LogSnag