Apollo 11.0
自动驾驶开放平台
apollo::perception::onboard::MsgBuffer< T > 模板类 参考

#include <msg_buffer.h>

apollo::perception::onboard::MsgBuffer< T > 的协作图:

Public 类型

typedef std::shared_ptr< T const > ConstPtr
 
typedef std::pair< double, ConstPtrObjectPair
 

Public 成员函数

 MsgBuffer ()
 
 ~MsgBuffer ()=default
 
 MsgBuffer (const MsgBuffer &)=delete
 
MsgBuffer operator= (const MsgBuffer &)=delete
 
void Init (const std::string &channel, const std::string &name)
 
int LookupNearest (double timestamp, ConstPtr *msg)
 
int LookupLatest (ConstPtr *msg)
 
int LookupPeriod (double timestamp, double period, std::vector< ObjectPair > *msgs)
 

详细描述

template<class T>
class apollo::perception::onboard::MsgBuffer< T >

在文件 msg_buffer.h37 行定义.

成员类型定义说明

◆ ConstPtr

template<class T >
typedef std::shared_ptr<T const> apollo::perception::onboard::MsgBuffer< T >::ConstPtr

在文件 msg_buffer.h39 行定义.

◆ ObjectPair

template<class T >
typedef std::pair<double, ConstPtr> apollo::perception::onboard::MsgBuffer< T >::ObjectPair

在文件 msg_buffer.h40 行定义.

构造及析构函数说明

◆ MsgBuffer() [1/2]

template<class T >
apollo::perception::onboard::MsgBuffer< T >::MsgBuffer ( )
inline

在文件 msg_buffer.h43 行定义.

43: buffer_queue_(FLAGS_obs_msg_buffer_size) {}

◆ ~MsgBuffer()

template<class T >
apollo::perception::onboard::MsgBuffer< T >::~MsgBuffer ( )
default

◆ MsgBuffer() [2/2]

template<class T >
apollo::perception::onboard::MsgBuffer< T >::MsgBuffer ( const MsgBuffer< T > &  )
delete

成员函数说明

◆ Init()

template<class T >
void apollo::perception::onboard::MsgBuffer< T >::Init ( const std::string &  channel,
const std::string &  name 
)

在文件 msg_buffer.h73 行定义.

73 {
74 int index = static_cast<int>(name.find_last_of('/'));
75 if (index != -1) {
76 node_name_ = name.substr(index + 1) + "_subscriber";
77 } else {
78 node_name_ = name + "_subscriber";
79 }
80 node_.reset(apollo::cyber::CreateNode(node_name_).release());
81
82 std::function<void(const ConstPtr&)> register_call =
83 std::bind(&MsgBuffer<T>::MsgCallback, this, std::placeholders::_1);
84 msg_subscriber_ = node_->CreateReader<T>(channel, register_call);
85
86 std::lock_guard<std::mutex> lock(buffer_mutex_);
87 buffer_queue_.set_capacity(FLAGS_obs_msg_buffer_size);
88 init_ = true;
89}
std::shared_ptr< T const > ConstPtr
Definition msg_buffer.h:39
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33

◆ LookupLatest()

template<class T >
int apollo::perception::onboard::MsgBuffer< T >::LookupLatest ( ConstPtr msg)

在文件 msg_buffer.h140 行定义.

140 {
141 std::lock_guard<std::mutex> lock(buffer_mutex_);
142 if (!init_) {
143 AERROR << "Message buffer is uninitialized.";
144 return false;
145 }
146 if (buffer_queue_.empty()) {
147 AERROR << "Message buffer is empty.";
148 return false;
149 }
150 *msg = buffer_queue_.back().second;
151 return true;
152}
#define AERROR
Definition log.h:44

◆ LookupNearest()

template<class T >
int apollo::perception::onboard::MsgBuffer< T >::LookupNearest ( double  timestamp,
ConstPtr msg 
)

在文件 msg_buffer.h99 行定义.

99 {
100 std::lock_guard<std::mutex> lock(buffer_mutex_);
101 if (!init_) {
102 AERROR << "msg buffer is uninitialized.";
103 return false;
104 }
105 if (buffer_queue_.empty()) {
106 AERROR << "msg buffer is empty.";
107 return false;
108 }
109 if (buffer_queue_.front().first - FLAGS_obs_buffer_match_precision >
110 timestamp) {
111 AERROR << "Your timestamp (" << timestamp
112 << ") is earlier than the oldest timestamp ("
113 << buffer_queue_.front().first << ").";
114 return false;
115 }
116 if (buffer_queue_.back().first + FLAGS_obs_buffer_match_precision <
117 timestamp) {
118 AERROR << "Your timestamp (" << timestamp
119 << ") is newer than the latest timestamp ("
120 << buffer_queue_.back().first << ").";
121 return false;
122 }
123
124 // loop to find nearest
125 double distance = std::numeric_limits<double>::max();
126 int idx = static_cast<int>(buffer_queue_.size()) - 1;
127 for (; idx >= 0; --idx) {
128 double temp_distance = fabs(timestamp - buffer_queue_[idx].first);
129 if (temp_distance >= distance) {
130 break;
131 }
132 distance = temp_distance;
133 }
134 *msg = buffer_queue_[idx + 1].second;
135
136 return true;
137}

◆ LookupPeriod()

template<class T >
int apollo::perception::onboard::MsgBuffer< T >::LookupPeriod ( double  timestamp,
double  period,
std::vector< ObjectPair > *  msgs 
)

在文件 msg_buffer.h155 行定义.

156 {
157 std::lock_guard<std::mutex> lock(buffer_mutex_);
158 if (!init_) {
159 AERROR << "Message buffer is uninitialized.";
160 return false;
161 }
162 if (buffer_queue_.empty()) {
163 AERROR << "Message buffer is empty.";
164 return false;
165 }
166 if (buffer_queue_.front().first - FLAGS_obs_buffer_match_precision >
167 timestamp) {
168 AERROR << "Your timestamp (" << timestamp << ") is earlier than the oldest "
169 << "timestamp (" << buffer_queue_.front().first << ").";
170 return false;
171 }
172 if (buffer_queue_.back().first + FLAGS_obs_buffer_match_precision <
173 timestamp) {
174 AERROR << "Your timestamp (" << timestamp << ") is newer than the latest "
175 << "timestamp (" << buffer_queue_.back().first << ").";
176 return false;
177 }
178
179 const double lower_timestamp = timestamp - period;
180 const double upper_timestamp = timestamp + period;
181 for (const auto& obj_pair : buffer_queue_) {
182 if (obj_pair.first < lower_timestamp) {
183 continue;
184 }
185 if (obj_pair.first > upper_timestamp) {
186 break;
187 }
188 msgs->push_back(obj_pair);
189 }
190
191 return true;
192}

◆ operator=()

template<class T >
MsgBuffer apollo::perception::onboard::MsgBuffer< T >::operator= ( const MsgBuffer< T > &  )
delete

该类的文档由以下文件生成: