ROS 学习笔记(三):自定义服务数据srv+server+client 示例运行
ROS 學(xué)習(xí)筆記(三):自定義服務(wù)數(shù)據(jù)srv+Server+Client 示例運(yùn)行
一、自定義服務(wù)數(shù)據(jù):
1.向功能包添加自定義服務(wù)文件(AddTwoInts.srv)
cd ~/catkin_ws/src/mypackage mkdir srv cd srv2.生成并編輯自定義服務(wù)文件:
gedit AddTwoInts.srv添加內(nèi)容:
int64 a int64 b --- int64 sum3.修改添加~/catkin_ws/src/mypackage/package.xml 文件中內(nèi)容
...<build_depend>message_generation</build_depend><exec_depend>message_runtime</exec_depend> ...4.修改添加~/catkin_ws/src/mypackage/CMakeList.txt 文件中內(nèi)容
(message_generation 同時(shí)針對(duì)話題消息和服務(wù)消息類型描述文件生成相應(yīng)的代碼)
5.編譯后查看服務(wù).srv
cd ~/catkin_ws rosservice list注意:對(duì)于AddTwoInts.h 是 AddTwoInts.srv文件在編譯后自動(dòng)生成的
二、Server+Client 示例:
1,~/catkin_ws/mypackage/src 目錄中,編寫倆個(gè)源文件server.cpp和client.cpp
(編譯器編寫或文本編寫)
server.cpp 源碼:
#include "ros/ros.h" #include "mypackage/AddTwoInts.h"// 回調(diào)函數(shù)部分(實(shí)現(xiàn)服務(wù)功能) bool add(mypackage::AddTwoInts::Request &req,mypackage::AddTwoInts::Response &res) {res.sum = req.a + req.b;ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);ROS_INFO("sending back response: [%ld]", (long int)res.sum);return true; }int main(int argc, char **argv) {// ROS節(jié)點(diǎn)初始化ros::init(argc, argv, "add_two_ints_server");// 創(chuàng)建節(jié)點(diǎn)句柄 ros::NodeHandle nh;// 創(chuàng)建名為 "add_two_ints" server 實(shí)例示例:ros::ServiceServer service = nh.advertiseService("add_two_ints", add);ROS_INFO("Ready to add two ints!");ros::spin();return 0; }client.cpp 源碼:
#include "ros/ros.h" #include "mypackage/AddTwoInts.h" #include <cstdlib>int main(int argc, char **argv) {// ROS節(jié)點(diǎn)初始化ros::init(argc, argv, "add_two_ints_client");// 終端獲取兩個(gè)數(shù)if (argc != 3){ROS_INFO("usage: rosrun package_name executable_name arguments");return 1;}// 創(chuàng)建節(jié)點(diǎn)句柄ros::NodeHandle nh;// 創(chuàng)建一個(gè)client,請(qǐng)求 "add_two_ints" service :ros::ServiceClient client = nh.serviceClient<mypackage::AddTwoInts>("add_two_ints");mypackage::AddTwoInts srv;srv.request.a = atol(argv[1]);srv.request.b = atol(argv[2]);// 發(fā)布service 請(qǐng)求,等待應(yīng)答if (client.call(srv)){ROS_INFO("with the given [%ld] and [%ld],the sum is: [%ld]", srv.request.a, srv.request.b, (long int)srv.response.sum);}else{ROS_ERROR("Failed to call service: add_two_ints");return 1;}return 0; }2 編譯功能包源碼(catkin_make方式編譯)
打開 CMakeList.txt 文件:
1)設(shè)置頭文件相對(duì)位置,默認(rèn)為功能包的所在目錄
2)生成可執(zhí)行文件 talker 和 listener
3)設(shè)置鏈接庫(kù)(系統(tǒng)或第三方庫(kù)函數(shù))此處并沒有使用其他庫(kù)
4)設(shè)置依賴
5) 生成server和client 兩個(gè)可執(zhí)行文件于 ~/catkin_ws/devel/lib/mypackage 中
CMakeList.txt 添加修改代碼如下:
include_directories(include ${catkin_INCLUDE_DIRS})add_executable(server src/server.cpp) add_dependencies(server ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) target_link_libraries(server ${catkin_LIBRARIES})add_executable(client src/client.cpp) add_dependencies(client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) target_link_libraries(client ${catkin_LIBRARIES})3.終端工作空間根目錄 ~/catkin_ws 開始編譯
cd ~/catkin_ws catkin_make source devel/setup.bash // 已在于終端環(huán)境變量文件中設(shè)置的無(wú)需重新設(shè)置三、直接運(yùn)行 server client
1.新建終端,運(yùn)行ros master
cd ~/catkin_ws roscore2.新建終端,運(yùn)行服務(wù)器節(jié)點(diǎn) server
rosrun mypackage server3.新建終端,運(yùn)行客戶端節(jié)點(diǎn) client
rosrun mypackage client 100 2224.結(jié)果顯示:
[INFO] [146235634.23453542]:with the given [100] and [222],the sum is: [322]四、創(chuàng)建launch啟動(dòng)文件運(yùn)行
1.創(chuàng)建launch文件夾,創(chuàng)建啟動(dòng)文件 mypackage.launch
cd catkin_ws/src/mypackage/ mkdir launch cd launch/ touch mypackage.launchmypackage.launch文件內(nèi)容,設(shè)置同時(shí)啟動(dòng)服務(wù)節(jié)點(diǎn):
<launch><node pkg="mypackage" type="server" name="add_two_ints_server" output="screen"/> </launch>2.運(yùn)行l(wèi)aunch文件并查看消息(默認(rèn)會(huì)運(yùn)行roscore)
roslaunch mypackage mypackage.launch3.新建終端,運(yùn)行client 執(zhí)行文件輸入兩個(gè)整型參數(shù):
rosrun mypackage client 100 1004.結(jié)果顯示:
[INFO] [146235634.23453542]:with the given [100] and [100],the sum is: [200]總結(jié)
以上是生活随笔為你收集整理的ROS 学习笔记(三):自定义服务数据srv+server+client 示例运行的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 私有private成员和保护protec
- 下一篇: 机器学习中防止过拟合的方法总结