|
| 1 | +// Copyright 2024 The Autoware Foundation. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef SIMPLE_PLANNING_SIMULATOR__UTILS__MECHANICAL_CONTROLLER_HPP_ |
| 16 | +#define SIMPLE_PLANNING_SIMULATOR__UTILS__MECHANICAL_CONTROLLER_HPP_ |
| 17 | + |
| 18 | +#include <deque> |
| 19 | +#include <map> |
| 20 | +#include <optional> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +namespace autoware::simple_planning_simulator::utils::mechanical_controller |
| 25 | +{ |
| 26 | + |
| 27 | +using DelayBuffer = std::deque<std::pair<double, double>>; |
| 28 | +using DelayOutput = std::pair<std::optional<double>, DelayBuffer>; |
| 29 | + |
| 30 | +DelayOutput delay( |
| 31 | + const double signal, const double delay_time, const DelayBuffer & buffer, |
| 32 | + const double elapsed_time); |
| 33 | + |
| 34 | +double sign(const double x); |
| 35 | + |
| 36 | +double apply_limits( |
| 37 | + const double current_angle, const double previous_angle, const double angle_limit, |
| 38 | + const double rate_limit, const double dt); |
| 39 | + |
| 40 | +double feedforward(const double input_angle, const double ff_gain); |
| 41 | + |
| 42 | +double polynomial_transform( |
| 43 | + const double torque, const double speed, const double a, const double b, const double c, |
| 44 | + const double d, const double e, const double f, const double g, const double h); |
| 45 | + |
| 46 | +struct PIDControllerParams |
| 47 | +{ |
| 48 | + double kp{0.0}; |
| 49 | + double ki{0.0}; |
| 50 | + double kd{0.0}; |
| 51 | +}; |
| 52 | + |
| 53 | +struct PIDControllerState |
| 54 | +{ |
| 55 | + double integral{0.0}; |
| 56 | + double error{0.0}; |
| 57 | +}; |
| 58 | + |
| 59 | +class PIDController |
| 60 | +{ |
| 61 | +public: |
| 62 | + PIDController(const double kp, const double ki, const double kd); |
| 63 | + |
| 64 | + [[nodiscard]] double compute( |
| 65 | + const double error, const double integral, const double prev_error, const double dt) const; |
| 66 | + |
| 67 | + void update_state(const double error, const double dt); |
| 68 | + |
| 69 | + void update_state( |
| 70 | + const double k1_error, const double k2_error, const double k3_error, const double k4_error, |
| 71 | + const double dt); |
| 72 | + |
| 73 | + [[nodiscard]] PIDControllerState get_state() const; |
| 74 | + |
| 75 | + void clear_state(); |
| 76 | + |
| 77 | +private: |
| 78 | + double kp_{0.0}, ki_{0.0}, kd_{0.0}; |
| 79 | + PIDControllerState state_; |
| 80 | +}; |
| 81 | + |
| 82 | +struct SteeringDynamicsParams |
| 83 | +{ |
| 84 | + double angular_position{0.0}; |
| 85 | + double angular_velocity{0.0}; |
| 86 | + double inertia{0.0}; |
| 87 | + double damping{0.0}; |
| 88 | + double stiffness{0.0}; |
| 89 | + double friction{0.0}; |
| 90 | + double dead_zone_threshold{0.0}; |
| 91 | +}; |
| 92 | + |
| 93 | +struct SteeringDynamicsState |
| 94 | +{ |
| 95 | + double angular_position{0.0}; |
| 96 | + double angular_velocity{0.0}; |
| 97 | + bool is_in_dead_zone{false}; |
| 98 | +}; |
| 99 | + |
| 100 | +struct SteeringDynamicsDeltaState |
| 101 | +{ |
| 102 | + double d_angular_position{0.0}; |
| 103 | + double d_angular_velocity{0.0}; |
| 104 | +}; |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief SteeringDynamics class |
| 108 | + * @details This class calculates the dynamics which receives the steering torque and outputs the |
| 109 | + * steering tire angle. The steering system is modeled as a spring-damper system with friction and |
| 110 | + * dead zone. |
| 111 | + */ |
| 112 | +class SteeringDynamics |
| 113 | +{ |
| 114 | +public: |
| 115 | + SteeringDynamics( |
| 116 | + const double angular_position, const double angular_velocity, const double inertia, |
| 117 | + const double damping, const double stiffness, const double friction, |
| 118 | + const double dead_zone_threshold); |
| 119 | + |
| 120 | + [[nodiscard]] bool is_in_dead_zone( |
| 121 | + const SteeringDynamicsState & state, const double input_torque) const; |
| 122 | + |
| 123 | + [[nodiscard]] SteeringDynamicsDeltaState calc_model( |
| 124 | + const SteeringDynamicsState & state, const double input_torque) const; |
| 125 | + |
| 126 | + void set_state(const SteeringDynamicsState & state); |
| 127 | + |
| 128 | + [[nodiscard]] SteeringDynamicsState get_state() const; |
| 129 | + |
| 130 | + void clear_state(); |
| 131 | + |
| 132 | + void set_steer(const double steer); |
| 133 | + |
| 134 | +private: |
| 135 | + SteeringDynamicsState state_; |
| 136 | + const double inertia_; |
| 137 | + const double damping_; |
| 138 | + const double stiffness_; |
| 139 | + const double friction_; |
| 140 | + const double dead_zone_threshold_; |
| 141 | +}; |
| 142 | + |
| 143 | +struct StepResult |
| 144 | +{ |
| 145 | + DelayBuffer delay_buffer{}; |
| 146 | + double pid_error{0.0}; |
| 147 | + SteeringDynamicsDeltaState dynamics_d_state{}; |
| 148 | + bool is_in_dead_zone{false}; |
| 149 | +}; |
| 150 | + |
| 151 | +struct MechanicalParams |
| 152 | +{ |
| 153 | + double kp{0.0}; |
| 154 | + double ki{0.0}; |
| 155 | + double kd{0.0}; |
| 156 | + double ff_gain{0.0}; |
| 157 | + double dead_zone_threshold{0.0}; |
| 158 | + double poly_a{0.0}; |
| 159 | + double poly_b{0.0}; |
| 160 | + double poly_c{0.0}; |
| 161 | + double poly_d{0.0}; |
| 162 | + double poly_e{0.0}; |
| 163 | + double poly_f{0.0}; |
| 164 | + double poly_g{0.0}; |
| 165 | + double poly_h{0.0}; |
| 166 | + double inertia{0.0}; |
| 167 | + double damping{0.0}; |
| 168 | + double stiffness{0.0}; |
| 169 | + double friction{0.0}; |
| 170 | + double delay_time{0.0}; |
| 171 | + |
| 172 | + // limit |
| 173 | + double angle_limit{0.0}; |
| 174 | + double rate_limit{0.0}; |
| 175 | + double steering_torque_limit{0.0}; |
| 176 | + double torque_delay_time{0.0}; |
| 177 | +}; |
| 178 | + |
| 179 | +class MechanicalController |
| 180 | +{ |
| 181 | +public: |
| 182 | + explicit MechanicalController(const MechanicalParams & mechanical_params); |
| 183 | + |
| 184 | + [[maybe_unused]] double update_euler( |
| 185 | + const double input_angle, const double speed, const double prev_input_angle, const double dt); |
| 186 | + |
| 187 | + double update_runge_kutta( |
| 188 | + const double input_angle, const double speed, const double prev_input_angle, const double dt); |
| 189 | + |
| 190 | + void clear_state(); |
| 191 | + |
| 192 | + void set_steer(const double steer); |
| 193 | + |
| 194 | +private: |
| 195 | + DelayBuffer delay_buffer_; |
| 196 | + PIDController pid_; |
| 197 | + SteeringDynamics steering_dynamics_; |
| 198 | + const MechanicalParams params_; |
| 199 | + |
| 200 | + [[nodiscard]] StepResult run_one_step( |
| 201 | + const double input_angle, const double speed, const double prev_input_angle, const double dt, |
| 202 | + const DelayBuffer & delay_buffer, const PIDController & pid, |
| 203 | + const SteeringDynamics & dynamics) const; |
| 204 | +}; |
| 205 | + |
| 206 | +} // namespace autoware::simple_planning_simulator::utils::mechanical_controller |
| 207 | + |
| 208 | +#endif // SIMPLE_PLANNING_SIMULATOR__UTILS__MECHANICAL_CONTROLLER_HPP_ |
0 commit comments