Apollo 11.0
自动驾驶开放平台
hesai_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 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 *****************************************************************************/
17
18#include "boost/format.hpp"
19
21
22namespace apollo {
23namespace drivers {
24namespace lidar {
25
27 if (!GetProtoConfig(&conf_)) {
28 AERROR << "load config error, file:" << config_file_path_;
29 return false;
30 }
31 this->InitBase(conf_.config_base());
32
33 if (conf_.has_convert_thread_nums()) {
34 convert_threads_num_ = conf_.convert_thread_nums();
35 }
36
37 driver_ptr_ = std::make_shared<HesaiLidarSdk<LidarPointXYZIRT>>();
38
39 DriverParam param;
40 param.input_param.source_type
41 = static_cast<SourceType>(conf_.source_type());
42 param.input_param.pcap_path = conf_.pcap_path();
43 param.input_param.correction_file_path = conf_.correction_file_path();
44 param.input_param.firetimes_path = conf_.firetimes_path();
45 param.input_param.device_ip_address = conf_.device_ip();
46 param.input_param.host_ip_address = conf_.host_ip();
47 param.input_param.ptc_port = conf_.ptc_port();
48 param.input_param.udp_port = conf_.udp_port();
49 param.input_param.multicast_ip_address = "";
50 param.input_param.read_pcap = true;
51
52 param.decoder_param.pcap_play_synchronization = false;
53 param.decoder_param.frame_start_azimuth = conf_.frame_start_azimuth();
54 param.decoder_param.enable_parser_thread = true;
55
56 param.decoder_param.transform_param.x = 0;
57 param.decoder_param.transform_param.y = 0;
58 param.decoder_param.transform_param.z = 0;
59 param.decoder_param.transform_param.pitch = 0;
60 param.decoder_param.transform_param.yaw = 0;
61 param.decoder_param.transform_param.roll = 0;
62
63 param.lidar_type = conf_.lidar_type();
64
65 driver_ptr_->RegRecvCallback(std::bind(
66 &HesaiComponent2::SendPointCloud, this, std::placeholders::_1));
67
68 if (conf_.config_base().source_type()
69 == LidarConfigBase_SourceType_RAW_PACKET) {
70 param.decoder_param.enable_udp_thread = false;
71 }
72
73 if (conf_.config_base().source_type()
74 == LidarConfigBase_SourceType_ONLINE_LIDAR) {
75 driver_ptr_->RegRecvCallback(std::bind(
77 this,
78 std::placeholders::_1,
79 std::placeholders::_2));
80 }
81
82 if (!driver_ptr_->Init(param)) {
83 AERROR << "init fail";
84 return false;
85 }
86 driver_ptr_->Start();
87
88 return true;
89}
90
92 const std::shared_ptr<HesaiUdpFrame>& scan_message) {
93 // UdpPacket packet;
94 // UdpFrame_t frame_t;
95 for (int i = 0, packet_size = scan_message->packets_size(); i < packet_size;
96 ++i) {
97 driver_ptr_->lidar_ptr_->origin_packets_buffer_.emplace_back(
98 reinterpret_cast<const uint8_t*>(
99 scan_message->packets(i).data().c_str()),
100 scan_message->packets(i).data().length());
101 }
102}
103
105 const LidarDecodedFrame<hesai::lidar::LidarPointXYZIRT>& msg) {
106 std::shared_ptr<PointCloud> cloud_message = this->AllocatePointCloud();
107
108 cloud_message->set_is_dense(false);
109 cloud_message->mutable_header()->set_timestamp_sec(
110 cyber::Time().Now().ToSecond());
111
112 double timestamp_diff = 0.0;
113 int point_size = msg.points_num;
114
115 if (point_size > 0) {
116 const double pcl_timestamp = msg.points[point_size - 1].timestamp;
117 cloud_message->set_measurement_time(pcl_timestamp);
118 timestamp_diff = pcl_timestamp - msg.points[0].timestamp;
119 } else {
120 cloud_message->set_measurement_time(0.0);
121 }
122
123 for (int i = 0; i < point_size; ++i) {
124 cloud_message->add_point();
125 }
126
127#pragma omp parallel for schedule(static) num_threads(convert_threads_num_)
128 for (int i = 0; i < point_size; ++i) {
129 cloud_message->mutable_point(i)->set_x(msg.points[i].x);
130 cloud_message->mutable_point(i)->set_y(msg.points[i].y);
131 cloud_message->mutable_point(i)->set_z(msg.points[i].z);
132 cloud_message->mutable_point(i)->set_timestamp(
134 msg.points[i].timestamp));
135 cloud_message->mutable_point(i)->set_intensity(msg.points[i].intensity);
136 }
137
138 AINFO << boost::format("point cnt = %d; timestamp_diff = %.9f s")
139 % point_size % timestamp_diff;
140 this->WritePointCloud(cloud_message);
141}
142
144 const hesai::lidar::UdpFrame_t& hesai_raw_msg,
145 double timestamp) {
146 std::shared_ptr<HesaiUdpFrame> udp_frame
147 = std::make_shared<HesaiUdpFrame>();
148
149 for (int i = 0; i < hesai_raw_msg.size(); ++i) {
150 auto packet = udp_frame->add_packets();
151 packet->set_timestamp_sec(timestamp);
152 packet->set_size(hesai_raw_msg[i].packet_len);
153 packet->mutable_data()->assign(
154 reinterpret_cast<const char*>(hesai_raw_msg[i].buffer),
155 reinterpret_cast<const char*>(hesai_raw_msg[i].buffer)
156 + hesai_raw_msg[i].packet_len);
157 }
158 this->WriteScan(udp_frame);
159}
160
161} // namespace lidar
162} // namespace drivers
163} // namespace apollo
bool GetProtoConfig(T *config) const
Cyber has builtin time type Time.
Definition time.h:31
void ReadScanCallback(const std::shared_ptr< HesaiUdpFrame > &scan_message) override
void SendPacket(const UdpFrame_t &hesai_raw_msg, double)
void SendPointCloud(const LidarDecodedFrame< LidarPointXYZIRT > &msg)
bool WriteScan(const std::shared_ptr< HesaiUdpFrame > &scan_message)
bool WritePointCloud(const std::shared_ptr< PointCloud > &point_cloud)
bool InitBase(const LidarConfigBase &lidar_config_base) override
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
uint64_t GetNanosecondTimestampFromSecondTimestamp(double second_timestamp)
class register implement
Definition arena_queue.h:37
required apollo::drivers::lidar::LidarConfigBase config_base