Apollo 10.0
自动驾驶开放平台
map_updater.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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#include<memory>
17#include<string>
18#include<vector>
19#include "google/protobuf/util/json_util.h"
23
24#include "modules/common_msgs/localization_msgs/localization.pb.h"
25
26namespace apollo {
27
28namespace dreamview {
29
30using google::protobuf::util::JsonStringToMessage;
31using apollo::common::util::ContainsKey;
33
35 const MapService *map_service)
36 : node_(cyber::CreateNode("map_updater")),
37 websocket_(websocket),
38 map_service_(map_service) {
39 localization_reader_ =
41 FLAGS_localization_topic,
42 [this](
43 const std::shared_ptr<apollo::localization::LocalizationEstimate>
44 &msg) { UpdateAdcPosition(msg); });
45}
46
47void MapUpdater::UpdateAdcPosition(
48 const std::shared_ptr<apollo::localization::LocalizationEstimate>
49 &localization) {
50 const auto &pose = localization->pose();
51 adc_point_.set_x(pose.position().x() + map_service_->GetXOffset());
52 adc_point_.set_y(pose.position().y() + map_service_->GetYOffset());
53}
54
55void MapUpdater::PublishMessage(const std::string& channel_name) {
56 auto retrieved = map_service_->RetrieveMapElements(map_element_ids_);
57 retrieved.SerializeToString(&retrieved_map_string_);
58 StreamData stream_data;
59 std::string stream_data_string;
60 stream_data.set_action("stream");
61 stream_data.set_data_name("map");
62 std::vector<uint8_t> byte_data(retrieved_map_string_.begin(),
63 retrieved_map_string_.end());
64 stream_data.set_data(&(byte_data[0]), byte_data.size());
65 stream_data.set_type("map");
66 stream_data.SerializeToString(&stream_data_string);
67 websocket_->BroadcastBinaryData(stream_data_string);
68}
69
70void MapUpdater::OnTimer(const std::string& channel_name) { PublishMessage(); }
71
72void MapUpdater::StartStream(const double &time_interval_ms,
73 const std::string &channel_name,
74 nlohmann::json *subscribe_param) {
75 if (subscribe_param == nullptr ||
76 !ContainsKey(*subscribe_param, "mapElementIds")) {
77 AERROR << "Subscribe map must bring elementIds param";
78 return;
79 }
80 // json to message
81 map_element_ids_.Clear();
82 nlohmann::json element_ids_json;
83 if (!JsonUtil::GetJsonByPath(*subscribe_param, {"mapElementIds"},
84 &element_ids_json)) {
85 AERROR << "ElementIds param is wrong type.";
86 return;
87 }
88 if (!JsonStringToMessage(element_ids_json.dump(), &map_element_ids_).ok()) {
89 AERROR << "Failed to parse elementIds from json!";
90 return;
91 }
92 if (time_interval_ms > 0) {
93 timer_.reset(new cyber::Timer(
94 time_interval_ms, [this]() { this->OnTimer(); }, false));
95 timer_->Start();
96 } else {
97 this->OnTimer();
98 }
99}
100
101void MapUpdater::StopStream(const std::string &channel_name) {
102 if (timer_) {
103 timer_->Stop();
104 }
105}
106} // namespace dreamview
107} // namespace apollo
static bool GetJsonByPath(const nlohmann::json &json, const std::vector< std::string > &paths, nlohmann::json *value)
Get the json from the given json and path.
Definition json_util.cc:73
Used to perform oneshot or periodic timing tasks
Definition timer.h:71
hdmap::Map RetrieveMapElements(const MapElementIds &ids) const
void OnTimer(const std::string &channel_name="")
MapUpdater(WebSocketHandler *websocket, const MapService *map_service)
Constructor with the websocket handler.
void StartStream(const double &time_interval_ms, const std::string &channel_name="", nlohmann::json *subscribe_param=nullptr) override
Start data flow.
void StopStream(const std::string &channel_name="") override
Stop data flow.
void PublishMessage(const std::string &channel_name="") override
Publish Message to dreamview frontend.
The WebSocketHandler, built on top of CivetWebSocketHandler, is a websocket handler that handles diff...
bool BroadcastBinaryData(const std::string &data, bool skippable=false)
Sends the provided binary data to all the connected clients.
#define AERROR
Definition log.h:44
Some map util functions.
class register implement
Definition arena_queue.h:37
optional apollo::localization::Pose pose