Albion Qovanaj
Albion Qovanaj

Reputation: 1

Definition of Wind Up limits for the integral term. how to express the max integral=10 cycles each iteration, min integral=0 output power

I am trying to determine the saturation of Integral Term. The goal is to determine the maximum value of integral with the maximum output power. Output power is controlles by how many cycle we send to the TRIAC. The problem is that in this code the limit is 10. The value of integral term is 10 which is wrongly expressed. The goal is to determine the maximum as 10 cycles. Because 10 cycles means full power. The loop is 200 miliseconds and we need 10 cycles to fully power the system. Like this I wanted to define also Max integral. Is it any other possibility to determine the Max Integral or can i do it this way but to code it differently?

#define MAXDO   12
#define MAXCS   11
#define MAXCLK  13

// Initialize MAX31855 thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

// Input and output pins
int firing_pin = 10;  // Output to control the TRIAC
int zero_cross = 2;   // Zero-cross detection input
float setpoint = 45;  // Target temperature
const float calibrationOffset = -1;
const float alpha = 0.2;
float smoothedTemperature = 0;

// PID variables
float error = 0;
float delta = 1.0; // Duty cycle (active cycles / total cycles)
float PID_p = 0, PID_i = 0, PID_d = 0, PID_value = 0;
float previous_error = 0;
float kp = 6.4, ki = 0.5; // kd = 0.3;

// Integral limits based on active cycles
const int minIntegralActiveCycles = 0;          // No pulses (0% power)
const int maxIntegralActiveCycles = 10;         // Pulses every half-cycle (100% power)
const int maxPower = maxIntegralActiveCycles;   // Maximum power in active cycles
const int minPower = minIntegralActiveCycles;   // Minimum power in active cycles

// Timing variables
unsigned long previous_time = 0;
volatile int active_cycles = 0;
const int total_cycles = 10; // 10 half-cycles (100ms for 50Hz)
volatile int cycle_counter = 0;

// Saturation function for integral term
float PIDController_saturate(float input, float minVal, float maxVal) {
if (input \> maxVal)
input = maxVal;
else if (input \< minVal)
input = minVal;
return input;
}

void setup() {
pinMode(firing_pin, OUTPUT);
pinMode(zero_cross, INPUT);

Serial.begin(9600);

// Attach zero-cross interrupt
attachInterrupt(digitalPinToInterrupt(zero_cross), zeroCrossISR, FALLING);
}

void loop() {
// Update temperature and PID every 300 ms
unsigned long current_time = millis();
if (current_time - previous_time \>= 200) {
float delta_t = (current_time - previous_time) / 1000.0; // Convert to seconds
previous_time = current_time;

    // Read temperature
    int celsius = thermocouple.readCelsius();
    celsius += calibrationOffset;
    
    // Smooth temperature reading
    if (abs(celsius - smoothedTemperature) > 10) {
      smoothedTemperature = celsius;  // Reset smoothing to raw value
    } else {
      smoothedTemperature = alpha * celsius + (1 - alpha) * smoothedTemperature;
    }
    
    float real_temperature = smoothedTemperature;
    
    // PID logic
    error = setpoint - real_temperature;
    
    // Proportional term
    PID_p = kp * error;
    
    // Trapezoidal integral term
    PID_i += 0.5 * ki * delta_t * (error + previous_error);
    
    // Scale and constrain the integral term for wind-up prevention
    PID_i = PIDController_saturate(PID_i, minIntegralActiveCycles, maxIntegralActiveCycles);
    
    // PID_d = kd * (error - previous_error);
    
    PID_value = PID_p + PID_i + PID_d;  // Combine PID terms
    
    // Map PID_value to duty cycle delta
    delta = constrain(PID_value / 100, 0.0, 1.0);
    
    previous_error = error;  // Update previous error
    
    // Update active_cycles for TRIAC control
    noInterrupts();
    active_cycles = round(delta * total_cycles);
    interrupts();
    
    // Debug output

Serial.print("Temperature: ");
Serial.print(real_temperature);
Serial.print(" °C, Setpoint: ");
Serial.print(setpoint);
Serial.print(" °C, Delta: ");
Serial.print(delta);
Serial.print(", PID error: ");
Serial.print(PID_value);
Serial.print(", Integral Term (PID_i): ");
Serial.print(PID_i);
Serial.print(", Active Cycles: ");
Serial.println(active_cycles);

}
}

// Zero-cross interrupt service routine
void zeroCrossISR() {
if (cycle_counter \< active_cycles) {
// Fire TRIAC
digitalWrite(firing_pin, HIGH);
delayMicroseconds(500); //
digitalWrite(firing_pin, LOW);
}

// Increment cycle counter
cycle_counter++;

// Reset cycle counter at the end of a period
if (cycle_counter \>= total_cycles) {
cycle_counter = 0;
}
}\`


Upvotes: 0

Views: 12

Answers (0)

Related Questions