From e94f5f21fb280e7ef00bf628f1f0627d5264236f Mon Sep 17 00:00:00 2001 From: hiikariri Date: Mon, 17 Jun 2024 04:43:32 +0700 Subject: [PATCH 1/2] feat: adapt main to remove ros arguments, add ros2 launch --- CMakeLists.txt | 11 +++++++++- launch/shisen_cpp_launch.py | 40 +++++++++++++++++++++++++++++++++++++ src/shisen_cpp_main.cpp | 14 ++++++------- 3 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 launch/shisen_cpp_launch.py diff --git a/CMakeLists.txt b/CMakeLists.txt index fa76690..d669f7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,13 @@ if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 14) endif() +# Set the build type to Debug if not specified +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Debug) +endif() + if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(-Wall -Wextra -Wpedantic -fPIC) + add_compile_options(-Wall -Wextra -Wpedantic -fPIC -g3) endif() find_package(ament_cmake REQUIRED) @@ -93,6 +98,10 @@ target_link_libraries(${PROJECT_NAME} install(DIRECTORY "include" DESTINATION ".") +install(DIRECTORY + launch + DESTINATION "share/${PROJECT_NAME}") + install(TARGETS ${PROJECT_NAME} EXPORT export_${PROJECT_NAME} ARCHIVE DESTINATION "lib" diff --git a/launch/shisen_cpp_launch.py b/launch/shisen_cpp_launch.py new file mode 100644 index 0000000..561b6c8 --- /dev/null +++ b/launch/shisen_cpp_launch.py @@ -0,0 +1,40 @@ +# Copyright (c) 2024 ICHIRO ITS +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import os +import socket +from launch import LaunchDescription +from launch_ros.actions import Node + +def generate_launch_description(): + hostname = socket.gethostname() + config_path = os.path.expanduser(f'~/ros2-ws/configuration/{hostname}/camera/') + + return LaunchDescription([ + Node( + package='shisen_cpp', + executable='camera', + name='camera', + output='screen', + arguments=[config_path], + respawn=True, + respawn_delay=1 + ) + ]) diff --git a/src/shisen_cpp_main.cpp b/src/shisen_cpp_main.cpp index 9a978e1..8164772 100644 --- a/src/shisen_cpp_main.cpp +++ b/src/shisen_cpp_main.cpp @@ -26,7 +26,7 @@ int main(int argc, char ** argv) { - rclcpp::init(argc, argv); + auto args = rclcpp::init_and_remove_ros_arguments(argc, argv); shisen_cpp::Options options; options.field_of_view = 78; @@ -50,20 +50,20 @@ int main(int argc, char ** argv) try { int i = 1; int pos = 0; - while (i < argc) { - std::string arg = argv[i++]; + while (i < args.size()) { + std::string arg = args[i++]; if (arg[0] == '-') { if (arg == "-h" || arg == "--help") { std::cout << help_message << std::endl; return 1; } else if (arg == "--camera-prefix") { - options.camera_prefix = argv[i++]; + options.camera_prefix = args[i++]; } else if (arg == "--compression") { - options.compression_quality = atoi(argv[i++]); + options.compression_quality = stoi(args[i++]); } else if (arg == "--capture-fps") { - options.capture_fps = atoi(argv[i++]); + options.capture_fps = stoi(args[i++]); } else if (arg == "--field-of-view") { - options.field_of_view = atoi(argv[i++]); + options.field_of_view = stoi(args[i++]); } else { std::cout << "Unknown option `" << arg << "`!\n\n" << help_message << std::endl; return 1; From 9cc5398a30afb0caa5e485b9580eb182f8d75d11 Mon Sep 17 00:00:00 2001 From: hiikariri Date: Wed, 19 Jun 2024 18:55:02 +0700 Subject: [PATCH 2/2] fix: remove debug argument in Cmakelists, use const reference to prevent copy var --- CMakeLists.txt | 7 +------ src/shisen_cpp_main.cpp | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d669f7a..cad45cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,13 +10,8 @@ if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 14) endif() -# Set the build type to Debug if not specified -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Debug) -endif() - if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(-Wall -Wextra -Wpedantic -fPIC -g3) + add_compile_options(-Wall -Wextra -Wpedantic -fPIC) endif() find_package(ament_cmake REQUIRED) diff --git a/src/shisen_cpp_main.cpp b/src/shisen_cpp_main.cpp index 8164772..359bc58 100644 --- a/src/shisen_cpp_main.cpp +++ b/src/shisen_cpp_main.cpp @@ -51,7 +51,7 @@ int main(int argc, char ** argv) int i = 1; int pos = 0; while (i < args.size()) { - std::string arg = args[i++]; + const std::string& arg = args[i++]; if (arg[0] == '-') { if (arg == "-h" || arg == "--help") { std::cout << help_message << std::endl;