Apollo 11.0
自动驾驶开放平台
apollo::drivers::canbus::CanAgent类 参考
apollo::drivers::canbus::CanAgent 的协作图:

Public 成员函数

 CanAgent (TestCanParam *param_ptr)
 
TestCanParam * param_ptr ()
 
CanAgent * other_agent ()
 
bool Start ()
 
void SendThreadFunc ()
 
void AddOtherAgent (CanAgent *agent)
 
bool is_receiving () const
 
void is_receiving (bool val)
 
bool is_sending_finish () const
 
void is_sending_finish (bool val)
 
void RecvThreadFunc ()
 
void WaitForFinish ()
 

详细描述

在文件 can_client_tool.cc 第 75 行定义.

构造及析构函数说明

◆ CanAgent()

apollo::drivers::canbus::CanAgent::CanAgent ( TestCanParam *  param_ptr)
inlineexplicit

在文件 can_client_tool.cc 第 77 行定义.

77: param_ptr_(param_ptr) {}

成员函数说明

◆ AddOtherAgent()

void apollo::drivers::canbus::CanAgent::AddOtherAgent ( CanAgent *  agent)
inline

在文件 can_client_tool.cc 第 169 行定义.

169{ other_agent_ = agent; }

◆ is_receiving() [1/2]

bool apollo::drivers::canbus::CanAgent::is_receiving ( ) const
inline

在文件 can_client_tool.cc 第 171 行定义.

171{ return is_receiving_; }

◆ is_receiving() [2/2]

void apollo::drivers::canbus::CanAgent::is_receiving ( bool  val)
inline

在文件 can_client_tool.cc 第 173 行定义.

173{ is_receiving_ = val; }

◆ is_sending_finish() [1/2]

bool apollo::drivers::canbus::CanAgent::is_sending_finish ( ) const
inline

在文件 can_client_tool.cc 第 175 行定义.

175{ return is_sending_finish_; }

◆ is_sending_finish() [2/2]

void apollo::drivers::canbus::CanAgent::is_sending_finish ( bool  val)
inline

在文件 can_client_tool.cc 第 177 行定义.

177{ is_sending_finish_ = val; }

◆ other_agent()

CanAgent * apollo::drivers::canbus::CanAgent::other_agent ( )
inline

在文件 can_client_tool.cc 第 81 行定义.

81{ return other_agent_; }

◆ param_ptr()

TestCanParam * apollo::drivers::canbus::CanAgent::param_ptr ( )
inline

在文件 can_client_tool.cc 第 79 行定义.

79{ return param_ptr_; }

◆ RecvThreadFunc()

void apollo::drivers::canbus::CanAgent::RecvThreadFunc ( )
inline

在文件 can_client_tool.cc 第 179 行定义.

179 {
180 using common::ErrorCode;
181 AINFO << "Receive thread starting...";
182 TestCanParam *param = param_ptr();
183 CanClient *client = param->can_client;
184 int64_t start = 0;
185 std::vector<CanFrame> buf;
186
187 bool first = true;
188 while (!other_agent()->is_sending_finish()) {
189 is_receiving(true);
190 int32_t len = MAX_CAN_RECV_FRAME_LEN;
191 ErrorCode ret = client->Receive(&buf, &len);
192 if (len == 0) {
193 AINFO << "recv frame:0";
194 continue;
195 }
196 if (first) {
197 start = Time::Now().ToMicrosecond();
198 first = false;
199 }
200 if (ret != ErrorCode::OK || len == 0) {
201 // AINFO << "channel:" << param->conf.channel_id()
202 // << ", recv frame:failed, code:" << ret;
203 AINFO << "recv error:" << ret;
204 continue;
205 }
206 for (int32_t i = 0; i < len; ++i) {
207 param->recv_cnt = param->recv_cnt + 1;
208 AINFO << "recv_frame#" << buf[i].CanFrameString()
209 << " conf:" << param->conf.ShortDebugString()
210 << ",recv_cnt: " << param->recv_cnt;
211 }
212 }
213 int64_t end = Time::Now().ToMicrosecond();
214 param->recv_time = static_cast<int32_t>(end - start);
215 AINFO << "Recv thread stopping..., conf:" << param->conf.ShortDebugString();
216 return;
217 }
uint64_t ToMicrosecond() const
convert time to microsecond (us).
Definition time.cc:85
static Time Now()
get the current time.
Definition time.cc:57
#define AINFO
Definition log.h:42
const int32_t MAX_CAN_RECV_FRAME_LEN

◆ SendThreadFunc()

void apollo::drivers::canbus::CanAgent::SendThreadFunc ( )
inline

在文件 can_client_tool.cc 第 97 行定义.

97 {
99 AINFO << "Send thread starting...";
100 TestCanParam *param = param_ptr();
101 CanClient *client = param->can_client;
102 std::vector<CanFrame> frames;
103 frames.resize(MAX_CAN_SEND_FRAME_LEN);
104
105 int32_t count = 0;
106 int32_t start_id = 0;
107 int32_t end_id = 0;
108 int32_t id = 0;
109 if (param->is_first_agent) {
110 start_id = 1;
111 end_id = 128;
112 } else {
113 start_id = 129;
114 end_id = start_id + 127;
115 }
116 id = start_id;
117 int32_t send_id = id;
118 AINFO << "port:" << param->conf.ShortDebugString()
119 << ", start_id:" << start_id << ", end_id:" << end_id;
120
121 // wait for other agent receiving is ok.
122 while (!other_agent()->is_receiving()) {
123 std::this_thread::yield();
124 }
125 int64_t start = Time::Now().ToMicrosecond();
126 while (true) {
127 // param->print();
128 if (count >= FLAGS_agent_mutual_send_frames) {
129 break;
130 }
131 for (int32_t i = 0; i < MAX_CAN_SEND_FRAME_LEN; ++i) {
132 // frames[i].id = id_count & 0x3FF;
133 send_id = id;
134 frames[i].id = id;
135 frames[i].len = 8;
136 frames[i].data[7] = static_cast<uint8_t>(count % 256);
137 for (uint8_t j = 0; j < 7; ++j) {
138 frames[i].data[j] = j;
139 }
140 ++count;
141 ++id;
142 if (id > end_id) {
143 id = start_id;
144 }
145 }
146 int32_t frame_num = MAX_CAN_SEND_FRAME_LEN;
147 if (client->Send(frames, &frame_num) != ErrorCode::OK) {
148 param->send_err_cnt += MAX_CAN_SEND_FRAME_LEN;
149 AERROR << "send_thread send msg failed!, id:" << send_id
150 << ", conf:" << param->conf.ShortDebugString();
151 } else {
152 param->send_cnt += frame_num;
153 param->send_lost_cnt += MAX_CAN_SEND_FRAME_LEN - frame_num;
154 AINFO << "send_frames: " << frame_num << "send_frame#"
155 << frames[0].CanFrameString()
156 << " send lost:" << MAX_CAN_SEND_FRAME_LEN - frame_num
157 << ", conf:" << param->conf.ShortDebugString();
158 }
159 }
160 int64_t end = Time::Now().ToMicrosecond();
161 param->send_time = static_cast<int32_t>(end - start);
162 // In case for finish too quick to receiver miss some msg
163 sleep(2);
164 AINFO << "Send thread stopping..." << param->conf.ShortDebugString();
165 is_sending_finish(true);
166 return;
167 }
#define AERROR
Definition log.h:44
const int32_t MAX_CAN_SEND_FRAME_LEN

◆ Start()

bool apollo::drivers::canbus::CanAgent::Start ( )
inline

在文件 can_client_tool.cc 第 83 行定义.

83 {
84 thread_recv_.reset(new std::thread([this] { RecvThreadFunc(); }));
85 if (thread_recv_ == nullptr) {
86 AERROR << "Unable to create recv thread.";
87 return false;
88 }
89 thread_send_.reset(new std::thread([this] { SendThreadFunc(); }));
90 if (thread_send_ == nullptr) {
91 AERROR << "Unable to create send thread.";
92 return false;
93 }
94 return true;
95 }

◆ WaitForFinish()

void apollo::drivers::canbus::CanAgent::WaitForFinish ( )
inline

在文件 can_client_tool.cc 第 219 行定义.

219 {
220 if (thread_send_ != nullptr && thread_send_->joinable()) {
221 thread_send_->join();
222 thread_send_.reset();
223 AINFO << "Send thread stopped. conf:"
224 << param_ptr_->conf.ShortDebugString();
225 }
226 if (thread_recv_ != nullptr && thread_recv_->joinable()) {
227 thread_recv_->join();
228 thread_recv_.reset();
229 AINFO << "Recv thread stopped. conf:"
230 << param_ptr_->conf.ShortDebugString();
231 }
232 }

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