Apollo 10.0
自动驾驶开放平台
guardian_component.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 *****************************************************************************/
17
18#include "cyber/common/log.h"
21
23
24namespace apollo {
25namespace guardian {
26
29using apollo::monitor::SystemStatus;
30
32 if (!GetProtoConfig(&guardian_conf_)) {
33 AERROR << "Unable to load canbus conf file: " << ConfigFilePath();
34 return false;
35 }
36
37 chassis_reader_ = node_->CreateReader<Chassis>(
38 FLAGS_chassis_topic, [this](const std::shared_ptr<Chassis>& chassis) {
39 ADEBUG << "Received chassis data: run chassis callback.";
40 std::lock_guard<std::mutex> lock(mutex_);
41 chassis_.CopyFrom(*chassis);
42 });
43
44 control_cmd_reader_ = node_->CreateReader<ControlCommand>(
45 FLAGS_control_command_topic,
46 [this](const std::shared_ptr<ControlCommand>& cmd) {
47 ADEBUG << "Received control data: run control callback.";
48 std::lock_guard<std::mutex> lock(mutex_);
49 control_cmd_.CopyFrom(*cmd);
50 });
51
52 system_status_reader_ = node_->CreateReader<SystemStatus>(
53 FLAGS_system_status_topic,
54 [this](const std::shared_ptr<SystemStatus>& status) {
55 ADEBUG << "Received system status data: run system status callback.";
56 std::lock_guard<std::mutex> lock(mutex_);
57 last_status_received_s_ = Time::Now().ToSecond();
58 system_status_.CopyFrom(*status);
59 });
60
61 guardian_writer_ = node_->CreateWriter<GuardianCommand>(FLAGS_guardian_topic);
62
63 return true;
64}
65
67 constexpr double kSecondsTillTimeout(2.5);
68
69 bool safety_mode_triggered = false;
70 if (guardian_conf_.guardian_enable()) {
71 std::lock_guard<std::mutex> lock(mutex_);
72 if (Time::Now().ToSecond() - last_status_received_s_ >
73 kSecondsTillTimeout) {
74 safety_mode_triggered = true;
75 }
76 safety_mode_triggered =
77 safety_mode_triggered || system_status_.has_safety_mode_trigger_time();
78 }
79
80 if (safety_mode_triggered) {
81 ADEBUG << "Safety mode triggered, enable safety mode";
82 TriggerSafetyMode();
83 } else {
84 ADEBUG << "Safety mode not triggered, bypass control command";
85 PassThroughControlCommand();
86 }
87
88 common::util::FillHeader(node_->Name(), &guardian_cmd_);
89 guardian_writer_->Write(guardian_cmd_);
90 return true;
91}
92
93void GuardianComponent::PassThroughControlCommand() {
94 std::lock_guard<std::mutex> lock(mutex_);
95 guardian_cmd_.mutable_control_command()->CopyFrom(control_cmd_);
96}
97
98void GuardianComponent::TriggerSafetyMode() {
99 AINFO << "Safety state triggered, with system safety mode trigger time : "
100 << system_status_.safety_mode_trigger_time();
101 std::lock_guard<std::mutex> lock(mutex_);
102 bool sensor_malfunction = false, obstacle_detected = false;
103 if (!chassis_.surround().sonar_enabled() ||
104 chassis_.surround().sonar_fault()) {
105 AINFO << "Ultrasonic sensor not enabled for faulted, will do emergency "
106 "stop!";
107 sensor_malfunction = true;
108 } else {
109 // TODO(QiL) : Load for config
110 for (int i = 0; i < chassis_.surround().sonar_range_size(); ++i) {
111 if ((chassis_.surround().sonar_range(i) > 0.0 &&
112 chassis_.surround().sonar_range(i) < 2.5) ||
113 chassis_.surround().sonar_range(i) > 30) {
114 AINFO << "Object detected or ultrasonic sensor fault output, will do "
115 "emergency stop!";
116 obstacle_detected = true;
117 }
118 }
119 }
120
121 guardian_cmd_.mutable_control_command()->set_throttle(0.0);
122 guardian_cmd_.mutable_control_command()->set_steering_target(0.0);
123 guardian_cmd_.mutable_control_command()->set_steering_rate(25.0);
124 guardian_cmd_.mutable_control_command()->set_is_in_safe_mode(true);
125
126 // TODO(QiL) : Remove this one once hardware re-alignment is done.
127 sensor_malfunction = false;
128 obstacle_detected = false;
129 AINFO << "Temporarily ignore the ultrasonic sensor output during hardware "
130 "re-alignment!";
131
132 if (system_status_.require_emergency_stop() || sensor_malfunction ||
133 obstacle_detected) {
134 AINFO << "Emergency stop triggered! with system status from monitor as : "
135 << system_status_.require_emergency_stop();
136 guardian_cmd_.mutable_control_command()->set_brake(
138 } else {
139 AINFO << "Soft stop triggered! with system status from monitor as : "
140 << system_status_.require_emergency_stop();
141 guardian_cmd_.mutable_control_command()->set_brake(
142 guardian_conf_.guardian_cmd_soft_stop_percentage());
143 }
144}
145
146} // namespace guardian
147} // namespace apollo
bool GetProtoConfig(T *config) const
const std::string & ConfigFilePath() const
std::shared_ptr< Node > node_
Cyber has builtin time type Time.
Definition time.h:31
static Time Now()
get the current time.
Definition time.cc:57
double ToSecond() const
convert time to second.
Definition time.cc:77
bool Proc() override
The Proc logic of the component, which called by the CyberRT frame.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Some string util functions.
class register implement
Definition arena_queue.h:37
optional Surround surround
optional bool sonar_fault
optional bool sonar_enabled
repeated double sonar_range
optional double guardian_cmd_emergency_stop_percentage
optional double guardian_cmd_soft_stop_percentage