Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions components/ts_drivers/include/ts_temp_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ extern "C" {

/** 数据超时(毫秒)- 超过此时间认为数据过期 */
#define TS_TEMP_DATA_TIMEOUT_MS 10000
#define TS_TEMP_AUTO_STALE_MS 6000

/** 最大加权绑定变量数量 */
#define TS_TEMP_MAX_BOUND_VARS 8

/*===========================================================================*/
/* Types */
Expand Down Expand Up @@ -96,11 +100,14 @@ typedef enum {
TS_TEMP_AUTO_VARIABLE_BUSY,
} ts_temp_auto_read_t;

/* Snapshot of the sole bound variable; independent of effective-source selection. */
/* Bound variables only, independent of effective-source selection. */
typedef struct {
int64_t timestamp_ms;
int64_t timestamp_ms; /**< Oldest report in the complete weighted reading. */
int64_t reports_ms[TS_TEMP_MAX_BOUND_VARS]; /**< Report time by binding slot. */
int64_t guard_timestamp_ms; /**< Report time of the hottest fresh member, zero if none. */
uint32_t binding_revision;
int16_t value;
int16_t value; /**< Weighted temperature, valid when result is AUTO_VALID. */
int16_t guard_value; /**< Hottest fresh member, even if the weighted reading is incomplete. */
ts_temp_auto_read_t result;
} ts_temp_auto_sample_t;

Expand Down Expand Up @@ -129,9 +136,6 @@ typedef struct {
/** 最大绑定变量名长度 */
#define TS_TEMP_MAX_VARNAME_LEN 64

/** 最大加权绑定变量数量 */
#define TS_TEMP_MAX_BOUND_VARS 8

/**
* @brief 加权绑定变量条目
*/
Expand Down
56 changes: 39 additions & 17 deletions components/ts_drivers/src/ts_fan.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#define FAN_AUTO_RISE_RATE_PER_SEC 12.0f
#define FAN_AUTO_FALL_RATE_PER_SEC 0.30f
#define FAN_AUTO_STALE_DUTY 25
#define FAN_AUTO_STALE_MS 6000
#define FAN_AUTO_REPORT_GAP_MS 2000 /* Bound variable reports about once a second. */
#define FAN_AUTO_HISTORY_SIZE 32

Expand Down Expand Up @@ -85,6 +84,7 @@ typedef struct {
int64_t guard_release_since_ms;
int64_t guard_last_observation_ms;
int64_t auto_report_ms;
int64_t auto_reports_ms[TS_TEMP_MAX_BOUND_VARS];
uint32_t auto_binding_revision;
bool auto_resume_pending;
bool response_observing;
Expand Down Expand Up @@ -270,6 +270,7 @@ static void reset_adaptive_auto_state(fan_instance_t *fan)
fan->guard_release_since_ms = 0;
fan->guard_last_observation_ms = 0;
fan->auto_report_ms = 0;
memset(fan->auto_reports_ms, 0, sizeof(fan->auto_reports_ms));
fan->auto_binding_revision = 0;
fan->auto_resume_pending = false;
fan->response_observing = false;
Expand Down Expand Up @@ -406,15 +407,17 @@ static void update_response_learning(fan_instance_t *fan, bool slope_valid,
}

static void update_guard_observation(fan_instance_t *fan, const ts_temp_auto_sample_t *sample,
bool valid, bool new_report, int64_t now_ms,
int64_t max_poll_gap_ms, int64_t max_report_gap_ms)
bool guard_valid, bool valid, bool new_report, bool report_gap,
int64_t now_ms, int64_t max_poll_gap_ms, int64_t max_report_gap_ms)
{
if (valid && sample->value >= FAN_AUTO_GUARD_TEMP) {
fan->guard_active = true;
/* A hot member is enough to engage protection; release needs the complete binding. */
if (guard_valid) {
fan->guard_temperature = sample->guard_value;
if (sample->guard_value >= FAN_AUTO_GUARD_TEMP) fan->guard_active = true;
}
if (!fan->guard_active) return;

if (!valid || sample->value > FAN_AUTO_GUARD_RELEASE_TEMP ||
if (!valid || sample->guard_value > FAN_AUTO_GUARD_RELEASE_TEMP ||
now_ms - sample->timestamp_ms > max_report_gap_ms) {
fan->guard_release_since_ms = 0;
fan->guard_last_observation_ms = 0;
Expand All @@ -423,8 +426,7 @@ static void update_guard_observation(fan_instance_t *fan, const ts_temp_auto_sam

if (fan->guard_last_observation_ms == 0 ||
now_ms <= fan->guard_last_observation_ms ||
now_ms - fan->guard_last_observation_ms > max_poll_gap_ms ||
(fan->auto_report_ms > 0 && sample->timestamp_ms - fan->auto_report_ms > max_report_gap_ms)) {
now_ms - fan->guard_last_observation_ms > max_poll_gap_ms || report_gap) {
fan->guard_release_since_ms = 0;
}
fan->guard_last_observation_ms = now_ms;
Expand Down Expand Up @@ -457,29 +459,49 @@ static esp_err_t apply_adaptive_auto(fan_instance_t *fan, const ts_temp_auto_sam
fan->auto_binding_revision = binding_revision;

int64_t sample_age_ms = now_ms - sample->timestamp_ms;
bool valid = s_auto_temp_enabled && sample->result == TS_TEMP_AUTO_VALID &&
sample->binding_revision == binding_revision && sample->timestamp_ms > 0 &&
sample_age_ms >= 0 && sample_age_ms < FAN_AUTO_STALE_MS &&
bool current_binding = s_auto_temp_enabled && sample->binding_revision == binding_revision;
int64_t guard_age_ms = now_ms - sample->guard_timestamp_ms;
bool guard_valid = current_binding && sample->guard_timestamp_ms > 0 &&
guard_age_ms >= 0 && guard_age_ms < TS_TEMP_AUTO_STALE_MS &&
sample->guard_timestamp_ms >= fan->auto_report_ms;
bool valid = current_binding && sample->result == TS_TEMP_AUTO_VALID && sample->timestamp_ms > 0 &&
sample_age_ms >= 0 && sample_age_ms < TS_TEMP_AUTO_STALE_MS &&
sample->timestamp_ms >= fan->auto_report_ms;
bool new_report = valid && sample->timestamp_ms > fan->auto_report_ms;
bool was_stale = fan->temp_stale;
bool was_guard = fan->guard_active;
bool recovered_from_stale = fan->auto_state == TS_FAN_AUTO_STATE_STALE;
uint8_t old_duty = fan->current_duty;
int64_t max_poll_gap_ms = 2LL * CONFIG_TS_DRIVERS_FAN_TEMP_UPDATE_MS;
if (max_poll_gap_ms > FAN_AUTO_STALE_MS) max_poll_gap_ms = FAN_AUTO_STALE_MS;
if (max_poll_gap_ms > TS_TEMP_AUTO_STALE_MS) max_poll_gap_ms = TS_TEMP_AUTO_STALE_MS;
/* Faster polling must not turn a normal producer interval into a missing report. */
int64_t max_report_gap_ms = max_poll_gap_ms > FAN_AUTO_REPORT_GAP_MS
? max_poll_gap_ms : FAN_AUTO_REPORT_GAP_MS;
update_guard_observation(fan, sample, valid, new_report, now_ms, max_poll_gap_ms, max_report_gap_ms);
/* Freshness uses the oldest input. Progress and report gaps belong to each binding slot. */
bool new_report = false;
bool report_gap = false;
if (valid) {
for (size_t i = 0; i < TS_TEMP_MAX_BOUND_VARS; i++) {
int64_t previous = fan->auto_reports_ms[i];
int64_t current = sample->reports_ms[i];
if (current < previous) {
valid = false;
break;
}
new_report |= current > previous;
report_gap |= previous > 0 && current - previous > max_report_gap_ms;
}
}
new_report = valid && new_report;
update_guard_observation(fan, sample, guard_valid, valid, new_report, report_gap,
now_ms, max_poll_gap_ms, max_report_gap_ms);

fan->temp_stale = !valid;
bool slope_valid = false;
if (new_report) {
fan->auto_report_ms = sample->timestamp_ms;
memcpy(fan->auto_reports_ms, sample->reports_ms, sizeof(fan->auto_reports_ms));
fan->temperature = sample->value;
fan->control_temperature = sample->value;
fan->guard_temperature = sample->value;
record_auto_temp_sample(fan, sample->value, now_ms);
slope_valid = calculate_auto_slope(fan, sample->value, now_ms,
FAN_AUTO_MIN_SAMPLE_MS, &fan->slope_c_per_min);
Expand Down Expand Up @@ -510,7 +532,7 @@ static esp_err_t apply_adaptive_auto(fan_instance_t *fan, const ts_temp_auto_sam
fan->last_auto_update_ms = now_ms;
fan->response_observing = false;
if (s_auto_temp_enabled && fan->duty_valid && !recovered_from_stale &&
fan->auto_report_ms > 0 && last_age_ms >= 0 && last_age_ms < FAN_AUTO_STALE_MS) {
fan->auto_report_ms > 0 && last_age_ms >= 0 && last_age_ms < TS_TEMP_AUTO_STALE_MS) {
return ESP_OK;
}
if (!recovered_from_stale) {
Expand All @@ -534,7 +556,7 @@ static esp_err_t apply_adaptive_auto(fan_instance_t *fan, const ts_temp_auto_sam
}

int16_t control_temp = sample->value;
int16_t guard_temp = sample->value;
int16_t guard_temp = sample->guard_value;
if (new_report) update_response_learning(fan, slope_valid, fan->slope_c_per_min, now_ms);
/* Explicit re-enable can use the same valid report once, without counting
* it as a new sample or accruing slew time while the fan was off. */
Expand Down
75 changes: 43 additions & 32 deletions components/ts_drivers/src/ts_temp_source.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ typedef struct {
int16_t temp;
int64_t timestamp_ms;
int16_t guard_temp;
int64_t guard_timestamp_ms;
int64_t reports_ms[TS_TEMP_MAX_BOUND_VARS];
bool guard_valid;
bool variable_busy;
bool partial_stale;
uint8_t valid_count;
uint8_t total_count;
Expand Down Expand Up @@ -105,8 +108,9 @@ static esp_err_t save_preferred_source_to_nvs(ts_temp_source_type_t type);
static float sanitize_bound_weight(float weight);
static bool has_positive_bound_weight(const ts_temp_bound_var_t *vars, uint8_t count);
static esp_err_t read_fresh_variable_float(const char *name, double *value,
int64_t *last_update_ms, bool try_lock);
static bool read_variable_temp_snapshot(variable_temp_snapshot_t *snapshot, bool try_lock);
int64_t *last_update_ms, int64_t max_age_ms, bool try_lock);
static bool read_variable_temp_snapshot(variable_temp_snapshot_t *snapshot,
int64_t max_age_ms, bool try_lock);

/*===========================================================================*/
/* Utility Functions */
Expand Down Expand Up @@ -173,7 +177,7 @@ static void store_variable_snapshot(const variable_temp_snapshot_t *snapshot)
}

static esp_err_t read_fresh_variable_float(const char *name, double *value,
int64_t *last_update_ms, bool try_lock)
int64_t *last_update_ms, int64_t max_age_ms, bool try_lock)
{
ts_variable_info_t var = {0};
esp_err_t ret = try_lock ? ts_variable_get_info_try(name, &var)
Expand All @@ -183,7 +187,7 @@ static esp_err_t read_fresh_variable_float(const char *name, double *value,
/* The writer stamps the variable under its lock. Take the clock after copying it. */
int64_t now = get_current_ms();
if (var.last_update_ms <= 0 || var.last_update_ms > now ||
now - var.last_update_ms > TS_TEMP_DATA_TIMEOUT_MS) {
now - var.last_update_ms > max_age_ms) {
return ESP_ERR_INVALID_STATE;
}

Expand All @@ -202,7 +206,8 @@ static esp_err_t read_fresh_variable_float(const char *name, double *value,
return ESP_OK;
}

static bool read_variable_temp_snapshot(variable_temp_snapshot_t *snapshot, bool try_lock)
static bool read_variable_temp_snapshot(variable_temp_snapshot_t *snapshot,
int64_t max_age_ms, bool try_lock)
{
double weighted_sum = 0.0;
int16_t max_bound = TS_TEMP_MIN_VALID;
Expand All @@ -228,19 +233,24 @@ static bool read_variable_temp_snapshot(variable_temp_snapshot_t *snapshot, bool

double value = 0.0;
int64_t last_update_ms = 0;
if (read_fresh_variable_float(s_state.bound_vars[i].name, &value,
&last_update_ms, try_lock) != ESP_OK) {
esp_err_t ret = read_fresh_variable_float(s_state.bound_vars[i].name, &value,
&last_update_ms, max_age_ms, try_lock);
if (ret != ESP_OK) {
snapshot->variable_busy |= ret == ESP_ERR_TIMEOUT;
TS_LOGD(TAG, "Variable '%s' unavailable or stale",
s_state.bound_vars[i].name);
continue;
}

snapshot->reports_ms[i] = last_update_ms;
weighted_sum += value * w;
snapshot->valid_weight += w;
snapshot->valid_count++;
int16_t temp_01c = (int16_t)(value * 10.0);
if (temp_01c > max_bound) {
if (temp_01c > max_bound ||
(temp_01c == max_bound && last_update_ms > snapshot->guard_timestamp_ms)) {
max_bound = temp_01c;
snapshot->guard_timestamp_ms = last_update_ms;
}
if (snapshot->timestamp_ms == 0 || last_update_ms < snapshot->timestamp_ms) {
snapshot->timestamp_ms = last_update_ms;
Expand All @@ -260,7 +270,8 @@ static bool read_variable_temp_snapshot(variable_temp_snapshot_t *snapshot, bool
}

double effective_temp = weighted_sum / snapshot->valid_weight;
int16_t temp_01c = (int16_t)(effective_temp * 10.0);
/* A single contributor already has the exact reading in tenths of a degree. */
int16_t temp_01c = snapshot->valid_count == 1 ? max_bound : (int16_t)(effective_temp * 10.0);
if (temp_01c < TS_TEMP_MIN_VALID || temp_01c > TS_TEMP_MAX_VALID) {
TS_LOGW(TAG, "Weighted temp out of range: %.1f°C", effective_temp);
return false;
Expand Down Expand Up @@ -296,7 +307,7 @@ static bool is_provider_valid(ts_temp_source_type_t type, int64_t now, bool try_

if (type == TS_TEMP_SOURCE_VARIABLE) {
variable_temp_snapshot_t snapshot = {0};
bool valid = read_variable_temp_snapshot(&snapshot, try_variable_lock);
bool valid = read_variable_temp_snapshot(&snapshot, TS_TEMP_DATA_TIMEOUT_MS, try_variable_lock);
store_variable_snapshot(&snapshot);
return valid;
}
Expand Down Expand Up @@ -338,7 +349,8 @@ static void evaluate_active_source_with_lock_mode(bool try_variable_lock)
ts_temp_source_type_t best_source = TS_TEMP_SOURCE_DEFAULT;
int16_t best_temp = TS_TEMP_DEFAULT_VALUE;
variable_temp_snapshot_t variable_snapshot = {0};
bool variable_snapshot_valid = read_variable_temp_snapshot(&variable_snapshot, try_variable_lock);
bool variable_snapshot_valid = read_variable_temp_snapshot(&variable_snapshot,
TS_TEMP_DATA_TIMEOUT_MS, try_variable_lock);
int64_t now = get_current_ms();
store_variable_snapshot(&variable_snapshot);

Expand Down Expand Up @@ -714,17 +726,17 @@ void ts_temp_read_auto_binding(ts_temp_auto_sample_t *sample)
}

sample->binding_revision = ts_temp_auto_binding_revision();
if (s_state.bound_var_count == 1) {
double value;
esp_err_t ret = read_fresh_variable_float(s_state.bound_vars[0].name, &value,
&sample->timestamp_ms, true);
if (ret == ESP_OK) {
sample->value = (int16_t)(value * 10.0);
sample->result = TS_TEMP_AUTO_VALID;
} else if (ret == ESP_ERR_TIMEOUT) {
sample->result = TS_TEMP_AUTO_VARIABLE_BUSY;
}
}
variable_temp_snapshot_t snapshot;
if (read_variable_temp_snapshot(&snapshot, TS_TEMP_AUTO_STALE_MS - 1, true)) {
sample->value = snapshot.temp;
sample->timestamp_ms = snapshot.timestamp_ms;
memcpy(sample->reports_ms, snapshot.reports_ms, sizeof(sample->reports_ms));
sample->result = TS_TEMP_AUTO_VALID;
} else if (snapshot.variable_busy) {
sample->result = TS_TEMP_AUTO_VARIABLE_BUSY;
}
sample->guard_value = snapshot.guard_temp;
sample->guard_timestamp_ms = snapshot.guard_timestamp_ms;
xSemaphoreGive(s_state.mutex);
}

Expand All @@ -737,7 +749,7 @@ esp_err_t ts_temp_get_by_source(ts_temp_source_type_t type, ts_temp_data_t *data

if (type == TS_TEMP_SOURCE_VARIABLE) {
variable_temp_snapshot_t snapshot = {0};
bool valid = read_variable_temp_snapshot(&snapshot, false);
bool valid = read_variable_temp_snapshot(&snapshot, TS_TEMP_DATA_TIMEOUT_MS, false);
store_variable_snapshot(&snapshot);
data->value = valid ? snapshot.temp : TS_TEMP_DEFAULT_VALUE;
data->source = TS_TEMP_SOURCE_VARIABLE;
Expand Down Expand Up @@ -1241,8 +1253,6 @@ esp_err_t ts_temp_bind_variables(const ts_temp_bound_var_t *vars, uint8_t count)
if (!vars || count == 0 || count > TS_TEMP_MAX_BOUND_VARS) return ESP_ERR_INVALID_ARG;

ts_temp_bound_var_t validated_vars[TS_TEMP_MAX_BOUND_VARS] = {0};
double total_weight = 0.0;

for (uint8_t i = 0; i < count; i++) {
if (vars[i].name[0] == '\0') return ESP_ERR_INVALID_ARG;
if (strlen(vars[i].name) >= TS_TEMP_MAX_VARNAME_LEN) return ESP_ERR_INVALID_SIZE;
Expand All @@ -1252,10 +1262,9 @@ esp_err_t ts_temp_bind_variables(const ts_temp_bound_var_t *vars, uint8_t count)

validated_vars[i] = vars[i];
validated_vars[i].weight = sanitize_bound_weight(vars[i].weight);
total_weight += validated_vars[i].weight;
}

if (total_weight <= 0.001) {
if (!has_positive_bound_weight(validated_vars, count)) {
TS_LOGW(TAG, "Rejected weighted binding with non-positive total weight");
return ESP_ERR_INVALID_ARG;
}
Expand All @@ -1267,11 +1276,13 @@ esp_err_t ts_temp_bind_variables(const ts_temp_bound_var_t *vars, uint8_t count)
changed = strcmp(s_state.bound_vars[i].name, validated_vars[i].name) != 0 ||
s_state.bound_vars[i].weight != validated_vars[i].weight;
}
if (changed) s_state.identity_revision++;
if ((count == 1 || s_state.bound_var_count == 1) &&
(count != s_state.bound_var_count ||
strcmp(s_state.bound_vars[0].name, validated_vars[0].name) != 0)) {
advance_auto_binding_revision();
if (changed) {
s_state.identity_revision++;
/* Changing only the weight of a single member leaves its reading unchanged. */
if (count != 1 || s_state.bound_var_count != 1 ||
strcmp(s_state.bound_vars[0].name, validated_vars[0].name) != 0) {
advance_auto_binding_revision();
}
}
s_state.bound_var_count = count;
memcpy(s_state.bound_vars, validated_vars, count * sizeof(ts_temp_bound_var_t));
Expand Down
7 changes: 5 additions & 2 deletions tests/fan_decay/test_decay.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ uint32_t ts_temp_auto_binding_revision(void) { return 1; }
bool ts_temp_auto_binding_begin_commit(uint32_t revision) { return revision == 1; }
void ts_temp_auto_binding_end_commit(void) {}
void ts_temp_read_auto_binding(ts_temp_auto_sample_t *sample) {
bool valid = input.valid && !input.partial_stale;
*sample = (ts_temp_auto_sample_t){
.timestamp_ms = input.timestamp_ms, .binding_revision = 1, .value = input.value,
.result = input.valid && !input.partial_stale ? TS_TEMP_AUTO_VALID : TS_TEMP_AUTO_UNAVAILABLE,
.timestamp_ms = input.timestamp_ms, .guard_timestamp_ms = valid ? input.timestamp_ms : 0,
.binding_revision = 1, .value = input.value, .guard_value = input.value,
.reports_ms = {valid ? input.timestamp_ms : 0},
.result = valid ? TS_TEMP_AUTO_VALID : TS_TEMP_AUTO_UNAVAILABLE,
};
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion tests/fan_monitor/run_host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ out.joinpath('fan_types.inc').write_text(
fan[start:fan.index('/*===========================================================================*/', start)] + structs(fan))
out.joinpath('temp_types.inc').write_text(structs(temp))
out.joinpath('temp_core.inc').write_text(functions(temp, [
'get_current_ms', 'advance_auto_binding_revision', 'sanitize_bound_weight', 'store_variable_snapshot',
'get_current_ms', 'advance_auto_binding_revision', 'sanitize_bound_weight', 'has_positive_bound_weight', 'store_variable_snapshot',
'read_fresh_variable_float', 'read_variable_temp_snapshot', 'is_provider_valid',
'evaluate_active_source_with_lock_mode', 'evaluate_active_source', 'sync_bound_variable_compat',
'ts_temp_bind_variables', 'ts_temp_unbind_variable', 'is_cached_active_source_valid_locked',
Expand Down
Loading
Loading