feat: chapt6
This commit is contained in:
31
chapt4/chapt4_ws/src/chapt4_interfaces/CMakeLists.txt
Normal file
31
chapt4/chapt4_ws/src/chapt4_interfaces/CMakeLists.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
cmake_minimum_required(VERSION 3.8)
|
||||
project(chapt4_interfaces)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
# find dependencies
|
||||
find_package(ament_cmake REQUIRED)
|
||||
find_package(rosidl_default_generators REQUIRED)
|
||||
find_package(sensor_msgs REQUIRED)
|
||||
|
||||
rosidl_generate_interfaces(${PROJECT_NAME}
|
||||
"srv/FaceDetector.srv"
|
||||
"srv/Patrol.srv"
|
||||
DEPENDENCIES sensor_msgs
|
||||
)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
find_package(ament_lint_auto REQUIRED)
|
||||
# the following line skips the linter which checks for copyrights
|
||||
# comment the line when a copyright and license is added to all source files
|
||||
set(ament_cmake_copyright_FOUND TRUE)
|
||||
# the following line skips cpplint (only works in a git repo)
|
||||
# comment the line when this package is in a git repo and when
|
||||
# a copyright and license is added to all source files
|
||||
set(ament_cmake_cpplint_FOUND TRUE)
|
||||
ament_lint_auto_find_test_dependencies()
|
||||
endif()
|
||||
|
||||
ament_package()
|
||||
202
chapt4/chapt4_ws/src/chapt4_interfaces/LICENSE
Normal file
202
chapt4/chapt4_ws/src/chapt4_interfaces/LICENSE
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
21
chapt4/chapt4_ws/src/chapt4_interfaces/package.xml
Normal file
21
chapt4/chapt4_ws/src/chapt4_interfaces/package.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0"?>
|
||||
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||
<package format="3">
|
||||
<name>chapt4_interfaces</name>
|
||||
<version>0.0.0</version>
|
||||
<description>TODO: Package description</description>
|
||||
<maintainer email="mzebra@foxmail.com">mzebra</maintainer>
|
||||
<license>Apache-2.0</license>
|
||||
|
||||
<buildtool_depend>ament_cmake</buildtool_depend>
|
||||
|
||||
<depend>rosidl_default_generators</depend>
|
||||
<depend>sensor_msgs</depend>
|
||||
<member_of_group>rosidl_interface_packages</member_of_group>
|
||||
<test_depend>ament_lint_auto</test_depend>
|
||||
<test_depend>ament_lint_common</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_cmake</build_type>
|
||||
</export>
|
||||
</package>
|
||||
@@ -0,0 +1,8 @@
|
||||
sensor_msgs/Image image # 原始图像
|
||||
---
|
||||
int16 number # 人脸个数
|
||||
float32 use_time # 识别耗时
|
||||
int32[] top # 人脸在图像中位置
|
||||
int32[] right
|
||||
int32[] bottom
|
||||
int32[] left
|
||||
6
chapt4/chapt4_ws/src/chapt4_interfaces/srv/Patrol.srv
Normal file
6
chapt4/chapt4_ws/src/chapt4_interfaces/srv/Patrol.srv
Normal file
@@ -0,0 +1,6 @@
|
||||
float32 target_x # 目标x值
|
||||
float32 target_y # 目标y值
|
||||
---
|
||||
int8 SUCCESS = 1 # 定义常量,表示成功
|
||||
int8 FAIL = 0 # 定义常量,表示失败
|
||||
int8 result # 处理结果
|
||||
45
chapt4/chapt4_ws/src/demo_cpp_service/CMakeLists.txt
Normal file
45
chapt4/chapt4_ws/src/demo_cpp_service/CMakeLists.txt
Normal file
@@ -0,0 +1,45 @@
|
||||
cmake_minimum_required(VERSION 3.8)
|
||||
project(demo_cpp_service)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
# find dependencies
|
||||
find_package(ament_cmake REQUIRED)
|
||||
find_package(chapt4_interfaces REQUIRED)
|
||||
find_package(rclcpp REQUIRED)
|
||||
find_package(geometry_msgs REQUIRED)
|
||||
find_package(turtlesim REQUIRED)
|
||||
|
||||
add_executable(turtle_control src/turtle_control.cpp)
|
||||
ament_target_dependencies(turtle_control rclcpp geometry_msgs turtlesim chapt4_interfaces)
|
||||
|
||||
add_executable(patrol_client src/patrol_client.cpp)
|
||||
ament_target_dependencies(patrol_client rclcpp geometry_msgs turtlesim chapt4_interfaces)
|
||||
|
||||
|
||||
install(TARGETS
|
||||
turtle_control
|
||||
patrol_client
|
||||
DESTINATION lib/${PROJECT_NAME}
|
||||
)
|
||||
|
||||
install(DIRECTORY launch
|
||||
DESTINATION share/${PROJECT_NAME}
|
||||
)
|
||||
|
||||
|
||||
if(BUILD_TESTING)
|
||||
find_package(ament_lint_auto REQUIRED)
|
||||
# the following line skips the linter which checks for copyrights
|
||||
# comment the line when a copyright and license is added to all source files
|
||||
set(ament_cmake_copyright_FOUND TRUE)
|
||||
# the following line skips cpplint (only works in a git repo)
|
||||
# comment the line when this package is in a git repo and when
|
||||
# a copyright and license is added to all source files
|
||||
set(ament_cmake_cpplint_FOUND TRUE)
|
||||
ament_lint_auto_find_test_dependencies()
|
||||
endif()
|
||||
|
||||
ament_package()
|
||||
202
chapt4/chapt4_ws/src/demo_cpp_service/LICENSE
Normal file
202
chapt4/chapt4_ws/src/demo_cpp_service/LICENSE
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
32
chapt4/chapt4_ws/src/demo_cpp_service/launch/demo.launch.py
Normal file
32
chapt4/chapt4_ws/src/demo_cpp_service/launch/demo.launch.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import launch
|
||||
import launch_ros
|
||||
|
||||
def generate_launch_description():
|
||||
action_declare_arg_max_spped = launch.actions.DeclareLaunchArgument('launch_max_speed', default_value='2.0')
|
||||
|
||||
action_node_turtle_control = launch_ros.actions.Node(
|
||||
package='demo_cpp_service',
|
||||
executable="turtle_control",
|
||||
output='screen',
|
||||
parameters=[{'max_speed': launch.substitutions.LaunchConfiguration(
|
||||
'launch_max_speed', default='2.0')}],
|
||||
|
||||
)
|
||||
action_node_patrol_client = launch_ros.actions.Node(
|
||||
package='demo_cpp_service',
|
||||
executable="patrol_client",
|
||||
output='log',
|
||||
)
|
||||
action_node_turtlesim_node = launch_ros.actions.Node(
|
||||
package='turtlesim',
|
||||
executable='turtlesim_node',
|
||||
output='both',
|
||||
)
|
||||
# 合成启动描述并返回
|
||||
launch_description = launch.LaunchDescription([
|
||||
action_declare_arg_max_spped,
|
||||
action_node_turtle_control,
|
||||
action_node_patrol_client,
|
||||
action_node_turtlesim_node
|
||||
])
|
||||
return launch_description
|
||||
23
chapt4/chapt4_ws/src/demo_cpp_service/package.xml
Normal file
23
chapt4/chapt4_ws/src/demo_cpp_service/package.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0"?>
|
||||
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||
<package format="3">
|
||||
<name>demo_cpp_service</name>
|
||||
<version>0.0.0</version>
|
||||
<description>TODO: Package description</description>
|
||||
<maintainer email="mzebra@foxmail.com">mzebra</maintainer>
|
||||
<license>Apache-2.0</license>
|
||||
|
||||
<buildtool_depend>ament_cmake</buildtool_depend>
|
||||
|
||||
<depend>chapt4_interfaces</depend>
|
||||
<depend>rclcpp</depend>
|
||||
<depend>geometry_msgs</depend>
|
||||
<depend>turtlesim</depend>
|
||||
|
||||
<test_depend>ament_lint_auto</test_depend>
|
||||
<test_depend>ament_lint_common</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_cmake</build_type>
|
||||
</export>
|
||||
</package>
|
||||
132
chapt4/chapt4_ws/src/demo_cpp_service/src/patrol_client.cpp
Normal file
132
chapt4/chapt4_ws/src/demo_cpp_service/src/patrol_client.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "chapt4_interfaces/srv/patrol.hpp"
|
||||
#include <chrono> // 引入时间相关头文件
|
||||
#include "rcl_interfaces/msg/parameter.hpp"
|
||||
#include "rcl_interfaces/msg/parameter_value.hpp"
|
||||
#include "rcl_interfaces/msg/parameter_type.hpp"
|
||||
#include "rcl_interfaces/srv/set_parameters.hpp"
|
||||
|
||||
// 使用时间单位的字面量,可以在代码中使用 s 和 ms 表示时间
|
||||
using namespace std::chrono_literals;
|
||||
using Patrol = chapt4_interfaces::srv::Patrol;
|
||||
using SetP = rcl_interfaces::srv::SetParameters;
|
||||
|
||||
class PatrolClient : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
PatrolClient() : Node("patrol_client")
|
||||
{
|
||||
patrol_client_ = this->create_client<Patrol>("patrol");
|
||||
timer_ = this->create_wall_timer(10s, std::bind(&PatrolClient::timer_callback, this));
|
||||
srand(time(NULL)); // 初始化随机数种子,使用当前时间作为种子
|
||||
}
|
||||
void update_server_param_k(double k)
|
||||
{
|
||||
// 1.创建一个参数对象
|
||||
auto param = rcl_interfaces::msg::Parameter();
|
||||
param.name = "k";
|
||||
// 2.创建参数值对象并赋值
|
||||
auto param_value = rcl_interfaces::msg::ParameterValue();
|
||||
param_value.type = rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE;
|
||||
param_value.double_value = k;
|
||||
param.value = param_value;
|
||||
// 3.请求更新参数并处理
|
||||
auto response = call_set_parameters(param);
|
||||
if (response == nullptr)
|
||||
{
|
||||
RCLCPP_WARN(this->get_logger(), "参数修改失败");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto result : response->results)
|
||||
{
|
||||
if (result.successful)
|
||||
{
|
||||
RCLCPP_INFO(this->get_logger(), "参数k 已修改为:%f", k);
|
||||
}
|
||||
else
|
||||
{
|
||||
RCLCPP_WARN(this->get_logger(), "参数k 失败原因:%s", result.reason.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<SetP::Response> call_set_parameters(
|
||||
rcl_interfaces::msg::Parameter ¶meter)
|
||||
{
|
||||
// 1. 创建客户端等待服务上线
|
||||
auto param_client = this->create_client<SetP>(
|
||||
"/turtle_controller/set_parameters");
|
||||
while (!param_client->wait_for_service(std::chrono::seconds(1)))
|
||||
{
|
||||
if (!rclcpp::ok())
|
||||
{
|
||||
RCLCPP_ERROR(this->get_logger(), "等待服务的过程中被打断...");
|
||||
return nullptr;
|
||||
}
|
||||
RCLCPP_INFO(this->get_logger(), "等待参数设置服务端上线中");
|
||||
}
|
||||
// 2. 创建请求对象
|
||||
auto request =
|
||||
std::make_shared<SetP::Request>();
|
||||
request->parameters.push_back(parameter);
|
||||
// 3. 异步调用、等待并返回响应结果
|
||||
auto future = param_client->async_send_request(request);
|
||||
rclcpp::spin_until_future_complete(this->get_node_base_interface(), future);
|
||||
auto response = future.get();
|
||||
return response;
|
||||
}
|
||||
|
||||
void timer_callback()
|
||||
{
|
||||
// 1.等待服务端上线
|
||||
while (!patrol_client_->wait_for_service(1s))
|
||||
{
|
||||
// 等待时检测rclcpp的状态
|
||||
if (!rclcpp::ok())
|
||||
{
|
||||
RCLCPP_ERROR(this->get_logger(), "等待服务的过程中被打断...");
|
||||
return;
|
||||
}
|
||||
RCLCPP_INFO(this->get_logger(), "等待服务端上线中");
|
||||
}
|
||||
// 2.构造请求的
|
||||
auto request = std::make_shared<Patrol::Request>();
|
||||
request->target_x = rand() % 15;
|
||||
request->target_y = rand() % 15;
|
||||
RCLCPP_INFO(this->get_logger(), "请求巡逻:(%f,%f)", request->target_x, request->target_y);
|
||||
// 3.发送异步请求,然后等待返回,返回时调用回调函数
|
||||
patrol_client_->async_send_request(
|
||||
request,
|
||||
[&](rclcpp::Client<Patrol>::SharedFuture result_future) -> void
|
||||
{
|
||||
auto response = result_future.get();
|
||||
if (response->result == Patrol::Response::SUCCESS)
|
||||
{
|
||||
RCLCPP_INFO(this->get_logger(), "目标点处理成功");
|
||||
}
|
||||
else if (response->result == Patrol::Response::FAIL)
|
||||
{
|
||||
RCLCPP_INFO(this->get_logger(), "目标点处理失败");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
rclcpp::TimerBase::SharedPtr timer_;
|
||||
rclcpp::Client<Patrol>::SharedPtr patrol_client_;
|
||||
};
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
auto node = std::make_shared<PatrolClient>();
|
||||
node->update_server_param_k(1.5);
|
||||
rclcpp::spin(node);
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
116
chapt4/chapt4_ws/src/demo_cpp_service/src/turtle_control.cpp
Normal file
116
chapt4/chapt4_ws/src/demo_cpp_service/src/turtle_control.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "geometry_msgs/msg/twist.hpp"
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "turtlesim/msg/pose.hpp"
|
||||
#include "chapt4_interfaces/srv/patrol.hpp"
|
||||
using Patrol = chapt4_interfaces::srv::Patrol;
|
||||
#include "rcl_interfaces/msg/set_parameters_result.hpp"
|
||||
using SetParametersResult = rcl_interfaces::msg::SetParametersResult;
|
||||
|
||||
|
||||
class TurtleController : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
TurtleController() : Node("turtle_controller")
|
||||
{
|
||||
velocity_publisher_ = this->create_publisher<geometry_msgs::msg::Twist>(
|
||||
"/turtle1/cmd_vel", 10);
|
||||
pose_subscription_ = this->create_subscription<turtlesim::msg::Pose>(
|
||||
"/turtle1/pose", 10,
|
||||
std::bind(&TurtleController::on_pose_received_, this, std::placeholders::_1));
|
||||
// 3.创建服务
|
||||
patrol_server_ = this->create_service<Patrol>(
|
||||
"patrol",
|
||||
[&](const std::shared_ptr<Patrol::Request> request,
|
||||
std::shared_ptr<Patrol::Response> response) -> void {
|
||||
// 判断巡逻点是否在模拟器边界内
|
||||
if ((0 < request->target_x && request->target_x < 12.0f)
|
||||
&& (0 < request->target_y && request->target_y < 12.0f)) {
|
||||
target_x_ = request->target_x;
|
||||
target_y_ = request->target_y;
|
||||
response->result = Patrol::Response::SUCCESS;
|
||||
}else{
|
||||
response->result = Patrol::Response::FAIL;
|
||||
}
|
||||
});
|
||||
// 声明和获取参数初始值
|
||||
this->declare_parameter("k", 1.0);
|
||||
this->declare_parameter("max_speed", 1.0);
|
||||
this->get_parameter("k", k_);
|
||||
this->get_parameter("max_speed", max_speed_);
|
||||
|
||||
// 添加参数设置回调
|
||||
parameters_callback_handle_ = this->add_on_set_parameters_callback(
|
||||
[&](const std::vector<rclcpp::Parameter> ¶ms)
|
||||
-> SetParametersResult {
|
||||
// 遍历参数
|
||||
for (auto param : params) {
|
||||
RCLCPP_INFO(this->get_logger(), "更新参数 %s 值为:%f",param.get_name().c_str(), param.as_double());
|
||||
if (param.get_name() == "k") {
|
||||
k_ = param.as_double();
|
||||
} else if (param.get_name() == "max_speed") {
|
||||
max_speed_ = param.as_double();
|
||||
}
|
||||
}
|
||||
auto result = SetParametersResult();
|
||||
result.successful = true;
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
void on_pose_received_(const turtlesim::msg::Pose::SharedPtr pose) {
|
||||
auto message = geometry_msgs::msg::Twist();
|
||||
// 1.记录当前位置
|
||||
double current_x = pose->x;
|
||||
double current_y = pose->y;
|
||||
RCLCPP_INFO(this->get_logger(), "当前位置:(x=%f,y=%f)", current_x, current_y);
|
||||
|
||||
// 2.计算距离目标的距离,与当前海龟朝向的角度差
|
||||
double distance =
|
||||
std::sqrt((target_x_ - current_x) * (target_x_ - current_x) +
|
||||
(target_y_ - current_y) * (target_y_ - current_y));
|
||||
double angle =
|
||||
std::atan2(target_y_ - current_y, target_x_ - current_x) - pose->theta;
|
||||
|
||||
// 3.控制策略:距离大于0.1继续运动,角度差大于0.2则原地旋转,否则直行
|
||||
if (distance > 0.1) {
|
||||
if(fabs(angle)>0.2)
|
||||
{
|
||||
message.angular.z = fabs(angle);
|
||||
}else{
|
||||
// 通过比例控制器计算输出速度
|
||||
message.linear.x = k_ * distance;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 4.限制最大值并发布消息
|
||||
if (message.linear.x > max_speed_) {
|
||||
message.linear.x = max_speed_;
|
||||
}
|
||||
velocity_publisher_->publish(message);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
// 2.添加 Patrol 类型服务共享指针 patrol_server_ 为成员变量
|
||||
rclcpp::Service<Patrol>::SharedPtr patrol_server_;
|
||||
|
||||
rclcpp::Subscription<turtlesim::msg::Pose>::SharedPtr pose_subscription_;
|
||||
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr velocity_publisher_;
|
||||
double target_x_{1.0}; // 目标位置X,设置默认值1.0
|
||||
double target_y_{1.0}; // 目标位置Y,设置默认值1.0
|
||||
double k_{1.0}; // 比例系数,控制输出=误差*比例系数
|
||||
double max_speed_{3.0}; // 最大线速度,设置默认值3.0
|
||||
OnSetParametersCallbackHandle::SharedPtr parameters_callback_handle_;
|
||||
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
auto node = std::make_shared<TurtleController>();
|
||||
rclcpp::spin(node);
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
202
chapt4/chapt4_ws/src/demo_python_service/LICENSE
Normal file
202
chapt4/chapt4_ws/src/demo_python_service/LICENSE
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,92 @@
|
||||
import rclpy
|
||||
from rclpy.node import Node
|
||||
from chapt4_interfaces.srv import FaceDetector
|
||||
from sensor_msgs.msg import Image
|
||||
from ament_index_python.packages import get_package_share_directory
|
||||
import cv2
|
||||
from cv_bridge import CvBridge
|
||||
from rcl_interfaces.srv import SetParameters
|
||||
from rcl_interfaces.msg import Parameter, ParameterValue, ParameterType
|
||||
|
||||
|
||||
class FaceDetectorClient(Node):
|
||||
def __init__(self):
|
||||
super().__init__('face_detect_client')
|
||||
self.client = self.create_client(FaceDetector, '/face_detect')
|
||||
self.bridge = CvBridge()
|
||||
self.test1_image_path = get_package_share_directory(
|
||||
'demo_python_service')+'/resource/test1.jpg'
|
||||
self.image = cv2.imread(self.test1_image_path)
|
||||
|
||||
def send_request(self):
|
||||
# 1.判断服务是否上线
|
||||
while self.client.wait_for_service(timeout_sec=1.0) is False:
|
||||
self.get_logger().info(f'等待服务端上线....')
|
||||
# 2.构造 Request
|
||||
request = FaceDetector.Request()
|
||||
request.image = self.bridge.cv2_to_imgmsg(self.image)
|
||||
# 3.发送并 spin 等待服务处理完成
|
||||
future = self.client.call_async(request)
|
||||
rclpy.spin_until_future_complete(self, future)
|
||||
# 4.根据处理结果
|
||||
response = future.result()
|
||||
self.get_logger().info(
|
||||
f'接收到响应: 图像中共有:{response.number}张脸,耗时{response.use_time}')
|
||||
# 注释show_face_locations,防止显示堵塞无法多次请求
|
||||
# self.show_face_locations(response)
|
||||
|
||||
def call_set_parameters(self, parameters):
|
||||
# 1. 创建一个客户端,并等待服务上线
|
||||
client = self.create_client(
|
||||
SetParameters, '/face_detection_node/set_parameters')
|
||||
while not client.wait_for_service(timeout_sec=1.0):
|
||||
self.get_logger().info('等待参数设置服务端上线....')
|
||||
# 2. 创建请求对象
|
||||
request = SetParameters.Request()
|
||||
request.parameters = parameters
|
||||
# 3. 异步调用、等待并返回响应结果
|
||||
future = client.call_async(request)
|
||||
rclpy.spin_until_future_complete(self, future)
|
||||
response = future.result()
|
||||
return response
|
||||
|
||||
def update_detect_model(self,model):
|
||||
# 1.创建一个参数对象
|
||||
param = Parameter()
|
||||
param.name = "face_locations_model"
|
||||
# 2.创建参数值对象并赋值
|
||||
new_model_value = ParameterValue()
|
||||
new_model_value.type = ParameterType.PARAMETER_STRING
|
||||
new_model_value.string_value = model
|
||||
param.value = new_model_value
|
||||
# 3.请求更新参数并处理
|
||||
response = self.call_set_parameters([param])
|
||||
for result in response.results:
|
||||
if result.successful:
|
||||
self.get_logger().info(f'参数 {param.name} 设置为{model}')
|
||||
else:
|
||||
self.get_logger().info(f'参数设置失败,原因为:{result.reason}')
|
||||
|
||||
|
||||
def show_face_locations(self, response):
|
||||
for i in range(response.number):
|
||||
top = response.top[i]
|
||||
right = response.right[i]
|
||||
bottom = response.bottom[i]
|
||||
left = response.left[i]
|
||||
cv2.rectangle(self.image, (left, top),
|
||||
(right, bottom), (255, 0, 0), 2)
|
||||
|
||||
cv2.imshow('Face Detection Result', self.image)
|
||||
cv2.waitKey(0)
|
||||
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
face_detect_client = FaceDetectorClient()
|
||||
face_detect_client.update_detect_model('hog')
|
||||
face_detect_client.send_request()
|
||||
face_detect_client.update_detect_model('cnn')
|
||||
face_detect_client.send_request()
|
||||
rclpy.spin(face_detect_client)
|
||||
rclpy.shutdown()
|
||||
@@ -0,0 +1,50 @@
|
||||
import rclpy
|
||||
from rclpy.node import Node
|
||||
from chapt4_interfaces.srv import FaceDetector
|
||||
from ament_index_python.packages import get_package_share_directory
|
||||
from cv_bridge import CvBridge # 用于转换格式
|
||||
import cv2
|
||||
import face_recognition
|
||||
import time
|
||||
|
||||
|
||||
class FaceDetectorionNode(Node):
|
||||
def __init__(self):
|
||||
super().__init__('face_detection_node')
|
||||
self.bridge = CvBridge()
|
||||
self.service = self.create_service(FaceDetector, '/face_detect', self.detect_face_callback)
|
||||
self.defaut_image_path = get_package_share_directory('demo_python_service')+'/resource/default.jpg'
|
||||
self.upsample_times = 1
|
||||
self.model = "hog"
|
||||
# 声明和获取参数
|
||||
self.declare_parameter('face_locations_upsample_times', 1)
|
||||
self.declare_parameter('face_locations_model', "hog")
|
||||
self.model = self.get_parameter("face_locations_model").value
|
||||
self.upsample_times = self.get_parameter("face_locations_upsample_times").value
|
||||
|
||||
def detect_face_callback(self, request, response):
|
||||
if request.image.data:
|
||||
cv_image = self.bridge.imgmsg_to_cv2(
|
||||
request.image)
|
||||
else:
|
||||
cv_image = cv2.imread(self.defaut_image_path)
|
||||
start_time = time.time()
|
||||
self.get_logger().info('加载完图像,开始检测')
|
||||
face_locations = face_recognition.face_locations(cv_image, number_of_times_to_upsample=self.upsample_times, model=self.model)
|
||||
end_time = time.time()
|
||||
self.get_logger().info(f'检测完成,耗时{end_time-start_time}')
|
||||
response.number = len(face_locations)
|
||||
response.use_time = end_time - start_time
|
||||
for top, right, bottom, left in face_locations:
|
||||
response.top.append(top)
|
||||
response.right.append(right)
|
||||
response.bottom.append(bottom)
|
||||
response.left.append(left)
|
||||
return response
|
||||
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
node = FaceDetectorionNode()
|
||||
rclpy.spin(node)
|
||||
rclpy.shutdown()
|
||||
@@ -0,0 +1,19 @@
|
||||
import face_recognition
|
||||
import cv2
|
||||
from ament_index_python.packages import get_package_share_directory
|
||||
|
||||
def main():
|
||||
# 获取图片真实路径
|
||||
defaut_image_path = get_package_share_directory(
|
||||
'demo_python_service')+'/resource/default.jpg'
|
||||
# 使用 opencv 加载图像
|
||||
image = cv2.imread(defaut_image_path)
|
||||
# 查找图像中所有的人脸
|
||||
face_locations = face_recognition.face_locations(
|
||||
image, number_of_times_to_upsample=1, model='hog')
|
||||
# 绘制每个人脸的边框
|
||||
for top, right, bottom, left in face_locations:
|
||||
cv2.rectangle(image, (left, top), (right, bottom), (255, 0, 0), 4)
|
||||
# 显示结果图像
|
||||
cv2.imshow('Face Detection', image)
|
||||
cv2.waitKey(0)
|
||||
@@ -0,0 +1,32 @@
|
||||
import launch
|
||||
import launch_ros
|
||||
|
||||
def generate_launch_description():
|
||||
action_declare_arg_max_spped = launch.actions.DeclareLaunchArgument('launch_max_speed', default_value='2.0')
|
||||
|
||||
action_node_turtle_control = launch_ros.actions.Node(
|
||||
package='demo_cpp_service',
|
||||
executable="turtle_control",
|
||||
output='screen',
|
||||
parameters=[{'max_speed': launch.substitutions.LaunchConfiguration(
|
||||
'launch_max_speed', default='2.0')}],
|
||||
|
||||
)
|
||||
action_node_patrol_client = launch_ros.actions.Node(
|
||||
package='demo_cpp_service',
|
||||
executable="patrol_client",
|
||||
output='log',
|
||||
)
|
||||
action_node_turtlesim_node = launch_ros.actions.Node(
|
||||
package='turtlesim',
|
||||
executable='turtlesim_node',
|
||||
output='both',
|
||||
)
|
||||
# 合成启动描述并返回
|
||||
launch_description = launch.LaunchDescription([
|
||||
action_declare_arg_max_spped,
|
||||
action_node_turtle_control,
|
||||
action_node_patrol_client,
|
||||
action_node_turtlesim_node
|
||||
])
|
||||
return launch_description
|
||||
21
chapt4/chapt4_ws/src/demo_python_service/package.xml
Normal file
21
chapt4/chapt4_ws/src/demo_python_service/package.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0"?>
|
||||
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||
<package format="3">
|
||||
<name>demo_python_service</name>
|
||||
<version>0.0.0</version>
|
||||
<description>TODO: Package description</description>
|
||||
<maintainer email="mzebra@foxmail.com">mzebra</maintainer>
|
||||
<license>Apache-2.0</license>
|
||||
|
||||
<depend>rclpy</depend>
|
||||
<depend>chapt4_interfaces</depend>
|
||||
|
||||
<test_depend>ament_copyright</test_depend>
|
||||
<test_depend>ament_flake8</test_depend>
|
||||
<test_depend>ament_pep257</test_depend>
|
||||
<test_depend>python3-pytest</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_python</build_type>
|
||||
</export>
|
||||
</package>
|
||||
BIN
chapt4/chapt4_ws/src/demo_python_service/resource/default.jpg
Normal file
BIN
chapt4/chapt4_ws/src/demo_python_service/resource/default.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
BIN
chapt4/chapt4_ws/src/demo_python_service/resource/test1.jpg
Normal file
BIN
chapt4/chapt4_ws/src/demo_python_service/resource/test1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
4
chapt4/chapt4_ws/src/demo_python_service/setup.cfg
Normal file
4
chapt4/chapt4_ws/src/demo_python_service/setup.cfg
Normal file
@@ -0,0 +1,4 @@
|
||||
[develop]
|
||||
script_dir=$base/lib/demo_python_service
|
||||
[install]
|
||||
install_scripts=$base/lib/demo_python_service
|
||||
30
chapt4/chapt4_ws/src/demo_python_service/setup.py
Normal file
30
chapt4/chapt4_ws/src/demo_python_service/setup.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from setuptools import find_packages, setup
|
||||
from glob import glob
|
||||
package_name = 'demo_python_service'
|
||||
|
||||
setup(
|
||||
name=package_name,
|
||||
version='0.0.0',
|
||||
packages=find_packages(exclude=['test']),
|
||||
data_files=[
|
||||
('share/ament_index/resource_index/packages',
|
||||
['resource/' + package_name]),
|
||||
('share/' + package_name, ['package.xml']),
|
||||
('share/' + package_name+"/resource", ['resource/default.jpg','resource/test1.jpg']),
|
||||
('share/' + package_name+'/launch', glob('launch/*.launch.py')),
|
||||
],
|
||||
install_requires=['setuptools'],
|
||||
zip_safe=True,
|
||||
maintainer='mzebra',
|
||||
maintainer_email='mzebra@foxmail.com',
|
||||
description='TODO: Package description',
|
||||
license='Apache-2.0',
|
||||
tests_require=['pytest'],
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'learn_face_detect=demo_python_service.learn_face_detect:main',
|
||||
'face_detect_node=demo_python_service.face_detect_node:main',
|
||||
'face_detect_client_node=demo_python_service.face_detect_client_node:main',
|
||||
],
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
# Copyright 2015 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from ament_copyright.main import main
|
||||
import pytest
|
||||
|
||||
|
||||
# Remove the `skip` decorator once the source file(s) have a copyright header
|
||||
@pytest.mark.skip(reason='No copyright header has been placed in the generated source file.')
|
||||
@pytest.mark.copyright
|
||||
@pytest.mark.linter
|
||||
def test_copyright():
|
||||
rc = main(argv=['.', 'test'])
|
||||
assert rc == 0, 'Found errors'
|
||||
25
chapt4/chapt4_ws/src/demo_python_service/test/test_flake8.py
Normal file
25
chapt4/chapt4_ws/src/demo_python_service/test/test_flake8.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# Copyright 2017 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from ament_flake8.main import main_with_errors
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.flake8
|
||||
@pytest.mark.linter
|
||||
def test_flake8():
|
||||
rc, errors = main_with_errors(argv=[])
|
||||
assert rc == 0, \
|
||||
'Found %d code style errors / warnings:\n' % len(errors) + \
|
||||
'\n'.join(errors)
|
||||
23
chapt4/chapt4_ws/src/demo_python_service/test/test_pep257.py
Normal file
23
chapt4/chapt4_ws/src/demo_python_service/test/test_pep257.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# Copyright 2015 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from ament_pep257.main import main
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.linter
|
||||
@pytest.mark.pep257
|
||||
def test_pep257():
|
||||
rc = main(argv=['.', 'test'])
|
||||
assert rc == 0, 'Found code style errors / warnings'
|
||||
Reference in New Issue
Block a user