Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
pid_controller.cpp
Go to the documentation of this file.
1/*------------------------------------------------------------------------------
2* Copyright (C) 2024 LEIDOS.
3*
4* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5* use this file except in compliance with the License. You may obtain a copy of
6* 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, WITHOUT
12* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13* License for the specific language governing permissions and limitations under
14* the License.
15
16------------------------------------------------------------------------------*/
17
19
20namespace platooning_control
21{
23
24 double PIDController::calculate( double setpoint, double pv ){
25 // Calculate error
26 double error = setpoint - pv;
27 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("platooning_control"),"PID error: " << error);
28
29 // Proportional term
30 double Pout = config_->kp * error;
31 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("platooning_control"), "Proportional term: " << Pout);
32
33 // Integral term
34 _integral += error * config_->dt;
35 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("platooning_control"),"Integral term: " << _integral);
36
37 if (_integral > config_->integrator_max){
38 _integral = config_->integrator_max;
39 }
40 else if (_integral < config_->integrator_min){
41 _integral = config_->integrator_min;
42 }
43 double Iout = config_->ki * _integral;
44 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("platooning_control"), "Iout: " << Iout);
45
46 // Derivative term
47 double derivative = (error - _pre_error) / config_->dt;
48 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("platooning_control"), "derivative term: " << derivative);
49 double Dout = config_->kd * derivative;
50 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("platooning_control"), "Dout: " << Dout);
51
52 // Calculate total output
53 double output = Pout + Iout + Dout;
54 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("platooning_control"), "total controller output: " << output);
55
56 // Restrict to max/min
57 if( output > config_->max_delta_speed_per_timestep )
58 output = config_->max_delta_speed_per_timestep;
59 else if( output < config_->min_delta_speed_per_timestep )
60 output = config_->min_delta_speed_per_timestep;
61 // Save error to previous error
62 _pre_error = error;
63
64 return output;
65
66 }
67
69 _integral = 0.0;
70 _pre_error = 0.0;
71 }
72
73
74}
std::shared_ptr< PlatooningControlPluginConfig > config_
plugin config object
PIDController()
Constructor for the PID controller class.
double calculate(double setpoint, double pv)
function to calculate control command based on setpoint and process vale