Apollo 9.0
自动驾驶开放平台
lidar_component_base_impl.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16#ifndef APOLLO_LIDAR_COMPONENT_BASE_IMPL_H
17#define APOLLO_LIDAR_COMPONENT_BASE_IMPL_H
18
19#include <memory>
20#include <string>
21
22#include "modules/common_msgs/sensor_msgs/pointcloud.pb.h"
23#include "modules/drivers/lidar/common/proto/lidar_config_base.pb.h"
24
25#include "cyber/cyber.h"
27
28namespace apollo {
29namespace drivers {
30namespace lidar {
31
32template <typename ScanType, typename ComponentType = apollo::cyber::NullType>
33class LidarComponentBaseImpl : public apollo::cyber::Component<ComponentType> {
34 public:
35 virtual ~LidarComponentBaseImpl() = default;
36
37 bool Init() override = 0;
38 virtual bool InitBase(const LidarConfigBase& lidar_config_base) = 0;
39 virtual void ReadScanCallback(const std::shared_ptr<ScanType>& scan_message)
40 = 0;
41
42 std::shared_ptr<ScanType> AcquireScanMessage();
43
44 protected:
45 bool InitConverter(const LidarConfigBase& lidar_config_base);
46
47 bool InitPacket(const LidarConfigBase& lidar_config_base);
48
49 virtual bool WriteScan(const std::shared_ptr<ScanType>& scan_message);
50
51 virtual std::shared_ptr<PointCloud> AllocatePointCloud();
52
53 virtual bool WritePointCloud(
54 const std::shared_ptr<PointCloud>& point_cloud);
55
56 static std::shared_ptr<PointCloud> PcdDefaultAllocator(
57 std::shared_ptr<cyber::Writer<PointCloud>>& pcd_writer);
58
59 static void PcdDefaultCleaner(
60 std::shared_ptr<PointCloud>& unused_pcd_frame);
61
62 std::string frame_id_;
63
64 private:
65 std::shared_ptr<cyber::Writer<ScanType>> scan_writer_ = nullptr;
66 std::shared_ptr<cyber::Reader<ScanType>> scan_reader_ = nullptr;
67 std::shared_ptr<cyber::Writer<PointCloud>> pcd_writer_ = nullptr;
68 std::shared_ptr<SyncBuffering<PointCloud>> pcd_buffer_ = nullptr;
69
70 std::atomic<int> pcd_sequence_num_{0};
71};
72
73template <typename ScanType, typename ComponentType>
75 const LidarConfigBase& lidar_config_base) {
76 pcd_writer_ = this->node_->template CreateWriter<PointCloud>(
77 lidar_config_base.point_cloud_channel());
78 RETURN_VAL_IF(pcd_writer_ == nullptr, false);
79
80 if (lidar_config_base.source_type()
81 == LidarConfigBase_SourceType_RAW_PACKET) {
82 scan_reader_ = this->node_->template CreateReader<ScanType>(
83 lidar_config_base.scan_channel(),
84 std::bind(
86 this,
87 std::placeholders::_1));
88 RETURN_VAL_IF(scan_reader_ == nullptr, false);
89 }
90
91 frame_id_ = lidar_config_base.frame_id();
92
93 pcd_buffer_ = std::make_shared<SyncBuffering<PointCloud>>(
94 [this](){
96 this->pcd_writer_);
97 },
99 pcd_buffer_->SetBufferSize(lidar_config_base.buffer_size());
100 pcd_buffer_->Init();
101 return true;
102}
103
104template <typename ScanType, typename ComponentType>
106 const LidarConfigBase& lidar_config_base) {
107 if (lidar_config_base.source_type()
108 == LidarConfigBase_SourceType_ONLINE_LIDAR) {
109 scan_writer_ = this->node_->template CreateWriter<ScanType>(
110 lidar_config_base.scan_channel());
111 RETURN_VAL_IF(scan_writer_ == nullptr, false);
112 }
113 return true;
114}
115
116template <typename ScanType, typename ComponentType>
117std::shared_ptr<ScanType> LidarComponentBaseImpl<
118 ScanType, ComponentType>::AcquireScanMessage() {
119 if (scan_writer_ == nullptr) {
120 return std::make_shared<ScanType>();
121 }
122 return scan_writer_->AcquireMessage();
123}
124
125template <typename ScanType, typename ComponentType>
127 const std::shared_ptr<ScanType>& scan_message) {
128 return scan_writer_->Write(scan_message);
129}
130
131template <typename ScanType, typename ComponentType>
132std::shared_ptr<PointCloud>
134 return pcd_buffer_->AllocateElement();
135}
136
137template <typename ScanType, typename ComponentType>
139 const std::shared_ptr<PointCloud>& point_cloud) {
140 point_cloud->mutable_header()->set_frame_id(frame_id_);
141 point_cloud->mutable_header()->set_sequence_num(
142 pcd_sequence_num_.fetch_add(1));
143 point_cloud->mutable_header()->set_timestamp_sec(
144 cyber::Time().Now().ToSecond());
145 RETURN_VAL_IF(!pcd_writer_->Write(point_cloud), false);
146 return true;
147}
148
149template <typename ScanType, typename ComponentType>
150std::shared_ptr<PointCloud>
152 std::shared_ptr<cyber::Writer<PointCloud>>& pcd_writer) {
153 constexpr int default_point_cloud_reserve = 170000;
154 std::shared_ptr<PointCloud> new_pcd_object = pcd_writer->AcquireMessage();
155 new_pcd_object->mutable_point()->Reserve(default_point_cloud_reserve);
156 AINFO << "new pcd frame memory allocated, reserve point size = "
157 << default_point_cloud_reserve;
158 return new_pcd_object;
159}
160
161template <typename ScanType, typename ComponentType>
163 std::shared_ptr<PointCloud>& unused_pcd_frame) {
164 unused_pcd_frame->clear_point();
165}
166} // namespace lidar
167} // namespace drivers
168} // namespace apollo
169#endif // APOLLO_LIDAR_COMPONENT_BASE_IMPL_H
Cyber has builtin time type Time.
Definition time.h:31
virtual bool WritePointCloud(const std::shared_ptr< PointCloud > &point_cloud)
bool InitConverter(const LidarConfigBase &lidar_config_base)
bool InitPacket(const LidarConfigBase &lidar_config_base)
virtual bool WriteScan(const std::shared_ptr< ScanType > &scan_message)
static void PcdDefaultCleaner(std::shared_ptr< PointCloud > &unused_pcd_frame)
static std::shared_ptr< PointCloud > PcdDefaultAllocator(std::shared_ptr< cyber::Writer< PointCloud > > &pcd_writer)
virtual std::shared_ptr< PointCloud > AllocatePointCloud()
virtual bool InitBase(const LidarConfigBase &lidar_config_base)=0
virtual void ReadScanCallback(const std::shared_ptr< ScanType > &scan_message)=0
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define AINFO
Definition log.h:42
class register implement