Apollo 11.0
自动驾驶开放平台
calibration_table_debug.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 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
17#include <termios.h>
18
19#include <cstdio>
20#include <iostream>
21#include <memory>
22#include <thread>
23
24#include "modules/common_msgs/chassis_msgs/chassis.pb.h"
25#include "modules/common_msgs/control_msgs/control_cmd.pb.h"
26#include "modules/control/control_component/proto/calibration_debug.pb.h"
27#include "modules/control/control_component/proto/calibration_table.pb.h"
28
29#include "cyber/common/file.h"
30#include "cyber/common/log.h"
31#include "cyber/common/macros.h"
32#include "cyber/cyber.h"
33#include "cyber/init.h"
34#include "cyber/time/clock.h"
35#include "cyber/time/time.h"
40
41// gflags
42DEFINE_string(calibration_table_debug_file,
43 "/apollo/modules/control/control_component/conf/"
44 "calibration_table.pb.txt",
45 "calibration table file");
46
47namespace apollo {
48namespace control {
49
58
60 public:
61 VehicleCalibrationDebug() : node_(CreateNode("calibration_debug")) {}
62 int32_t init() {
63 if (is_running_) {
64 AERROR << "Already running.";
65 return -1;
66 }
67 is_running_ = true;
68 chassis_reader_ = node_->CreateReader<Chassis>(
69 FLAGS_chassis_topic, [this](const std::shared_ptr<Chassis> &chassis) {
70 on_chassis(*chassis);
71 });
72
73 control_reader_ = node_->CreateReader<ControlCommand>(
74 FLAGS_control_command_topic,
75 [this](const std::shared_ptr<ControlCommand> &cmd) { on_debug(*cmd); });
76
77 calibration_debug_writer_ = node_->CreateWriter<CalibrationDebug>(
78 "/apollo/control/calibration_debug");
79
80 keyboard_thread_.reset(
81 new std::thread([this] { KeyboardLoopThreadFunc(); }));
82 if (keyboard_thread_ == nullptr) {
83 AERROR << "Unable to create can client receiver thread.";
84 return -1;
85 }
86
87 if (!LoadCalibrationTable(&calibration_table_)) {
88 AERROR << "failed to load calibration table";
89 }
90
92
93 return 0;
94 }
95
96 void Send() {
97 apollo::common::util::FillHeader("calibration_debug", &calibration_debug_);
98 calibration_debug_writer_->Write(calibration_debug_);
99 ADEBUG << "Calibration Debug send OK:"
100 << calibration_debug_.ShortDebugString();
101 }
102
103 bool LoadCalibrationTable(calibration_table *calibration_table_conf) {
104 std::string calibration_table_path = FLAGS_calibration_table_debug_file;
105
106 if (!apollo::cyber::common::GetProtoFromFile(calibration_table_path,
107 calibration_table_conf)) {
108 AERROR << "Load calibration table failed!";
109 return false;
110 }
111 AINFO << "Load the calibraiton table file successfully, file path: "
112 << calibration_table_path;
113 return true;
114 }
115
117 AINFO << "Control calibration table size is "
118 << calibration_table_.calibration_size();
120 for (const auto &calibration : calibration_table_.calibration()) {
121 xyz.push_back(std::make_tuple(calibration.speed(),
122 calibration.acceleration(),
123 calibration.command()));
124 }
125 control_interpolation_.reset(new Interpolation2D);
126 ACHECK(control_interpolation_->Init(xyz))
127 << "Fail to load control calibration table";
128 }
129
131 chassis_reader_->Observe();
132 const auto &chassis_msg = chassis_reader_->GetLatestObserved();
133 if (chassis_msg == nullptr) {
134 AERROR << "Chassis msg is not ready!";
135 return;
136 }
137
138 double current_speed = 0.0;
139
140 double input_acc_lookup = 0.0;
141
142 double input_throttle_cmd = 0.0;
143 double output_throttle_cmd = 0.0;
144 double input_brake_cmd = 0.0;
145 double output_brake_cmd = 0.0;
146 double input_calibration_value = 0.0;
147 double output_calibration_value = 0.0;
148
149 if (cmd.debug().simple_lon_debug().has_acceleration_lookup()) {
150 current_speed = chassis_msg->speed_mps();
151 input_acc_lookup = cmd.debug().simple_lon_debug().acceleration_lookup();
152 input_calibration_value =
154
155 output_calibration_value = control_interpolation_->Interpolate(
156 std::make_pair(std::fabs(current_speed), input_acc_lookup));
157
158 if (input_acc_lookup * input_calibration_value < 0.0) {
159 AINFO << "current_speed:" << current_speed
160 << ", input_acc_lookup:" << input_acc_lookup
161 << ", input_calibration_value:" << input_calibration_value
162 << ", output_calibration_value:" << output_calibration_value;
163 }
164 }
165
166 Send();
167 }
168
169 void on_chassis(const Chassis &chassis) {
170 ADEBUG << "Received chassis data: run chassis callback.";
171 std::lock_guard<std::mutex> lock(mutex_);
172 latest_chassis_.CopyFrom(chassis);
173 }
174
176 char c = 0;
177 int32_t kfd_ = 0;
178 while (IsRunning()) {
179 if (read(kfd_, &c, 1) < 0) {
180 perror("read():");
181 exit(-1);
182 }
183 }
184 }
185
186 bool IsRunning() const { return is_running_; }
187
188 void stop() {
189 if (is_running_) {
190 is_running_ = false;
191 if (keyboard_thread_ != nullptr && keyboard_thread_->joinable()) {
192 keyboard_thread_->join();
193 keyboard_thread_.reset();
194 AINFO << "keyboard stopped [ok].";
195 }
196 }
197 }
198
199 private:
200 std::unique_ptr<std::thread> keyboard_thread_;
201 std::shared_ptr<Reader<Chassis>> chassis_reader_;
202 std::shared_ptr<Reader<ControlCommand>> control_reader_;
203 std::shared_ptr<Writer<CalibrationDebug>> calibration_debug_writer_;
204 std::shared_ptr<Node> node_;
205
206 std::mutex mutex_;
207
208 CalibrationDebug calibration_debug_;
209 canbus::Chassis latest_chassis_;
210 calibration_table calibration_table_;
211
212 std::unique_ptr<Interpolation2D> control_interpolation_;
213 bool is_running_ = false;
214};
215} // namespace control
216} // namespace apollo
217
218int main(int argc, char **argv) {
219 apollo::cyber::Init(argv[0]);
220 FLAGS_alsologtostderr = true;
221 FLAGS_v = 3;
222 google::ParseCommandLineFlags(&argc, &argv, true);
224 if (calibration_debug.init() != 0) {
225 AERROR << "Fail to init calibration_debug";
226 return -1;
227 };
229 calibration_debug.stop();
230 return 0;
231}
int main(int argc, char **argv)
DEFINE_string(calibration_table_debug_file, "/apollo/modules/control/control_component/conf/" "calibration_table.pb.txt", "calibration table file")
linear interpolation from key (double, double) to one double value.
std::vector< std::tuple< double, double, double > > DataType
void on_debug(const control::ControlCommand &cmd)
bool LoadCalibrationTable(calibration_table *calibration_table_conf)
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
Node is the fundamental building block of Cyber RT.
Definition node.h:44
Reader subscribes a channel, it has two main functions:
Definition reader.h:69
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Some string util functions.
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
void WaitForShutdown()
Definition state.h:50
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:99
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33
class register implement
Definition arena_queue.h:37
optional SimpleLongitudinalDebug simple_lon_debug
repeated ControlCalibrationInfo calibration