-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlivox_ros_driver.cpp
177 lines (151 loc) · 5.8 KB
/
livox_ros_driver.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// The MIT License (MIT)
//
// Copyright (c) 2019 Livox. All rights reserved.
//
// 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.
//
#include "include/livox_ros_driver.h"
#include <chrono>
#include <vector>
#include <csignal>
#include <ros/ros.h>
#include "lddc.h"
#include "lds_hub.h"
#include "lds_lidar.h"
#include "lds_lvx.h"
#include "livox_sdk.h"
using namespace livox_ros;
const int32_t kSdkVersionMajorLimit = 2;
inline void SignalHandler(int signum) {
printf("livox ros driver will exit\r\n");
ros::shutdown();
exit(signum);
}
int main(int argc, char **argv) {
/** Ros related */
if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME,
ros::console::levels::Debug)) {
ros::console::notifyLoggerLevelsChanged();
}
ros::init(argc, argv, "livox_lidar_publisher");
ros::NodeHandle livox_node;
ROS_INFO("Livox Ros Driver Version: %s", LIVOX_ROS_DRIVER_VERSION_STRING);
signal(SIGINT, SignalHandler);
/** Check sdk version */
LivoxSdkVersion _sdkversion;
GetLivoxSdkVersion(&_sdkversion);
if (_sdkversion.major < kSdkVersionMajorLimit) {
ROS_INFO("The SDK version[%d.%d.%d] is too low", _sdkversion.major,
_sdkversion.minor, _sdkversion.patch);
return 0;
}
/** Init default system parameter */
int xfer_format = kPointCloud2Msg;
int multi_topic = 0;
int data_src = kSourceRawLidar;
double publish_freq = 10.0; /* Hz */
int output_type = kOutputToRos;
std::string frame_id = "livox_frame";
bool lidar_bag = true;
bool imu_bag = false;
livox_node.getParam("xfer_format", xfer_format);
livox_node.getParam("multi_topic", multi_topic);
livox_node.getParam("data_src", data_src);
livox_node.getParam("publish_freq", publish_freq);
livox_node.getParam("output_data_type", output_type);
livox_node.getParam("frame_id", frame_id);
livox_node.getParam("enable_lidar_bag", lidar_bag);
livox_node.getParam("enable_imu_bag", imu_bag);
if (publish_freq > 100.0) {
publish_freq = 100.0;
} else if (publish_freq < 0.1) {
publish_freq = 0.1;
} else {
publish_freq = publish_freq;
}
/** Lidar data distribute control and lidar data source set */
Lddc *lddc = new Lddc(xfer_format, multi_topic, data_src, output_type,
publish_freq, frame_id, lidar_bag, imu_bag);
lddc->SetRosNode(&livox_node);
int ret = 0;
if (data_src == kSourceRawLidar) {
ROS_INFO("Data Source is raw lidar.");
std::string user_config_path;
livox_node.getParam("user_config_path", user_config_path);
ROS_INFO("Config file : %s", user_config_path.c_str());
std::string cmdline_bd_code;
livox_node.getParam("cmdline_str", cmdline_bd_code);
std::vector<std::string> bd_code_list;
ParseCommandlineInputBdCode(cmdline_bd_code.c_str(), bd_code_list);
LdsLidar *read_lidar = LdsLidar::GetInstance(1000 / publish_freq);
lddc->RegisterLds(static_cast<Lds *>(read_lidar));
ret = read_lidar->InitLdsLidar(bd_code_list, user_config_path.c_str());
if (!ret) {
ROS_INFO("Init lds lidar success!");
} else {
ROS_ERROR("Init lds lidar fail!");
}
} else if (data_src == kSourceRawHub) {
ROS_INFO("Data Source is hub.");
std::string user_config_path;
livox_node.getParam("user_config_path", user_config_path);
ROS_INFO("Config file : %s", user_config_path.c_str());
std::string cmdline_bd_code;
livox_node.getParam("cmdline_str", cmdline_bd_code);
std::vector<std::string> bd_code_list;
ParseCommandlineInputBdCode(cmdline_bd_code.c_str(), bd_code_list);
LdsHub *read_hub = LdsHub::GetInstance(1000 / publish_freq);
lddc->RegisterLds(static_cast<Lds *>(read_hub));
ret = read_hub->InitLdsHub(bd_code_list, user_config_path.c_str());
if (!ret) {
ROS_INFO("Init lds hub success!");
} else {
ROS_ERROR("Init lds hub fail!");
}
} else {
ROS_INFO("Data Source is lvx file.");
std::string cmdline_file_path;
livox_node.getParam("cmdline_file_path", cmdline_file_path);
do {
if (!IsFilePathValid(cmdline_file_path.c_str())) {
ROS_ERROR("File path invalid : %s !", cmdline_file_path.c_str());
break;
}
std::string rosbag_file_path;
int path_end_pos = cmdline_file_path.find_last_of('.');
rosbag_file_path = cmdline_file_path.substr(0, path_end_pos);
rosbag_file_path += ".bag";
LdsLvx *read_lvx = LdsLvx::GetInstance(1000 / publish_freq);
lddc->RegisterLds(static_cast<Lds *>(read_lvx));
lddc->CreateBagFile(rosbag_file_path);
int ret = read_lvx->InitLdsLvx(cmdline_file_path.c_str());
if (!ret) {
ROS_INFO("Init lds lvx file success!");
} else {
ROS_ERROR("Init lds lvx file fail!");
}
} while (0);
}
ros::Time::init();
while (ros::ok()) {
lddc->DistributeLidarData();
}
return 0;
}