#include "ros/ros.h" #include "std_msgs/String.h" bool serviceCallback(std_srvs::Empty::Request& req, std_srvs::Empty::Response& res) { ROS_INFO("Received service request"); return true; } int main(int argc, char** argv) { ros::init(argc, argv, "service_node"); ros::NodeHandle n; ros::ServiceServer service = n.advertiseService("my_service", serviceCallback); ROS_INFO("Service server ready"); ros::spin(); return 0; }
#include "ros/ros.h" #include "math_service/AddTwoInts.h" bool addCallback(math_service::AddTwoInts::Request& req, math_service::AddTwoInts::Response& res) { int sum = req.a + req.b; res.sum = sum; ROS_INFO("Received service request: %d + %d = %d", req.a, req.b, sum); return true; } int main(int argc, char** argv) { ros::init(argc, argv, "add_node"); ros::NodeHandle n; ros::ServiceServer service = n.advertiseService("add_two_ints", addCallback); ROS_INFO("Service server ready"); ros::spin(); return 0; }In this example, the `advertiseService()` function is used to create a service server called `add_two_ints`. The function `addCallback()` is executed whenever a request is received, and in this case it adds two integers passed in the request and returns the sum in the response. The package library used in this example is likely a custom library called `math_service`. Based on the above examples, it appears that the package library used with `advertiseService()` can vary depending on the specific service being implemented and the associated messages.