PID
Constructorsโ
PID()โ
Build PID
object given constants and the sample_time.
- Prototype
- Example
PID(const float _kp, const float _ki, const float _kd,unsigned int _sample_time, const float _scale);
lightning::PID position_controller (2,1,1,10,1);
Parameters | |
---|---|
_kp | The proportional constant. |
_ki | The integral constant. |
_kd | The integral constant. |
_sample_time | The derivative constant. |
_scale | A scaling factor that limits the output of the PID controller. For example, a value of 0.5 limits the output to 50% of its maximum value. |
PID Copy constructor
- Prototype
- Example
PID(const PID& other);
lightning::PID controller (2,1,1,10,1);
//Using copy constructor
lightning:: PID other_controller(controller);
Parameters | |
---|---|
other | other PID object. |
Functionsโ
update()โ
Updates the PID controller.
You should put this function inside of while loop to update the PID input.
With this function you will update the proportional, integral, derivative and output value.
- Prototype
- Example
virtual void update(const float error);
//A Function to control the arms position using a PID.
void move_arm(PID& arm_controller, float target){
arm_controller.set_integral_zone(target * .3);
arm_controller.initialization();
//some function to get current data.
float current_position = get_current_arm_position();
float error = target - current_position;
while (!arm_controller.target_arrived()) {
current_position= get_current_arm_position();
error = target - current_position;
arm_controller.update(error); //Updating pid controller.
left_arm.move_velocity(arm_controller.output());
right_arm.move_velocity(arm_controller.output());
pros::delay(turn_control.get_sample_time());
}
stop_arms();
}
Parameters | |
---|---|
error | The error of PID controller (target - actual reading). |
target_arrived()โ
Checks if the target was reached.
- Prototype
- Example
bool target_arrived() const;
//A Function to control the arms position using a PID.
void move_arm(PID& arm_controller, float target){
arm_controller.set_integral_zone(target * .3);
arm_controller.initialization();
//some function to get current data.
float current_position = get_current_arm_position();
float error = target - current_position;
while (!arm_controller.target_arrived()) { //Have the arms arrived?.
current_position= get_current_arm_position();
error = target - current_position;
arm_controller.update(error); //Updating pid controller.
left_arm.move_velocity(arm_controller.output());
right_arm.move_velocity(arm_controller.output());
pros::delay(turn_control.get_sample_time());
}
stop_arms();
}
Returns: True if the target was reached, False if the target wasnยดt reached.
initialization()โ
Initialize the controller.
This function is neccesary to start in any procces or function where the controller is involve.
- Prototype
- Example
void initialization();
//A Function to control the arms position using a PID.
void move_arm(PID& arm_controller, float target){
arm_controller.set_integral_zone(target * .3);
arm_controller.initialization();
//some function to get current data.
float current_position = get_current_arm_position();
float error = target - current_position;
while (!arm_controller.target_arrived()) { //Have the arms arrived?.
current_position= get_current_arm_position();
error = target - current_position;
arm_controller.update(error); //Updating pid controller.
left_arm.move_velocity(arm_controller.output());
right_arm.move_velocity(arm_controller.output());
pros::delay(turn_control.get_sample_time());
}
stop_arms();
}
Settersโ
set_stop_time()โ
Sets the stop time limit for the PID controller.
This function allows the user to set the maximum time limit after which the PID controller output will be set to zero.
- Prototype
- Example
void set_stop_time(const float _stop_time_msec);
lightning::PID arm_controller (2,1,1,10,1);
void autonomous(){
arm_controller.set_stop_time(3000);
}
Parameters | |
---|---|
_stop_time_msec | The stop time limit in milliseconds. If more than this time elapses,the PID controller output will be forced to zero |
set_error_tolerance()โ
Sets the error tolerance.
For example: In a distance controller maybe the error tolerance would be .02 inches
- Prototype
- Example
void set_error_tolerance(const float _error_tolerance);
lightning::PID arm_controller (2,1,1,10,1);
void autonomous(){
arm_controller.set_stop_time(3000);
arm_controller.set_error_tolerance(10);
}
Parameters | |
---|---|
_error_tolerance | The error tolerance for the controller. |
set_derivative_tolerance()โ
Sets derivative tolerance.
- Prototype
- Example
void set_derivative_tolerance(const float _derivative_tolerance);
lightning::PID arm_controller (2,1,1,10,1);
void autonomous(){
arm_controller.set_stop_time(3000);
arm_controller.set_error_tolerance(10);
arm_controller.set_derivative_tolerance(50);
}
Parameters | |
---|---|
_derivative_tolerance | The derivative tolerance. |
set_integral_zone()โ
Sets integral zone.
The integral zone is the zone where the intregal will not act.
- Prototype
- Example
void set_integral_zone(const float _integral_zone);
//A Function to control the arms position using a PID.
void move_arm(PID& arm_controller, float target){
arm_controller.set_integral_zone(target * .3);
arm_controller.initialization();
//some function to get current data.
float current_position = get_current_arm_position();
float error = target - current_position;
while (!arm_controller.target_arrived()) { //Have the arms arrived?.
current_position= get_current_arm_position();
error = target - current_position;
arm_controller.update(error); //Updating pid controller.
left_arm.move_velocity(arm_controller.output());
right_arm.move_velocity(arm_controller.output());
pros::delay(turn_control.get_sample_time());
}
stop_arms();
}
Parameters | |
---|---|
_integral_zone | The integral zone. |
set_integral_power_limitโ
Sets integral power limit.
The integral power limit would be the highest value of the integral.
- Prototype
- Example
void set_integral_power_limit(const float _integral_power_limit);
lightning::PID arm_controller (2,1,.135,10,1);
void autonomous(){
arm_controller.set_stop_time(3000);
arm_controller.set_error_tolerance(10);
arm_controller.set_derivative_tolerance(50);
arm_controller.set_integral_power_limit(100/arm_controller.get_ki());
}
Parameters | |
---|---|
_integral_power_limit | The integral power limit. |
set_jump_time()โ
Sets the jump time limit for the PID controller.
When the PID controller is approaching the target, a timer starts. If the timer reaches the jump time limit, the PID controller operation is halted.
- Prototype
- Example
void set_jump_time(const float _jump_time);
lightning::PID arm_controller (2,1,.135,10,1);
void autonomous(){
arm_controller.set_stop_time(3000);
arm_controller.set_error_tolerance(10);
arm_controller.set_derivative_tolerance(50);
arm_controller.set_integral_power_limit(100/arm_controller.get_ki());
arm_controller.set_jump_time(500);
}
Parameters | |
---|---|
_jump_time | The jump time limit in milliseconds. |
set_max()โ
Sets the maximum output for the PID output.
- Prototype
- Example
void set_max(const float _max);
lightning::PID arm_controller (2,1,.135,10,1);
void autonomous(){
arm_controller.set_stop_time(3000);
arm_controller.set_error_tolerance(10);
arm_controller.set_derivative_tolerance(50);
arm_controller.set_integral_power_limit(100/arm_controller.get_ki());
arm_controller.set_jump_time(500);
arm_controller.set_max(300);
}
Parameters | |
---|---|
_max | The max value of the PID output. |
set_scale()โ
Set the scale for the PID controller.
- Prototype
- Example
void set_scale(const float _scale);
lightning::PID arm_controller (2,1,.135,10,1);
void autonomous(){
arm_controller.set_stop_time(3000);
arm_controller.set_error_tolerance(10);
arm_controller.set_derivative_tolerance(50);
arm_controller.set_integral_power_limit(100/arm_controller.get_ki());
arm_controller.set_jump_time(500);
arm_controller.set_max(300);
arm_controller.set_min(0);
arm_controller.set_scale(1); //Default
}
Parameters | |
---|---|
_scale | scaling factor that limits the output of the PID controller.For example, a value of 0.5 limits the output to 50% of its maximum value. |
set_kp()โ
Sets the proportional constant (kp) of the PID controller.
- Prototype
- Example
void set_kp(const float _kp);
void autonomous(){
controller.set_kp (5);
}
Parameters | |
---|---|
_kp | The new value for the proportional constant. |
set_ki()โ
Sets the integral constant (ki) of the PID controller.
- Prototype
- Example
void set_ki(const float _ki);
void autonomous(){
controller.set_kp(5);
controller.set_ki(.012);
}
Parameters | |
---|---|
_ki | The new value for the integral constant. |
set_kdโ
Sets the derivative constant (kd) of the PID controller.
- Prototype
- Example
void set_kd(const float _kd);
void autonomous(){
controller.set_kp(5);
controller.set_ki(.012);
controller.set_kd(1);
}
Parameters | |
---|---|
_kd | The new value for the derivative constant. |
set_sample_timeโ
Sets the sample time of the PID controller.
- Prototype
- Example
void set_sample_time(const unsigned int time_msec);
void autonomous(){
controller.set_kp(5);
controller.set_ki(.012);
controller.set_kd(1);
controller.set_sample_time(10);
}
Parameters | |
---|---|
time_msec | The new sample time in milliseconds. |
Gettersโ
get_error()โ
Gets the current error.
- Prototype
- Example
float get_error() const;
float current_error = controller.get_error();
Returns: The current error.
get_kp()โ
Gets proportional constant [Kp].
- Prototype
- Example
float get_kp() const;
float kp_constant = controller.get_kp();
Returns: The kp constant.
get_ki()โ
Gets proportional constant [Ki].
- Prototype
- Example
float get_ki() const;
float ki_constant = controller.get_ki();
Returns: The ki constant.
get_kd()โ
Gets proportional constant [Kd].
- Prototype
- Example
float get_kd() const;
float kd_constant = controller.get_kd();
Returns: The kd constant.
get_proportion()โ
Gets the proportion part of the output.
- Prototype
- Example
double get_proportion() const;
//A Function to control the arms position using a PID.
void move_arm(PID& arm_controller, float target){
arm_controller.set_integral_zone(target * .3);
arm_controller.initialization();
//some function to get current data.
float current_position = get_current_arm_position();
float error = target - current_position;
while (!arm_controller.target_arrived()) { //Have the arms arrived?.
current_position= get_current_arm_position();
error = target - current_position;
arm_controller.update(error); //Updating pid controller.
float proportion = arm.controller.get_proportion(); //Proportion part
left_arm.move_velocity(proportion); //P controller
right_arm.move_velocity(proportion); // P controller
pros::delay(turn_control.get_sample_time());
}
stop_arms();
}
Returns: The proportion value.
get_integral()โ
Gets the proportion part of the output.
- Prototype
- Example
double get_integral() const;
//A Function to control the arms position using a PID.
void move_arm(PID& arm_controller, float target){
arm_controller.set_integral_zone(target * .3);
arm_controller.initialization();
//some function to get current data.
float current_position = get_current_arm_position();
float error = target - current_position;
while (!arm_controller.target_arrived()) { //Have the arms arrived?.
current_position= get_current_arm_position();
error = target - current_position;
arm_controller.update(error); //Updating pid controller.
float proportion = arm.controller.get_proportion(); //Proportion part
float integral = arm.controller.get_integral(); // Integral part
left_arm.move_velocity(proportion+integral); //PI controller
right_arm.move_velocity(proportion+integral); // PI controller
pros::delay(turn_control.get_sample_time());
}
stop_arms();
}
Returns: The integral value.
get_derivative()โ
Gets the proportion part of the output.
- Prototype
- Example
double get_derivative() const;
//A Function to control the arms position using a PID.
void move_arm(PID& arm_controller, float target){
arm_controller.set_integral_zone(target * .3);
arm_controller.initialization();
//some function to get current data.
float current_position = get_current_arm_position();
float error = target - current_position;
while (!arm_controller.target_arrived()) { //Have the arms arrived?.
current_position= get_current_arm_position();
error = target - current_position;
arm_controller.update(error); //Updating pid controller.
float proportion = arm.controller.get_proportion(); //Proportion part
float integral = arm.controller.get_integral(); // Integral part
float derivative = arm.controller.get_derivative(); //Derivative part
left_arm.move_velocity(proportion+integral+derivative); //PID controller
right_arm.move_velocity(proportion+integral+derivative); // PID controller
pros::delay(turn_control.get_sample_time());
}
stop_arms();
}
Returns: The derivative value.
get_sample_time()โ
Gets the sample time.
- Prototype
- Example
unsigned int get_sample_time() const;
int controller_sample_time = controller.get_sample_time();
Returns: The sample time.
get_output()โ
Gets the current output from controller.
- Prototype
- Example
double get_output() const;
//A Function to control the arms position using a PID.
void move_arm(PID& arm_controller, float target){
arm_controller.set_integral_zone(target * .3);
arm_controller.initialization();
//some function to get current data.
float current_position = get_current_arm_position();
float error = target - current_position;
while (!arm_controller.target_arrived()) { //Have the arms arrived?.
current_position= get_current_arm_position();
error = target - current_position;
arm_controller.update(error); //Updating pid controller.
float output = arm_controller.get_output();
left_arm.move_velocity(output);
right_arm.move_velocity(output);
pros::delay(turn_control.get_sample_time());
}
stop_arms();
}
Returns: The current output from controller.
get_error_tolerance()โ
Retrieves the error tolerance of the PID controller.
- Prototype
- Example
float get_error_tolerance() const;
float error_tolerance = controller.get_error_tolerance();
Returns: The current error tolerance value.
get_derivative_tolerance()โ
- Prototype
- Example
float get_derivative_tolerance() const;
float derivative_tolerance = controller.get_derivative_tolerance();
Returns: The current derivative tolerance value.
get_integral_zone()โ
Retrieves the integral zone of the PID controller.
- Prototype
- Example
float get_integral_zone() const;
float integral_zone = controller.get_integral_zone();
Returns: The current integral zone value.
get_integral_power_limit()โ
Retrieves the integral power limit of the PID controller.
- Prototype
- Example
float get_integral_power_limit() const;
float integral_power_limit = controller.get_integral_power_limit();
Returns: The current integral power limit value.
get_max()โ
Retrieves the maximum value allowed for the PID controller output.
- Prototype
- Example
float get_max() const;
float max_value = controller.get_max();
Returns: The maximum values allowed.