Apollo 11.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
26#include "cyber/cyber.h"
29
30namespace apollo {
31namespace drivers {
32namespace lidar {
33
34template <typename ScanType, typename ComponentType = apollo::cyber::NullType>
35class LidarComponentBaseImpl : public apollo::cyber::Component<ComponentType> {
36 public:
37 virtual ~LidarComponentBaseImpl() = default;
38
39 bool Init() override = 0;
40 virtual bool InitBase(const LidarConfigBase& lidar_config_base) = 0;
41 virtual void ReadScanCallback(
42 const std::shared_ptr<ScanType>& scan_message) = 0;
43
44 std::shared_ptr<ScanType> AcquireScanMessage();
45
46 protected:
47 bool InitConverter(const LidarConfigBase& lidar_config_base);
48
49 bool InitPacket(const LidarConfigBase& lidar_config_base);
50
51 virtual bool WriteScan(const std::shared_ptr<ScanType>& scan_message);
52
53 virtual std::shared_ptr<PointCloud> AllocatePointCloud();
54
55 virtual bool WritePointCloud(const std::shared_ptr<PointCloud>& point_cloud);
56
57 virtual std::shared_ptr<apollo::cyber::base::ArenaQueue<PointXYZIT>>
59 // static std::shared_ptr<PointCloud> PcdDefaultAllocator(
60 // std::shared_ptr<cyber::Writer<PointCloud>>& pcd_writer);
61
62 static void PcdDefaultCleaner(std::shared_ptr<PointCloud>& unused_pcd_frame);
63
64 std::string frame_id_;
67
68 private:
69 std::shared_ptr<cyber::Writer<ScanType>> scan_writer_ = nullptr;
70 std::shared_ptr<cyber::Reader<ScanType>> scan_reader_ = nullptr;
71 std::shared_ptr<cyber::Writer<PointCloud>> pcd_writer_ = nullptr;
72 // std::shared_ptr<SyncBuffering<PointCloud>> pcd_buffer_ = nullptr;
73
74 std::atomic<int> pcd_sequence_num_{0};
75};
76
77template <typename ScanType, typename ComponentType>
79 const LidarConfigBase& lidar_config_base) {
80 pcd_writer_ = this->node_->template CreateWriter<PointCloud>(
81 lidar_config_base.point_cloud_channel());
82 RETURN_VAL_IF(pcd_writer_ == nullptr, false);
83
84 if (lidar_config_base.source_type() ==
85 LidarConfigBase_SourceType_RAW_PACKET) {
86 scan_reader_ = this->node_->template CreateReader<ScanType>(
87 lidar_config_base.scan_channel(),
89 std::placeholders::_1));
90 RETURN_VAL_IF(scan_reader_ == nullptr, false);
91 }
92
93 frame_id_ = lidar_config_base.frame_id();
94 write_scan_ = lidar_config_base.write_scan();
95 default_reserved_points_num_ =
96 lidar_config_base.default_reserved_points_num();
97
98 // pcd_buffer_ = std::make_shared<SyncBuffering<PointCloud>>(
99 // [this](){
100 // return LidarComponentBaseImpl::PcdDefaultAllocator(
101 // this->pcd_writer_);
102 // },
103 // LidarComponentBaseImpl::PcdDefaultCleaner);
104 // pcd_buffer_->SetBufferSize(lidar_config_base.buffer_size());
105 // pcd_buffer_->Init();
106 return true;
107}
108
109template <typename ScanType, typename ComponentType>
111 const LidarConfigBase& lidar_config_base) {
112 if (lidar_config_base.source_type() ==
113 LidarConfigBase_SourceType_ONLINE_LIDAR) {
114 scan_writer_ = this->node_->template CreateWriter<ScanType>(
115 lidar_config_base.scan_channel());
116 RETURN_VAL_IF(scan_writer_ == nullptr, false);
117 }
118 return true;
119}
120
121template <typename ScanType, typename ComponentType>
122std::shared_ptr<ScanType>
124 if (scan_writer_ == nullptr) {
125 return std::make_shared<ScanType>();
126 }
127 return scan_writer_->AcquireMessage();
128}
129
130template <typename ScanType, typename ComponentType>
132 const std::shared_ptr<ScanType>& scan_message) {
133 if (write_scan_) {
134 return scan_writer_->Write(scan_message);
135 }
136 return true;
137}
138
139template <typename ScanType, typename ComponentType>
140std::shared_ptr<apollo::cyber::base::ArenaQueue<PointXYZIT>>
142 std::shared_ptr<apollo::cyber::base::ArenaQueue<PointXYZIT>> ret;
143 auto channel_id = pcd_writer_->GetChannelId();
144
145 auto arena_manager =
146 apollo::cyber::transport::ProtobufArenaManager::Instance();
147 if (!arena_manager->Enable()) {
148 ADEBUG << "arena manager enable failed.";
149 } else {
151 arena_manager->RegisterQueue<apollo::drivers::PointXYZIT>(channel_id,
152 170000);
153 ret = std::shared_ptr<apollo::cyber::base::ArenaQueue<PointXYZIT>>(
154 reinterpret_cast<
156 arena_manager->GetAvailableBuffer(channel_id)),
158 }
159 return ret;
160}
161
162template <typename ScanType, typename ComponentType>
163std::shared_ptr<PointCloud>
165 std::shared_ptr<PointCloud> new_pcd_object = pcd_writer_->AcquireMessage();
166 new_pcd_object->Clear();
167 new_pcd_object->mutable_point()->Reserve(default_reserved_points_num_);
168 return new_pcd_object;
169}
170
171template <typename ScanType, typename ComponentType>
173 const std::shared_ptr<PointCloud>& point_cloud) {
174 point_cloud->mutable_header()->set_frame_id(frame_id_);
175 point_cloud->mutable_header()->set_sequence_num(
176 pcd_sequence_num_.fetch_add(1));
177 point_cloud->mutable_header()->set_timestamp_sec(
178 cyber::Time().Now().ToSecond());
179 RETURN_VAL_IF(!pcd_writer_->Write(point_cloud), false);
180 return true;
181}
182
183// template <typename ScanType, typename ComponentType>
184// std::shared_ptr<PointCloud>
185// LidarComponentBaseImpl<ScanType, ComponentType>::PcdDefaultAllocator(
186// std::shared_ptr<cyber::Writer<PointCloud>>& pcd_writer) {
187// constexpr int default_point_cloud_reserve = 170000;
188// std::shared_ptr<PointCloud> new_pcd_object =
189// pcd_writer->AcquireMessage();
190// new_pcd_object->mutable_point()->Reserve(default_point_cloud_reserve);
191// AINFO << "new pcd frame memory allocated, reserve point size = "
192// << default_point_cloud_reserve;
193// return new_pcd_object;
194// }
195
196template <typename ScanType, typename ComponentType>
198 std::shared_ptr<PointCloud>& unused_pcd_frame) {
199 unused_pcd_frame->clear_point();
200}
201} // namespace lidar
202} // namespace drivers
203} // namespace apollo
204#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 std::shared_ptr< apollo::cyber::base::ArenaQueue< PointXYZIT > > GetPointQueue()
virtual bool WriteScan(const std::shared_ptr< ScanType > &scan_message)
static void PcdDefaultCleaner(std::shared_ptr< PointCloud > &unused_pcd_frame)
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 ADEBUG
Definition log.h:41
class register implement
Definition arena_queue.h:37