12.ROS action server

In the last lesson, we learned about the principle of action communication and how to create an action client. This lesson explains how to write an action server.

12.1. Write action client code

Under the learn_action function package, create a new file named action_server.cpp and copy the following content into it.

Modify CMakeLists.txt file and add the following content,

Return to the workspace to compile,

Let’s look at the core code first,

A server is defined and the service provided is "do_dishes", and then the server starts running. The specific content of the service is the excute function.

The main service content here has two points. One is as->publishFeedback(feedback), where the feedback is posted back to the client. After the client receives it, it will execute the content in the feedbackCb function;The other is as->setSucceeded();, which sets the status of the active target to successful. Then the client receives it and executes the content in the doneCb function.

12.2 Run DoDishes program

Combined with the client program written in the previous section, let's take a look at how the entire DoDishes runs. First, we still need to start roscore and enter in the terminal.

Run action client program,

Run action server program,

After the program is started, the client will print the feedback information sent back by the server at a frequency of one point. After 100 seconds, the entire action is completed and "Yay!The dishes are now clean", at that time, the server will also print "Dishwasher 1 finishing working.".

image-20231026160732023