Skip to content

Commit 02ed221

Browse files
committed
[feat]:Add ROS2 component
1 parent b897430 commit 02ed221

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

CMakeLists.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ if(rclcpp_FOUND)
142142
find_package(sensor_msgs REQUIRED)
143143
find_package(rslidar_msg REQUIRED)
144144
find_package(std_msgs REQUIRED)
145+
find_package(rclcpp_components REQUIRED)
145146

146147
else(rclcpp_FOUND)
147148
message(=============================================================)
@@ -187,6 +188,20 @@ endif(roscpp_FOUND)
187188

188189
#Ros2#
189190
if(rclcpp_FOUND)
191+
add_library(rslidar_sdk_component SHARED
192+
node/rslidar_sdk_node_component.cpp
193+
src/manager/node_manager.cpp)
194+
195+
target_link_libraries(rslidar_sdk_component
196+
${YAML_CPP_LIBRARIES}
197+
${rs_driver_LIBRARIES})
198+
# Component dependencies
199+
ament_target_dependencies(rslidar_sdk_component
200+
rclcpp
201+
rclcpp_components
202+
std_msgs
203+
sensor_msgs
204+
rslidar_msg)
190205

191206
ament_target_dependencies(rslidar_sdk_node
192207
rclcpp
@@ -196,7 +211,12 @@ if(rclcpp_FOUND)
196211

197212
install(TARGETS
198213
rslidar_sdk_node
199-
DESTINATION lib/${PROJECT_NAME})
214+
rslidar_sdk_component
215+
RUNTIME DESTINATION lib/${PROJECT_NAME}
216+
LIBRARY DESTINATION lib)
217+
218+
# Register component with ament index
219+
rclcpp_components_register_nodes(rslidar_sdk_component "robosense::lidar::RSLidarSDKComponent")
200220

201221
install(DIRECTORY
202222
launch
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*********************************************************************************************************************
2+
Copyright (c) 2020 RoboSense
3+
All rights reserved
4+
5+
By downloading, copying, installing or using the software you agree to this license. If you do not agree to this
6+
license, do not download, install, copy or use the software.
7+
8+
License Agreement
9+
For RoboSense LiDAR SDK Library
10+
(3-clause BSD License)
11+
12+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
13+
following conditions are met:
14+
15+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
16+
disclaimer.
17+
18+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
19+
disclaimer in the documentation and/or other materials provided with the distribution.
20+
21+
3. Neither the names of the RoboSense, nor Suteng Innovation Technology, nor the names of other contributors may be used
22+
to endorse or promote products derived from this software without specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*********************************************************************************************************************/
32+
33+
#include "manager/node_manager.hpp"
34+
#include <rs_driver/macro/version.hpp>
35+
#include <rclcpp/rclcpp.hpp>
36+
#include <rclcpp_components/register_node_macro.hpp>
37+
#include <memory>
38+
#include <thread>
39+
#include <csignal>
40+
41+
namespace robosense
42+
{
43+
namespace lidar
44+
{
45+
46+
class RSLidarSDKComponent : public rclcpp::Node
47+
{
48+
public:
49+
explicit RSLidarSDKComponent(const rclcpp::NodeOptions & options)
50+
: Node("rslidar_sdk_node_component", options)
51+
{
52+
// Print version information
53+
RCLCPP_INFO(this->get_logger(), "********************************************************");
54+
RCLCPP_INFO(this->get_logger(), "********** **********");
55+
RCLCPP_INFO(this->get_logger(), "********** RSLidar_SDK Version: v%d.%d.%d **********",
56+
RSLIDAR_VERSION_MAJOR, RSLIDAR_VERSION_MINOR, RSLIDAR_VERSION_PATCH);
57+
RCLCPP_INFO(this->get_logger(), "********** **********");
58+
RCLCPP_INFO(this->get_logger(), "********************************************************");
59+
60+
// Set up config path
61+
std::string config_path;
62+
63+
#ifdef RUN_IN_ROS_WORKSPACE
64+
config_path = ament_index_cpp::get_package_share_directory("rslidar_sdk");
65+
#else
66+
config_path = (std::string)PROJECT_PATH;
67+
#endif
68+
69+
config_path += "/config/config.yaml";
70+
71+
std::string path = this->declare_parameter<std::string>("config_path", "");
72+
if (!path.empty())
73+
{
74+
config_path = path;
75+
}
76+
77+
YAML::Node config;
78+
try
79+
{
80+
config = YAML::LoadFile(config_path);
81+
RCLCPP_INFO(this->get_logger(), "--------------------------------------------------------");
82+
RCLCPP_INFO(this->get_logger(), "Config loaded from PATH:");
83+
RCLCPP_INFO(this->get_logger(), "%s", config_path.c_str());
84+
RCLCPP_INFO(this->get_logger(), "--------------------------------------------------------");
85+
}
86+
catch (...)
87+
{
88+
RCLCPP_ERROR(this->get_logger(), "The format of config file %s is wrong. Please check (e.g. indentation).",
89+
config_path.c_str());
90+
throw std::runtime_error("Failed to load config file");
91+
}
92+
93+
node_manager_ = std::make_shared<NodeManager>();
94+
node_manager_->init(config);
95+
node_manager_->start();
96+
}
97+
98+
private:
99+
std::shared_ptr<NodeManager> node_manager_;
100+
};
101+
102+
} // namespace lidar
103+
} // namespace robosense
104+
105+
// Register the component with class_loader
106+
RCLCPP_COMPONENTS_REGISTER_NODE(robosense::lidar::RSLidarSDKComponent)

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<depend condition="$ROS_VERSION == 1">roscpp</depend>
1616
<depend condition="$ROS_VERSION == 1">roslib</depend>
1717
<depend condition="$ROS_VERSION == 2">rslidar_msg</depend>
18+
<depend condition="$ROS_VERSION == 2">rclcpp_components</depend>
1819
<depend>sensor_msgs</depend>
1920
<depend>std_msgs</depend>
2021
<depend>yaml-cpp</depend>

0 commit comments

Comments
 (0)