compute steering target and throttle/ brake based on current vehicle status and target trajectory
341 {
343 std::move(TrajectoryAnalyzer(planning_published_trajectory));
344 auto vehicle_state =
injector_->vehicle_state();
345
346
347
353 }
354
355
356
357
359
360
361
362
363
364
365
366
367
372 } else {
373
374
375
376
377
378
379
380
381
386 }
387
388 SimpleMPCDebug *debug = cmd->mutable_debug()->mutable_simple_mpc_debug();
389 debug->Clear();
390
393 } else {
395 }
396
397
399
401
403
404
405 if (FLAGS_enable_gain_scheduler) {
411 vehicle_state->linear_velocity());
415 vehicle_state->linear_velocity());
418 vehicle_state->linear_velocity());
419 } else {
423 }
424
429
432
434 std::vector<Matrix> control(
horizon_, control_matrix);
435
437 std::vector<Matrix> control_gain(
horizon_, control_gain_matrix);
438
440 std::vector<Matrix> addition_gain(
horizon_, addition_gain_matrix);
441
443 std::vector<Matrix> reference(
horizon_, reference_state);
444
447
450
451 const double max = std::numeric_limits<double>::max();
454
455
456
457 lower_state_bound << -1.0 * max, -1.0 * max, -1.0 * M_PI, -1.0 * max,
458 -1.0 * max, -1.0 * max;
459 upper_state_bound << max, max, M_PI, max, max, max;
460
462 double steer_angle_feedback = 0.0;
463 double acc_feedback = 0.0;
464 double steer_angle_ff_compensation = 0.0;
465 double unconstrained_control_diff = 0.0;
466 double control_gain_truncation_ratio = 0.0;
467 double unconstrained_control = 0.0;
468 double steer_angle_feedback_augment = 0.0;
469 const double v =
injector_->vehicle_state()->linear_velocity();
470
471 std::vector<double> control_cmd(
controls_, 0);
472
478 if (!mpc_osqp.Solve(&control_cmd)) {
479 AERROR <<
"MPC OSQP solver failed";
480 } else {
481 ADEBUG <<
"MPC OSQP problem solved! ";
482 control[0](0, 0) = control_cmd.at(0);
483 control[0](1, 0) = control_cmd.at(1);
484 }
485
487 acc_feedback = control[0](1, 0);
489 unconstrained_control += control_gain[0](0, i) *
matrix_state_(i, 0);
490 }
491 unconstrained_control += addition_gain[0](0, 0) * v * debug->curvature();
493 unconstrained_control_diff =
496 steer_angle_ff_compensation =
498 (control_gain[0](0, 2) *
500 addition_gain[0](0, 0) * v));
501 } else {
502 control_gain_truncation_ratio = control[0](0, 0) / unconstrained_control;
503 steer_angle_ff_compensation =
505 (control_gain[0](0, 2) *
507 addition_gain[0](0, 0) * v) *
508 control_gain_truncation_ratio);
509 }
510 if (std::isnan(steer_angle_ff_compensation)) {
511 ADEBUG <<
"steer_angle_ff_compensation is nan";
512 steer_angle_ff_compensation = 0.0;
513 }
514 } else {
515 steer_angle_ff_compensation = 0.0;
516 }
517
519
520 ADEBUG <<
"MPC core algorithm: calculation time is: "
521 << (mpc_end_timestamp - mpc_start_timestamp) * 1000 << " ms.";
522
526 steer_angle_feedback_augment =
529 if (std::fabs(vehicle_state->linear_velocity()) >
531
532
536 }
537 }
538 }
539
540
541 double steer_angle =
543 steer_angle_ff_compensation + steer_angle_feedback_augment;
544
545 if (FLAGS_set_steer_limit) {
547 (vehicle_state->linear_velocity() *
548 vehicle_state->linear_velocity())) *
551
552
553 double steer_angle_limited =
556 steer_angle = steer_angle_limited;
557 debug->set_steer_angle_limited(steer_angle_limited);
558 }
560
562 cmd->set_steering_target(steer_angle);
563
564 debug->set_acceleration_cmd_closeloop(acc_feedback);
565
566 double vehicle_pitch = 0.0;
568 vehicle_pitch =
570 } else {
571 vehicle_pitch =
injector_->vehicle_state()->pitch();
572 }
573
574 if (std::isnan(vehicle_pitch)) {
575 AINFO <<
"pitch angle is nan.";
576 vehicle_pitch = 0;
577 }
578 debug->set_vehicle_pitch(vehicle_pitch);
579
580 double slope_offset_compensation =
GRA_ACC * std::sin(vehicle_pitch);
581 if (std::isnan(slope_offset_compensation)) {
582 slope_offset_compensation = 0;
583 }
584 debug->set_slope_offset_compensation(slope_offset_compensation);
585
586 double acceleration_cmd =
587 acc_feedback + debug->acceleration_reference() +
588 control_conf_.enable_slope_offset() * debug->slope_offset_compensation();
589
590
592
593
594
595 if ((planning_published_trajectory->trajectory_type() ==
597 (planning_published_trajectory->trajectory_type() ==
599 (planning_published_trajectory->trajectory_type() ==
602 (std::fabs(debug->preview_acceleration_reference()) <=
603 FLAGS_max_acceleration_when_stopped) &&
604 std::fabs(debug->preview_speed_reference()) <=
606 debug->set_is_full_stop(true);
607 ADEBUG <<
"Into full stop within preview acc and reference speed, "
608 << "is_full_stop is " << debug->is_full_stop();
609 }
610 if (std::abs(debug->path_remain()) < FLAGS_max_acceleration_when_stopped) {
611 debug->set_is_full_stop(true);
612 ADEBUG <<
"Into full stop within path remain, "
613 << "is_full_stop is " << debug->is_full_stop();
614 }
615 }
616
617 if (debug->is_full_stop()) {
618 acceleration_cmd =
623 }
624
625 debug->set_acceleration_cmd(acceleration_cmd);
626
629
630 double acceleration_lookup_error =
631 acceleration_cmd - debug->acceleration_vrf();
632 double acceleration_lookup_offset = 0.0;
633
636 acceleration_lookup_error,
ts_);
637 } else {
638 acceleration_lookup_offset = 0.0;
639 }
640
641 double acceleration_lookup = acceleration_lookup_offset + acceleration_cmd;
642
643 debug->set_acceleration_lookup_offset(acceleration_lookup_offset);
644
645 debug->set_acceleration_lookup(acceleration_lookup);
646
647 double calibration_value = 0.0;
648 if (FLAGS_use_preview_speed_for_table) {
650 std::make_pair(debug->preview_speed_reference(), acceleration_lookup));
651 } else {
653 injector_->vehicle_state()->linear_velocity(), acceleration_lookup));
654 }
655
656 debug->set_calibration_value(calibration_value);
657
658 double throttle_cmd = 0.0;
659 double brake_cmd = 0.0;
660 if (acceleration_cmd >= 0) {
661 if (calibration_value >= 0) {
663 } else {
665 }
666 brake_cmd = 0.0;
667 } else {
668 throttle_cmd = 0.0;
669 if (calibration_value >= 0) {
671 } else {
673 }
674 }
675
676 cmd->set_steering_rate(FLAGS_steer_angle_rate);
677
678 cmd->set_throttle(throttle_cmd);
679 cmd->set_brake(brake_cmd);
680 cmd->set_acceleration(acceleration_cmd);
681
682 debug->set_heading(vehicle_state->heading());
683 debug->set_steering_position(chassis->steering_percentage());
684 debug->set_steer_angle(steer_angle);
686 debug->set_steer_angle_feedforward_compensation(steer_angle_ff_compensation);
687 debug->set_steer_unconstrained_control_diff(unconstrained_control_diff);
688 debug->set_steer_angle_feedback(steer_angle_feedback);
689 debug->set_steering_position(chassis->steering_percentage());
690 debug->set_steer_angle_feedback_augment(steer_angle_feedback_augment);
691
692 if (std::fabs(vehicle_state->linear_velocity()) <=
695 cmd->set_gear_location(planning_published_trajectory->gear());
696 } else {
697 cmd->set_gear_location(chassis->gear_location());
698 }
699
702}
double Filter(const double x_insert)
Processes a new measurement with the filter.
static Status OK()
generate a success status.
virtual double Control(const double error, const double dt)
compute control value based on the error
PIDController acceleration_lookup_pid_controller_
TrajectoryAnalyzer trajectory_analyzer_
void FeedforwardUpdate(SimpleMPCDebug *debug)
Eigen::MatrixXd matrix_state_
Eigen::MatrixXd matrix_bd_
void GetPathRemain(const planning::ADCTrajectory *planning_published_trajectory, SimpleMPCDebug *debug)
void ProcessLogs(const SimpleMPCDebug *debug, const canbus::Chassis *chassis)
double standstill_acceleration_
void UpdateMatrix(SimpleMPCDebug *debug)
MPCControllerConf control_conf_
std::unique_ptr< Interpolation1D > heading_err_interpolation_
Eigen::MatrixXd matrix_q_
LeadlagController leadlag_controller_
std::unique_ptr< Interpolation1D > feedforwardterm_interpolation_
Eigen::MatrixXd matrix_ad_
std::unique_ptr< Interpolation2D > control_interpolation_
Eigen::MatrixXd matrix_r_updated_
std::unique_ptr< Interpolation1D > lat_err_interpolation_
common::DigitalFilter digital_filter_pitch_angle_
bool enable_mpc_feedforward_compensation_
const int basic_state_size_
std::unique_ptr< Interpolation1D > steer_weight_interpolation_
bool use_pitch_angle_filter_
double Wheel2SteerPct(const double wheel_angle)
Eigen::MatrixXd matrix_r_
double steer_angle_feedforwardterm_updated_
double wheel_single_direction_max_degree_
common::VehicleParam vehicle_param_
common::Status Reset() override
reset MPC Controller
double throttle_lowerbound_
Eigen::MatrixXd matrix_a_
std::shared_ptr< DependencyInjector > injector_
double steer_single_direction_max_degree_
double steer_angle_feedforwardterm_
common::DigitalFilter digital_filter_
Eigen::MatrixXd matrix_q_updated_
Eigen::MatrixXd matrix_a_coeff_
double unconstrained_control_diff_limit_
void UpdateState(SimpleMPCDebug *debug)
void ComputeLongitudinalErrors(const TrajectoryAnalyzer *trajectory, SimpleMPCDebug *debug)
void SetPID(const PidConf &pid_conf)
set pid controller coefficients for the proportional, integral, and derivative
virtual double Control(const double error, const double dt)
compute control value based on the error
void TrajectoryTransformToCOM(const double rear_to_com_distance)
Transform the current trajectory points to the center of mass(COM) of the vehicle,...
static double NowInSeconds()
gets the current time in second.
T lerp(const T &x0, const double t0, const T &x1, const double t1, const double t)
Linear interpolation between two points of type T.
T Clamp(const T value, T bound1, T bound2)
Clamp a value between two bounds.
optional float max_abs_speed_when_stopped