#include <ros/ros.h>
#include <service_client_pkg/ServiceClientExMsg.h> // 注意文件扩展名应该是 .h 而不是 .hx
using namespace std;
// 修正了 service_client_pkg::ServiceClientExMsgRequest 和 service_client_pkg::ServiceClientExMsgResponse 的命名空间
bool infoInquiry(service_client_pkg::ServiceClientExMsgRequest& request,
service_client_pkg::ServiceClientExMsgResponse& response) {
ROS_INFO("Callback activated");
string inputName = request.name;
response.in_class = false;
if (inputName.compare("Tom") == 0) {
ROS_INFO("Student information about Tom");
response.in_class = true;
response.boy = true;
response.age = 20;
response.personality = "outgoing";
} else if (inputName.compare("Mary") == 0) {
ROS_INFO("Student information about Mary");
response.in_class = true;
response.boy = false;
response.age = 21;
response.personality = "introverted";
}
return true;
}
int main(int argc, char **argv) {
ros::init(argc, argv, "service_example_node");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("info_inquiry_by_name", infoInquiry);
ROS_INFO("Ready to inquiry names.");
ros::spin();
return 0;
}
标签:,INFO,service,pkg,ros,true,response
From: https://www.cnblogs.com/lgqlht/p/18470260