|
|
@ -13,15 +13,13 @@ |
|
|
|
|
|
|
|
|
|
|
|
const char *TAG = "fc"; |
|
|
|
const char *TAG = "fc"; |
|
|
|
|
|
|
|
|
|
|
|
#define UNIDIR_T_MEAS_PERIOD 60 |
|
|
|
#define UNIDIR_T_MEAS_PERIOD 15 /* s */ |
|
|
|
struct FanControlState gState = {}; |
|
|
|
struct FanControlState gState = {}; |
|
|
|
|
|
|
|
|
|
|
|
static void timerCallback(); |
|
|
|
static void timerCallback(); |
|
|
|
|
|
|
|
|
|
|
|
static void invalidate_temps(); |
|
|
|
static void invalidate_temps(); |
|
|
|
|
|
|
|
|
|
|
|
static float absf(float f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void settings_blind_time_set(uint16_t blind_time) |
|
|
|
void settings_blind_time_set(uint16_t blind_time) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// if the blind is surely at the end
|
|
|
|
// if the blind is surely at the end
|
|
|
@ -87,39 +85,13 @@ static const char * vent_mode_labels[] = { |
|
|
|
[VENT_MODE_RECUP] = "RECUP", |
|
|
|
[VENT_MODE_RECUP] = "RECUP", |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
//static const char * recup_mode_labels[] = {
|
|
|
|
static const char * recup_mode_labels[] = { |
|
|
|
// [RECUP_MODE_TIME] = "TIME",
|
|
|
|
[RECUP_MODE_TIME] = "TIME", |
|
|
|
// [RECUP_MODE_TEMP] = "TEMP",
|
|
|
|
[RECUP_MODE_TEMP] = "TEMP", |
|
|
|
//};
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static void timerCallback() |
|
|
|
static void timerCallback() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Measure temperatures
|
|
|
|
|
|
|
|
cels_t tin = act_temp1(); |
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gState.tempSerial = act_temps_serial(); |
|
|
|
|
|
|
|
gState.t_actual_in = tin; |
|
|
|
|
|
|
|
gState.t_actual_out = tout; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
// posun rolety
|
|
|
|
if (gAct.blind) { |
|
|
|
if (gAct.blind) { |
|
|
|
if (gState.blind_position < gSettings.blind_time) { |
|
|
|
if (gState.blind_position < gSettings.blind_time) { |
|
|
@ -143,9 +115,7 @@ static void timerCallback() |
|
|
|
gState.ramp++; |
|
|
|
gState.ramp++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (gState.run_time < 0xFFFF) { |
|
|
|
gState.run_time++; |
|
|
|
gState.run_time++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (gState.ramp > 0) { |
|
|
|
if (gState.ramp > 0) { |
|
|
|
gState.ramp--; |
|
|
|
gState.ramp--; |
|
|
@ -171,8 +141,7 @@ static void timerCallback() |
|
|
|
if (gState.blind_position >= gSettings.blind_time) { |
|
|
|
if (gState.blind_position >= gSettings.blind_time) { |
|
|
|
act_motor_power_set(gState.set_power); |
|
|
|
act_motor_power_set(gState.set_power); |
|
|
|
} |
|
|
|
} |
|
|
|
// some time is needed before the temperature means anything
|
|
|
|
if (gState.ramp >= gSettings.ramp_time * 2) { |
|
|
|
if (gState.run_time >= UNIDIR_T_MEAS_PERIOD) { |
|
|
|
|
|
|
|
end_temp_meas = true; |
|
|
|
end_temp_meas = true; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
@ -182,7 +151,7 @@ static void timerCallback() |
|
|
|
if (gState.blind_position >= gSettings.blind_time) { |
|
|
|
if (gState.blind_position >= gSettings.blind_time) { |
|
|
|
act_motor_power_set(gState.set_power); |
|
|
|
act_motor_power_set(gState.set_power); |
|
|
|
} |
|
|
|
} |
|
|
|
if (gState.run_time >= UNIDIR_T_MEAS_PERIOD) { |
|
|
|
if (gState.ramp >= gSettings.ramp_time * 2) { |
|
|
|
end_temp_meas = true; |
|
|
|
end_temp_meas = true; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
@ -195,49 +164,53 @@ static void timerCallback() |
|
|
|
// Stop condition
|
|
|
|
// Stop condition
|
|
|
|
bool do_switch = false; |
|
|
|
bool do_switch = false; |
|
|
|
|
|
|
|
|
|
|
|
// vyfukovaci strana trubice, libovona strana
|
|
|
|
// TODO questionable logic, verify
|
|
|
|
if (gState.real_direction == MOTOR_DIR_IN) { |
|
|
|
|
|
|
|
if (gState.t_actual_in <= gSettings.t_in_min || gState.t_actual_in >= gSettings.t_in_max) { |
|
|
|
|
|
|
|
ESP_LOGW(TAG, "Temp limit reached, change dir!"); |
|
|
|
|
|
|
|
do_switch = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float t_output = gState.real_direction == MOTOR_DIR_OUT ? gState.t_actual_out : gState.t_actual_in; |
|
|
|
|
|
|
|
float t_input = gState.real_direction == MOTOR_DIR_IN ? gState.t_actual_out : gState.t_actual_in; |
|
|
|
|
|
|
|
float delta = absf(t_input - t_output); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (gState.real_direction == MOTOR_DIR_IN) { |
|
|
|
|
|
|
|
if (delta < gSettings.t_stopdelta_in) { |
|
|
|
|
|
|
|
ESP_LOGW(TAG, "IN stop delta reached, change dir!"); |
|
|
|
|
|
|
|
do_switch = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (delta < gSettings.t_stopdelta_out) { |
|
|
|
|
|
|
|
ESP_LOGW(TAG, "OUT stop delta reached, change dir!"); |
|
|
|
|
|
|
|
do_switch = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (gSettings.recup_mode == RECUP_MODE_TIME) { |
|
|
|
if (gSettings.recup_mode == RECUP_MODE_TIME) { |
|
|
|
if (gState.run_time >= gSettings.recup_time) { |
|
|
|
do_switch = gState.run_time >= gSettings.recup_time; |
|
|
|
ESP_LOGW(TAG, "Recup time elapsed, change dir!"); |
|
|
|
} else if (gState.run_time >= gSettings.max_recup_time) { |
|
|
|
do_switch = true; |
|
|
|
do_switch = true; |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (gState.run_time >= gSettings.max_recup_time) { |
|
|
|
// temp-based switching - magic(tm)
|
|
|
|
ESP_LOGW(TAG, "Max time elapsed, change dir!"); |
|
|
|
// Delta = IN - OUT
|
|
|
|
// Max time elapsed, switch even if the condition was not reached
|
|
|
|
const int16_t ideal_delta = gState.t_indoor - gState.t_outdoor; |
|
|
|
do_switch = true; |
|
|
|
int16_t stop_delta = ((int32_t) ideal_delta |
|
|
|
|
|
|
|
* (int32_t) (100 - gSettings.recup_factor)) / 100; |
|
|
|
|
|
|
|
int16_t delta = 0; |
|
|
|
|
|
|
|
bool allow_temp_switch = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (gState.real_direction == MOTOR_DIR_OUT) { |
|
|
|
|
|
|
|
if (gState.valid_t_indoor && gState.valid_t_exhaust && gState.valid_t_outdoor) { |
|
|
|
|
|
|
|
delta = gState.t_indoor - gState.t_exhaust; |
|
|
|
|
|
|
|
allow_temp_switch = true; |
|
|
|
|
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (!gSettings.summer_mode) { |
|
|
|
// IN
|
|
|
|
// expecting some change in temps
|
|
|
|
if (gState.valid_t_inflow && gState.valid_t_indoor && gState.valid_t_outdoor) { |
|
|
|
float speed = absf(gState.previous_t_ins[NUM_PREVIOUS_T_INS - 1] - gState.previous_t_ins[0]) / (float)NUM_PREVIOUS_T_INS; |
|
|
|
delta = gState.t_inflow - gState.t_outdoor; |
|
|
|
|
|
|
|
allow_temp_switch = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Delta now %d, max %d, stop %d (RF %d%%), allow_switch %d Cx10", delta, ideal_delta, stop_delta, gSettings.recup_factor, allow_temp_switch); |
|
|
|
|
|
|
|
|
|
|
|
if (speed < gSettings.t_stop_speed) { |
|
|
|
if (allow_temp_switch) { |
|
|
|
ESP_LOGW(TAG, "Near-equilibrium reached, change dir!"); |
|
|
|
if (gSettings.summer_mode) { |
|
|
|
do_switch = true; |
|
|
|
if (ideal_delta < 0) { |
|
|
|
|
|
|
|
// warmer outside, trying to keep cool in
|
|
|
|
|
|
|
|
if (delta >= stop_delta) { |
|
|
|
|
|
|
|
do_switch = true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// colder outside - no stopping (will run to max recup time)
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (ideal_delta > 0) { |
|
|
|
|
|
|
|
// colder outside, trying to keep warmth in
|
|
|
|
|
|
|
|
if (delta <= stop_delta) { |
|
|
|
|
|
|
|
do_switch = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// warmer outside - no stopping (will run to max recup time)
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -269,6 +242,15 @@ static void timerCallback() |
|
|
|
gState.instantaneous_vent_mode = gState.effective_vent_mode; |
|
|
|
gState.instantaneous_vent_mode = gState.effective_vent_mode; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Measure temperatures
|
|
|
|
|
|
|
|
int16_t t1 = act_temp1(); |
|
|
|
|
|
|
|
int16_t t2 = act_temp2(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gState.t_actual_in = t1; |
|
|
|
|
|
|
|
gState.t_actual_out = t2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gState.valid_t_actual_in = gState.valid_t_actual_out = tempSensorsOk; |
|
|
|
|
|
|
|
|
|
|
|
if (end_temp_meas) { |
|
|
|
if (end_temp_meas) { |
|
|
|
switch (gState.real_direction) { |
|
|
|
switch (gState.real_direction) { |
|
|
|
case MOTOR_DIR_IN: |
|
|
|
case MOTOR_DIR_IN: |
|
|
@ -291,33 +273,29 @@ static void timerCallback() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, |
|
|
|
ESP_LOGI(TAG, |
|
|
|
"%s (%s), B%ds, M%ds, %d%%, Tid %.2f%s, Tod %.2f%s, Tit %.2f%s, Teh %.2f%s, T1 %.2f%s, T2 %.2f%s C", |
|
|
|
"%s (ef %s, inst %s), rt %ds %d%%m, Tid %d%s, Tod %d%s, Tit %d%s, Teh %d%s, T1 %d%s, T2 %d%s Cx10", |
|
|
|
vent_mode_labels[gState.set_vent_mode], vent_mode_labels[gState.instantaneous_vent_mode], |
|
|
|
vent_mode_labels[gState.set_vent_mode], vent_mode_labels[gState.effective_vent_mode], vent_mode_labels[gState.instantaneous_vent_mode], |
|
|
|
gState.blind_position, |
|
|
|
|
|
|
|
gState.run_time, |
|
|
|
gState.run_time, |
|
|
|
gAct.power, |
|
|
|
gAct.power, |
|
|
|
|
|
|
|
|
|
|
|
gState.t_indoor, gState.valid_t_indoor ? "" : "!", |
|
|
|
gState.t_indoor, |
|
|
|
gState.t_outdoor, gState.valid_t_outdoor ? "" : "!", |
|
|
|
gState.valid_t_indoor ? "" : "!", |
|
|
|
gState.t_inflow, gState.valid_t_inflow ? "" : "!", |
|
|
|
gState.t_outdoor, |
|
|
|
gState.t_exhaust, gState.valid_t_exhaust ? "" : "!", |
|
|
|
gState.valid_t_outdoor ? "" : "!", |
|
|
|
gState.t_actual_in, gState.valid_t_actual_in ? "" : "!", |
|
|
|
gState.t_inflow, |
|
|
|
gState.t_actual_out, gState.valid_t_actual_out ? "" : "!" |
|
|
|
gState.valid_t_inflow ? "" : "!", |
|
|
|
|
|
|
|
gState.t_exhaust, |
|
|
|
|
|
|
|
gState.valid_t_exhaust ? "" : "!", |
|
|
|
|
|
|
|
gState.t_actual_in, |
|
|
|
|
|
|
|
gState.valid_t_actual_in ? "" : "!", |
|
|
|
|
|
|
|
gState.t_actual_out, |
|
|
|
|
|
|
|
gState.valid_t_actual_out ? "" : "!" |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static float absf(float f) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (f < 0) { |
|
|
|
|
|
|
|
return -f; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return f; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void fan_set_vent_mode(enum ventilation_mode mode) |
|
|
|
void fan_set_vent_mode(enum ventilation_mode mode) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ESP_LOGI(TAG, "Set vent mode = %s", vent_mode_labels[mode]); |
|
|
|
ESP_LOGI(TAG, "Set vent mode = %d", mode); |
|
|
|
|
|
|
|
|
|
|
|
if (mode == gState.set_vent_mode) { |
|
|
|
if (mode == gState.set_vent_mode) { |
|
|
|
return; |
|
|
|
return; |
|
|
@ -345,7 +323,7 @@ static void invalidate_temps() |
|
|
|
gState.valid_t_actual_out = false; |
|
|
|
gState.valid_t_actual_out = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void fan_set_power(perc_t power) |
|
|
|
void fan_set_power(uint16_t power) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ESP_LOGI(TAG, "Set power = %d%%", power); |
|
|
|
ESP_LOGI(TAG, "Set power = %d%%", power); |
|
|
|
|
|
|
|
|
|
|
|