ros actionlib server that receives a pose goal, gives a float feedback and returns a boolean in the end in python

Here is an example code snippet to implement an Actionlib server in Python that receives a pose goal, gives a float feedback and returns a boolean in the end:

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

import rospy
import actionlib
from geometry_msgs.msg import PoseStamped
from std_msgs.msg import Float32
from your_package.msg import PoseAction, PoseResult, PoseFeedback

class PoseActionServer(object):
    def __init__(self):
        self._as = actionlib.SimpleActionServer("pose_action_server", PoseAction, self.goal_callback, False)
        self._as.start()
        self._result = PoseResult()
        self._feedback = PoseFeedback()

    def goal_callback(self, goal):
        success = True
        rate = rospy.Rate(1)
        for i in range(1, 6):
            self._feedback.progress = i * 20
            self._as.publish_feedback(self._feedback)
            rospy.loginfo('Moving to position ({},{},{})...'.format(goal.pose.position.x, goal.pose.position.y, goal.pose.position.z))
            rate.sleep()
        self._result.success = success
        rospy.loginfo('Reached position ({},{},{})'.format(goal.pose.position.x, goal.pose.position.y, goal.pose.position.z))
        self._as.set_succeeded(self._result)

if __name__ == '__main__':
    rospy.init_node('pose_action_server')
    server = PoseActionServer()
    rospy.spin()
1167 chars
32 lines

Here, we define a class PoseActionServer which contains a function goal_callback that is called whenever a goal is received by the server. Inside the goal_callback function, we execute some task and give feedback to the client as the task progresses. Once the task is complete, we set the result of the action and set the server to have succeeded.

Note that we also define the message types PoseAction, PoseFeedback and PoseResult in a separate your_package.msg file. These messages are used to define the interface between the client and the server.

gistlibby LogSnag