diff --git a/main/fancontrol.c b/main/fancontrol.c index b3ab800..9d1d9c4 100644 --- a/main/fancontrol.c +++ b/main/fancontrol.c @@ -99,8 +99,8 @@ static void timerCallback() cels_t tout = act_temp2(); uint16_t old_tempSerial = gState.tempSerial; - float old_tin = gState.t_actual_in; - float old_tout = gState.t_actual_out; +// float old_tin = gState.t_actual_in; +// float old_tout = gState.t_actual_out; gState.tempSerial = act_temps_serial(); gState.t_actual_in = tin; @@ -108,6 +108,18 @@ static void timerCallback() gState.valid_t_actual_in = gState.valid_t_actual_out = tempSensorsOk; + if (gState.tempSerial != old_tempSerial) { + // measuring speed of temp change + for (int i = NUM_PREVIOUS_T_INS - 1; i > 0; i--) { + gState.previous_t_ins[i] = gState.previous_t_ins[i - 1]; + } + if (gState.real_direction == MOTOR_DIR_IN) { + gState.previous_t_ins[0] = tin; + } else { + gState.previous_t_ins[0] = tout; + } + } + // posun rolety if (gAct.blind) { if (gState.blind_position < gSettings.blind_time) { @@ -218,14 +230,9 @@ static void timerCallback() // Max time elapsed, switch even if the condition was not reached do_switch = true; } else { - if (!gSettings.summer_mode && gState.tempSerial != old_tempSerial) { + if (!gSettings.summer_mode) { // expecting some change in temps - float speed; - if (gState.real_direction == MOTOR_DIR_IN) { - speed = absf(tin - old_tin); - } else { - speed = absf(tout - old_tout); - } + float speed = absf(gState.previous_t_ins[NUM_PREVIOUS_T_INS - 1] - gState.previous_t_ins[0]) / (float)NUM_PREVIOUS_T_INS; if (speed < gSettings.t_stop_speed) { ESP_LOGW(TAG, "Near-equilibrium reached, change dir!"); diff --git a/main/fancontrol.h b/main/fancontrol.h index 1c93e3e..4e56057 100644 --- a/main/fancontrol.h +++ b/main/fancontrol.h @@ -27,6 +27,7 @@ enum recup_mode { RECUP_MODE_TEMP = 1, }; +#define NUM_PREVIOUS_T_INS 5 struct FanControlState { /** @@ -57,6 +58,8 @@ struct FanControlState { uint16_t tempSerial; cels_t t_actual_in; cels_t t_actual_out; + // buffer used to calculate the current temp change speed + cels_t previous_t_ins[NUM_PREVIOUS_T_INS]; secs_t real_recup_time_in; secs_t real_recup_time_out;