Apollo 10.0
自动驾驶开放平台
mpc_controller_submodule.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
20#include "cyber/time/clock.h"
25
26namespace apollo {
27namespace control {
28
38
40 : monitor_logger_buffer_(common::monitor::MonitorMessageItem::CONTROL) {}
41
43
44std::string MPCControllerSubmodule::Name() const {
45 return FLAGS_mpc_controller_submodule_name;
46}
47
49 injector_ = std::make_shared<DependencyInjector>();
50
52 "apollo::control::MPCController");
53
54 if (!mpc_controller_->Init(injector_).ok()) {
55 monitor_logger_buffer_.ERROR(
56 "Control init MPC controller failed! Stopping...");
57 return false;
58 }
59
60 control_core_writer_ =
61 node_->CreateWriter<ControlCommand>(FLAGS_control_core_command_topic);
62 ACHECK(control_core_writer_ != nullptr);
63 return true;
64}
65
67 const std::shared_ptr<Preprocessor>& preprocessor_status) {
68 const auto start_time = Clock::Now();
69
70 ControlCommand control_core_command;
71 // recording pad msg
72 if (preprocessor_status->received_pad_msg()) {
73 control_core_command.mutable_pad_msg()->CopyFrom(
74 preprocessor_status->local_view().pad_msg());
75 }
76 ADEBUG << "MPC controller submodule started ....";
77
78 // skip produce control command when estop for MPC controller
79 StatusPb pre_status = preprocessor_status->header().status();
80 if (pre_status.error_code() != ErrorCode::OK) {
81 control_core_command.mutable_header()->mutable_status()->CopyFrom(
82 pre_status);
83 AERROR << "Error in preprocessor submodule.";
84 return false;
85 }
86
87 Status status = ProduceControlCoreCommand(preprocessor_status->local_view(),
88 &control_core_command);
89 AERROR_IF(!status.ok()) << "Failed to produce control command:"
90 << status.error_message();
91
92 control_core_command.mutable_header()->set_lidar_timestamp(
93 preprocessor_status->header().lidar_timestamp());
94 control_core_command.mutable_header()->set_camera_timestamp(
95 preprocessor_status->header().camera_timestamp());
96 control_core_command.mutable_header()->set_radar_timestamp(
97 preprocessor_status->header().radar_timestamp());
98 common::util::FillHeader(Name(), &control_core_command);
99
100 const auto end_time = Clock::Now();
101
102 static apollo::common::LatencyRecorder latency_recorder(
103 FLAGS_control_core_command_topic);
104 latency_recorder.AppendLatencyRecord(
105 control_core_command.header().lidar_timestamp(), start_time, end_time);
106
107 control_core_command.mutable_header()->mutable_status()->set_error_code(
108 status.code());
109 control_core_command.mutable_header()->mutable_status()->set_msg(
110 status.error_message());
111
112 control_core_writer_->Write(control_core_command);
113
114 return status.ok();
115}
116
117Status MPCControllerSubmodule::ProduceControlCoreCommand(
118 const LocalView& local_view, ControlCommand* control_core_command) {
119 if (local_view.chassis().driving_mode() == Chassis::COMPLETE_MANUAL) {
120 mpc_controller_->Reset();
121 AINFO_EVERY(100) << "Reset Controllers in Manual Mode";
122 }
123
124 Status status = mpc_controller_->ComputeControlCommand(
125 &local_view.localization(), &local_view.chassis(),
126 &local_view.trajectory(), control_core_command);
127
128 ADEBUG << "MPC controller submodule finished.";
129
130 return status;
131}
132
133} // namespace control
134} // namespace apollo
void AppendLatencyRecord(const uint64_t message_id, const apollo::cyber::Time &begin_time, const apollo::cyber::Time &end_time)
A general class to denote the return status of an API call.
Definition status.h:43
bool ok() const
check whether the return status is OK.
Definition status.h:67
ErrorCode code() const
get the error code
Definition status.h:73
const std::string & error_message() const
returns the error message of the status, empty if the status is OK.
Definition status.h:91
MPCControllerSubmodule()
Construct a new MPCControllerSubmodule object
std::string Name() const
Get name of the node
bool Proc(const std::shared_ptr< Preprocessor > &preprocessor_status) override
generate control command
bool Init() override
Initialize the node
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
static Time Now()
PRECISION >= 1000000 means the precision is at least 1us.
Definition clock.cc:40
std::shared_ptr< Node > node_
static PluginManager * Instance()
get singleton instance of PluginManager
std::shared_ptr< Base > CreateInstance(const std::string &derived_class)
create plugin instance of derived class based on Base
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AINFO_EVERY(freq)
Definition log.h:82
#define AERROR
Definition log.h:44
#define AERROR_IF(cond)
Definition log.h:74
class register implement
Definition arena_queue.h:37
optional uint64 lidar_timestamp
Definition header.proto:19
optional ErrorCode error_code
optional apollo::common::Header header
LocalView contains all necessary data as planning input