其他分享
首页 > 其他分享> > ros(8)自定义service数据

ros(8)自定义service数据

作者:互联网

创建Persom.srv

新建srv文件夹

在srv文件夹中创建Persom.srv,编辑文件

string name
uint8  age
uint8  sex
 
uint8 unknown = 0
uint8 male    = 1
uint8 female  = 2
 
---
string result

修改package,xml

add_service_files(
  FILES
  Person.srv
)
 
generate_messages(
  DEPENDENCIES
  std_msgs
)

修改cmake

find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  roscpp
  rospy
  std_msgs
  turtlesim
  message_generation #查找依赖
)
 
 
add_service_files(
  FILES
  Person.srv
)
 
generate_messages(
  DEPENDENCIES
  std_msgs
)
 
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES learning_service
   CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim message_runtime
#  DEPENDS system_lib
)

编译

catkin_make

编辑person_client.cpp

/**
 * 该例程将请求/show_person服务,服务数据类型learning_service::Person
 */
 
#include <ros/ros.h>
#include "learning_service/Person.h"
 
int main(int argc, char** argv)
{
    // 初始化ROS节点
    ros::init(argc, argv, "person_client");
 
    // 创建节点句柄
    ros::NodeHandle node;
 
    // 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
    ros::service::waitForService("/show_person");
    ros::ServiceClient person_client = node.serviceClient<learning_service::Person>("/show_person");
 
    // 初始化learning_service::Person的请求数据
    learning_service::Person srv;
    srv.request.name = "Tom";
    srv.request.age  = 20;
    srv.request.sex  = learning_service::Person::Request::male;
 
    // 请求服务调用
    ROS_INFO("Call service to show person[name:%s, age:%d, sex:%d]", 
             srv.request.name.c_str(), srv.request.age, srv.request.sex);
 
    person_client.call(srv);
 
    // 显示服务调用结果
    ROS_INFO("Show person result : %s", srv.response.result.c_str());
 
    return 0;
};

编辑person_server.cpp

/**
 * 该例程将执行/show_person服务,服务数据类型learning_service::Person
 */
 
#include <ros/ros.h>
#include "learning_service/Person.h"
 
// service回调函数,输入参数req,输出参数res
bool personCallback(learning_service::Person::Request  &req,
                     learning_service::Person::Response &res)
{
    // 显示请求数据
    ROS_INFO("Person: name:%s  age:%d  sex:%d", req.name.c_str(), req.age, req.sex);
 
    // 设置反馈数据
    res.result = "OK";
 
    return true;
}
 
int main(int argc, char **argv)
{
    // ROS节点初始化
    ros::init(argc, argv, "person_server");
 
    // 创建节点句柄
    ros::NodeHandle n;
 
    // 创建一个名为/show_person的server,注册回调函数personCallback
    ros::ServiceServer person_service = n.advertiseService("/show_person", personCallback);
 
    // 循环等待回调函数
    ROS_INFO("Ready to show person informtion.");
    ros::spin();
 
    return 0;
}

修改cmake

add_executable(person_server src/person_server.cpp)
target_link_libraries(person_server ${catkin_LIBRARIES})
add_dependencies(person_server ${PROJECT_NAME}_gencpp)
 
add_executable(person_client src/person_client.cpp)
target_link_libraries(person_client ${catkin_LIBRARIES})
add_dependencies(person_client ${PROJECT_NAME}_gencpp)

编译:

catkin_make

运行:

rosrun service_example person_client

rosrun service_example person_server

标签:自定义,service,Person,person,srv,learning,ros
来源: https://www.cnblogs.com/gjianli/p/15642134.html