ROS实践(4)-消息服务
来源:互联网 发布:java ee为什么这么小 编辑:程序博客网 时间:2024/06/05 21:03
一 编辑msg文件,并编译
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# cd ~/dev/root@yangkai04-Inspiron-3650:~/dev# ls
rosbook
root@yangkai04-Inspiron-3650:~/dev# cd rosbook/
root@yangkai04-Inspiron-3650:~/dev/rosbook# ls
chapter2_tutorials
root@yangkai04-Inspiron-3650:~/dev/rosbook# cd chapter2_tutorials/
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# ls
bin build CMakeLists.txt include mainpage.dox Makefile manifest.xml src
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# ls
bin build CMakeLists.txt include mainpage.dox Makefile manifest.xml src
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# mkdir msg
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# ls
bin CMakeLists.txt mainpage.dox manifest.xml src
build include Makefile msg
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# cd msg/
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/msg# vim chapter2_msg1.msg
int32 Aint32 Bint32 C
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/msg# rosmake chapter2_tutorials
[ rosmake ] Results:
[ rosmake ] Built 25 packages with 0 failures.
[ rosmake ] Summary output to directory
[ rosmake ] /root/.ros/rosmake/rosmake_output-20161109-190250
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/msg# rosmsg show chapter2_tutorials/chapter2_msg1
int32 A
int32 B
int32 C
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/msg# rosed chapter2_tutorials CMakeLists.txt
osbuild_genmsg()解注释
二 编辑svr文件
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/msg# cd ..root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# mkdir srv
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# vim srv/chapter2_srv1.srv
int32 Aint32 Bint32 C---int32 sum
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# rosed chapter2_tutorials CMakeLists.txt
rosbuild_gensrv()解注释
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# rosmake chapter2_tutorials
[ rosmake ] Results:
[ rosmake ] Built 25 packages with 0 failures.
[ rosmake ] Summary output to directory
[ rosmake ] /root/.ros/rosmake/rosmake_output-20161109-190846
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# rossrv show chapter2_tutorials/chapter2_srv1
int32 A
int32 B
int32 C
---
int32 sum
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials#
三 添加代码并编译
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# pwd/root/dev/rosbook/chapter2_tutorials/src
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# cp /home/yangkai04/Documents/Learning\ ROS\ for\ Robotics\ Programming\ 1448OS_Code/1448OS_02_Code/chapter2_tutorials/src/example2_* .
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# rosmake chapter2_tutorials
[ rosmake ] Results:
[ rosmake ] Built 25 packages with 0 failures.
[ rosmake ] Summary output to directory
[ rosmake ] /root/.ros/rosmake/rosmake_output-20161109-191544
修改CMakefile
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# rosed chapter2_tutorials CMakeLists.txt
rosbuild_add_executable(example2_a src/example2_a.cpp)rosbuild_add_executable(example2_b src/example2_b.cpp)
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# rosmake chapter2_tutorials
四 启动
terminal1
root@yangkai04-Inspiron-3650:/home/yangkai04# roscoreterminal2
root@yangkai04-Inspiron-3650:/home/yangkai04# rosrun chapter2_tutorials example2_a[ INFO] [1478690632.264029358]: Ready to add 3 ints.
terminal3
root@yangkai04-Inspiron-3650:/home/yangkai04# rosrun chapter2_tutorials example2_b[ INFO] [1478690678.339046946]: usage: add_3_ints_client A B C
root@yangkai04-Inspiron-3650:/home/yangkai04# rosrun chapter2_tutorials example2_b 1 3 4
[ INFO] [1478690687.261165534]: Sum: 8terminal2root@yangkai04-Inspiron-3650:/home/yangkai04# rosrun chapter2_tutorials example2_a
[ INFO] [1478690632.264029358]: Ready to add 3 ints.
[ INFO] [1478690687.260884068]: request: A=140733193388033, B=140733193388035 C=4
[ INFO] [1478690687.260910991]: sending back response: [140733193388040]
五 代码
example2_a.cpp
include "ros/ros.h"#include "chapter2_tutorials/chapter2_srv1.h"bool add(chapter2_tutorials::chapter2_srv1::Request &req, chapter2_tutorials::chapter2_srv1::Response &res){ res.sum = req.A + req.B + req.C; ROS_INFO("request: A=%ld, B=%ld C=%ld", (int)req.A, (int)req.B, (int)req.C); ROS_INFO("sending back response: [%ld]", (int)res.sum); return true;}int main(int argc, char **argv){ ros::init(argc, argv, "add_3_ints_server"); ros::NodeHandle n; ros::ServiceServer service = n.advertiseService("add_3_ints", add); ROS_INFO("Ready to add 3 ints."); ros::spin(); return 0;}
example2_b.cpp
#include "ros/ros.h"#include "chapter2_tutorials/chapter2_srv1.h"#include <cstdlib>int main(int argc, char **argv){ ros::init(argc, argv, "add_3_ints_client"); if (argc != 4) { ROS_INFO("usage: add_3_ints_client A B C "); return 1; } ros::NodeHandle n; ros::ServiceClient client = n.serviceClient<chapter2_tutorials::chapter2_srv1>("add_3_ints"); chapter2_tutorials::chapter2_srv1 srv; srv.request.A = atoll(argv[1]); srv.request.B = atoll(argv[2]); srv.request.C = atoll(argv[3]); if (client.call(srv)) { ROS_INFO("Sum: %ld", (long int)srv.response.sum); } else { ROS_ERROR("Failed to call service add_two_ints"); return 1; } return 0;}
0 0
- ROS实践(4)-消息服务
- ROS教程(十):创建ROS消息和ROS服务
- ROS笔记:(7)创建ROS消息和ROS服务
- 创建ROS消息和ROS服务(九)
- ROS 服务 消息传递
- ROS Learning-012 beginner_Tutorials (编程) 创建自定义的ROS消息和ROS服务
- ROS学习第四弹 (ROS编辑器及配置/ROS msg/srv 消息和服务)
- 创建ROS消息和ROS服务
- 创建ROS消息和ROS服务
- ROS入门_1.13 创建ROS消息和ROS服务
- ROS总结——ROS消息和ROS服务
- ROS学习笔记4-使用节点,消息,主题和服务
- ROS(6)消息和服务具体实现
- ROS学习笔记(消息msg和服务srv)
- ROS学习笔记(七):新建一个消息和服务
- ROS学习(基于Ubuntu 15.04 和ROS Jade)第三章 ROS核心教程 之 10 创建ROS消息和ROS服务
- ros:(4)理解ROS服务和参数
- ROS节点、消息、服务、主题的关系
- Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条(三十一)
- HTTP 状态代码
- Java IO使用和总结
- 基于Servlet的MVC模式Demo
- 欢迎使用CSDN-markdown编辑器
- ROS实践(4)-消息服务
- SQL游标(循环)单层和双层
- 用route命令添加永久路由
- 视觉SLAM漫谈(二):图优化理论与g2o的使用
- python的 a,b=b,a+b 和 a=b b=a+b 的区别
- UVA - 10125 Sumsets
- 2016/11/9
- 视觉SLAM漫谈 (三): 研究点介绍
- Linux 双网卡(内外网) 同时使用路由设置