Apollo 10.0
自动驾驶开放平台
swerve_trigger.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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 <cmath>
20#include <memory>
21
22#include "cyber/common/log.h"
24#include "modules/common_msgs/control_msgs/control_cmd.pb.h"
25
26namespace apollo {
27namespace data {
28
30constexpr float MAX_STEER_PER = 100.0;
31constexpr float MIN_STEER_PER = -100.0;
32
34
36 if (!trigger_obj_->enabled()) {
37 return;
38 }
39
40 if (msg.channel_name == FLAGS_chassis_topic) {
41 Chassis chassis_msg;
42 chassis_msg.ParseFromString(msg.content);
43 const float steer_per = chassis_msg.steering_percentage();
44
45 if (IsNoisy(steer_per)) {
46 return;
47 }
48
49 EnqueueMessage(steer_per);
50
51 if (IsSwerve()) {
52 AINFO << "swerve trigger is pulled: " << msg.time << " - "
53 << msg.channel_name;
54 TriggerIt(msg.time);
55 }
56 }
57}
58
59bool SwerveTrigger::IsNoisy(const float steer) const {
60 if (steer > MAX_STEER_PER || steer < MIN_STEER_PER) {
61 return true;
62 }
63 const float pre_steer_mps =
64 (current_steer_queue_.empty() ? 0.0f : current_steer_queue_.back());
65 return fabs(pre_steer_mps - steer) > noisy_diff_;
66}
67
68bool SwerveTrigger::IsSwerve() const {
69 if (current_steer_queue_.size() < queue_size_ ||
70 history_steer_queue_.size() < queue_size_) {
71 return false;
72 }
73 const float delta =
74 (history_total_ - current_total_) / static_cast<float>(queue_size_);
75 return delta > max_delta_;
76}
77
78// TODO(Leisheng Mu): reuse the code with hard_brake_trigger in next iteration
79void SwerveTrigger::EnqueueMessage(const float steer) {
80 current_steer_queue_.emplace_back(steer);
81 current_total_ += steer;
82 if (current_steer_queue_.size() > queue_size_) {
83 const float current_front = current_steer_queue_.front();
84 current_steer_queue_.pop_front();
85 current_total_ -= current_front;
86
87 history_steer_queue_.emplace_back(current_front);
88 history_total_ += current_front;
89 if (history_steer_queue_.size() > queue_size_) {
90 history_total_ -= history_steer_queue_.front();
91 history_steer_queue_.pop_front();
92 }
93 }
94}
95
96} // namespace data
97} // namespace apollo
void Pull(const cyber::record::RecordMessage &msg) override
std::unique_ptr< Trigger > trigger_obj_
void TriggerIt(const uint64_t msg_time) const
#define AINFO
Definition log.h:42
constexpr float MIN_STEER_PER
constexpr float MAX_STEER_PER
class register implement
Definition arena_queue.h:37
optional float steering_percentage
Definition chassis.proto:88
Basic data struct of record message.
std::string content
The content of the message.
uint64_t time
The time (nanosecond) of the message.
std::string channel_name
The channel name of the message.