The KP, KI, and KD parameters are part of a PID (Proportional-Integral-Derivative) controller, which is a widely used control algorithm in systems requiring precise regulation, such as your constant current (CC) charging setup. Below, I'll explain their roles, how to tune them, and whether default values can be reliably used.
1. Role of KP, KI, and KD
A PID controller calculates an "error" (the difference between the desired setpoint and the measured value) and uses this error to compute a control output. In your case, the setpoint is the target charging current, the measured value is the actual current, and the control output is the adjustment to the USB-C supply voltage.
The PID controller's output is the sum of three terms:
a. Proportional Term (KP)
- Role: The proportional term provides an output that is directly proportional to the current error. It reacts immediately to deviations from the setpoint.
- Formula: ( P = K_P \cdot \text{error} )
- Effect: A larger ( K_P ) results in a stronger response to errors, reducing the time it takes to reach the setpoint. However, if ( K_P ) is too large, the system can become unstable, leading to oscillations or overshoot.
- Analogy: Think of ( K_P ) as the "stiffness" of a spring—too weak, and the system is sluggish; too stiff, and it bounces excessively.
b. Integral Term (KI)
- Role: The integral term accounts for the accumulation of past errors over time. It helps eliminate steady-state error (i.e., when the system stabilizes but is slightly off the setpoint).
- Formula: ( I = K_I \cdot \int \text{error} , dt )
- Effect: A larger ( K_I ) increases the correction for persistent errors, ensuring the system eventually reaches the exact setpoint. However, if ( K_I ) is too large, it can cause overshoot or oscillations due to "windup" (accumulated error becoming too large).
- Analogy: Think of ( K_I ) as a "memory" that remembers how long the system has been off-target and pushes harder over time to correct it.
c. Derivative Term (KD)
- Role: The derivative term predicts future behavior by looking at the rate of change of the error. It acts as a damping mechanism to reduce overshoot and oscillations.
- Formula: ( D = K_D \cdot \frac{d(\text{error})}{dt} )
- Effect: A larger ( K_D ) increases damping, stabilizing the system by counteracting rapid changes. However, if ( K_D ) is too large, it can make the system sluggish or amplify noise in the measurements.
- Analogy: Think of ( K_D ) as a "brake" that slows down the system's response when it's changing too quickly.
Total PID Output
The total control output is the sum of these three terms:
[
\text{Output} = K_P \cdot \text{error} + K_I \cdot \int \text{error} , dt + K_D \cdot \frac{d(\text{error})}{dt}
]
In your case, this output is used to adjust the USB-C supply voltage to maintain constant current.
2. How to Tune KP, KI, and KD
Tuning PID parameters is both an art and a science, requiring a balance between responsiveness, stability, and accuracy. Here's a step-by-step process to tune them manually (a common method for beginners):
Step 1: Start with Only Proportional Control
- Set ( K_I = 0 ) and ( K_D = 0 ).
- Start with a small ( K_P ) (e.g., 0.1 in your code).
- Increase ( K_P ) gradually until the system starts to oscillate slightly around the setpoint (target current). This is the "critical gain" (( K_{P_{\text{critical}}} )).
- Note the oscillation period (( T_{\text{critical}} ))—the time between consecutive peaks in the oscillation.
Step 2: Add Integral Control
- Once you have a reasonable ( K_P ), introduce ( K_I ).
- Start with a small ( K_I ) (e.g., 0.01 in your code).
- Increase ( K_I ) until the steady-state error (the difference between the setpoint and the actual value after stabilization) is eliminated. Be cautious—too large a ( K_I ) can cause overshoot or oscillations.
- A rule of thumb (Ziegler-Nichols method) for initial tuning is:
[
K_P = 0.6 \cdot K_{P_{\text{critical}}}, \quad K_I = \frac{1.2 \cdot K_{P_{\text{critical}}}}{T_{\text{critical}}}
]
Step 3: Add Derivative Control
- Introduce ( K_D ) to dampen oscillations and reduce overshoot.
- Start with a small ( K_D ) (e.g., 0.05 in your code).
- Increase ( K_D ) until the system stabilizes without excessive oscillations. Be cautious—too large a ( K_D ) can amplify noise in your sensor readings, making the system jittery.
- A rule of thumb (Ziegler-Nichols method) for initial tuning is:
[
K_D = \frac{0.075 \cdot K_{P_{\text{critical}}} \cdot T_{\text{critical}}}{}
]
Step 4: Fine-Tune
- After setting initial values, fine-tune by observing the system's response:
- If the system is too sluggish, increase ( K_P ).
- If there is persistent steady-state error, increase ( K_I ).
- If there is excessive overshoot or oscillation, increase ( K_D ).
- Test the system under different conditions (e.g., different battery states of charge, temperatures) to ensure robustness.
Step 5: Consider Noise and Filtering
- If your current or voltage sensors are noisy, the derivative term (( K_D )) can amplify this noise, causing jittery behavior. To mitigate this, you can:
- Apply a low-pass filter to the sensor readings.
- Use a filtered derivative (e.g., calculate the derivative over a longer time window).
Step 6: Test Safety and Stability
- Ensure the system remains stable and safe under all conditions, including:
- Sudden changes in load (e.g., battery disconnection).
- Fault conditions (e.g., overcurrent, overvoltage).
- Different initial battery voltages.
3. Can Default Values Be Used Reliably?
Short Answer: No, default values (like ( K_P = 0.1 ), ( K_I = 0.01 ), ( K_D = 0.05 )) are not reliable for all systems. They are starting points, but they must be tuned for your specific setup.
Why Default Values Are Unreliable:
- System Dynamics Vary: The response of your charging system depends on factors like the battery's internal resistance, circuit resistance, sensor response time, and USB-C power supply dynamics. These vary between setups, so default values may be too aggressive (causing instability) or too conservative (causing sluggish response).
- Safety Risks: In a battery charging application, an unstable PID controller could lead to overvoltage, overcurrent, or overheating, posing safety risks.
- Performance Goals: Default values may not meet your performance goals (e.g., fast charging, minimal overshoot, stability).
When Default Values Might Work:
- If your system has very low resistance (e.g., minimal wiring and connector losses) and a fast-responding power supply, default values might be "good enough" for initial testing.
- However, even in this case, tuning is necessary to optimize performance and ensure safety.
Recommendation: Always tune the PID parameters for your specific hardware and application. Use the default values in the code as a starting point, but expect to adjust them based on experimental results.
4. Practical Tips for Tuning in Your Application
Here are some practical tips specific to your lithium-ion charging setup:
5. Example of a Well-Tuned System
A well-tuned PID controller for your application might look like this:
- Fast Response Without Overshoot: The current quickly reaches the target (e.g., 1A) without exceeding it by more than a few percent.
- Stable Steady-State: The current remains steady at the target value without oscillations.
- Smooth Mode Transition: When switching to CV mode, the voltage stabilizes at 4.2V without significant overshoot, and the current tapers off naturally.
If your default values (( K_P = 0.1 ), ( K_I = 0.01 ), ( K_D = 0.05 )) achieve this, great! But in most cases, you'll need to adjust them based on your system's behavior.
Final Answer
Role of KP, KI, KD:
- ( K_P ): Provides immediate correction proportional to the error, controlling responsiveness.
- ( K_I ): Eliminates steady-state error by accumulating past errors, ensuring accuracy.
- ( K_D ): Dampens oscillations by reacting to the rate of change of error, improving stability.
Tuning Process:
- Start with ( K_P ), increase until slight oscillations, then add ( K_I ) to eliminate steady-state error, and finally add ( K_D ) to reduce overshoot and stabilize.
- Use experimental data and visualization to fine-tune.
Default Values Reliability:
- Default values are not reliable for all systems and should be treated as starting points. Tuning is necessary for optimal performance and safety, especially in a battery charging application.
[
\boxed{\text{Tune } K_P, K_I, K_D \text{ experimentally for your specific system; default values are not reliable.}}
]