Apollo 10.0
自动驾驶开放平台
dreamview.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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
18
19#include <vector>
20
21#include "cyber/common/file.h"
22#include "cyber/time/clock.h"
25namespace {
26std::map<std::string, int> plugin_function_map = {
27 {"UpdateRecordToStatus", 1},
28 {"UpdateDynamicModelToStatus", 2},
29 {"UpdateVehicleToStatus", 3},
30 {"UpdateMapToStatus", 4}};
31std::map<std::string, int> hmi_function_map = {
32 {"MapServiceReloadMap", 1},
33 // {"RestartDynamicModel", 6},
34 {"GetDataHandlerConf", 7},
35 {"ClearDataHandlerConfChannelMsgs", 8},
36};
37std::map<std::string, int> socket_manager_function_map = {
38 {"SimControlRestart", 0},
39 {"MapServiceReloadMap", 1},
40};
41} // namespace
42
43namespace apollo {
44namespace dreamview {
45
49
51
52void Dreamview::TerminateProfilingMode() {
53 Stop();
54 AWARN << "Profiling timer called shutdown!";
55}
56
59
60 if (FLAGS_dreamview_profiling_mode &&
61 FLAGS_dreamview_profiling_duration > 0.0) {
62 exit_timer_.reset(new cyber::Timer(
63 FLAGS_dreamview_profiling_duration,
64 [this]() { this->TerminateProfilingMode(); }, false));
65
66 exit_timer_->Start();
67 AWARN << "============================================================";
68 AWARN << "| Dreamview running in profiling mode, exit in "
69 << FLAGS_dreamview_profiling_duration << " seconds |";
70 AWARN << "============================================================";
71 }
72
73 // Initialize and run the web server which serves the dreamview htmls and
74 // javascripts and handles websocket requests.
75 std::vector<std::string> options = {
76 "document_root", FLAGS_static_file_dir,
77 "listening_ports", FLAGS_server_ports,
78 "websocket_timeout_ms", FLAGS_websocket_timeout_ms,
79 "request_timeout_ms", FLAGS_request_timeout_ms,
80 "enable_keep_alive", "yes",
81 "tcp_nodelay", "1",
82 "keep_alive_timeout_ms", "500"};
83 if (PathExists(FLAGS_ssl_certificate)) {
84 options.push_back("ssl_certificate");
85 options.push_back(FLAGS_ssl_certificate);
86 } else if (FLAGS_ssl_certificate.size() > 0) {
87 AERROR << "Certificate file " << FLAGS_ssl_certificate
88 << " does not exist!";
89 }
90 server_.reset(new CivetServer(options));
91
92 websocket_.reset(new WebSocketHandler("websocket"));
93 map_ws_.reset(new WebSocketHandler("Map"));
94 point_cloud_ws_.reset(new WebSocketHandler("PointCloud"));
95 camera_ws_.reset(new WebSocketHandler("Camera"));
96 plugin_ws_.reset(new WebSocketHandler("Plugin"));
97 hmi_ws_.reset(new WebSocketHandler("HMI"));
98 sim_world_ws_.reset(new WebSocketHandler("SimWorld"));
99 obstacle_ws_.reset(new WebSocketHandler("Obstacle"));
100 // auto registered when dreamview start to publish specified channels info
101 channels_info_ws_.reset(new WebSocketHandler("ChannelsInfo"));
102 map_service_.reset(new MapService());
103 image_.reset(new ImageHandler());
104 proto_handler_.reset(new ProtoHandler());
105 perception_camera_updater_.reset(
106 new PerceptionCameraUpdater(camera_ws_.get()));
107 hmi_.reset(new HMI(websocket_.get(), map_service_.get(), hmi_ws_.get()));
108 plugin_manager_.reset(new PluginManager(plugin_ws_.get()));
109 sim_world_updater_.reset(new SimulationWorldUpdater(
110 websocket_.get(), map_ws_.get(), plugin_ws_.get(), map_service_.get(),
111 plugin_manager_.get(), sim_world_ws_.get(), hmi_.get(),
112 FLAGS_routing_from_file));
113 point_cloud_updater_.reset(new PointCloudUpdater(point_cloud_ws_.get()));
114 map_updater_.reset(new MapUpdater(map_ws_.get(), map_service_.get()));
115 obstacle_updater_.reset(new ObstacleUpdater(obstacle_ws_.get()));
116 channels_info_updater_.reset(new ChannelsUpdater(channels_info_ws_.get()));
117 updater_manager_.reset(new UpdaterManager());
119 dv_plugin_manager_.reset(
120 new DvPluginManager(server_.get(), updater_manager_.get()));
121 server_->addWebSocketHandler("/websocket", *websocket_);
122 server_->addWebSocketHandler("/map", *map_ws_);
123 server_->addWebSocketHandler("/pointcloud", *point_cloud_ws_);
124 server_->addWebSocketHandler("/camera", *camera_ws_);
125 server_->addWebSocketHandler("/plugin", *plugin_ws_);
126 server_->addWebSocketHandler("/simworld", *sim_world_ws_);
127 server_->addWebSocketHandler("/hmi", *hmi_ws_);
128 server_->addWebSocketHandler("/socketmanager", *socket_manager_ws_);
129 server_->addWebSocketHandler("/obstacle", *obstacle_ws_);
130 server_->addWebSocketHandler("/channelsinfo", *channels_info_ws_);
131 server_->addHandler("/image", *image_);
132 server_->addHandler("/proto", *proto_handler_);
133 dv_plugin_manager_->Init();
134 socket_manager_.reset(new SocketManager(
135 websocket_.get(), updater_manager_.get(), dv_plugin_manager_.get()));
136#if WITH_TELEOP == 1
137 teleop_ws_.reset(new WebSocketHandler("Teleop"));
138 teleop_.reset(new TeleopService(teleop_ws_.get()));
139#endif
140 return Status::OK();
141}
142
144 hmi_->Start([this](const std::string& function_name,
145 const nlohmann::json& param_json) -> nlohmann::json {
146 nlohmann::json ret = HMICallbackOtherService(function_name, param_json);
147 ADEBUG << "ret: " << ret.dump();
148 return ret;
149 });
150 plugin_manager_->Start([this](const std::string& function_name,
151 const nlohmann::json& param_json) -> bool {
152 return PluginCallbackHMI(function_name, param_json);
153 });
154 dv_plugin_manager_->Start();
155 // sim_world_updater_->StartStream(100);
156 // perception_camera_updater_->Start();
157 // ([this](const std::string& param_string) -> bool {
158 // return PerceptionCameraCallback(param_string);
159 // });
160#if WITH_TELEOP == 1
161 teleop_->Start();
162#endif
163 return Status::OK();
164}
165
166void Dreamview::Stop() {
167 dv_plugin_manager_->Stop();
168 server_->close();
169 SimControlManager::Instance()->Stop();
170 point_cloud_updater_->Stop();
171 hmi_->Stop();
172 perception_camera_updater_->Stop();
173 obstacle_updater_->Stop();
174 plugin_manager_->Stop();
175 // sim_world_updater_->StopStream();
176}
177
178nlohmann::json Dreamview::HMICallbackOtherService(
179 const std::string& function_name, const nlohmann::json& param_json) {
180 nlohmann::json callback_res = {};
181 callback_res["result"] = false;
182 if (hmi_function_map.find(function_name) == hmi_function_map.end()) {
183 AERROR << "Donnot support this callback";
184 return callback_res;
185 }
186 switch (hmi_function_map[function_name]) {
187 case 1: {
188 callback_res["result"] = map_service_->ReloadMap(true);
189 break;
190 }
191 // todo(@lijin):reconstruct map and sim control set point logic
192 // case 6: {
193 // // restart dynamic model
194 // map_service_->ReloadMap(true);
195 // sim_control_manager_->Restart();
196 // callback_res["result"] = true;
197 // break;
198 // }
199 // use socket manager to broadcast data handler conf after play record
200 case 7: {
201 socket_manager_->BrocastDataHandlerConf();
202 callback_res["result"] = true;
203 break;
204 }
205 case 8: {
206 socket_manager_->BrocastDataHandlerConf(true);
207 callback_res["result"] = true;
208 break;
209 }
210 default:
211 break;
212 }
213 return callback_res;
214}
215
216bool Dreamview::PluginCallbackHMI(const std::string& function_name,
217 const nlohmann::json& param_json) {
218 bool callback_res;
219 if (plugin_function_map.find(function_name) == plugin_function_map.end()) {
220 AERROR << "Donnot support this callback";
221 return false;
222 }
223 switch (plugin_function_map[function_name]) {
224 case 1: {
225 callback_res = hmi_->UpdateRecordToStatus();
226 } break;
227 case 2: {
228 if (param_json["data"].contains("dynamic_model_name")) {
229 const std::string dynamic_model_name =
230 param_json["data"]["dynamic_model_name"];
231 if (!dynamic_model_name.empty()) {
232 callback_res = hmi_->UpdateDynamicModelToStatus(dynamic_model_name);
233 }
234 }
235 } break;
236 case 3: {
237 callback_res = hmi_->UpdateVehicleToStatus();
238 } break;
239 case 4: {
240 if (param_json["data"].contains("resource_id") &&
241 param_json["data"].contains("resource_id")) {
242 const std::string map_name = param_json["data"]["resource_id"];
243 callback_res = hmi_->UpdateMapToStatus(map_name);
244 } else {
245 callback_res = hmi_->UpdateMapToStatus();
246 }
247 } break;
248 default:
249 break;
250 }
251 return callback_res;
252}
253
254// bool Dreamview::PerceptionCameraCallback(const std::string& param_string) {
255// bool callback_res = false;
256// callback_res = hmi_->UpdateCameraChannelToStatus(param_string);
257// return callback_res;
258// }
259
260bool Dreamview::PointCloudCallback(const std::string& param_string) {
261 bool callback_res = false;
262 callback_res = hmi_->UpdatePointChannelToStatus(param_string);
263 return callback_res;
264}
265
267 updater_manager_->RegisterUpdater("simworld", sim_world_updater_.get());
268 updater_manager_->RegisterUpdater("hmistatus", hmi_.get());
269 updater_manager_->RegisterUpdater("camera", perception_camera_updater_.get());
270 updater_manager_->RegisterUpdater("pointcloud", point_cloud_updater_.get());
271 updater_manager_->RegisterUpdater("map", map_updater_.get());
272 updater_manager_->RegisterUpdater("obstacle", obstacle_updater_.get());
273 updater_manager_->RegisterUpdater("channelsinfo",
274 channels_info_updater_.get());
275}
276
277} // namespace dreamview
278} // namespace apollo
A module that collects camera image and localization (by collecting localization & static transforms)...
A module that maintains all dv-related plugin information.
A general class to denote the return status of an API call.
Definition status.h:43
@Brief This is a helper class that can load vehicle configurations.
static void Init()
Initialize vehicle configurations with default configuration file pointed by gflags FLAGS_vehicle_con...
apollo::common::Status Init()
Definition dreamview.cc:54
apollo::common::Status Start()
Definition dreamview.cc:124
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
bool PathExists(const std::string &path)
Check if the path exists.
Definition file.cc:195
class register implement
Definition arena_queue.h:37