Apollo 10.0
自动驾驶开放平台
camera_monitor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 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 <memory>
20#include <set>
21#include <string>
22#include <utility>
23
24#include "absl/strings/str_cat.h"
25
26#include "modules/common_msgs/sensor_msgs/sensor_image.pb.h"
27
28#include "cyber/common/log.h"
29#include "cyber/cyber.h"
34
35DEFINE_string(camera_monitor_name, "CameraMonitor",
36 "Name of the camera monitor.");
37DEFINE_double(camera_monitor_interval, 5,
38 "Camera monitor checking interval in seconds.");
39DEFINE_string(camera_component_name, "Camera", "Camera component name.");
40
41namespace apollo {
42namespace monitor {
43namespace {
44
45using ReaderAndMessagePair = std::pair<std::shared_ptr<cyber::ReaderBase>,
46 std::shared_ptr<drivers::Image>>;
47
48ReaderAndMessagePair CreateReaderAndLatestsMessage(const std::string& camera) {
49 const auto reader =
50 MonitorManager::Instance()->CreateReader<drivers::Image>(camera);
51 reader->Observe();
52 const auto message = reader->GetLatestObserved();
53 reader->ClearData();
54 return {reader, message};
55}
56
57static const auto camera_topic_set = std::set<std::string>{
58 FLAGS_image_long_topic, FLAGS_camera_image_long_topic,
59 FLAGS_camera_image_short_topic, FLAGS_camera_front_6mm_topic,
60 FLAGS_camera_front_6mm_2_topic, FLAGS_camera_front_12mm_topic,
61 // Add more cameras here if you want to monitor.
62};
63
64} // namespace
65
67 : RecurrentRunner(FLAGS_camera_monitor_name,
68 FLAGS_camera_monitor_interval) {}
69
70void CameraMonitor::RunOnce(const double current_time) {
71 auto* manager = MonitorManager::Instance();
72 auto* component = apollo::common::util::FindOrNull(
73 *manager->GetStatus()->mutable_components(), FLAGS_camera_component_name);
74 if (component == nullptr) {
75 // camera is not monitored in current mode, skip.
76 return;
77 }
78 auto* status = component->mutable_other_status();
79 UpdateStatus(status);
80}
81
82void CameraMonitor::UpdateStatus(ComponentStatus* status) {
83 status->clear_status();
84 std::string frame_id = "";
85 for (const auto& topic : camera_topic_set) {
86 const auto& reader_message_pair = CreateReaderAndLatestsMessage(topic);
87 const auto& reader = reader_message_pair.first;
88 const auto& message = reader_message_pair.second;
89 if (reader != nullptr && message != nullptr) {
90 if (frame_id.empty()) {
91 const auto& header = message->header();
92 if (header.has_frame_id()) {
93 frame_id = header.frame_id();
94 }
95 } else {
97 ComponentStatus::ERROR,
98 absl::StrCat("Only one camera is permitted"), status);
99 }
100 }
101 }
102 if (frame_id.empty()) {
104 ComponentStatus::ERROR, absl::StrCat("No camera is detected"), status);
105 } else {
107 ComponentStatus::OK, absl::StrCat("Detected one camera: ", frame_id),
108 status);
109 }
110}
111
112} // namespace monitor
113} // namespace apollo
DEFINE_double(camera_monitor_interval, 5, "Camera monitor checking interval in seconds.")
DEFINE_string(camera_monitor_name, "CameraMonitor", "Name of the camera monitor.")
void RunOnce(const double current_time) override
static void EscalateStatus(const ComponentStatus::Status new_status, const std::string &message, ComponentStatus *current_status)
Some map util functions.
std::string frame_id
Point cloud frame id
class register implement
Definition arena_queue.h:37