-
Notifications
You must be signed in to change notification settings - Fork 0
/
ros的msg和srv
128 lines (109 loc) · 3.57 KB
/
ros的msg和srv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
功能包:chapter2_tutorials
创建一个msg和srv文件:
msg文件
首先在功能包文件夹下创建msg文件,并且创建一个新的文件chapter2_msg1.msg.
内容:
int32 A;
int32 B;
int32 C;
编辑package.xml,从<build_depend>message_genneration</build_depend>和<run_depend>message_runtime</run_depend>行删除<!--------------->;
编辑CMakelist.txt:
1 按照下面表示加入message_generation:
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msg
message_generation
)
2 找到如下行,并且取消注释,并且加入新的消息名称:
## Generate message in the 'msg' folder
add_message_files(
FILES
chapter2_msg1.msg
)
##Generate added messages and services with any dependencise listed here
generate_messages(
generate_messages(
DEPENDENCIES
std_msgs
)
)
srv文件:
创建一个srv文件夹.在chapter2_tutorials功能包下创建一个名为srv的文件夹,并创建文件chapter2_srv1.srv,并在文件中增加以下
int32 A;
int32 B;
int32 C;
---
int32 sum;
为了编译新的msg和srv文件,必须取消package.xml和CMakelist.txt的如下的注释.
找到下面行,并取消注释:
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
找到CMakelist.txt找到下面行,并且取消注释
catkin_package(
CATKIN_DEPENDS message_runtime
)
为了生成消息,需要在find_package中添加message_generation行:
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
message_generation
)
在 add_message_files(
FILES
chapter2_msg1.msg //添加
)
在add_message_files(
FILES
chapter2_srv1.srv
)
取消generate_message部分的注释,可以使消息顺利生成;
在功能包package下的src创建example2_a.cpp和example2_b.cpp为名称.
example2_a.cpp代码:
#inlcued "ros/ros.h"
#include "chapter2_tutorials/chapter2_srv1.h" //包含必要的头文件和创建的srv文件
bool add(chapter2_tutorials::chapter2_srv1::Request &req, //这个函数会对三个变量求和,并将计算结果发送给给他节点.
chapter2_tutorials::chapter2_srv1::Respons &res)
{
res.sum = req.A + req.B + req.c;
ROS_INFO("request:A=%1d, B=%1d, C=%1d", (int)req.A, (int)req.B, (int)req.C);
ROS_INFO("sending back response:[%1d]", (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中发布广播
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, chat **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 = ato11(argv[1]);
srv.request.B = ato11(argv[2]);
srv.request.C = ato11(argv[3]);
if(client.call(srv))
{
ROS_INFO("sum:%1d",(long int)srv.response.sum);
}
else
{
ROS_INFO("Failed to call service add_3_ints");
return 1;
}
return 0;
}