From 03c8d71a56a58e0ab8b95f9a55809c1bb7030f95 Mon Sep 17 00:00:00 2001 From: Robert Konklewski Date: Mon, 18 Apr 2016 13:18:05 +0200 Subject: [PATCH] Temp: have residency updates on 1s clock. Temperature residency time / temperature achieved check is in the order of seconds, while updates for residency time were being called every 10ms - unnecessarily often. --- clock.c | 2 ++ temp.c | 52 +++++++++++++++++++++++++++++++--------------------- temp.h | 2 ++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/clock.c b/clock.c index 93eb9e3..3d47f16 100644 --- a/clock.c +++ b/clock.c @@ -91,6 +91,8 @@ static void clock_250ms(void) { display_clock(); #endif + temp_residency_tick(); + if (DEBUG_POSITION && (debug_flags & DEBUG_POSITION)) { // current position update_current_position(); diff --git a/temp.c b/temp.c index 779d707..1686a45 100644 --- a/temp.c +++ b/temp.c @@ -380,27 +380,7 @@ void temp_sensor_tick() { (EWMA_SCALE-EWMA_ALPHA) * temp_sensors_runtime[i].last_read_temp ) / EWMA_SCALE); } - if (labs((int16_t)(temp_sensors_runtime[i].last_read_temp - temp_sensors_runtime[i].target_temp)) < (TEMP_HYSTERESIS*4)) { - if (temp_sensors_runtime[i].temp_residency < (TEMP_RESIDENCY_TIME*120)) - temp_sensors_runtime[i].temp_residency++; - } - else { - // Deal with flakey sensors which occasionally report a wrong value - // by setting residency back, but not entirely to zero. - if (temp_sensors_runtime[i].temp_residency > 10) - temp_sensors_runtime[i].temp_residency -= 10; - else - temp_sensors_runtime[i].temp_residency = 0; - } - - if (DEBUG_PID && (debug_flags & DEBUG_PID)) - sersendf_P(PSTR("DU temp: {%d %d %d.%d}"), i, - temp_sensors_runtime[i].last_read_temp, - temp_sensors_runtime[i].last_read_temp / 4, - (temp_sensors_runtime[i].last_read_temp & 0x03) * 25); - } - if (DEBUG_PID && (debug_flags & DEBUG_PID)) - sersendf_P(PSTR("\n")); + } } /** @@ -418,6 +398,36 @@ void temp_heater_tick() { } } +/** + Called every 1s from clock.c. Update temperature residency info. +*/ +void temp_residency_tick() { + temp_sensor_t i; + + for (i = 0; i < NUM_TEMP_SENSORS; i++) { + if (labs((int16_t)(temp_sensors_runtime[i].last_read_temp - temp_sensors_runtime[i].target_temp)) < (TEMP_HYSTERESIS*4)) { + if (temp_sensors_runtime[i].temp_residency < (TEMP_RESIDENCY_TIME*120)) + temp_sensors_runtime[i].temp_residency += 100; + } + else { + // Deal with flakey sensors which occasionally report a wrong value + // by setting residency back, but not entirely to zero. + if (temp_sensors_runtime[i].temp_residency > 100) + temp_sensors_runtime[i].temp_residency -= 100; + else + temp_sensors_runtime[i].temp_residency = 0; + } + + if (DEBUG_PID && (debug_flags & DEBUG_PID)) + sersendf_P(PSTR("DU temp: {%d %d %d.%d}"), i, + temp_sensors_runtime[i].last_read_temp, + temp_sensors_runtime[i].last_read_temp / 4, + (temp_sensors_runtime[i].last_read_temp & 0x03) * 25); + } + if (DEBUG_PID && (debug_flags & DEBUG_PID)) + sersendf_P(PSTR("\n")); +} + /** * Report whether all temp sensors in use are reading their target * temperatures. Used for M116 and friends. diff --git a/temp.h b/temp.h index 8c6e0e4..5e252bd 100644 --- a/temp.h +++ b/temp.h @@ -38,6 +38,8 @@ void temp_sensor_tick(void); void temp_heater_tick(void); +void temp_residency_tick(void); + uint8_t temp_achieved(void); void temp_set(temp_sensor_t index, uint16_t temperature);