simulation_world_updater.cc 5.2 KB
Newer Older
S
siyangy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

17
#include "modules/dreamview/backend/simulation_world/simulation_world_updater.h"
S
siyangy 已提交
18 19 20 21

#include <string>

#include "google/protobuf/util/json_util.h"
22
#include "modules/dreamview/backend/common/dreamview_gflags.h"
23
#include "modules/map/hdmap/hdmap_util.h"
S
siyangy 已提交
24 25 26 27

namespace apollo {
namespace dreamview {

S
siyangy 已提交
28
using apollo::common::adapter::AdapterManager;
29
using apollo::routing::RoutingRequest;
S
siyangy 已提交
30 31 32
using google::protobuf::util::MessageToJsonString;
using Json = nlohmann::json;

33
SimulationWorldUpdater::SimulationWorldUpdater(WebSocketHandler *websocket,
34
                                               const MapService *map_service,
S
siyangy 已提交
35 36
                                               bool routing_from_file)
    : sim_world_service_(map_service, routing_from_file),
37
      map_service_(map_service),
S
siyangy 已提交
38
      websocket_(websocket) {
39 40 41 42 43 44

  // Initialize default end point
  CHECK(apollo::common::util::GetProtoFromASCIIFile(
      apollo::hdmap::EndWayPointFile(),
      &default_end_point_));

S
siyangy 已提交
45 46 47 48 49 50
  websocket_->RegisterMessageHandler(
      "RetrieveMapData",
      [this](const Json &json, WebSocketHandler::Connection *conn) {
        auto iter = json.find("elements");
        if (iter != json.end()) {
          MapElementIds map_element_ids(*iter);
51
          auto retrieved = map_service_->RetrieveMapElements(map_element_ids);
S
siyangy 已提交
52 53 54 55 56 57 58 59 60 61 62

          std::string retrieved_json_string;
          MessageToJsonString(retrieved, &retrieved_json_string);

          Json response;
          response["type"] = "MapData";
          response["data"] = Json::parse(retrieved_json_string);

          websocket_->SendData(response.dump(), conn);
        }
      });
63 64 65 66

  websocket_->RegisterMessageHandler(
      "SendRoutingRequest",
      [this](const Json &json, WebSocketHandler::Connection *conn) {
67
        RoutingRequest routing_request;
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        bool succeed = ConstructRoutingRequest(json, &routing_request);
        if (succeed) {
          AdapterManager::FillRoutingRequestHeader(FLAGS_dreamview_module_name,
                                                   &routing_request);
          AdapterManager::PublishRoutingRequest(routing_request);
        }

        Json response;
        response["type"] = "RoutingRequestSent";
        response["status"] = succeed ? "Sent" : "Failed";
        websocket_->SendData(response.dump(), conn);
      });
}

bool SimulationWorldUpdater::ConstructRoutingRequest(
      const Json &json,
84
      RoutingRequest* routing_request) {
85 86 87
  // set start point
  auto start = json["start"];
  if (start.find("x") == start.end() || start.find("y") == start.end()) {
88
    AERROR << "Failed to prepare a routing request: start point not found";
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    return false;
  }
  map_service_->ConstructLaneWayPoint(start["x"], start["y"],
                                      routing_request->mutable_start());

  // set way point(s) if any
  auto iter = json.find("waypoint");
  if (iter != json.end()) {
    auto* waypoint = routing_request->mutable_waypoint();
    for (size_t i = 0; i < iter->size(); ++i) {
      auto& point = (*iter)[i];
      if (!map_service_->ConstructLaneWayPoint(point["x"], point["y"],
                                               waypoint->Add())) {
        waypoint->RemoveLast();
      }
    }
  }

  // set end point
108 109 110 111 112 113 114 115 116 117 118 119 120 121
  RoutingRequest::LaneWaypoint *endLane = routing_request->mutable_end();
  if (json["sendDefaultRoute"]) {
    endLane->set_id(default_end_point_.id());
    endLane->set_s(default_end_point_.s());
    auto *pose = endLane->mutable_pose();
    pose->set_x(default_end_point_.pose().x());
    pose->set_y(default_end_point_.pose().y());
  } else {
    auto end = json["end"];
    if (end.find("x") == end.end() || end.find("y") == end.end()) {
      AERROR << "Failed to prepare a routing request: end point not found";
      return false;
    }
    map_service_->ConstructLaneWayPoint(end["x"], end["y"], endLane);
122 123 124
  }

  return true;
S
siyangy 已提交
125 126
}

S
siyangy 已提交
127 128 129 130 131 132 133
void SimulationWorldUpdater::Start() {
  // start ROS timer, one-shot = false, auto-start = true
  timer_ =
      AdapterManager::CreateTimer(ros::Duration(kSimWorldTimeInterval),
                                  &SimulationWorldUpdater::OnPushTimer, this);
}

S
siyangy 已提交
134 135 136 137 138 139 140
void SimulationWorldUpdater::OnPushTimer(const ros::TimerEvent &event) {
  sim_world_service_.Update();
  if (!sim_world_service_.ReadyToPush()) {
    AWARN << "Not sending simulation world as the data is not ready!";
    return;
  }
  auto json = sim_world_service_.GetUpdateAsJson();
141
  websocket_->BroadcastData(json.dump());
S
siyangy 已提交
142 143 144 145
}

}  // namespace dreamview
}  // namespace apollo