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# roscore

terminal2

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: 8terminal2

root@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
原创粉丝点击