Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
pid.cpp
Go to the documentation of this file.
2
3using namespace common_lib::structures;
4
9PID::PID(const ControlParameters& params) : LongitudinalController(params) {}
10
19double PID::update(double setpoint, double measurement) {
20 /*
21 * Error signal
22 */
23 double error = calculate_error(setpoint, measurement);
24
25 this->calculate_proportional_term(error);
26
27 this->calculate_integral_term(error);
28
29 /*
30 * Derivative term , derivative on measurement
31 */
32 this->calculate_derivative_term(measurement);
33
34 /*
35 * Anti-wind-up integrator
36 */
37 this->anti_wind_up();
38
39 /*
40 * Compute output and apply limits
41 */
42 this->compute_output();
43
44 /*
45 * Store error and measurement for the next iteration
46 */
47 this->prev_error_ = error;
48 this->prev_measurement_ = measurement;
49
50 /*
51 * return output value
52 */
53 return this->out_;
54}
55
56double PID::calculate_error(double setpoint, double measurement) const {
57 double error = setpoint - measurement;
58 if (error > this->params_->pid_max_positive_error_) {
59 error = this->params_->pid_max_positive_error_;
60 } else if (error < this->params_->pid_max_negative_error_) {
61 error = this->params_->pid_max_negative_error_;
62 }else {
63 // do nothing, error within limits
64 }
65 return error;
66}
67
69 this->proportional_ = this->params_->pid_kp_ * error;
70}
71
72void PID::calculate_integral_term(double error) {
73 this->integrator_ = this->integrator_ + 0.5f * this->params_->pid_ki_ * this->params_->pid_t_ *
74 (error + this->prev_error_);
75}
76
78 double curr_output = this->proportional_ + this->integrator_ + this->differentiator_;
79 if (curr_output > this->params_->pid_lim_max_ || curr_output < this->params_->pid_lim_min_) {
80 this->integrator_ = this->integrator_ * this->params_->pid_anti_windup_;
81 }
82}
83
84void PID::calculate_derivative_term(double measurement) {
85 this->differentiator_ =
86 (-2.0f * this->params_->pid_kd_ * (measurement - this->prev_measurement_) +
87 (2.0f * this->params_->pid_tau_ - this->params_->pid_t_) * this->differentiator_) /
88 (2.0f * this->params_->pid_tau_ + this->params_->pid_t_);
89}
90
92 this->out_ = this->proportional_ + this->integrator_ + this->differentiator_;
93
94 if (this->out_ > this->params_->pid_lim_max_) {
95 this->out_ = this->params_->pid_lim_max_;
96 } else if (this->out_ < this->params_->pid_lim_min_) {
97 this->out_ = this->params_->pid_lim_min_;
98 } else {
99 // do nothing, output within limits
100 }
101}
102
103void PID::path_callback(const custom_interfaces::msg::PathPointArray& msg) {
104 this->last_path_msg_ = msg.pathpoint_array;
105 this->received_first_path_ = true;
106}
107void PID::vehicle_state_callback(const custom_interfaces::msg::Velocities& msg) {
108 this->last_velocity_msg_ = msg;
109 this->absolute_velocity_ = std::sqrt(msg.velocity_x * msg.velocity_x + msg.velocity_y * msg.velocity_y);
110 this->received_first_state_ = true;
111}
112void PID::vehicle_pose_callback(const custom_interfaces::msg::Pose& msg) {
113 this->last_pose_msg_ = msg;
114 this->received_first_pose_ = true;
115}
116
119
121 Position vehicle_cog = Position(this->last_pose_msg_.x, this->last_pose_msg_.y);
122 Position rear_axis = ::rear_axis_position(vehicle_cog, this->last_pose_msg_.theta,
123 this->params_->car_parameters_.dist_cg_2_rear_axis);
124
125 auto [closest_point, closest_point_id, closest_point_velocity] =
126 ::get_closest_point(this->last_path_msg_, rear_axis);
127
128 if (closest_point_id != -1) {
129 command.throttle_rl = command.throttle_rr = update(closest_point_velocity, this->absolute_velocity_);
130 }
131 }
132
133 return command;
134}
135
136void PID::publish_solver_data(std::shared_ptr<rclcpp::Node> node, std::map<std::string, std::shared_ptr<rclcpp::PublisherBase>>& publisher_map) {}
Base (abstract) class for longitudinal controllers (the ones that calculate throttle)
std::shared_ptr< ControlParameters > params_
void calculate_integral_term(double error)
Calculate the integral term.
Definition pid.cpp:72
std::vector< custom_interfaces::msg::PathPoint > last_path_msg_
Definition pid.hpp:20
PID(const ControlParameters &params)
Construct a new PID object.
Definition pid.cpp:9
void publish_solver_data(std::shared_ptr< rclcpp::Node > node, std::map< std::string, std::shared_ptr< rclcpp::PublisherBase > > &publisher_map) override
Publishes solver specific data using the provided ControlNode.
Definition pid.cpp:136
bool received_first_pose_
Definition pid.hpp:27
void calculate_derivative_term(double measurement)
Calculate the derivative term (derivative on measurement)
Definition pid.cpp:84
custom_interfaces::msg::Pose last_pose_msg_
Definition pid.hpp:22
void compute_output()
Compute the output value and apply limits.
Definition pid.cpp:91
double differentiator_
Differentiator term current value.
Definition pid.hpp:31
double proportional_
Proportional term current value.
Definition pid.hpp:29
void path_callback(const custom_interfaces::msg::PathPointArray &msg) override
Called when a new path is sent by Path Planning.
Definition pid.cpp:103
double update(double setpoint, double measurement)
Calculate the output value.
Definition pid.cpp:19
void vehicle_state_callback(const custom_interfaces::msg::Velocities &msg) override
Called when the car state (currently just velocity) is updated.
Definition pid.cpp:107
void vehicle_pose_callback(const custom_interfaces::msg::Pose &msg) override
Called when the car pose is updated by SLAM.
Definition pid.cpp:112
bool received_first_path_
Definition pid.hpp:25
double absolute_velocity_
Definition pid.hpp:23
double out_
Current output value.
Definition pid.hpp:36
common_lib::structures::ControlCommand get_throttle_command() override
Returns the throttle command calculated by the solver (only throttle)
Definition pid.cpp:117
double integrator_
Integrator term current value.
Definition pid.hpp:30
custom_interfaces::msg::Velocities last_velocity_msg_
Definition pid.hpp:21
void anti_wind_up()
Anti-wind-up via dynamic integrator clamping.
Definition pid.cpp:77
double prev_measurement_
Previous measurement value, required for defferentiator.
Definition pid.hpp:34
bool received_first_state_
Definition pid.hpp:26
double calculate_error(double setpoint, double measurement) const
Calculate the error signal.
Definition pid.cpp:56
double prev_error_
Previous error value, required for integrator.
Definition pid.hpp:33
void calculate_proportional_term(double error)
Calculate the proportional term.
Definition pid.cpp:68
std::tuple< common_lib::structures::Position, int, double > get_closest_point(const std::vector< custom_interfaces::msg::PathPoint > &pathpoint_array, const common_lib::structures::Position &position)
Find the closest point on the path.
Definition utils.cpp:14
common_lib::structures::Position rear_axis_position(const common_lib::structures::Position &cg, double orientation, double dist_cg_2_rear_axis)
Calculate rear axis coordinates.
Definition utils.cpp:5