23 #include <condition_variable> 28 #include <unordered_map> 33 #include "modules/common/proto/error_code.pb.h" 45 using apollo::common::ErrorCode;
47 using micros = std::chrono::microseconds;
67 template <
typename SensorType>
85 virtual void Parse(
const uint32_t message_id,
const uint8_t *data,
88 void ClearSensorData();
90 std::condition_variable *GetMutableCVar();
98 const uint32_t message_id);
105 common::ErrorCode GetSensorData(
SensorType *
const sensor_data);
110 void ResetSendMessages();
113 template <
class T,
bool need_check>
114 void AddRecvProtocolData();
116 template <
class T,
bool need_check>
117 void AddSendProtocolData();
128 bool is_received_on_time_ =
false;
133 template <
typename SensorType>
134 template <
class T,
bool need_check>
136 recv_protocol_data_.emplace_back(
new T());
137 auto *dt = recv_protocol_data_.back().get();
141 protocol_data_map_[T::ID] = dt;
143 check_ids_[T::ID].period = dt->GetPeriod();
144 check_ids_[T::ID].real_period = 0;
145 check_ids_[T::ID].last_time = 0;
146 check_ids_[T::ID].error_count = 0;
150 template <
typename SensorType>
151 template <
class T,
bool need_check>
153 send_protocol_data_.emplace_back(
new T());
154 auto *dt = send_protocol_data_.back().get();
158 protocol_data_map_[T::ID] = dt;
160 check_ids_[T::ID].period = dt->GetPeriod();
161 check_ids_[T::ID].real_period = 0;
162 check_ids_[T::ID].last_time = 0;
163 check_ids_[T::ID].error_count = 0;
167 template <
typename SensorType>
170 const uint32_t message_id) {
171 if (protocol_data_map_.find(message_id) == protocol_data_map_.end()) {
172 ADEBUG <<
"Unable to get protocol data because of invalid message_id:" 176 return protocol_data_map_[message_id];
179 template <
typename SensorType>
181 const uint8_t *data, int32_t length) {
183 GetMutableProtocolDataById(message_id);
184 if (protocol_data ==
nullptr) {
188 std::lock_guard<std::mutex> lock(sensor_data_mutex_);
189 protocol_data->
Parse(data, length, &sensor_data_);
191 received_ids_.insert(message_id);
193 const auto it = check_ids_.find(message_id);
194 if (it != check_ids_.end()) {
196 it->second.real_period = time - it->second.last_time;
198 const double period_multiplier = 1.5;
199 if (static_cast<double>(it->second.real_period) >
200 (static_cast<double>(it->second.period) * period_multiplier)) {
201 it->second.error_count += 1;
203 it->second.error_count = 0;
205 it->second.last_time = time;
209 template <
typename SensorType>
211 std::lock_guard<std::mutex> lock(sensor_data_mutex_);
212 sensor_data_.Clear();
215 template <
typename SensorType>
220 template <
typename SensorType>
223 if (sensor_data ==
nullptr) {
224 AERROR <<
"Failed to get sensor_data due to nullptr.";
225 return ErrorCode::CANBUS_ERROR;
227 std::lock_guard<std::mutex> lock(sensor_data_mutex_);
228 sensor_data->CopyFrom(sensor_data_);
232 template <
typename SensorType>
234 for (
auto &protocol_data : send_protocol_data_) {
235 if (protocol_data ==
nullptr) {
236 AERROR <<
"Invalid protocol data.";
238 protocol_data->Reset();
::apollo::cyber::Time Time
Definition: racobit_radar_message_manager.h:41
int64_t real_period
Definition: message_manager.h:56
common::ErrorCode GetSensorData(SensorType *const sensor_data)
get chassis detail. used lock_guard in this function to avoid concurrent read/write issue...
Definition: message_manager.h:221
this struct include data for check ids.
Definition: message_manager.h:54
static std::string byte_to_hex(const uint8_t value)
Transform an integer with the size of one byte to its hexadecimal represented by a string...
MessageManager()
Definition: message_manager.h:73
void AddSendProtocolData()
Definition: message_manager.h:152
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual void Parse(const uint32_t message_id, const uint8_t *data, int32_t length)
parse data and store parsed info in protocol data
Definition: message_manager.h:180
This is the base class of protocol data.
Definition: protocol_data.h:44
SensorType sensor_data_
Definition: message_manager.h:127
std::vector< std::unique_ptr< ProtocolData< SensorType > > > recv_protocol_data_
Definition: message_manager.h:120
int32_t error_count
Definition: message_manager.h:58
void ResetSendMessages()
Definition: message_manager.h:233
std::condition_variable cvar_
Definition: message_manager.h:130
std::set< uint32_t > received_ids_
Definition: message_manager.h:124
ProtocolData< SensorType > * GetMutableProtocolDataById(const uint32_t message_id)
get mutable protocol data by message id
Definition: message_manager.h:169
virtual void Parse(const uint8_t *bytes, int32_t length, SensorType *sensor_data) const
Definition: protocol_data.h:139
std::vector< std::unique_ptr< ProtocolData< SensorType > > > send_protocol_data_
Definition: message_manager.h:119
std::condition_variable * GetMutableCVar()
Definition: message_manager.h:216
#define ADEBUG
Definition: log.h:41
std::chrono::microseconds micros
Definition: message_manager.h:47
std::unordered_map< uint32_t, CheckIdArg > check_ids_
Definition: message_manager.h:123
void AddRecvProtocolData()
Definition: message_manager.h:135
int64_t last_time
Definition: message_manager.h:57
The class of ProtocolData.
#define AERROR
Definition: log.h:44
void ClearSensorData()
Definition: message_manager.h:210
std::mutex sensor_data_mutex_
Definition: message_manager.h:126
message manager manages protocols. It supports parse and can get protocol data by message id...
Definition: message_manager.h:68
SensorType
Sensor types are set in the order of lidar, radar, camera, ultrasonic Please make sure SensorType has...
Definition: sensor_meta.h:29
uint64_t ToNanosecond() const
convert time to nanosecond.
bool OK()
Definition: state.h:44
std::unordered_map< uint32_t, ProtocolData< SensorType > * > protocol_data_map_
Definition: message_manager.h:122
static Time Now()
get the current time.
int64_t period
Definition: message_manager.h:55