#################################### ##FILL ME IN #################################### ## links to any required tutorials ## note.0= [[actionlib_tutorials/Tutorials/SimpleActionServer(ExecuteCallbackMethod)|writing a simple action server using the execute callback method]] ## descriptive title for the tutorial ## title = Writing a Simple Action Client ## multi-line description to be displayed in search ## description = This tutorial covers using the simple_action_client library to create a Fibonacci action client. This example program creates an action client and sends a goal to the action server. ## the next tutorial description ## next = ## links to next tutorial ## next.0.link= ## what level user is this tutorial for ## level= BeginnerCategory #################################### <> <> == The Code == First, create `actionlib_tutorials/src/fibonacci_client.cpp` in your favorite editor, and place the following inside it: {{{ #!cplusplus block=action #include #include #include #include int main (int argc, char **argv) { ros::init(argc, argv, "test_fibonacci"); // create the action client // true causes the client to spin its own thread actionlib::SimpleActionClient ac("fibonacci", true); ROS_INFO("Waiting for action server to start."); // wait for the action server to start ac.waitForServer(); //will wait for infinite time ROS_INFO("Action server started, sending goal."); // send a goal to the action actionlib_tutorials::FibonacciGoal goal; goal.order = 20; ac.sendGoal(goal); //wait for the action to return bool finished_before_timeout = ac.waitForResult(ros::Duration(30.0)); if (finished_before_timeout) { actionlib::SimpleClientGoalState state = ac.getState(); ROS_INFO("Action finished: %s",state.toString().c_str()); } else { ROS_INFO("Action did not finish before the time out."); ac.cancelGoal(); } //exit return 0; } }}} (Original can be found on [[https://github.com/ros/common_tutorials/blob/hydro-devel/actionlib_tutorials/src/fibonacci_client.cpp|the repository of the tutorial package]]) == The Code Explained == Now, let's break down the code piece by piece. <> * `actionlib/client/simple_action_client.h` is the action library used from implementing simple action clients. * `actionlib/client/terminal_state.h` defines the possible goal states. <> This includes action message generated from the `Fibonacci.action` file shown above. This is a header generated automatically from the [[http://www.ros.org/doc/api/actionlib_tutorials/html/msg/FibonacciAction.html|FibonacciAction.msg]] file. For more information on message definitions, see the [[msg]] page. <> The action client is templated on the action definition, specifying what message types to communicate to the action server with. The action client constructor also takes two arguments, the server name to connect to and a boolean option to automatically spin a thread. If you prefer not to use threads (and you want `actionlib` to do the 'thread magic' behind the scenes), this is a good option for you. Here the action client is constructed with the server name and the auto spin option set to true. <> Since the action server may not be up and running, the action client will wait for the action server to start before continuing. <> Here a goal message is created, the goal value is set and sent to the action server. <> The action client now waits for the goal to finish before continuing. The timeout on the wait is set to 30 seconds, this means after 30 seconds the function will return with false if the goal has not finished. <> If the goal finished before the time out the goal status is reported, else the user is notified that the goal did not finish in the allotted time. == Compiling == Add the following lines to your `CMakeLists.txt` file: <> {{{{#!wiki buildsystem catkin {{{ add_executable(fibonacci_client src/fibonacci_client.cpp) target_link_libraries( fibonacci_client ${catkin_LIBRARIES} ) add_dependencies( fibonacci_client ${actionlib_tutorials_EXPORTED_TARGETS} ) }}} Then build: {{{ cd %TOPDIR_YOUR_CATKIN_WORKSPACE% catkin_make source devel/setup.bash }}} }}}} {{{{#!wiki buildsystem rosbuild {{{ rosbuild_add_executable(fibonacci_client src/fibonacci_client.cpp) rosbuild_link_boost(fibonacci_client thread) }}} }}}} == Running the Action client == After you have successfully built the executable, start a new terminal and run the client: {{{ $ rosrun actionlib_tutorials fibonacci_client }}} You will see something similar to: . {{{ [ INFO] 1250806286.804217000: Started node [/test_fibonacci], pid [9414], bound on [aqy], xmlrpc port [35466], tcpros port [55866], logging to [~/ros/ros/log/test_fibonacci_9414.log], using [real] time [ INFO] 1250806287.828279000: Waiting for action server to start. }}} To check that your client is running properly, list ROS topics being published: {{{ $ rostopic list -v }}} You will see something similar to: . {{{ Published topics: * /fibonacci/goal [actionlib_tutorials/FibonacciActionGoal] 1 publisher * /fibonacci/cancel [actionlib_msgs/GoalID] 1 publisher * /rosout [rosgraph_msgs/Log] 1 publisher * /rosout_agg [rosgraph_msgs/Log] 1 publisher Subscribed topics: * /fibonacci/feedback [actionlib_tutorials/FibonacciActionFeedback] 1 subscriber * /rosout [rosgraph_msgs/Log] 1 subscriber * /fibonacci/status [actionlib_msgs/GoalStatusArray] 1 subscriber * /fibonacci/result [actionlib_tutorials/FibonacciActionResult] 1 subscriber }}} Alternatively you can look at the nodes: {{{ $ rxgraph }}} Or, starting with Groovy: {{{ $ rqt_graph }}} {{attachment:fibonacci_client.png||width="100%"}} This shows that your client is subscribing to the feedback, status, and result channels as expected and publishing to the goal and cancel channels as expected. The client is up and running properly. == Connecting the Server and Client == For the next step in using your action, you need to `Ctrl-C` the action client and [[actionlib_tutorials/Tutorials/RunningServerAndClient|run the action server and client]]. ## TutorialCategory ## CommonStackTutorial