Apollo 11.0
自动驾驶开放平台
grpc_client.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
23
24#include <chrono>
25
26#include "cyber/common/log.h"
27#include "cyber/cyber.h"
28
29namespace apollo {
30namespace v2x {
31
32using ::apollo::v2x::CarStatus;
33using ::apollo::v2x::UpdateStatus;
34using ::apollo::v2x::obu::ObuTrafficLight;
35using grpc::Channel;
36using grpc::ClientContext;
37using grpc::Status;
38
39GrpcClientImpl::GrpcClientImpl(std::shared_ptr<Channel> channel)
40 : stub_(::apollo::v2x::CarToObu::NewStub(channel)) {
41 AINFO << "GrpcClientImpl initial success";
42 car_status_tv_nsec_ = (1000000000 / FLAGS_v2x_car_status_timer_frequency / 2);
43 init_flag_ = true;
44}
45
46void GrpcClientImpl::SendMsgToGrpc(const std::shared_ptr<CarStatus> &msg) {
47 if (!msg->has_localization()) {
48 return;
49 }
50 // set timeout
51 ClientContext context;
52 gpr_timespec timespec;
53 timespec.tv_sec = 0;
54 timespec.tv_nsec = car_status_tv_nsec_; // 80000000; // 80ms
55 timespec.clock_type = GPR_TIMESPAN;
56 context.set_deadline(timespec);
57 UpdateStatus response;
58
59 // time used statistics
60 auto start = std::chrono::steady_clock::now();
61 Status status = stub_->PushCarStatus(&context, *msg, &response);
62 auto end = std::chrono::steady_clock::now();
63 std::chrono::duration<double> time_used = end - start;
64
65 // response check: error_code 4: time out; 0: success;
66 AINFO << "stub PushCarStatus Time used: " << time_used.count() * 1000
67 << " ms";
68}
69
70} // namespace v2x
71} // namespace apollo
GrpcClientImpl(std::shared_ptr< grpc::Channel > channel)
void SendMsgToGrpc(const std::shared_ptr<::apollo::v2x::CarStatus > &msg)
define v2x proxy module and onboard unit interface grpc implement
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37