Apollo  6.0
Open source self driving car software
lat_controller.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 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 
22 #pragma once
23 
24 #include <fstream>
25 #include <memory>
26 #include <string>
27 
28 #include "Eigen/Core"
29 #include "modules/common/configs/proto/vehicle_config.pb.h"
38 
43 namespace apollo {
44 namespace control {
45 
53 class LatController : public Controller {
54  public:
58  LatController();
59 
63  virtual ~LatController();
64 
70  common::Status Init(std::shared_ptr<DependencyInjector> injector,
71  const ControlConf *control_conf) override;
72 
83  const localization::LocalizationEstimate *localization,
84  const canbus::Chassis *chassis, const planning::ADCTrajectory *trajectory,
85  ControlCommand *cmd) override;
86 
91  common::Status Reset() override;
92 
96  void Stop() override;
97 
102  std::string Name() const override;
103 
104  protected:
105  void UpdateState(SimpleLateralDebug *debug);
106 
107  // logic for reverse driving mode
109 
110  void UpdateMatrix();
111 
112  void UpdateMatrixCompound();
113 
114  double ComputeFeedForward(double ref_curvature) const;
115 
116  void ComputeLateralErrors(const double x, const double y, const double theta,
117  const double linear_v, const double angular_v,
118  const double linear_a,
119  const TrajectoryAnalyzer &trajectory_analyzer,
120  SimpleLateralDebug *debug);
121  bool LoadControlConf(const ControlConf *control_conf);
122  void InitializeFilters(const ControlConf *control_conf);
123  void LoadLatGainScheduler(const LatControllerConf &lat_controller_conf);
124  void LogInitParameters();
125  void ProcessLogs(const SimpleLateralDebug *debug,
126  const canbus::Chassis *chassis);
127 
128  void CloseLogFile();
129 
130  // vehicle
131  const ControlConf *control_conf_ = nullptr;
132 
133  // vehicle parameter
134  common::VehicleParam vehicle_param_;
135 
136  // a proxy to analyze the planning trajectory
138 
139  // the following parameters are vehicle physics related.
140  // control time interval
141  double ts_ = 0.0;
142  // corner stiffness; front
143  double cf_ = 0.0;
144  // corner stiffness; rear
145  double cr_ = 0.0;
146  // distance between front and rear wheel center
147  double wheelbase_ = 0.0;
148  // mass of the vehicle
149  double mass_ = 0.0;
150  // distance from front wheel center to COM
151  double lf_ = 0.0;
152  // distance from rear wheel center to COM
153  double lr_ = 0.0;
154  // rotational inertia
155  double iz_ = 0.0;
156  // the ratio between the turn of the steering wheel and the turn of the wheels
157  double steer_ratio_ = 0.0;
158  // the maximum turn of steer
160 
161  // limit steering to maximum theoretical lateral acceleration
162  double max_lat_acc_ = 0.0;
163 
164  // number of control cycles look ahead (preview controller)
166 
167  // longitudial length for look-ahead lateral error estimation during forward
168  // driving and look-back lateral error estimation during backward driving
169  // (look-ahead controller)
174 
175  // number of states without previews, includes
176  // lateral error, lateral error rate, heading error, heading error rate
177  const int basic_state_size_ = 4;
178  // vehicle state matrix
179  Eigen::MatrixXd matrix_a_;
180  // vehicle state matrix (discrete-time)
181  Eigen::MatrixXd matrix_ad_;
182  // vehicle state matrix compound; related to preview
183  Eigen::MatrixXd matrix_adc_;
184  // control matrix
185  Eigen::MatrixXd matrix_b_;
186  // control matrix (discrete-time)
187  Eigen::MatrixXd matrix_bd_;
188  // control matrix compound
189  Eigen::MatrixXd matrix_bdc_;
190  // gain matrix
191  Eigen::MatrixXd matrix_k_;
192  // control authority weighting matrix
193  Eigen::MatrixXd matrix_r_;
194  // state weighting matrix
195  Eigen::MatrixXd matrix_q_;
196  // updated state weighting matrix
197  Eigen::MatrixXd matrix_q_updated_;
198  // vehicle state matrix coefficients
199  Eigen::MatrixXd matrix_a_coeff_;
200  // 4 by 1 matrix; state matrix
201  Eigen::MatrixXd matrix_state_;
202 
203  // parameters for lqr solver; number of iterations
205  // parameters for lqr solver; threshold for computation
206  double lqr_eps_ = 0.0;
207 
209 
210  std::unique_ptr<Interpolation1D> lat_err_interpolation_;
211 
212  std::unique_ptr<Interpolation1D> heading_err_interpolation_;
213 
214  // MeanFilter heading_rate_filter_;
217 
218  // Lead/Lag controller
219  bool enable_leadlag_ = false;
221 
222  // Mrac controller
223  bool enable_mrac_ = false;
225 
226  // Look-ahead controller
228 
229  // for compute the differential valute to estimate acceleration/lon_jerk
231 
234 
237 
238  // for logging purpose
239  std::ofstream steer_log_file_;
240 
241  const std::string name_;
242 
244 
245  double pre_steer_angle_ = 0.0;
246 
248 
250 
252 
253  double init_vehicle_x_ = 0.0;
254 
255  double init_vehicle_y_ = 0.0;
256 
257  double init_vehicle_heading_ = 0.0;
258 
259  double low_speed_bound_ = 0.0;
260 
261  double low_speed_window_ = 0.0;
262 
263  double driving_orientation_ = 0.0;
264 
265  std::shared_ptr<DependencyInjector> injector_;
266 };
267 
268 } // namespace control
269 } // namespace apollo
double lookahead_station_high_speed_
Definition: lat_controller.h:172
Eigen::MatrixXd matrix_b_
Definition: lat_controller.h:185
void UpdateState(SimpleLateralDebug *debug)
double previous_ref_heading_rate_
Definition: lat_controller.h:233
int preview_window_
Definition: lat_controller.h:165
bool enable_look_ahead_back_control_
Definition: lat_controller.h:227
Eigen::MatrixXd matrix_state_
Definition: lat_controller.h:201
Eigen::MatrixXd matrix_k_
Definition: lat_controller.h:191
void ComputeLateralErrors(const double x, const double y, const double theta, const double linear_v, const double angular_v, const double linear_a, const TrajectoryAnalyzer &trajectory_analyzer, SimpleLateralDebug *debug)
double cr_
Definition: lat_controller.h:145
common::Status Reset() override
reset Lateral Controller
common::Status Init(std::shared_ptr< DependencyInjector > injector, const ControlConf *control_conf) override
initialize Lateral Controller
const std::string name_
Definition: lat_controller.h:241
std::ofstream steer_log_file_
Definition: lat_controller.h:239
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void Stop() override
stop Lateral controller
Eigen::MatrixXd matrix_q_
Definition: lat_controller.h:195
double cf_
Definition: lat_controller.h:143
The MeanFilter class is used to smoothen a series of noisy numbers, such as sensor data or the output...
Definition: mean_filter.h:45
double driving_orientation_
Definition: lat_controller.h:263
double iz_
Definition: lat_controller.h:155
double previous_ref_heading_acceleration_
Definition: lat_controller.h:236
Eigen::MatrixXd matrix_a_coeff_
Definition: lat_controller.h:199
bool LoadControlConf(const ControlConf *control_conf)
std::string Name() const override
Lateral controller name.
double lqr_eps_
Definition: lat_controller.h:206
common::MeanFilter heading_error_filter_
Definition: lat_controller.h:216
double low_speed_bound_
Definition: lat_controller.h:259
double ComputeFeedForward(double ref_curvature) const
Eigen::MatrixXd matrix_r_
Definition: lat_controller.h:193
common::MeanFilter lateral_error_filter_
Definition: lat_controller.h:215
Defines the MeanFilter class.
Eigen::MatrixXd matrix_bdc_
Definition: lat_controller.h:189
base class for all controllers.
Definition: controller.h:46
double pre_steering_position_
Definition: lat_controller.h:247
double low_speed_window_
Definition: lat_controller.h:261
int lqr_max_iteration_
Definition: lat_controller.h:204
double steer_single_direction_max_degree_
Definition: lat_controller.h:159
double lookback_station_high_speed_
Definition: lat_controller.h:173
TrajectoryAnalyzer trajectory_analyzer_
Definition: lat_controller.h:137
Eigen::MatrixXd matrix_q_updated_
Definition: lat_controller.h:197
LQR-Based lateral controller, to compute steering target. For more details, please refer to "Vehicle ...
Definition: lat_controller.h:53
double pre_steer_angle_
Definition: lat_controller.h:245
common::Status ComputeControlCommand(const localization::LocalizationEstimate *localization, const canbus::Chassis *chassis, const planning::ADCTrajectory *trajectory, ControlCommand *cmd) override
compute steering target based on current vehicle status and target trajectory
double init_vehicle_x_
Definition: lat_controller.h:253
const int basic_state_size_
Definition: lat_controller.h:177
std::unique_ptr< Interpolation1D > heading_err_interpolation_
Definition: lat_controller.h:212
double query_relative_time_
Definition: lat_controller.h:243
common::VehicleParam vehicle_param_
Definition: lat_controller.h:134
double lf_
Definition: lat_controller.h:151
std::unique_ptr< Interpolation1D > lat_err_interpolation_
Definition: lat_controller.h:210
double previous_heading_rate_
Definition: lat_controller.h:232
double lookback_station_low_speed_
Definition: lat_controller.h:171
A mrac controller for actuation system (throttle/brake and steering)
Definition: mrac_controller.h:43
Eigen::MatrixXd matrix_adc_
Definition: lat_controller.h:183
double previous_lateral_acceleration_
Definition: lat_controller.h:230
Eigen::MatrixXd matrix_a_
Definition: lat_controller.h:179
std::shared_ptr< DependencyInjector > injector_
Definition: lat_controller.h:265
process point query and conversion related to trajectory
Definition: trajectory_analyzer.h:44
double steer_ratio_
Definition: lat_controller.h:157
double lookahead_station_low_speed_
Definition: lat_controller.h:170
double lr_
Definition: lat_controller.h:153
Defines the DigitalFilter class.
double init_vehicle_heading_
Definition: lat_controller.h:257
void ProcessLogs(const SimpleLateralDebug *debug, const canbus::Chassis *chassis)
Eigen::MatrixXd matrix_ad_
Definition: lat_controller.h:181
Defines the Controller base class.
double previous_heading_acceleration_
Definition: lat_controller.h:235
double mass_
Definition: lat_controller.h:149
double init_vehicle_y_
Definition: lat_controller.h:255
Functions to generate coefficients for digital filter.
bool enable_mrac_
Definition: lat_controller.h:223
bool enable_leadlag_
Definition: lat_controller.h:219
Eigen::MatrixXd matrix_bd_
Definition: lat_controller.h:187
void LoadLatGainScheduler(const LatControllerConf &lat_controller_conf)
const ControlConf * control_conf_
Definition: lat_controller.h:131
double current_trajectory_timestamp_
Definition: lat_controller.h:251
A lead/lag controller for speed and steering using defualt integral hold.
Definition: leadlag_controller.h:38
The DigitalFilter class is used to pass signals with a frequency lower than a certain cutoff frequenc...
Definition: digital_filter.h:40
LeadlagController leadlag_controller_
Definition: lat_controller.h:220
double minimum_speed_protection_
Definition: lat_controller.h:249
void InitializeFilters(const ControlConf *control_conf)
A general class to denote the return status of an API call. It can either be an OK status for success...
Definition: status.h:43
virtual ~LatController()
destructor
common::DigitalFilter digital_filter_
Definition: lat_controller.h:208
double wheelbase_
Definition: lat_controller.h:147
MracController mrac_controller_
Definition: lat_controller.h:224
double max_lat_acc_
Definition: lat_controller.h:162
double ts_
Definition: lat_controller.h:141
Defines the TrajectoryAnalyzer class.