-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathos_driver_nodelet.cpp
More file actions
267 lines (229 loc) · 10.1 KB
/
os_driver_nodelet.cpp
File metadata and controls
267 lines (229 loc) · 10.1 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Copyright (c) 2018-2023, Ouster, Inc.
* All rights reserved.
*
* @file os_driver_nodelet.cpp
* @brief This node combines the capabilities of os_sensor, os_cloud and os_img
* into a single ROS nodelet
*/
// prevent clang-format from altering the location of "ouster_ros/os_ros.h", the
// header file needs to be the first include due to PCL_NO_PRECOMPILE flag
// clang-format off
#include "ouster_ros/os_ros.h"
// clang-format on
#include <pluginlib/class_list_macros.h>
#include <sensor_msgs/Imu.h>
#include <sensor_msgs/PointCloud2.h>
#include "os_sensor_nodelet.h"
#include "os_transforms_broadcaster.h"
#include "imu_packet_handler.h"
#include "lidar_packet_handler.h"
#include "point_cloud_processor.h"
#include "laser_scan_processor.h"
#include "image_processor.h"
#include "point_cloud_processor_factory.h"
#include "telemetry_handler.h"
namespace sensor = ouster::sensor;
using ouster::sensor::ImuPacket;
using ouster::sensor::LidarPacket;
namespace ouster_ros {
class OusterDriver : public OusterSensor {
public:
OusterDriver() : tf_bcast(getName()) {}
~OusterDriver() override {
NODELET_DEBUG("OusterDriver::~OusterDriver() called");
}
protected:
virtual void onInit() override {
auto& pnh = getPrivateNodeHandle();
auto proc_mask =
pnh.param("proc_mask", std::string{"IMU|PCL|SCAN|IMG|RAW"});
auto tokens = impl::parse_tokens(proc_mask, '|');
if (impl::check_token(tokens, "IMU")) create_imu_pub();
if (impl::check_token(tokens, "PCL")) create_point_cloud_pubs();
if (impl::check_token(tokens, "SCAN")) create_laser_scan_pubs();
if (impl::check_token(tokens, "IMG")) create_image_pubs();
if (impl::check_token(tokens, "TLM")) create_telemetry_pub();
publish_raw = impl::check_token(tokens, "RAW");
OusterSensor::onInit();
}
private:
void on_metadata_updated(const sensor::sensor_info& info) override {
// for OusterDriver we are going to always assume static broadcast
// at least for now
tf_bcast.parse_parameters(getPrivateNodeHandle());
if (tf_bcast.publish_static_tf()) {
tf_bcast.broadcast_transforms(info);
}
create_handlers();
}
void create_imu_pub() {
imu_pub = getNodeHandle().advertise<sensor_msgs::Imu>("imu", 100);
}
void create_telemetry_pub() {
telemetry_pub =
getNodeHandle().advertise<ouster_ros::Telemetry>("telemetry", 1280);
}
void create_point_cloud_pubs() {
// NOTE: always create the 2nd topic
lidar_pubs.resize(2);
for (int i = 0; i < 2; ++i) {
lidar_pubs[i] = getNodeHandle().advertise<sensor_msgs::PointCloud2>(
topic_for_return("points", i), 10);
}
}
void create_laser_scan_pubs() {
// NOTE: always create the 2nd topic
scan_pubs.resize(2);
for (int i = 0; i < 2; ++i) {
scan_pubs[i] = getNodeHandle().advertise<sensor_msgs::LaserScan>(
topic_for_return("scan", i), 10);
}
}
void create_image_pubs() {
// NOTE: always create the 2nd topics
const std::map<sensor::ChanField, std::string> channel_field_topic_map{
{sensor::ChanField::RANGE, "range_image"},
{sensor::ChanField::SIGNAL, "signal_image"},
{sensor::ChanField::REFLECTIVITY, "reflec_image"},
{sensor::ChanField::NEAR_IR, "nearir_image"},
{sensor::ChanField::RANGE2, "range_image2"},
{sensor::ChanField::SIGNAL2, "signal_image2"},
{sensor::ChanField::REFLECTIVITY2, "reflec_image2"}};
for (auto it : channel_field_topic_map) {
image_pubs[it.first] =
getNodeHandle().advertise<sensor_msgs::Image>(it.second, 100);
}
}
virtual void create_handlers() {
auto& pnh = getPrivateNodeHandle();
auto proc_mask =
pnh.param("proc_mask", std::string{"IMU|IMG|PCL|SCAN"});
auto tokens = impl::parse_tokens(proc_mask, '|');
auto timestamp_mode = pnh.param("timestamp_mode", std::string{});
double ptp_utc_tai_offset = pnh.param("ptp_utc_tai_offset", -37.0);
if (impl::check_token(tokens, "IMU")) {
imu_packet_handler = ImuPacketHandler::create(
info, tf_bcast.imu_frame_id(), timestamp_mode,
static_cast<int64_t>(ptp_utc_tai_offset * 1e+9));
}
auto min_scan_valid_columns_ratio = pnh.param("min_scan_valid_columns_ratio", 0.0);
if (min_scan_valid_columns_ratio < 0.0 || min_scan_valid_columns_ratio > 1.0) {
NODELET_FATAL("min_scan_valid_columns_ratio needs to be in the range [0, 1]");
throw std::runtime_error("min_scan_valid_columns_ratio out of bounds!");
}
std::vector<LidarScanProcessor> processors;
if (impl::check_token(tokens, "PCL")) {
auto point_type = pnh.param("point_type", std::string{"original"});
auto organized = pnh.param("organized", true);
auto destagger = pnh.param("destagger", true);
auto min_range_m = pnh.param("min_range", 0.0);
auto max_range_m = pnh.param("max_range", 10000.0);
if (min_range_m < 0.0 || max_range_m < 0.0) {
NODELET_FATAL("min_range and max_range need to be positive");
throw std::runtime_error("negative range limits!");
}
if (min_range_m >= max_range_m) {
const auto error_msg =
"min_range can't be equal or exceed max_range";
NODELET_FATAL(error_msg);
throw std::runtime_error(error_msg);
}
// convert to millimeters
uint32_t min_range = impl::ulround(min_range_m * 1000);
uint32_t max_range = impl::ulround(max_range_m * 1000);
auto rows_step = pnh.param("rows_step", 1);
processors.push_back(
PointCloudProcessorFactory::create_point_cloud_processor(
point_type, info, tf_bcast.point_cloud_frame_id(),
tf_bcast.apply_lidar_to_sensor_transform(), organized,
destagger, min_range, max_range, rows_step,
[this](PointCloudProcessor_OutputType msgs) {
for (size_t i = 0; i < msgs.size(); ++i)
lidar_pubs[i].publish(*msgs[i]);
}));
// warn about profile incompatibility
if (PointCloudProcessorFactory::point_type_requires_intensity(
point_type) &&
!PointCloudProcessorFactory::profile_has_intensity(
info.format.udp_profile_lidar)) {
NODELET_WARN_STREAM(
"selected point type '" << point_type
<< "' is not compatible with the udp profile: "
<< to_string(info.format.udp_profile_lidar));
}
}
if (impl::check_token(tokens, "SCAN")) {
// TODO: avoid duplication in os_cloud_node
int beams_count = static_cast<int>(get_beams_count(info));
int scan_ring = pnh.param("scan_ring", 0);
scan_ring = std::min(std::max(scan_ring, 0), beams_count - 1);
if (scan_ring != pnh.param("scan_ring", 0)) {
NODELET_WARN_STREAM(
"scan ring is set to a value that exceeds available range "
"please choose a value between "
<< " [0, " << beams_count << "],"
<< " ring value clamped to: " << scan_ring);
}
processors.push_back(LaserScanProcessor::create(
info, tf_bcast.lidar_frame_id(), scan_ring,
[this](LaserScanProcessor::OutputType msgs) {
for (size_t i = 0; i < msgs.size(); ++i) {
scan_pubs[i].publish(*msgs[i]);
}
}));
}
if (impl::check_token(tokens, "IMG")) {
processors.push_back(ImageProcessor::create(
info, tf_bcast.point_cloud_frame_id(),
[this](ImageProcessor::OutputType msgs) {
for (auto it = msgs.begin(); it != msgs.end(); ++it) {
image_pubs[it->first].publish(*it->second);
}
}));
}
if (impl::check_token(tokens, "PCL") ||
impl::check_token(tokens, "SCAN") ||
impl::check_token(tokens, "IMG")) {
lidar_packet_handler = LidarPacketHandler::create(
info, processors, timestamp_mode,
static_cast<int64_t>(ptp_utc_tai_offset * 1e+9),
min_scan_valid_columns_ratio);
}
if (impl::check_token(tokens, "TLM")) {
telemetry_handler = TelemetryHandler::create(
info, timestamp_mode,
static_cast<int64_t>(ptp_utc_tai_offset * 1e+9));
}
}
virtual void on_lidar_packet_msg(const LidarPacket& lidar_packet) override {
if (telemetry_handler) {
auto telemetry = telemetry_handler(lidar_packet);
telemetry_pub.publish(telemetry);
}
if (lidar_packet_handler) {
lidar_packet_handler(lidar_packet);
}
if (publish_raw) OusterSensor::on_lidar_packet_msg(lidar_packet);
}
virtual void on_imu_packet_msg(const ImuPacket& imu_packet) override {
if (imu_packet_handler) {
auto imu_msg = imu_packet_handler(imu_packet);
imu_pub.publish(imu_msg);
}
if (publish_raw) OusterSensor::on_imu_packet_msg(imu_packet);
}
private:
ros::Publisher imu_pub;
std::vector<ros::Publisher> lidar_pubs;
std::vector<ros::Publisher> scan_pubs;
std::map<sensor::ChanField, ros::Publisher> image_pubs;
OusterTransformsBroadcaster tf_bcast;
ImuPacketHandler::HandlerType imu_packet_handler;
LidarPacketHandler::HandlerType lidar_packet_handler;
bool publish_raw = false;
ros::Publisher telemetry_pub;
TelemetryHandler::HandlerType telemetry_handler;
};
} // namespace ouster_ros
PLUGINLIB_EXPORT_CLASS(ouster_ros::OusterDriver, nodelet::Nodelet)