Apollo 11.0
自动驾驶开放平台
sim_control_manager.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 *****************************************************************************/
17
18#include "nlohmann/json.hpp"
19
20namespace apollo {
21namespace dreamview {
22
26using Json = nlohmann::json;
27
28SimControlManager::SimControlManager() {}
29
30std::string SimControlManager::Name() const {
31 return FLAGS_sim_control_module_name;
32}
33
35 auto *model_factory = DynamicModelFactory::Instance();
36 Json result = model_factory->RegisterDynamicModels();
37 return result;
38}
39
41 if (IsEnabled() && model_ptr_) {
42 model_ptr_->Reset();
43 }
44}
45
47 if (!current_dynamic_model_.empty()) {
48 if (!model_ptr_) {
49 auto *model_factory = DynamicModelFactory::Instance();
50 model_ptr_ = model_factory->GetModelType(current_dynamic_model_);
51 }
52 if (model_ptr_) {
53 model_ptr_->Stop();
54 }
55 }
56 return;
57}
58
59bool SimControlManager::AddDynamicModel(const std::string &dynamic_model_name) {
60 if (IsEnabled()) {
61 auto *model_factory = DynamicModelFactory::Instance();
62 return model_factory->RegisterDynamicModel(dynamic_model_name);
63 } else {
64 AERROR << "Sim control manager is not enabled! Can not download dynamic "
65 "model to local!";
66 return false;
67 }
68}
69
71 const std::string &dynamic_model_name) {
72 auto *model_factory = DynamicModelFactory::Instance();
73 auto next_model_ptr_ = model_factory->GetModelType(dynamic_model_name);
74 if (!next_model_ptr_) {
75 AERROR << "Can not get dynamic model to start.Use original dynamic model!";
76 return false;
77 }
79 model_ptr_ = next_model_ptr_;
80 next_model_ptr_ = nullptr;
81 model_ptr_->Start();
82 current_dynamic_model_ = dynamic_model_name;
83 return true;
84}
85
87 const std::string &dynamic_model_name) {
88 auto *model_factory = DynamicModelFactory::Instance();
89 return model_factory->UnregisterDynamicModel(dynamic_model_name);
90}
91
93 // enabled_ only performs switching function
94 if (!enabled_) {
95 enabled_ = true;
96 }
97}
98
99void SimControlManager::ReSetPoinstion(double x, double y, double heading) {
100 if (!IsEnabled() || !model_ptr_) {
101 AERROR << "Sim control is invalid,Failed to ReSetPoinstion!";
102 return;
103 }
104 model_ptr_->ReSetPoinstion(x, y, heading);
105 return;
106}
107
108void SimControlManager::Restart(double x, double y, double v, double a) {
109 // reset start point for dynamic model.
110 if (!IsEnabled() || !model_ptr_) {
111 AERROR << "Sim control is invalid,Failed to restart!";
112 return;
113 }
114 model_ptr_->Stop();
115 model_ptr_->Start(x, y, v, a);
116 return;
117}
118
120 // reset start point for dynamic model.
121 if (!IsEnabled() || !model_ptr_) {
122 AERROR << "Sim control is invalid,Failed to restart!";
123 return;
124 }
125 model_ptr_->Stop();
126 model_ptr_->Start();
127 return;
128}
129
130void SimControlManager::RunOnce() { model_ptr_->RunOnce(); }
131
133 if (enabled_) {
134 enabled_ = false;
136 std::system(FLAGS_sim_obstacle_stop_command.data());
137 model_ptr_ = nullptr;
138 current_dynamic_model_ = "";
139 }
140}
141
142} // namespace dreamview
143} // namespace apollo
virtual void Start()=0
Starts running the simulated control algorithm, e.g., publish simulated localization and chassis mess...
virtual void Stop()=0
Stops the algorithm.
virtual void Reset()=0
Resets the internal state.
virtual void ReSetPoinstion(double x, double y, double heading)=0
Set vehicle position.
virtual void RunOnce()=0
Main logic of the simulated control algorithm.
bool DeleteDynamicModel(const std::string &dynamic_model_name)
bool AddDynamicModel(const std::string &dynamic_model_name)
bool ChangeDynamicModel(const std::string &dynamic_model_name)
void ReSetPoinstion(double x, double y, double heading)
#define AERROR
Definition log.h:44
nlohmann::json Json
class register implement
Definition arena_queue.h:37