完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
自定义服务数据
定义srv文件 cd ~/catkin_workspace/src/learning_service mkdir srv nano ~/catkin_workspace/src/learning_service/srv/Person.srv string name uint8 age uint8 sex uint8 unknown = 0 uint8 male = 1 uint8 female = 2 --- string result 在package.xml中添加功能包依赖 nano ~/catkin_workspace/src/learning_service/package.xml 在CMakeLists.txt添加编译选项 ... find_package(... message_generation) ... add_service_files(FILES Person.srv) generate_messages(DEPENDENCIES std_msgs) ... catkin_package(... message_runtime) ... nano ~/catkin_workspace/src/learning_service/CMakeLists.txt 编译生成语言相关文件 cd ~/catkin_workspacecatkin_make
cd ~/catkin_workspace/devel/include/learning_servicels
使用 C++ 编写程序 person_server.cpp nano ~/catkin_workspace/src/learning_service/src/person_server.cpp /*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程将执行/show_person服务,服务数据类型learning_service::Person */ #include #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; } person_client.cpp nano ~/catkin_workspace/src/learning_service/src/person_client.cpp /*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程将请求/show_person服务,服务数据类型learning_service::Person */ #include #include "learning_service/Person.h" int main(int argc, char** argv) { // 初始化ROS节点 ros::init(argc, argv, "person_client"); // 创建节点句柄 ros::NodeHandle node; // 发现/show_person服务后,创建一个服务客户端,连接名为/show_person的service ros::service::waitForService("/show_person"); ros::ServiceClient person_client = node.serviceClient // 初始化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; } 配置CMakeLists.txt nano ~/catkin_workspace/src/learning_service/CMakeLists.txt 添加 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) 设置需要编译的代码和生成的可执行文件; 设置链接库; 添加依赖项。 编译并运行 cd ~/catkin_workspace catkin_make # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_service person_server # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_service person_client python 编写程序 person_server.py nano ~/catkin_workspace/src/learning_service/scripts/person_server.py person_client.py nano ~/catkin_workspace/src/learning_service/scripts/person_client.py #!/usr/bin/env python # -*- coding: utf-8 -*- ######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################## # 该例程将请求/show_person服务,服务数据类型learning_service::Person import sys import rospy from learning_service.srv import Person, PersonRequest def person_client(): # ROS节点初始化 rospy.init_node('person_client') # 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service rospy.wait_for_service('/show_person') try: person_client = rospy.ServiceProxy('/show_person', Person) # 请求服务调用,输入请求数据 response = person_client("Tom", 20, PersonRequest.male) return response.result except rospy.ServiceException, e: print "Service call failed: %s"%e if __name__ == "__main__": #服务调用并显示调用结果 print "Show person result : %s" %(person_client()) 运行 sudo chmod +x ~/catkin_workspace/src/learning_service/scripts/person_server.py sudo chmod +x ~/catkin_workspace/src/learning_service/scripts/person_client.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_service person_server.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_service person_client.py |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
基于米尔瑞芯微RK3576核心板/开发板的人脸疲劳检测应用方案
678 浏览 0 评论
920 浏览 1 评论
814 浏览 1 评论
2030 浏览 1 评论
3275 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-25 01:37 , Processed in 0.484033 second(s), Total 41, Slave 35 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号