Skip to content

Commit 5399935

Browse files
committed
add: learning_service
1 parent 4d6de6f commit 5399935

11 files changed

+666
-0
lines changed

Diff for: catkin_wp/src/learning_service/CMakeLists.txt

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(learning_service)
3+
4+
## Compile as C++11, supported in ROS Kinetic and newer
5+
# add_compile_options(-std=c++11)
6+
7+
## Find catkin macros and libraries
8+
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
9+
## is used, also find other catkin packages
10+
find_package(catkin REQUIRED COMPONENTS
11+
geometry_msgs
12+
roscpp
13+
rospy
14+
std_msgs
15+
turtlesim
16+
message_generation
17+
)
18+
19+
## System dependencies are found with CMake's conventions
20+
# find_package(Boost REQUIRED COMPONENTS system)
21+
22+
23+
## Uncomment this if the package has a setup.py. This macro ensures
24+
## modules and global scripts declared therein get installed
25+
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
26+
# catkin_python_setup()
27+
28+
################################################
29+
## Declare ROS messages, services and actions ##
30+
################################################
31+
32+
## To declare and build messages, services or actions from within this
33+
## package, follow these steps:
34+
## * Let MSG_DEP_SET be the set of packages whose message types you use in
35+
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
36+
## * In the file package.xml:
37+
## * add a build_depend tag for "message_generation"
38+
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
39+
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
40+
## but can be declared for certainty nonetheless:
41+
## * add a exec_depend tag for "message_runtime"
42+
## * In this file (CMakeLists.txt):
43+
## * add "message_generation" and every package in MSG_DEP_SET to
44+
## find_package(catkin REQUIRED COMPONENTS ...)
45+
## * add "message_runtime" and every package in MSG_DEP_SET to
46+
## catkin_package(CATKIN_DEPENDS ...)
47+
## * uncomment the add_*_files sections below as needed
48+
## and list every .msg/.srv/.action file to be processed
49+
## * uncomment the generate_messages entry below
50+
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
51+
52+
## Generate messages in the 'msg' folder
53+
# add_message_files(
54+
# FILES
55+
# Message1.msg
56+
# Message2.msg
57+
# )
58+
59+
## Generate services in the 'srv' folder
60+
# add_service_files(
61+
# FILES
62+
# Service1.srv
63+
# Service2.srv
64+
# )
65+
66+
## Generate actions in the 'action' folder
67+
# add_action_files(
68+
# FILES
69+
# Action1.action
70+
# Action2.action
71+
# )
72+
73+
## Generate added messages and services with any dependencies listed here
74+
# generate_messages(
75+
# DEPENDENCIES
76+
# geometry_msgs# std_msgs
77+
# )
78+
79+
add_service_files(
80+
FILES
81+
Person.srv
82+
)
83+
84+
generate_messages(
85+
DEPENDENCIES
86+
std_msgs
87+
)
88+
89+
################################################
90+
## Declare ROS dynamic reconfigure parameters ##
91+
################################################
92+
93+
## To declare and build dynamic reconfigure parameters within this
94+
## package, follow these steps:
95+
## * In the file package.xml:
96+
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
97+
## * In this file (CMakeLists.txt):
98+
## * add "dynamic_reconfigure" to
99+
## find_package(catkin REQUIRED COMPONENTS ...)
100+
## * uncomment the "generate_dynamic_reconfigure_options" section below
101+
## and list every .cfg file to be processed
102+
103+
## Generate dynamic reconfigure parameters in the 'cfg' folder
104+
# generate_dynamic_reconfigure_options(
105+
# cfg/DynReconf1.cfg
106+
# cfg/DynReconf2.cfg
107+
# )
108+
109+
###################################
110+
## catkin specific configuration ##
111+
###################################
112+
## The catkin_package macro generates cmake config files for your package
113+
## Declare things to be passed to dependent projects
114+
## INCLUDE_DIRS: uncomment this if your package contains header files
115+
## LIBRARIES: libraries you create in this project that dependent projects also need
116+
## CATKIN_DEPENDS: catkin_packages dependent projects also need
117+
## DEPENDS: system dependencies of this project that dependent projects also need
118+
catkin_package(
119+
# INCLUDE_DIRS include
120+
# LIBRARIES learning_service
121+
CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim message_runtime
122+
# DEPENDS system_lib
123+
)
124+
125+
###########
126+
## Build ##
127+
###########
128+
129+
## Specify additional locations of header files
130+
## Your package locations should be listed before other locations
131+
include_directories(
132+
include
133+
${catkin_INCLUDE_DIRS}
134+
)
135+
136+
## Declare a C++ library
137+
# add_library(${PROJECT_NAME}
138+
# src/${PROJECT_NAME}/learning_service.cpp
139+
# )
140+
141+
## Add cmake target dependencies of the library
142+
## as an example, code may need to be generated before libraries
143+
## either from message generation or dynamic reconfigure
144+
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
145+
146+
## Declare a C++ executable
147+
## With catkin_make all packages are built within a single CMake context
148+
## The recommended prefix ensures that target names across packages don't collide
149+
# add_executable(${PROJECT_NAME}_node src/learning_service_node.cpp)
150+
151+
## Rename C++ executable without prefix
152+
## The above recommended prefix causes long target names, the following renames the
153+
## target back to the shorter version for ease of user use
154+
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
155+
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
156+
157+
## Add cmake target dependencies of the executable
158+
## same as for the library above
159+
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
160+
161+
## Specify libraries to link a library or executable target against
162+
# target_link_libraries(${PROJECT_NAME}_node
163+
# ${catkin_LIBRARIES}
164+
# )
165+
166+
add_executable(turtle_spawn src/turtle_spawn.cpp) # 将cpp编译为turtle_spawn
167+
target_link_libraries(turtle_spawn ${catkin_LIBRARIES})
168+
169+
170+
add_executable(person_server src/person_server.cpp)
171+
target_link_libraries(person_server ${catkin_LIBRARIES})
172+
add_dependencies(person_server ${PROJECT_NAME}_gencpp)
173+
174+
add_executable(person_client src/person_client.cpp)
175+
target_link_libraries(person_client ${catkin_LIBRARIES})
176+
add_dependencies(person_client ${PROJECT_NAME}_gencpp)
177+
178+
#############
179+
## Install ##
180+
#############
181+
182+
# all install targets should use catkin DESTINATION variables
183+
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
184+
185+
## Mark executable scripts (Python etc.) for installation
186+
## in contrast to setup.py, you can choose the destination
187+
# install(PROGRAMS
188+
# scripts/my_python_script
189+
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
190+
# )
191+
192+
## Mark executables and/or libraries for installation
193+
# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node
194+
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
195+
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
196+
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
197+
# )
198+
199+
## Mark cpp header files for installation
200+
# install(DIRECTORY include/${PROJECT_NAME}/
201+
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
202+
# FILES_MATCHING PATTERN "*.h"
203+
# PATTERN ".svn" EXCLUDE
204+
# )
205+
206+
## Mark other files for installation (e.g. launch and bag files, etc.)
207+
# install(FILES
208+
# # myfile1
209+
# # myfile2
210+
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
211+
# )
212+
213+
#############
214+
## Testing ##
215+
#############
216+
217+
## Add gtest based cpp test target and link libraries
218+
# catkin_add_gtest(${PROJECT_NAME}-test test/test_learning_service.cpp)
219+
# if(TARGET ${PROJECT_NAME}-test)
220+
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
221+
# endif()
222+
223+
## Add folders to be run by python nosetests
224+
# catkin_add_nosetests(test)

Diff for: catkin_wp/src/learning_service/package.xml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0"?>
2+
<package format="2">
3+
<name>learning_service</name>
4+
<version>0.0.0</version>
5+
<description>The learning_service package</description>
6+
7+
<!-- One maintainer tag required, multiple allowed, one person per tag -->
8+
<!-- Example: -->
9+
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
10+
<maintainer email="[email protected]">hcx</maintainer>
11+
12+
13+
<!-- One license tag required, multiple allowed, one license per tag -->
14+
<!-- Commonly used license strings: -->
15+
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
16+
<license>TODO</license>
17+
18+
19+
<!-- Url tags are optional, but multiple are allowed, one per tag -->
20+
<!-- Optional attribute type can be: website, bugtracker, or repository -->
21+
<!-- Example: -->
22+
<!-- <url type="website">http://wiki.ros.org/learning_service</url> -->
23+
24+
25+
<!-- Author tags are optional, multiple are allowed, one per tag -->
26+
<!-- Authors do not have to be maintainers, but could be -->
27+
<!-- Example: -->
28+
<!-- <author email="[email protected]">Jane Doe</author> -->
29+
30+
31+
<!-- The *depend tags are used to specify dependencies -->
32+
<!-- Dependencies can be catkin packages or system dependencies -->
33+
<!-- Examples: -->
34+
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
35+
<!-- <depend>roscpp</depend> -->
36+
<!-- Note that this is equivalent to the following: -->
37+
<!-- <build_depend>roscpp</build_depend> -->
38+
<!-- <exec_depend>roscpp</exec_depend> -->
39+
<!-- Use build_depend for packages you need at compile time: -->
40+
<!-- <build_depend>message_generation</build_depend> -->
41+
<!-- Use build_export_depend for packages you need in order to build against this package: -->
42+
<!-- <build_export_depend>message_generation</build_export_depend> -->
43+
<!-- Use buildtool_depend for build tool packages: -->
44+
<!-- <buildtool_depend>catkin</buildtool_depend> -->
45+
<!-- Use exec_depend for packages you need at runtime: -->
46+
<!-- <exec_depend>message_runtime</exec_depend> -->
47+
<!-- Use test_depend for packages you need only for testing: -->
48+
<!-- <test_depend>gtest</test_depend> -->
49+
<!-- Use doc_depend for packages you need only for building documentation: -->
50+
<!-- <doc_depend>doxygen</doc_depend> -->
51+
<buildtool_depend>catkin</buildtool_depend>
52+
<build_depend>geometry_msgs</build_depend>
53+
<build_depend>roscpp</build_depend>
54+
<build_depend>rospy</build_depend>
55+
<build_depend>std_msgs</build_depend>
56+
<build_depend>turtlesim</build_depend>
57+
<build_export_depend>geometry_msgs</build_export_depend>
58+
<build_export_depend>roscpp</build_export_depend>
59+
<build_export_depend>rospy</build_export_depend>
60+
<build_export_depend>std_msgs</build_export_depend>
61+
<build_export_depend>turtlesim</build_export_depend>
62+
<exec_depend>geometry_msgs</exec_depend>
63+
<exec_depend>roscpp</exec_depend>
64+
<exec_depend>rospy</exec_depend>
65+
<exec_depend>std_msgs</exec_depend>
66+
<exec_depend>turtlesim</exec_depend>
67+
68+
<build_depend>message_generation</build_depend>
69+
<exec_depend>message_runtime</exec_depend>
70+
71+
<!-- The export tag contains other, unspecified, tags -->
72+
<export>
73+
<!-- Other tools can request additional information be placed here -->
74+
75+
</export>
76+
</package>
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
########################################################################
5+
#### Copyright 2020 GuYueHome (www.guyuehome.com). ###
6+
########################################################################
7+
8+
# 该例程将请求/show_person服务,服务数据类型learning_service::Person
9+
10+
import sys
11+
import rospy
12+
from learning_service.srv import Person, PersonRequest
13+
14+
def person_client():
15+
# ROS节点初始化
16+
rospy.init_node('person_client')
17+
18+
# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
19+
rospy.wait_for_service('/show_person')
20+
try:
21+
person_client = rospy.ServiceProxy('/show_person', Person)
22+
23+
# 请求服务调用,输入请求数据
24+
response = person_client("Tom", 20, PersonRequest.male)
25+
return response.result
26+
except rospy.ServiceException, e:
27+
print "Service call failed: %s"%e
28+
29+
if __name__ == "__main__":
30+
#服务调用并显示调用结果
31+
print "Show person result : %s" %(person_client())
32+
33+
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
########################################################################
5+
#### Copyright 2020 GuYueHome (www.guyuehome.com). ###
6+
########################################################################
7+
8+
# 该例程将执行/show_person服务,服务数据类型learning_service::Person
9+
10+
import rospy
11+
from learning_service.srv import Person, PersonResponse
12+
13+
def personCallback(req):
14+
# 显示请求数据
15+
rospy.loginfo("Person: name:%s age:%d sex:%d", req.name, req.age, req.sex)
16+
17+
# 反馈数据
18+
return PersonResponse("OK")
19+
20+
def person_server():
21+
# ROS节点初始化
22+
rospy.init_node('person_server')
23+
24+
# 创建一个名为/show_person的server,注册回调函数personCallback
25+
s = rospy.Service('/show_person', Person, personCallback)
26+
27+
# 循环等待回调函数
28+
print "Ready to show person informtion."
29+
rospy.spin()
30+
31+
if __name__ == "__main__":
32+
person_server()
33+
34+

0 commit comments

Comments
 (0)