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;
}
}`


New contributor

Albion Qovanaj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

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;
}
}`


New contributor

Albion Qovanaj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật