## For instruction on writing tutorials ## http://www.ros.org/wiki/WritingTutorials #################################### ##FILL ME IN #################################### ## for a custom note with links: ## note = ## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links ## note.0= ## descriptive title for the tutorial ## title = Writing a Simple Action Client (Python) ## multi-line description to be displayed in search ## ## description = This tutorial covers using the action_client library to create a Fibonacci simple action client in Python. ## ## the next tutorial description (optional) ## next = ## links to next tutorial (optional) ## next.0.link= ## next.1.link= ## what level user is this tutorial for ## level= BeginnerCategory ## keywords = #################################### <> <> The code and examples used in this tutorial can be found in the [[actionlib_tutorials]] package. You may want to read about the [[actionlib]] package before starting this tutorial. == The Code == The following code can be found in [[https://github.com/ros/common_tutorials/blob/hydro-devel/actionlib_tutorials/scripts/fibonacci_client.py|actionlib_tutorials repository]], and implements a simple python action client for the [[http://docs.ros.org/api/actionlib_tutorials/html/classfibonacci__server_1_1FibonacciAction.html|fibonacci action]]. {{{ #!python block=action #! /usr/bin/env python import rospy from __future__ import print_function # Brings in the SimpleActionClient import actionlib # Brings in the messages used by the fibonacci action, including the # goal message and the result message. import actionlib_tutorials.msg def fibonacci_client(): # Creates the SimpleActionClient, passing the type of the action # (FibonacciAction) to the constructor. client = actionlib.SimpleActionClient('fibonacci', actionlib_tutorials.msg.FibonacciAction) # Waits until the action server has started up and started # listening for goals. client.wait_for_server() # Creates a goal to send to the action server. goal = actionlib_tutorials.msg.FibonacciGoal(order=20) # Sends the goal to the action server. client.send_goal(goal) # Waits for the server to finish performing the action. client.wait_for_result() # Prints out the result of executing the action return client.get_result() # A FibonacciResult if __name__ == '__main__': try: # Initializes a rospy node so that the SimpleActionClient can # publish and subscribe over ROS. rospy.init_node('fibonacci_client_py') result = fibonacci_client() print("Result:", ', '.join([str(n) for n in result.sequence])) except rospy.ROSInterruptException: print("program interrupted before completion", file=sys.stderr) }}} == The Code, explained == <> The action specification generates several messages for sending goals, receiving feedback, etc... This line imports the generated messages. <> The action client and server communicate over a set of topics, described in [[actionlib/DetailedDescription|the actionlib protocol]]. The action name describes the namespace containing these topics, and the action specification message describes what messages should be passed along these topics. <> Sending goals before the action server comes up would be useless. This line waits until we are connected to the action server. <> Creates a goal and sends it to the action server. <> The action server will process the goal and eventually terminate. We want the result from the termination, but we wait until the server has finished with the goal. == Running the Client == Before running the client, we assume `roscore` ans Action server are already running from [[actionlib_tutorials/Tutorials/Writing a Simple Action Server using the Execute Callback (Python)#Compiling|previous page]]. Start the client. It will start up, send a goal to the server, wait for the goal to complete, and then exit. {{{ $ rosrun actionlib_tutorials fibonacci_client.py }}} ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE