This repository contains the implementation of a PID controller in C++. The PID controller is a feedback control algorithm widely used in control systems to regulate a process. It calculates an output based on the error between the desired setpoint and the current value, utilizing proportional, integral, and derivative control actions.
To use the PID controller, follow these steps:
-
Include the necessary header file in your project:
#include "PID.h"
-
Create an instance of the PID controller by providing the appropriate coefficients: kp (proportional gain), ki (integral gain), and kd (derivative gain):
PID pidController(kp, ki, kd);
-
Use the
compute
method to calculate the control output based on the current value and the desired setpoint:double controlOutput = pidController.compute(currValue, desiredValue);
currValue
is the current value of the process being controlled.desiredValue
is the desired setpoint.
-
(Optional) Reset the PID controller coefficients to new values using the
reset
method:pidController.reset(newKp, newKi, newKd);
newKp
,newKi
, andnewKd
are the new values for the proportional, integral
To compile the PID controller code, you can follow these steps:
-
Create a new C++ source file in your preferred editor and include the required header file:
#include "PID.h"
-
Implement the main function and define the PID controller parameters, current value, and desired value:
int main() { double kp = 0.5; double ki = 0.2; double kd = 0.1; double currValue = 5.0; double desiredValue = 10.0; // Create an instance of the PID controller PID pidController(kp, ki, kd); // Compute the control output double controlOutput = pidController.compute(currValue, desiredValue); // Print the control output std::cout << "Control Output: " << controlOutput << std::endl; return 0; }
-
Save the source file with a
.cpp
extension, for example,main.cpp
. -
Open your terminal or command prompt and navigate to the directory where you saved the source file.
-
Compile the code using a C++ compiler, such as
g++
:g++ main.cpp PID.cpp -o pid_controller
This command compiles the main file along with
To learn more about PID (Proportional-Integral-Derivative) controllers, you can visit the following resources:
- PID Controller on Wikipedia: Find detailed information about PID controllers, including their working principles, advantages, and applications.
We encourage you to explore these resources to gain a deeper understanding of PID controllers and how they can be used in various control systems.