Apollo  6.0
Open source self driving car software
message_manager.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 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 
21 #pragma once
22 
23 #include <condition_variable>
24 #include <memory>
25 #include <mutex>
26 #include <set>
27 #include <thread>
28 #include <unordered_map>
29 #include <vector>
30 
31 #include "cyber/common/log.h"
32 #include "cyber/time/time.h"
33 #include "modules/common/proto/error_code.pb.h"
36 
41 namespace apollo {
42 namespace drivers {
43 namespace canbus {
44 
45 using apollo::common::ErrorCode;
47 using micros = std::chrono::microseconds;
48 
54 struct CheckIdArg {
55  int64_t period = 0;
56  int64_t real_period = 0;
57  int64_t last_time = 0;
58  int32_t error_count = 0;
59 };
60 
67 template <typename SensorType>
69  public:
70  /*
71  * @brief constructor function
72  */
74  /*
75  * @brief destructor function
76  */
77  virtual ~MessageManager() = default;
78 
85  virtual void Parse(const uint32_t message_id, const uint8_t *data,
86  int32_t length);
87 
88  void ClearSensorData();
89 
90  std::condition_variable *GetMutableCVar();
91 
97  ProtocolData<SensorType> *GetMutableProtocolDataById(
98  const uint32_t message_id);
99 
105  common::ErrorCode GetSensorData(SensorType *const sensor_data);
106 
107  /*
108  * @brief reset send messages
109  */
110  void ResetSendMessages();
111 
112  protected:
113  template <class T, bool need_check>
114  void AddRecvProtocolData();
115 
116  template <class T, bool need_check>
117  void AddSendProtocolData();
118 
119  std::vector<std::unique_ptr<ProtocolData<SensorType>>> send_protocol_data_;
120  std::vector<std::unique_ptr<ProtocolData<SensorType>>> recv_protocol_data_;
121 
122  std::unordered_map<uint32_t, ProtocolData<SensorType> *> protocol_data_map_;
123  std::unordered_map<uint32_t, CheckIdArg> check_ids_;
124  std::set<uint32_t> received_ids_;
125 
126  std::mutex sensor_data_mutex_;
128  bool is_received_on_time_ = false;
129 
130  std::condition_variable cvar_;
131 };
132 
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();
138  if (dt == nullptr) {
139  return;
140  }
141  protocol_data_map_[T::ID] = dt;
142  if (need_check) {
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;
147  }
148 }
149 
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();
155  if (dt == nullptr) {
156  return;
157  }
158  protocol_data_map_[T::ID] = dt;
159  if (need_check) {
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;
164  }
165 }
166 
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:"
173  << Byte::byte_to_hex(message_id);
174  return nullptr;
175  }
176  return protocol_data_map_[message_id];
177 }
178 
179 template <typename SensorType>
180 void MessageManager<SensorType>::Parse(const uint32_t message_id,
181  const uint8_t *data, int32_t length) {
182  ProtocolData<SensorType> *protocol_data =
183  GetMutableProtocolDataById(message_id);
184  if (protocol_data == nullptr) {
185  return;
186  }
187  {
188  std::lock_guard<std::mutex> lock(sensor_data_mutex_);
189  protocol_data->Parse(data, length, &sensor_data_);
190  }
191  received_ids_.insert(message_id);
192  // check if need to check period
193  const auto it = check_ids_.find(message_id);
194  if (it != check_ids_.end()) {
195  const int64_t time = Time::Now().ToNanosecond() / 1e3;
196  it->second.real_period = time - it->second.last_time;
197  // if period 1.5 large than base period, inc error_count
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;
202  } else {
203  it->second.error_count = 0;
204  }
205  it->second.last_time = time;
206  }
207 }
208 
209 template <typename SensorType>
211  std::lock_guard<std::mutex> lock(sensor_data_mutex_);
212  sensor_data_.Clear();
213 }
214 
215 template <typename SensorType>
216 std::condition_variable *MessageManager<SensorType>::GetMutableCVar() {
217  return &cvar_;
218 }
219 
220 template <typename SensorType>
222  SensorType *const sensor_data) {
223  if (sensor_data == nullptr) {
224  AERROR << "Failed to get sensor_data due to nullptr.";
225  return ErrorCode::CANBUS_ERROR;
226  }
227  std::lock_guard<std::mutex> lock(sensor_data_mutex_);
228  sensor_data->CopyFrom(sensor_data_);
229  return ErrorCode::OK;
230 }
231 
232 template <typename SensorType>
234  for (auto &protocol_data : send_protocol_data_) {
235  if (protocol_data == nullptr) {
236  AERROR << "Invalid protocol data.";
237  } else {
238  protocol_data->Reset();
239  }
240  }
241 }
242 
243 } // namespace canbus
244 } // namespace drivers
245 } // namespace apollo
::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
Defines the Byte class.
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