Heater for threaded program
Dependents: LEX_Threaded_Programming_V3
Diff: Heater.cpp
- Revision:
- 37:688dad0e1b76
- Parent:
- 36:a8130bd29349
- Child:
- 38:3a2778f1f90a
--- a/Heater.cpp Mon Sep 23 17:30:05 2019 +0000 +++ b/Heater.cpp Tue Sep 24 08:38:19 2019 +0000 @@ -46,30 +46,23 @@ R = cal_a + cal_b*R; //Convert to Ohms } - //Get error values + //Get error and integrated error values error = R_ref - R; + error_integrated += error * (float) thermal.control_loop_interval_ms; //Only allow positive integrated errors and limit change in integrated error //to help avoid integral windup - - if (abs(error) > thermal.pid_wind_up_limit_ohm) {error = error * thermal.pid_wind_up_limit_ohm / abs(error);} - - error_integrated += error* (float) thermal.control_loop_interval_ms; - - if (error_integrated < 0.0) { - error_integrated = 0.0; - } + if (error_integrated < 0.0) error_integrated = 0.0; + if (abs(error) > thermal.pid_wind_up_limit_ohm) error = error * thermal.pid_wind_up_limit_ohm / abs(error); } void Heater::update() { //Update PWM from setpoint and resistance - double duty_cycle = thermal.pid_kp_mho * ( error + error_integrated/thermal.pid_integral_time_ms); + double duty_cycle = thermal.pid_kp_mho * (error + error_integrated/thermal.pid_integral_time_ms); - if (duty_cycle > thermal.pid_pwm_limit) - duty_cycle = thermal.pid_pwm_limit; - else if (duty_cycle < 0) - duty_cycle = 0; + if (duty_cycle > thermal.pid_pwm_limit) duty_cycle = thermal.pid_pwm_limit; + else if (duty_cycle < 0) duty_cycle = 0; drive->write(duty_cycle); guard->write(duty_cycle * thermal.guard_drive_ratio);