Apollo 10.0
自动驾驶开放平台
functional_safety_monitor.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 *****************************************************************************/
16
18
19#include <string>
20
21#include "absl/strings/str_cat.h"
23
24DEFINE_string(functional_safety_monitor_name, "FunctionalSafetyMonitor",
25 "Name of the functional safety monitor.");
26
27DEFINE_double(safety_mode_seconds_before_estop, 10.0,
28 "Interval before sending estop after we found critical errors.");
29
30namespace apollo {
31namespace monitor {
32namespace {
33
34bool IsSafe(const std::string& name, const ComponentStatus& status) {
35 if (status.status() == ComponentStatus::ERROR ||
36 status.status() == ComponentStatus::FATAL) {
37 MonitorManager::Instance()->LogBuffer().ERROR(
38 absl::StrCat(name, " triggers safe mode: ", status.message()));
39 return false;
40 }
41 return true;
42}
43
44} // namespace
45
47 : RecurrentRunner(FLAGS_functional_safety_monitor_name, 0) {}
48
49void FunctionalSafetyMonitor::RunOnce(const double current_time) {
50 auto* system_status = MonitorManager::Instance()->GetStatus();
51 // Everything looks good or has been handled properly.
52 if (CheckSafety()) {
53 system_status->clear_passenger_msg();
54 system_status->clear_safety_mode_trigger_time();
55 system_status->clear_require_emergency_stop();
56 return;
57 }
58 if (system_status->require_emergency_stop()) {
59 // EStop has already been triggered.
60 return;
61 }
62
63 // Newly entered safety mode.
64 system_status->set_passenger_msg("Error! Please disengage.");
65 if (!system_status->has_safety_mode_trigger_time()) {
66 system_status->set_safety_mode_trigger_time(current_time);
67 return;
68 }
69
70 // Trigger EStop if no action was taken in time.
71 if (system_status->safety_mode_trigger_time() +
72 FLAGS_safety_mode_seconds_before_estop <
73 current_time) {
74 system_status->set_require_emergency_stop(true);
75 }
76}
77
78bool FunctionalSafetyMonitor::CheckSafety() {
79 // We only check safety in self driving mode.
80 auto manager = MonitorManager::Instance();
81 if (!manager->IsInAutonomousMode()) {
82 return true;
83 }
84
85 // Check HMI modules status.
86 const auto& mode = manager->GetHMIMode();
87 const auto& hmi_modules = manager->GetStatus()->hmi_modules();
88 for (const auto& iter : mode.modules()) {
89 const std::string& module_name = iter.first;
90 const auto& module = iter.second;
91 if (module.required_for_safety() &&
92 !IsSafe(module_name, hmi_modules.at(module_name))) {
93 return false;
94 }
95 }
96
97 // Check monitored components status.
98 const auto& components = manager->GetStatus()->components();
99 for (const auto& iter : mode.monitored_components()) {
100 const std::string& component_name = iter.first;
101 const auto& component = iter.second;
102 if (component.required_for_safety() &&
103 !IsSafe(component_name, components.at(component_name).summary())) {
104 return false;
105 }
106 }
107
108 // Everything looks good.
109 return true;
110}
111
112} // namespace monitor
113} // namespace apollo
DEFINE_string(functional_safety_monitor_name, "FunctionalSafetyMonitor", "Name of the functional safety monitor.")
DEFINE_double(safety_mode_seconds_before_estop, 10.0, "Interval before sending estop after we found critical errors.")
class register implement
Definition arena_queue.h:37