36 #include "modules/common/proto/error_code.pb.h" 54 template <
typename SensorType>
90 ::apollo::common::ErrorCode
Start();
98 void RecvThreadFunc();
100 int32_t
Start(
bool is_blocked);
103 std::atomic<bool> is_running_ = {
false};
107 bool enable_log_ =
false;
108 bool is_init_ =
false;
109 std::future<void> async_result_;
114 template <
typename SensorType>
118 can_client_ = can_client;
119 pt_manager_ = pt_manager;
120 enable_log_ = enable_log;
121 if (can_client_ ==
nullptr) {
122 AERROR <<
"Invalid can client.";
123 return ::apollo::common::ErrorCode::CANBUS_ERROR;
125 if (pt_manager_ ==
nullptr) {
126 AERROR <<
"Invalid protocol manager.";
127 return ::apollo::common::ErrorCode::CANBUS_ERROR;
133 template <
typename SensorType>
135 AINFO <<
"Can client receiver thread starts.";
136 CHECK_NOTNULL(can_client_);
137 CHECK_NOTNULL(pt_manager_);
139 int32_t receive_error_count = 0;
140 int32_t receive_none_count = 0;
141 const int32_t ERROR_COUNT_MAX = 10;
142 auto default_period = 10 * 1000;
145 std::vector<CanFrame> buf;
147 if (can_client_->
Receive(&buf, &frame_num) !=
149 LOG_IF_EVERY_N(ERROR, receive_error_count++ > ERROR_COUNT_MAX,
151 <<
"Received " << receive_error_count <<
" error messages.";
152 cyber::USleep(default_period);
155 receive_error_count = 0;
157 if (buf.size() !=
static_cast<size_t>(frame_num)) {
158 AERROR_EVERY(100) <<
"Receiver buf size [" << buf.size()
159 <<
"] does not match can_client returned length[" 160 << frame_num <<
"].";
163 if (frame_num == 0) {
164 LOG_IF_EVERY_N(ERROR, receive_none_count++ > ERROR_COUNT_MAX,
166 <<
"Received " << receive_none_count <<
" empty messages.";
167 cyber::USleep(default_period);
170 receive_none_count = 0;
172 for (
const auto &frame : buf) {
173 uint8_t len = frame.len;
174 uint32_t uid = frame.id;
175 const uint8_t *data = frame.data;
176 pt_manager_->Parse(uid, data, len);
178 ADEBUG <<
"recv_can_frame#" << frame.CanFrameString();
183 AINFO <<
"Can client receiver thread stopped.";
186 template <
typename SensorType>
188 return is_running_.load();
191 template <
typename SensorType>
193 if (is_init_ ==
false) {
194 return ::apollo::common::ErrorCode::CANBUS_ERROR;
196 is_running_.exchange(
true);
202 template <
typename SensorType>
205 AINFO <<
"Stopping can client receiver ...";
206 is_running_.exchange(
false);
207 async_result_.wait();
209 AINFO <<
"Can client receiver is not running.";
211 AINFO <<
"Can client receiver stopped [ok].";
const int32_t MAX_CAN_RECV_FRAME_LEN
Definition: canbus_consts.h:35
void Stop()
Stop the CAN receiver.
Definition: can_receiver.h:203
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Defines the CanFrame struct and CanClient interface.
The class which defines the CAN client to send and receive message.
Definition: can_client.h:92
The class of MessageManager.
#define AERROR_EVERY(freq)
Definition: log.h:86
#define ADEBUG
Definition: log.h:41
virtual apollo::common::ErrorCode Receive(std::vector< CanFrame > *const frames, int32_t *const frame_num)=0
Receive messages.
virtual ~CanReceiver()=default
Destructor.
bool IsRunning() const
Get the working status of this CAN receiver. To check if it is running.
Definition: can_receiver.h:187
common::ErrorCode Init(CanClient *can_client, MessageManager< SensorType > *pt_manager, bool enable_log)
Initialize by a CAN client, message manager.
Definition: can_receiver.h:115
CanReceiver()=default
Constructor.
#define AERROR
Definition: log.h:44
message manager manages protocols. It supports parse and can get protocol data by message id...
Definition: message_manager.h:68
bool OK()
Definition: state.h:44
#define AINFO
Definition: log.h:42
CAN receiver.
Definition: can_receiver.h:55
::apollo::common::ErrorCode Start()
Start the CAN receiver.
Definition: can_receiver.h:192