-
Notifications
You must be signed in to change notification settings - Fork 152
/
lddc.cpp
703 lines (630 loc) · 22.2 KB
/
lddc.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
//
// The MIT License (MIT)
//
// Copyright (c) 2022 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 "lddc.h"
#include "comm/ldq.h"
#include "comm/comm.h"
#include <inttypes.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdint.h>
#include "include/ros_headers.h"
#include "driver_node.h"
#include "lds_lidar.h"
namespace livox_ros {
/** Lidar Data Distribute Control--------------------------------------------*/
#ifdef BUILDING_ROS1
Lddc::Lddc(int format, int multi_topic, int data_src, int output_type,
double frq, std::string &frame_id, bool lidar_bag, bool imu_bag)
: transfer_format_(format),
use_multi_topic_(multi_topic),
data_src_(data_src),
output_type_(output_type),
publish_frq_(frq),
frame_id_(frame_id),
enable_lidar_bag_(lidar_bag),
enable_imu_bag_(imu_bag) {
publish_period_ns_ = kNsPerSecond / publish_frq_;
lds_ = nullptr;
memset(private_pub_, 0, sizeof(private_pub_));
memset(private_imu_pub_, 0, sizeof(private_imu_pub_));
global_pub_ = nullptr;
global_imu_pub_ = nullptr;
cur_node_ = nullptr;
bag_ = nullptr;
}
#elif defined BUILDING_ROS2
Lddc::Lddc(int format, int multi_topic, int data_src, int output_type,
double frq, std::string &frame_id)
: transfer_format_(format),
use_multi_topic_(multi_topic),
data_src_(data_src),
output_type_(output_type),
publish_frq_(frq),
frame_id_(frame_id) {
publish_period_ns_ = kNsPerSecond / publish_frq_;
lds_ = nullptr;
#if 0
bag_ = nullptr;
#endif
}
#endif
Lddc::~Lddc() {
#ifdef BUILDING_ROS1
if (global_pub_) {
delete global_pub_;
}
if (global_imu_pub_) {
delete global_imu_pub_;
}
#endif
PrepareExit();
#ifdef BUILDING_ROS1
for (uint32_t i = 0; i < kMaxSourceLidar; i++) {
if (private_pub_[i]) {
delete private_pub_[i];
}
}
for (uint32_t i = 0; i < kMaxSourceLidar; i++) {
if (private_imu_pub_[i]) {
delete private_imu_pub_[i];
}
}
#endif
std::cout << "lddc destory!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
}
int Lddc::RegisterLds(Lds *lds) {
if (lds_ == nullptr) {
lds_ = lds;
return 0;
} else {
return -1;
}
}
void Lddc::DistributePointCloudData(void) {
if (!lds_) {
std::cout << "lds is not registered" << std::endl;
return;
}
if (lds_->IsRequestExit()) {
std::cout << "DistributePointCloudData is RequestExit" << std::endl;
return;
}
lds_->pcd_semaphore_.Wait();
for (uint32_t i = 0; i < lds_->lidar_count_; i++) {
uint32_t lidar_id = i;
LidarDevice *lidar = &lds_->lidars_[lidar_id];
LidarDataQueue *p_queue = &lidar->data;
if ((kConnectStateSampling != lidar->connect_state) || (p_queue == nullptr)) {
continue;
}
PollingLidarPointCloudData(lidar_id, lidar);
}
}
void Lddc::DistributeImuData(void) {
if (!lds_) {
std::cout << "lds is not registered" << std::endl;
return;
}
if (lds_->IsRequestExit()) {
std::cout << "DistributeImuData is RequestExit" << std::endl;
return;
}
lds_->imu_semaphore_.Wait();
for (uint32_t i = 0; i < lds_->lidar_count_; i++) {
uint32_t lidar_id = i;
LidarDevice *lidar = &lds_->lidars_[lidar_id];
LidarImuDataQueue *p_queue = &lidar->imu_data;
if ((kConnectStateSampling != lidar->connect_state) || (p_queue == nullptr)) {
continue;
}
PollingLidarImuData(lidar_id, lidar);
}
}
void Lddc::PollingLidarPointCloudData(uint8_t index, LidarDevice *lidar) {
LidarDataQueue *p_queue = &lidar->data;
if (p_queue == nullptr || p_queue->storage_packet == nullptr) {
return;
}
while (!lds_->IsRequestExit() && !QueueIsEmpty(p_queue)) {
if (kPointCloud2Msg == transfer_format_) {
PublishPointcloud2(p_queue, index);
} else if (kLivoxCustomMsg == transfer_format_) {
PublishCustomPointcloud(p_queue, index);
} else if (kPclPxyziMsg == transfer_format_) {
PublishPclMsg(p_queue, index);
}
}
}
void Lddc::PollingLidarImuData(uint8_t index, LidarDevice *lidar) {
LidarImuDataQueue& p_queue = lidar->imu_data;
while (!lds_->IsRequestExit() && !p_queue.Empty()) {
PublishImuData(p_queue, index);
}
}
void Lddc::PrepareExit(void) {
#ifdef BUILDING_ROS1
if (bag_) {
DRIVER_INFO(*cur_node_, "Waiting to save the bag file!");
bag_->close();
DRIVER_INFO(*cur_node_, "Save the bag file successfully!");
bag_ = nullptr;
}
#endif
if (lds_) {
lds_->PrepareExit();
lds_ = nullptr;
}
}
void Lddc::PublishPointcloud2(LidarDataQueue *queue, uint8_t index) {
while(!QueueIsEmpty(queue)) {
StoragePacket pkg;
QueuePop(queue, &pkg);
if (pkg.points.empty()) {
printf("Publish point cloud2 failed, the pkg points is empty.\n");
continue;
}
PointCloud2 cloud;
uint64_t timestamp = 0;
InitPointcloud2Msg(pkg, cloud, timestamp);
PublishPointcloud2Data(index, timestamp, cloud);
}
}
void Lddc::PublishCustomPointcloud(LidarDataQueue *queue, uint8_t index) {
while(!QueueIsEmpty(queue)) {
StoragePacket pkg;
QueuePop(queue, &pkg);
if (pkg.points.empty()) {
printf("Publish custom point cloud failed, the pkg points is empty.\n");
continue;
}
CustomMsg livox_msg;
InitCustomMsg(livox_msg, pkg, index);
FillPointsToCustomMsg(livox_msg, pkg);
PublishCustomPointData(livox_msg, index);
}
}
/* for pcl::pxyzi */
void Lddc::PublishPclMsg(LidarDataQueue *queue, uint8_t index) {
#ifdef BUILDING_ROS2
static bool first_log = true;
if (first_log) {
std::cout << "error: message type 'pcl::PointCloud' is NOT supported in ROS2, "
<< "please modify the 'xfer_format' field in the launch file"
<< std::endl;
}
first_log = false;
return;
#endif
while(!QueueIsEmpty(queue)) {
StoragePacket pkg;
QueuePop(queue, &pkg);
if (pkg.points.empty()) {
printf("Publish point cloud failed, the pkg points is empty.\n");
continue;
}
PointCloud cloud;
uint64_t timestamp = 0;
InitPclMsg(pkg, cloud, timestamp);
FillPointsToPclMsg(pkg, cloud);
PublishPclData(index, timestamp, cloud);
}
return;
}
void Lddc::InitPointcloud2MsgHeader(PointCloud2& cloud) {
cloud.header.frame_id.assign(frame_id_);
cloud.height = 1;
cloud.width = 0;
cloud.fields.resize(7);
cloud.fields[0].offset = 0;
cloud.fields[0].name = "x";
cloud.fields[0].count = 1;
cloud.fields[0].datatype = PointField::FLOAT32;
cloud.fields[1].offset = 4;
cloud.fields[1].name = "y";
cloud.fields[1].count = 1;
cloud.fields[1].datatype = PointField::FLOAT32;
cloud.fields[2].offset = 8;
cloud.fields[2].name = "z";
cloud.fields[2].count = 1;
cloud.fields[2].datatype = PointField::FLOAT32;
cloud.fields[3].offset = 12;
cloud.fields[3].name = "intensity";
cloud.fields[3].count = 1;
cloud.fields[3].datatype = PointField::FLOAT32;
cloud.fields[4].offset = 16;
cloud.fields[4].name = "tag";
cloud.fields[4].count = 1;
cloud.fields[4].datatype = PointField::UINT8;
cloud.fields[5].offset = 17;
cloud.fields[5].name = "line";
cloud.fields[5].count = 1;
cloud.fields[5].datatype = PointField::UINT8;
cloud.fields[6].offset = 18;
cloud.fields[6].name = "timestamp";
cloud.fields[6].count = 1;
cloud.fields[6].datatype = PointField::FLOAT64;
cloud.point_step = sizeof(LivoxPointXyzrtlt);
}
void Lddc::InitPointcloud2Msg(const StoragePacket& pkg, PointCloud2& cloud, uint64_t& timestamp) {
InitPointcloud2MsgHeader(cloud);
cloud.point_step = sizeof(LivoxPointXyzrtlt);
cloud.width = pkg.points_num;
cloud.row_step = cloud.width * cloud.point_step;
cloud.is_bigendian = false;
cloud.is_dense = true;
if (!pkg.points.empty()) {
timestamp = pkg.base_time;
}
#ifdef BUILDING_ROS1
cloud.header.stamp = ros::Time( timestamp / 1000000000.0);
#elif defined BUILDING_ROS2
cloud.header.stamp = rclcpp::Time(timestamp);
#endif
std::vector<LivoxPointXyzrtlt> points;
for (size_t i = 0; i < pkg.points_num; ++i) {
LivoxPointXyzrtlt point;
point.x = pkg.points[i].x;
point.y = pkg.points[i].y;
point.z = pkg.points[i].z;
point.reflectivity = pkg.points[i].intensity;
point.tag = pkg.points[i].tag;
point.line = pkg.points[i].line;
point.timestamp = static_cast<double>(pkg.points[i].offset_time);
points.push_back(std::move(point));
}
cloud.data.resize(pkg.points_num * sizeof(LivoxPointXyzrtlt));
memcpy(cloud.data.data(), points.data(), pkg.points_num * sizeof(LivoxPointXyzrtlt));
}
void Lddc::PublishPointcloud2Data(const uint8_t index, const uint64_t timestamp, const PointCloud2& cloud) {
#ifdef BUILDING_ROS1
PublisherPtr publisher_ptr = Lddc::GetCurrentPublisher(index);
#elif defined BUILDING_ROS2
Publisher<PointCloud2>::SharedPtr publisher_ptr =
std::dynamic_pointer_cast<Publisher<PointCloud2>>(GetCurrentPublisher(index));
#endif
if (kOutputToRos == output_type_) {
publisher_ptr->publish(cloud);
} else {
#ifdef BUILDING_ROS1
if (bag_ && enable_lidar_bag_) {
bag_->write(publisher_ptr->getTopic(), ros::Time(timestamp / 1000000000.0), cloud);
}
#endif
}
}
void Lddc::InitCustomMsg(CustomMsg& livox_msg, const StoragePacket& pkg, uint8_t index) {
livox_msg.header.frame_id.assign(frame_id_);
#ifdef BUILDING_ROS1
static uint32_t msg_seq = 0;
livox_msg.header.seq = msg_seq;
++msg_seq;
#endif
uint64_t timestamp = 0;
if (!pkg.points.empty()) {
timestamp = pkg.base_time;
}
livox_msg.timebase = timestamp;
#ifdef BUILDING_ROS1
livox_msg.header.stamp = ros::Time(timestamp / 1000000000.0);
#elif defined BUILDING_ROS2
livox_msg.header.stamp = rclcpp::Time(timestamp);
#endif
livox_msg.point_num = pkg.points_num;
if (lds_->lidars_[index].lidar_type == kLivoxLidarType) {
livox_msg.lidar_id = lds_->lidars_[index].handle;
} else {
printf("Init custom msg lidar id failed, the index:%u.\n", index);
livox_msg.lidar_id = 0;
}
}
void Lddc::FillPointsToCustomMsg(CustomMsg& livox_msg, const StoragePacket& pkg) {
uint32_t points_num = pkg.points_num;
const std::vector<PointXyzlt>& points = pkg.points;
for (uint32_t i = 0; i < points_num; ++i) {
CustomPoint point;
point.x = points[i].x;
point.y = points[i].y;
point.z = points[i].z;
point.reflectivity = points[i].intensity;
point.tag = points[i].tag;
point.line = points[i].line;
point.offset_time = static_cast<uint32_t>(points[i].offset_time - pkg.base_time);
livox_msg.points.push_back(std::move(point));
}
}
void Lddc::PublishCustomPointData(const CustomMsg& livox_msg, const uint8_t index) {
#ifdef BUILDING_ROS1
PublisherPtr publisher_ptr = Lddc::GetCurrentPublisher(index);
#elif defined BUILDING_ROS2
Publisher<CustomMsg>::SharedPtr publisher_ptr = std::dynamic_pointer_cast<Publisher<CustomMsg>>(GetCurrentPublisher(index));
#endif
if (kOutputToRos == output_type_) {
publisher_ptr->publish(livox_msg);
} else {
#ifdef BUILDING_ROS1
if (bag_ && enable_lidar_bag_) {
bag_->write(publisher_ptr->getTopic(), ros::Time(livox_msg.timebase / 1000000000.0), livox_msg);
}
#endif
}
}
void Lddc::InitPclMsg(const StoragePacket& pkg, PointCloud& cloud, uint64_t& timestamp) {
#ifdef BUILDING_ROS1
cloud.header.frame_id.assign(frame_id_);
cloud.height = 1;
cloud.width = pkg.points_num;
if (!pkg.points.empty()) {
timestamp = pkg.base_time;
}
cloud.header.stamp = timestamp / 1000.0; // to pcl ros time stamp
#elif defined BUILDING_ROS2
std::cout << "warning: pcl::PointCloud is not supported in ROS2, "
<< "please check code logic"
<< std::endl;
#endif
return;
}
void Lddc::FillPointsToPclMsg(const StoragePacket& pkg, PointCloud& pcl_msg) {
#ifdef BUILDING_ROS1
if (pkg.points.empty()) {
return;
}
uint32_t points_num = pkg.points_num;
const std::vector<PointXyzlt>& points = pkg.points;
for (uint32_t i = 0; i < points_num; ++i) {
pcl::PointXYZI point;
point.x = points[i].x;
point.y = points[i].y;
point.z = points[i].z;
point.intensity = points[i].intensity;
pcl_msg.points.push_back(std::move(point));
}
#elif defined BUILDING_ROS2
std::cout << "warning: pcl::PointCloud is not supported in ROS2, "
<< "please check code logic"
<< std::endl;
#endif
return;
}
void Lddc::PublishPclData(const uint8_t index, const uint64_t timestamp, const PointCloud& cloud) {
#ifdef BUILDING_ROS1
PublisherPtr publisher_ptr = Lddc::GetCurrentPublisher(index);
if (kOutputToRos == output_type_) {
publisher_ptr->publish(cloud);
} else {
if (bag_ && enable_lidar_bag_) {
bag_->write(publisher_ptr->getTopic(), ros::Time(timestamp / 1000000000.0), cloud);
}
}
#elif defined BUILDING_ROS2
std::cout << "warning: pcl::PointCloud is not supported in ROS2, "
<< "please check code logic"
<< std::endl;
#endif
return;
}
void Lddc::InitImuMsg(const ImuData& imu_data, ImuMsg& imu_msg, uint64_t& timestamp) {
imu_msg.header.frame_id = "livox_frame";
timestamp = imu_data.time_stamp;
#ifdef BUILDING_ROS1
imu_msg.header.stamp = ros::Time(timestamp / 1000000000.0); // to ros time stamp
#elif defined BUILDING_ROS2
imu_msg.header.stamp = rclcpp::Time(timestamp); // to ros time stamp
#endif
imu_msg.angular_velocity.x = imu_data.gyro_x;
imu_msg.angular_velocity.y = imu_data.gyro_y;
imu_msg.angular_velocity.z = imu_data.gyro_z;
imu_msg.linear_acceleration.x = imu_data.acc_x;
imu_msg.linear_acceleration.y = imu_data.acc_y;
imu_msg.linear_acceleration.z = imu_data.acc_z;
}
void Lddc::PublishImuData(LidarImuDataQueue& imu_data_queue, const uint8_t index) {
ImuData imu_data;
if (!imu_data_queue.Pop(imu_data)) {
//printf("Publish imu data failed, imu data queue pop failed.\n");
return;
}
ImuMsg imu_msg;
uint64_t timestamp;
InitImuMsg(imu_data, imu_msg, timestamp);
#ifdef BUILDING_ROS1
PublisherPtr publisher_ptr = GetCurrentImuPublisher(index);
#elif defined BUILDING_ROS2
Publisher<ImuMsg>::SharedPtr publisher_ptr = std::dynamic_pointer_cast<Publisher<ImuMsg>>(GetCurrentImuPublisher(index));
#endif
if (kOutputToRos == output_type_) {
publisher_ptr->publish(imu_msg);
} else {
#ifdef BUILDING_ROS1
if (bag_ && enable_imu_bag_) {
bag_->write(publisher_ptr->getTopic(), ros::Time(timestamp / 1000000000.0), imu_msg);
}
#endif
}
}
#ifdef BUILDING_ROS2
std::shared_ptr<rclcpp::PublisherBase> Lddc::CreatePublisher(uint8_t msg_type,
std::string &topic_name, uint32_t queue_size) {
if (kPointCloud2Msg == msg_type) {
DRIVER_INFO(*cur_node_,
"%s publish use PointCloud2 format", topic_name.c_str());
return cur_node_->create_publisher<PointCloud2>(topic_name, queue_size);
} else if (kLivoxCustomMsg == msg_type) {
DRIVER_INFO(*cur_node_,
"%s publish use livox custom format", topic_name.c_str());
return cur_node_->create_publisher<CustomMsg>(topic_name, queue_size);
}
#if 0
else if (kPclPxyziMsg == msg_type) {
DRIVER_INFO(*cur_node_,
"%s publish use pcl PointXYZI format", topic_name.c_str());
return cur_node_->create_publisher<PointCloud>(topic_name, queue_size);
}
#endif
else if (kLivoxImuMsg == msg_type) {
DRIVER_INFO(*cur_node_,
"%s publish use imu format", topic_name.c_str());
return cur_node_->create_publisher<ImuMsg>(topic_name,
queue_size);
} else {
PublisherPtr null_publisher(nullptr);
return null_publisher;
}
}
#endif
#ifdef BUILDING_ROS1
PublisherPtr Lddc::GetCurrentPublisher(uint8_t index) {
ros::Publisher **pub = nullptr;
uint32_t queue_size = kMinEthPacketQueueSize;
if (use_multi_topic_) {
pub = &private_pub_[index];
queue_size = queue_size / 8; // queue size is 4 for only one lidar
} else {
pub = &global_pub_;
queue_size = queue_size * 8; // shared queue size is 256, for all lidars
}
if (*pub == nullptr) {
char name_str[48];
memset(name_str, 0, sizeof(name_str));
if (use_multi_topic_) {
std::string ip_string = IpNumToString(lds_->lidars_[index].handle);
snprintf(name_str, sizeof(name_str), "livox/lidar_%s",
ReplacePeriodByUnderline(ip_string).c_str());
DRIVER_INFO(*cur_node_, "Support multi topics.");
} else {
DRIVER_INFO(*cur_node_, "Support only one topic.");
snprintf(name_str, sizeof(name_str), "livox/lidar");
}
*pub = new ros::Publisher;
if (kPointCloud2Msg == transfer_format_) {
**pub =
cur_node_->GetNode().advertise<sensor_msgs::PointCloud2>(name_str, queue_size);
DRIVER_INFO(*cur_node_,
"%s publish use PointCloud2 format, set ROS publisher queue size %d",
name_str, queue_size);
} else if (kLivoxCustomMsg == transfer_format_) {
**pub = cur_node_->GetNode().advertise<livox_ros_driver2::CustomMsg>(name_str,
queue_size);
DRIVER_INFO(*cur_node_,
"%s publish use livox custom format, set ROS publisher queue size %d",
name_str, queue_size);
} else if (kPclPxyziMsg == transfer_format_) {
**pub = cur_node_->GetNode().advertise<PointCloud>(name_str, queue_size);
DRIVER_INFO(*cur_node_,
"%s publish use pcl PointXYZI format, set ROS publisher queue "
"size %d",
name_str, queue_size);
}
}
return *pub;
}
PublisherPtr Lddc::GetCurrentImuPublisher(uint8_t handle) {
ros::Publisher **pub = nullptr;
uint32_t queue_size = kMinEthPacketQueueSize;
if (use_multi_topic_) {
pub = &private_imu_pub_[handle];
queue_size = queue_size * 2; // queue size is 64 for only one lidar
} else {
pub = &global_imu_pub_;
queue_size = queue_size * 8; // shared queue size is 256, for all lidars
}
if (*pub == nullptr) {
char name_str[48];
memset(name_str, 0, sizeof(name_str));
if (use_multi_topic_) {
DRIVER_INFO(*cur_node_, "Support multi topics.");
std::string ip_string = IpNumToString(lds_->lidars_[handle].handle);
snprintf(name_str, sizeof(name_str), "livox/imu_%s",
ReplacePeriodByUnderline(ip_string).c_str());
} else {
DRIVER_INFO(*cur_node_, "Support only one topic.");
snprintf(name_str, sizeof(name_str), "livox/imu");
}
*pub = new ros::Publisher;
**pub = cur_node_->GetNode().advertise<sensor_msgs::Imu>(name_str, queue_size);
DRIVER_INFO(*cur_node_, "%s publish imu data, set ROS publisher queue size %d", name_str,
queue_size);
}
return *pub;
}
#elif defined BUILDING_ROS2
std::shared_ptr<rclcpp::PublisherBase> Lddc::GetCurrentPublisher(uint8_t handle) {
uint32_t queue_size = kMinEthPacketQueueSize;
if (use_multi_topic_) {
if (!private_pub_[handle]) {
char name_str[48];
memset(name_str, 0, sizeof(name_str));
std::string ip_string = IpNumToString(lds_->lidars_[handle].handle);
snprintf(name_str, sizeof(name_str), "livox/lidar_%s",
ReplacePeriodByUnderline(ip_string).c_str());
std::string topic_name(name_str);
queue_size = queue_size * 2; // queue size is 64 for only one lidar
private_pub_[handle] = CreatePublisher(transfer_format_, topic_name, queue_size);
}
return private_pub_[handle];
} else {
if (!global_pub_) {
std::string topic_name("livox/lidar");
queue_size = queue_size * 8; // shared queue size is 256, for all lidars
global_pub_ = CreatePublisher(transfer_format_, topic_name, queue_size);
}
return global_pub_;
}
}
std::shared_ptr<rclcpp::PublisherBase> Lddc::GetCurrentImuPublisher(uint8_t handle) {
uint32_t queue_size = kMinEthPacketQueueSize;
if (use_multi_topic_) {
if (!private_imu_pub_[handle]) {
char name_str[48];
memset(name_str, 0, sizeof(name_str));
std::string ip_string = IpNumToString(lds_->lidars_[handle].handle);
snprintf(name_str, sizeof(name_str), "livox/imu_%s",
ReplacePeriodByUnderline(ip_string).c_str());
std::string topic_name(name_str);
queue_size = queue_size * 2; // queue size is 64 for only one lidar
private_imu_pub_[handle] = CreatePublisher(kLivoxImuMsg, topic_name,
queue_size);
}
return private_imu_pub_[handle];
} else {
if (!global_imu_pub_) {
std::string topic_name("livox/imu");
queue_size = queue_size * 8; // shared queue size is 256, for all lidars
global_imu_pub_ = CreatePublisher(kLivoxImuMsg, topic_name, queue_size);
}
return global_imu_pub_;
}
}
#endif
void Lddc::CreateBagFile(const std::string &file_name) {
#ifdef BUILDING_ROS1
if (!bag_) {
bag_ = new rosbag::Bag;
bag_->open(file_name, rosbag::bagmode::Write);
DRIVER_INFO(*cur_node_, "Create bag file :%s!", file_name.c_str());
}
#endif
}
} // namespace livox_ros