The USB Power Supply or the battery being charged is likely to be changed several times, without me having a chance to get into the code and adjust the parameters. Does PID tuning still make sense in that case?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This is an excellent observation. In a system where both the power supply characteristics and the load (battery) frequently change, fixed PID parameters become much less useful and could even be problematic. Let's explore a more robust approach:
Simple Proportional Control with Safety Margins
python
def simple_current_control(target_current, measured_current, current_voltage):
# Conservative voltage step (0.05V or 50mV per adjustment)
VOLTAGE_STEP = 0.05
# Calculate current error
error = target_current - measured_current
# Simple threshold-based adjustment
if abs(error) > 0.05: # If error > 50mA
if error > 0:
return current_voltage + VOLTAGE_STEP
else:
return current_voltage - VOLTAGE_STEP
return current_voltage
def charging_loop():
TARGET_CURRENT = 1.0 # 1A
current_voltage = 4.0 # Starting voltage
while True:
batt_v = read_battery_voltage()
batt_i = read_charge_current()
if batt_v >= 4.2: # CV mode
# Switch to CV mode
current_voltage = 4.4 # Slight overhead for drops
else: # CC mode
current_voltage = simple_current_control(
TARGET_CURRENT,
batt_i,
current_voltage
)
# Apply limits
current_voltage = min(max(current_voltage, 3.0), 5.0)
set_usb_supply_voltage(current_voltage)
time.sleep(0.1) # 100ms delay
Advantages of This Approach:
Self-Adjusting: Works with any reasonable power supply or battery combination
No Tuning Required: Simple step adjustments are more forgiving
Predictable Behavior: Cannot oscillate like poorly-tuned PID
Fail-Safe: Small steps prevent sudden large voltage changes
Alternative: Adaptive Step Size
If you want slightly better performance while maintaining robustness:
python
def adaptive_current_control(target_current, measured_current, current_voltage):
# Error calculation
error = target_current - measured_current
# Adaptive step size based on error magnitude
if abs(error) > 0.2: # Large error > 200mA
step = 0.1 # 100mV step
elif abs(error) > 0.1: # Medium error > 100mA
step = 0.05 # 50mV step
elif abs(error) > 0.02: # Small error > 20mA
step = 0.02 # 20mV step
else:
return current_voltage # No adjustment needed
# Apply step in appropriate direction
if error > 0:
return current_voltage + step
else:
return current_voltage - step
Safety-First Approach with Rate Limiting
Here's an even more robust version that includes rate limiting and safety checks:
python
class ChargeController:
def __init__(self):
self.last_voltage = 4.0
self.last_current = 0
self.last_time = time.time()
# Safety limits
self.MAX_VOLTAGE_STEP = 0.2 # Max 200mV change per step
self.MAX_VOLTAGE_RATE = 1.0 # Max 1V per second
self.MIN_VOLTAGE = 3.0
self.MAX_VOLTAGE = 5.0
def adjust_voltage(self, target_current, measured_current, measured_voltage):
current_time = time.time()
dt = current_time - self.last_time
# Basic error calculation
error = target_current - measured_current
# Determine step size
if abs(error) > 0.1: # >100mA error
step = 0.05 * (error / abs(error)) # 50mV step
else:
step = 0.02 * (error / abs(error)) # 20mV step
# Calculate new voltage
new_voltage = self.last_voltage + step
# Rate limiting
voltage_rate = abs(new_voltage - self.last_voltage) / dt
if voltage_rate > self.MAX_VOLTAGE_RATE:
# Limit the change based on maximum rate
allowed_change = self.MAX_VOLTAGE_RATE * dt
step = allowed_change * (step / abs(step))
new_voltage = self.last_voltage + step
# Apply absolute limits
new_voltage = min(max(new_voltage, self.MIN_VOLTAGE), self.MAX_VOLTAGE)
# Update state
self.last_voltage = new_voltage
self.last_current = measured_current
self.last_time = current_time
return new_voltage
# Usage
controller = ChargeController()
while True:
batt_v = read_battery_voltage()
batt_i = read_charge_current()
if batt_v >= 4.2: # CV mode
voltage = 4.4 # Fixed CV voltage with overhead
else: # CC mode
voltage = controller.adjust_voltage(1.0, batt_i, batt_v)
set_usb_supply_voltage(voltage)
time.sleep(0.1)
Key Features of This Approach:
Rate Limiting: Prevents sudden voltage changes
Adaptive Steps: Larger steps for big errors, smaller for fine-tuning
Safety Bounds: Hard limits on voltage changes
No Memory Effects: Unlike PID, doesn't accumulate historical errors
Self-Stabilizing: Will work with any reasonable power supply/battery combination
Predictable: Behavior is easy to understand and debug
This approach is much more suitable for a system where the components change frequently, as it:
Doesn't require tuning
Adapts to different power supplies
Works with different battery types
Fails safely if something goes wrong
Is easy to understand and modify
The tradeoff is slightly slower convergence to the target current compared to a well-tuned PID, but this is usually acceptable for battery charging where safety and reliability are more important than speed.