ros(7)自定义service数据
生活随笔
收集整理的這篇文章主要介紹了
ros(7)自定义service数据
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
創(chuàng)建Persom.srv
新建srv文件夾
在srv文件夾中創(chuàng)建Persom.srv,編輯文件
string name uint8 age uint8 sexuint8 unknown = 0 uint8 male = 1 uint8 female = 2--- string result修改package,xml
add_service_files(FILESPerson.srv )generate_messages(DEPENDENCIESstd_msgs )修改cmake
find_package(catkin REQUIRED COMPONENTSgeometry_msgsroscpprospystd_msgsturtlesimmessage_generation #查找依賴 )add_service_files(FILESPerson.srv )generate_messages(DEPENDENCIESstd_msgs )catkin_package( # INCLUDE_DIRS include # LIBRARIES learning_serviceCATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim message_runtime # DEPENDS system_lib )編譯
catkin_make
編輯person_client.cpp
?
/*** 該例程將請求/show_person服務(wù),服務(wù)數(shù)據(jù)類型learning_service::Person*/#include <ros/ros.h> #include "learning_service/Person.h"int main(int argc, char** argv) {// 初始化ROS節(jié)點ros::init(argc, argv, "person_client");// 創(chuàng)建節(jié)點句柄ros::NodeHandle node;// 發(fā)現(xiàn)/spawn服務(wù)后,創(chuàng)建一個服務(wù)客戶端,連接名為/spawn的serviceros::service::waitForService("/show_person");ros::ServiceClient person_client = node.serviceClient<learning_service::Person>("/show_person");// 初始化learning_service::Person的請求數(shù)據(jù)learning_service::Person srv;srv.request.name = "Tom";srv.request.age = 20;srv.request.sex = learning_service::Person::Request::male;// 請求服務(wù)調(diào)用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);// 顯示服務(wù)調(diào)用結(jié)果ROS_INFO("Show person result : %s", srv.response.result.c_str());return 0; };?
編輯person_server.cpp
/*** 該例程將執(zhí)行/show_person服務(wù),服務(wù)數(shù)據(jù)類型learning_service::Person*/#include <ros/ros.h> #include "learning_service/Person.h"// service回調(diào)函數(shù),輸入?yún)?shù)req,輸出參數(shù)res bool personCallback(learning_service::Person::Request &req,learning_service::Person::Response &res) {// 顯示請求數(shù)據(jù)ROS_INFO("Person: name:%s age:%d sex:%d", req.name.c_str(), req.age, req.sex);// 設(shè)置反饋數(shù)據(jù)res.result = "OK";return true; }int main(int argc, char **argv) {// ROS節(jié)點初始化ros::init(argc, argv, "person_server");// 創(chuàng)建節(jié)點句柄ros::NodeHandle n;// 創(chuàng)建一個名為/show_person的server,注冊回調(diào)函數(shù)personCallbackros::ServiceServer person_service = n.advertiseService("/show_person", personCallback);// 循環(huán)等待回調(diào)函數(shù)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
總結(jié)
以上是生活随笔為你收集整理的ros(7)自定义service数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面试70问 经典回答
- 下一篇: Hive 外部表关联分区数据