Apollo  6.0
Open source self driving car software
alignment_agent.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 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 #pragma once
17 
18 #include <chrono>
19 #include <memory>
20 #include <thread>
21 #include <vector>
22 
23 #include "grpc++/grpc++.h"
24 
25 #include "modules/map/tools/map_datachecker/proto/collection_error_code.pb.h"
26 #include "modules/map/tools/map_datachecker/proto/collection_service.pb.h"
30 
31 namespace apollo {
32 namespace hdmap {
33 
35 
36 template <typename ALIGNMENT_TYPE, typename REQUEST_TYPE,
37  typename RESPONSE_TYPE>
39  public:
41  std::shared_ptr<JsonConf> sp_conf,
42  std::shared_ptr<PoseCollectionAgent> sp_pose_collection_agent) {
43  sp_conf_ = sp_conf;
44  sp_pose_collection_agent_ = sp_pose_collection_agent;
45  }
46 
47  void Reset() {
48  sp_alignment_ = std::make_shared<ALIGNMENT_TYPE>(sp_conf_);
50  need_stop_ = false;
51  }
52 
53  grpc::Status ProcessGrpcRequest(grpc::ServerContext *context,
54  REQUEST_TYPE *request,
55  RESPONSE_TYPE *response) {
56  AINFO << "AlignmentAgent request: " << request->DebugString();
57  switch (request->cmd()) {
58  case CmdType::START:
59  AINFO << "AlignmentAgent start";
60  AlignmentStart(request, response);
61  break;
62  case CmdType::CHECK:
63  AINFO << "AlignmentAgent check";
64  AlignmentCheck(request, response);
65  break;
66  case CmdType::STOP:
67  AINFO << "AlignmentAgent stop";
68  AlignmentStop(request, response);
69  break;
70  default:
71  response->set_code(ErrorCode::ERROR_REQUEST);
72  response->set_progress(sp_alignment_->GetProgress());
73  AERROR << "command error";
74  }
75  AINFO << "AlignmentAgent progress: " << response->progress();
76  return grpc::Status::OK;
77  }
78 
79  int AlignmentStart(REQUEST_TYPE *request, RESPONSE_TYPE *response) {
81  AINFO << "AlignmentAgent is running. do need start again";
82  response->set_code(ErrorCode::ERROR_REPEATED_START);
83  response->set_progress(0.0);
84  return 0;
85  }
86  Reset();
88  response->set_code(ErrorCode::SUCCESS);
89  response->set_progress(0.0);
90  return 0;
91  }
92 
95  std::thread alignment_thread([=]() {
96  sp_alignment_->SetStartTime(UnixNow());
97  AINFO << "set state RUNNING";
98  while (!need_stop_ && !apollo::cyber::IsShutdown()) {
99  std::shared_ptr<std::vector<FramePose>> sp_poses = GetPoses();
100  if (sp_poses == nullptr) {
101  AINFO << "error, pose pointer is null";
102  return;
103  }
104  sp_alignment_->Process(*sp_poses);
105  ErrorCode code = sp_alignment_->GetReturnState();
106  if (code == ErrorCode::ERROR_VERIFY_NO_GNSSPOS ||
107  code == ErrorCode::ERROR_GNSS_SIGNAL_FAIL) {
108  AERROR << "Some error occurred, while loop will exit";
109  break;
110  }
111  AINFO << "get progress:" << sp_alignment_->GetProgress();
112  if (fabs(1 - sp_alignment_->GetProgress()) < 1e-8) {
113  AINFO << "alignment progress reached 1.0, thread exit";
114  break;
115  }
116  AINFO << "sleep " << sp_conf_->alignment_featch_pose_sleep << " sec";
117  auto seconds =
118  std::chrono::seconds(sp_conf_->alignment_featch_pose_sleep);
119  std::this_thread::sleep_for(seconds);
120  }
121  stopped_ = true;
122  AINFO << "Align thread complete";
123  });
124  alignment_thread.detach();
125  return 0;
126  }
127 
128  std::shared_ptr<std::vector<FramePose>> GetPoses() const {
129  if (sp_pose_collection_agent_ == nullptr) {
130  return nullptr;
131  }
132  return sp_pose_collection_agent_->GetPoses();
133  }
134 
135  int AlignmentCheck(REQUEST_TYPE *request, RESPONSE_TYPE *response) {
137  AINFO << "AlignmentAgent is idle. this call will be refused";
138  response->set_code(ErrorCode::ERROR_CHECK_BEFORE_START);
139  response->set_progress(0.0);
140  return 0;
141  }
142  if (sp_alignment_ == nullptr) {
143  AINFO << "sp_alignment_ is null, check later";
144  response->set_code(ErrorCode::SUCCESS);
145  response->set_progress(0.0);
146  return 0;
147  }
148 
149  ErrorCode code = sp_alignment_->GetReturnState();
150  double progress = sp_alignment_->GetProgress();
151  response->set_code(code);
152  response->set_progress(progress);
153  if (code == ErrorCode::ERROR_VERIFY_NO_GNSSPOS ||
154  code == ErrorCode::ERROR_GNSS_SIGNAL_FAIL) {
155  stopped_ = true;
157  return -1;
158  }
159  return 0;
160  }
161 
162  int AlignmentStop(REQUEST_TYPE *request, RESPONSE_TYPE *response) {
163  response->set_code(ErrorCode::SUCCESS);
164  if (sp_alignment_ == nullptr) {
165  response->set_progress(0.0);
166  } else {
167  response->set_progress(sp_alignment_->GetProgress());
168  need_stop_ = true;
169  }
171  return 0;
172  }
173 
174  void SetState(AlignmentAgentState state) { state_ = state; }
175  AlignmentAgentState GetState() const { return state_; }
176 
177  private:
178  std::shared_ptr<JsonConf> sp_conf_ = nullptr;
179  std::shared_ptr<ALIGNMENT_TYPE> sp_alignment_ = nullptr;
180  std::shared_ptr<PoseCollectionAgent> sp_pose_collection_agent_ = nullptr;
181  AlignmentAgentState state_;
182  bool need_stop_;
183  bool stopped_;
184 };
185 
186 } // namespace hdmap
187 } // namespace apollo
void SetState(AlignmentAgentState state)
Definition: alignment_agent.h:174
int AlignmentStop(REQUEST_TYPE *request, RESPONSE_TYPE *response)
Definition: alignment_agent.h:162
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
int AsyncStartAlignment()
Definition: alignment_agent.h:93
std::shared_ptr< std::vector< FramePose > > GetPoses() const
Definition: alignment_agent.h:128
AlignmentAgent(std::shared_ptr< JsonConf > sp_conf, std::shared_ptr< PoseCollectionAgent > sp_pose_collection_agent)
Definition: alignment_agent.h:40
bool IsShutdown()
Definition: state.h:46
int AlignmentStart(REQUEST_TYPE *request, RESPONSE_TYPE *response)
Definition: alignment_agent.h:79
int AlignmentCheck(REQUEST_TYPE *request, RESPONSE_TYPE *response)
Definition: alignment_agent.h:135
void Reset()
Definition: alignment_agent.h:47
State AlignmentAgentState
Definition: alignment_agent.h:34
State
Definition: common.h:56
grpc::Status ProcessGrpcRequest(grpc::ServerContext *context, REQUEST_TYPE *request, RESPONSE_TYPE *response)
Definition: alignment_agent.h:53
AlignmentAgentState GetState() const
Definition: alignment_agent.h:175
#define AERROR
Definition: log.h:44
bool OK()
Definition: state.h:44
#define AINFO
Definition: log.h:42
double UnixNow()
Definition: client_common.h:37
Definition: alignment_agent.h:38