Apollo 10.0
自动驾驶开放平台
apollo::drivers::canbus::can::HermesCanClient类 参考

The class which defines a BCAN client which inherits CanClient. 更多...

#include <hermes_can_client.h>

类 apollo::drivers::canbus::can::HermesCanClient 继承关系图:
apollo::drivers::canbus::can::HermesCanClient 的协作图:

Public 成员函数

virtual ~HermesCanClient ()
 Initialize the BCAN client by specified CAN card parameters.
 
bool Init (const CANCardParameter &parameter) override
 Start the ESD CAN client.
 
apollo::common::ErrorCode Start () override
 Start the ESD CAN client.
 
virtual void Stop ()
 Stop the ESD CAN client.
 
virtual apollo::common::ErrorCode Send (const std::vector< CanFrame > &frames, int32_t *const frame_num)
 Send messages
 
virtual apollo::common::ErrorCode Receive (std::vector< CanFrame > *const frames, int32_t *const frame_num)
 Receive messages
 
virtual std::string GetErrorString (const int32_t status)
 Get the error string.
 
void SetInited (bool init)
 Set inited status.
 
- Public 成员函数 继承自 apollo::drivers::canbus::CanClient
 CanClient ()=default
 Constructor
 
virtual ~CanClient ()=default
 Destructor
 
virtual apollo::common::ErrorCode SendSingleFrame (const std::vector< CanFrame > &frames)
 Send a single message.
 

额外继承的成员函数

- Protected 属性 继承自 apollo::drivers::canbus::CanClient
bool is_started_ = false
 The CAN client is started.
 

详细描述

The class which defines a BCAN client which inherits CanClient.

在文件 hermes_can_client.h46 行定义.

构造及析构函数说明

◆ ~HermesCanClient()

apollo::drivers::canbus::can::HermesCanClient::~HermesCanClient ( )
virtual

Initialize the BCAN client by specified CAN card parameters.

参数
parameterCAN card parameters to initialize the CAN client.

Destructor

在文件 hermes_can_client.cc31 行定义.

31 {
32 if (dev_handler_) {
33 Stop();
34 }
35}
virtual void Stop()
Stop the ESD CAN client.

成员函数说明

◆ GetErrorString()

std::string apollo::drivers::canbus::can::HermesCanClient::GetErrorString ( const int32_t  status)
virtual

Get the error string.

参数
statusThe status to get the error string.

实现了 apollo::drivers::canbus::CanClient.

在文件 hermes_can_client.cc186 行定义.

186{ return ""; }

◆ Init()

bool apollo::drivers::canbus::can::HermesCanClient::Init ( const CANCardParameter parameter)
overridevirtual

Start the ESD CAN client.

返回
The status of the start action which is defined by apollo::common::ErrorCode.

实现了 apollo::drivers::canbus::CanClient.

在文件 hermes_can_client.cc37 行定义.

37 {
38 if (!parameter.has_channel_id()) {
39 AERROR << "Init CAN failed: parameter does not have channel id. The "
40 "parameter is "
41 << parameter.DebugString();
42 return false;
43 }
44 port_ = parameter.channel_id();
45 auto num_ports = parameter.num_ports();
46 if (port_ > static_cast<int32_t>(num_ports) || port_ < 0) {
47 AERROR << "Can port number [" << port_ << "] is out of bound [0,"
48 << num_ports << ")";
49 return false;
50 }
51
52 return true;
53}
#define AERROR
Definition log.h:44

◆ Receive()

apollo::common::ErrorCode apollo::drivers::canbus::can::HermesCanClient::Receive ( std::vector< CanFrame > *const  frames,
int32_t *const  frame_num 
)
virtual

Receive messages

参数
framesThe messages to receive.
frame_numThe amount of messages to receive.
返回
The status of the receiving action which is defined by apollo::common::ErrorCode.

实现了 apollo::drivers::canbus::CanClient.

在文件 hermes_can_client.cc146 行定义.

147 {
148 if (!is_init_) {
149 AERROR << "Hermes can client is not init! Please init first!";
150 return ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED;
151 }
152 if (*frame_num > MAX_CAN_RECV_FRAME_LEN || *frame_num < 0) {
153 AERROR << "recv can frame num not in range[0, " << MAX_CAN_RECV_FRAME_LEN
154 << "], frame_num:" << *frame_num;
155 return ErrorCode::CAN_CLIENT_ERROR_FRAME_NUM;
156 }
157
158 int32_t ret = bcan_recv(dev_handler_, _recv_frames, *frame_num);
159 // don't log timeout
160 if (ret == RX_TIMEOUT) {
161 *frame_num = 0;
162 return ErrorCode::OK;
163 }
164 if (ret < 0) {
165 int ret_rece_error = bcan_get_status(dev_handler_);
166 AERROR << "receive message failed, error code:" << ret
167 << "receive error:" << ret_rece_error;
168 return ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED;
169 }
170 *frame_num = ret;
171
172 // is ret num is equal *frame_num?
173 for (int i = 0; i < *frame_num; ++i) {
174 CanFrame cf;
175 cf.id = _recv_frames[i].bcan_msg_id;
176 cf.len = _recv_frames[i].bcan_msg_datalen;
177 cf.timestamp.tv_sec = _recv_frames[i].bcan_msg_timestamp.tv_sec;
178 cf.timestamp.tv_usec = _recv_frames[i].bcan_msg_timestamp.tv_usec;
179 memcpy(cf.data, _recv_frames[i].bcan_msg_data, cf.len);
180 frames->push_back(cf);
181 }
182
183 return ErrorCode::OK;
184}
int bcan_recv(bcan_hdl_t hdl, bcan_msg_t *buf, uint32_t num_msg)
int bcan_get_status(bcan_hdl_t hdl)
const int32_t MAX_CAN_RECV_FRAME_LEN
unsigned int bcan_msg_id
Definition bcan_defs.h:29
struct timeval bcan_msg_timestamp
Definition bcan_defs.h:33
unsigned char bcan_msg_datalen
Definition bcan_defs.h:30

◆ Send()

apollo::common::ErrorCode apollo::drivers::canbus::can::HermesCanClient::Send ( const std::vector< CanFrame > &  frames,
int32_t *const  frame_num 
)
virtual

Send messages

参数
framesThe messages to send.
frame_numThe amount of messages to send.
返回
The status of the sending action which is defined by apollo::common::ErrorCode.

实现了 apollo::drivers::canbus::CanClient.

在文件 hermes_can_client.cc101 行定义.

102 {
103 /*
104 typedef struct bcan_msg {
105 uint32_t bcan_msg_id; // source CAN node id
106 uint8_t bcan_msg_datalen; // message data length
107 uint8_t bcan_msg_rsv[3]; // reserved
108 uint8_t bcan_msg_data[8]; // message data
109 uint64_t bcan_msg_timestamp; // TBD
110 } bcan_msg_t;
111 */
112 CHECK_NOTNULL(frame_num);
113 CHECK_EQ(frames.size(), static_cast<size_t>(*frame_num));
114
115 if (!is_init_) {
116 AERROR << "Hermes can client is not init! Please init first!";
117 return ErrorCode::CAN_CLIENT_ERROR_SEND_FAILED;
118 }
119 // if (*frame_num > MAX_CAN_SEND_FRAME_LEN || *frame_num < 0) {
120 // AERROR << "send can frame num not in range[0, "
121 // << MAX_CAN_SEND_FRAME_LEN << "], frame_num:" << *frame_num;
122 // return ErrorCode::CAN_CLIENT_ERROR_FRAME_NUM;
123 // }
124 for (int i = 0; i < *frame_num; ++i) {
125 _send_frames[i].bcan_msg_id = frames[i].id;
126 _send_frames[i].bcan_msg_datalen = frames[i].len;
127 memcpy(_send_frames[i].bcan_msg_data, frames[i].data, frames[i].len);
128 }
129
130 // Synchronous transmission of CAN messages
131 int32_t send_num = *frame_num;
132 int32_t ret = bcan_send(dev_handler_, _send_frames, send_num);
133 if (ret < 0) {
134 int ret_send_error = bcan_get_status(dev_handler_);
135 AERROR << "send message failed, error code: " << ret
136 << ", send error: " << ret_send_error;
137 return ErrorCode::CAN_CLIENT_ERROR_SEND_FAILED;
138 }
139 *frame_num = ret;
140 return ErrorCode::OK;
141}
int bcan_send(bcan_hdl_t hdl, bcan_msg_t *buf, uint32_t num_msg)

◆ SetInited()

void apollo::drivers::canbus::can::HermesCanClient::SetInited ( bool  init)

Set inited status.

参数
ifstatus is inited.

在文件 hermes_can_client.cc188 行定义.

188{ is_init_ = init; }

◆ Start()

ErrorCode apollo::drivers::canbus::can::HermesCanClient::Start ( )
overridevirtual

Start the ESD CAN client.

返回
The status of the start action which is defined by apollo::common::ErrorCode.

实现了 apollo::drivers::canbus::CanClient.

在文件 hermes_can_client.cc55 行定义.

55 {
56 if (is_init_) {
57 return ErrorCode::OK;
58 }
59
60 // open device
61 int32_t ret = bcan_open(port_, 0,
62 5, // 5ms for rx timeout
63 5, // 5ms for tx timeout
64 &dev_handler_);
65
66 if (ret != ErrorCode::OK) {
67 AERROR << "Open device error code: " << ret << ", channel id: " << port_;
68 return ErrorCode::CAN_CLIENT_ERROR_BASE;
69 }
70 AINFO << "Open device success, channel id: " << port_;
71
72 // 1. set baudrate to 500k
73 ret = bcan_set_baudrate(dev_handler_, BCAN_BAUDRATE_500K);
74 if (ret != ErrorCode::OK) {
75 AERROR << "Set baudrate error Code: " << ret;
76 return ErrorCode::CAN_CLIENT_ERROR_BASE;
77 }
78
79 // 2. start receive
80 ret = bcan_start(dev_handler_);
81 if (ret != ErrorCode::OK) {
82 AERROR << "Start hermes can card failed: " << ret;
83 return ErrorCode::CAN_CLIENT_ERROR_BASE;
84 }
85
86 is_init_ = true;
87 return ErrorCode::OK;
88}
int bcan_set_baudrate(bcan_hdl_t hdl, uint32_t rate)
@ BCAN_BAUDRATE_500K
Definition bcan.h:52
int bcan_open(uint32_t dev_index, uint32_t flags, uint64_t tx_to, uint64_t rx_to, bcan_hdl_t *hdl)
int bcan_start(bcan_hdl_t hdl)
#define AINFO
Definition log.h:42

◆ Stop()

void apollo::drivers::canbus::can::HermesCanClient::Stop ( )
virtual

Stop the ESD CAN client.

实现了 apollo::drivers::canbus::CanClient.

在文件 hermes_can_client.cc90 行定义.

90 {
91 if (is_init_) {
92 is_init_ = false;
93 int32_t ret = bcan_close(dev_handler_);
94 if (ret != ErrorCode::OK) {
95 AERROR << "close error code: " << ret;
96 }
97 }
98}
int bcan_close(bcan_hdl_t hdl)

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