[feat] add concentration neg drift compensation

This commit is contained in:
zhji 2024-12-28 16:02:18 +08:00
parent a3391b6089
commit dde4e58234
2 changed files with 45 additions and 1 deletions

View File

@ -38,6 +38,49 @@ void concentration_init(void)
} }
} }
int32_t concentration_neg_drift_compensation(uint8_t ch, int32_t reading)
{
static uint8_t drift_update_enable[4] = {1, 1, 1, 1};
static uint8_t neg_reading_num[4] = {0, 0, 0, 0};
static int32_t neg_reading_sum[4] = {0, 0, 0, 0};
static int32_t zero_neg_drift[4] = {0, 0, 0, 0};
static int32_t reading_fixed[4] = {0, 0, 0, 0};
static uint8_t neg_fixed_num[4] = {0, 0, 0, 0};
reading_fixed[ch] = reading;
if(drift_update_enable[ch]) {
if (reading < 0) {
neg_reading_sum[ch] += reading;
neg_reading_num[ch] ++;
reading_fixed[ch] = 0;
if(neg_reading_num[ch] >= 10) {
zero_neg_drift[ch] = neg_reading_sum[ch] / neg_reading_num[ch];
drift_update_enable[ch] = 0;
neg_reading_num[ch] = 0;
neg_reading_sum[ch] = 0;
}
} else {
neg_reading_num[ch] = 0;
neg_reading_sum[ch] = 0;
}
}
if (zero_neg_drift[ch]) {
reading_fixed[ch] = reading - zero_neg_drift[ch];
if (reading_fixed[ch] < 0) {
neg_fixed_num[ch]++;
if (neg_fixed_num[ch] >= 10) {
drift_update_enable[ch] = 1;
neg_fixed_num[ch] = 0;
}
} else {
neg_fixed_num[ch] = 0;
}
}
return ((reading_fixed[ch] > 0) ? reading_fixed[ch] : 0);
}
int32_t concentration_calc(uint8_t ch) int32_t concentration_calc(uint8_t ch)
{ {
struct data_lookup_table_s *p; /* just for reduce text char in code */ struct data_lookup_table_s *p; /* just for reduce text char in code */
@ -105,7 +148,7 @@ int32_t concentration_calc(uint8_t ch)
k = (p->concentration[idx_c] - p->concentration[idx_c - 1]) / (p->calc_b[idx_c] - p->calc_b[idx_c - 1]); k = (p->concentration[idx_c] - p->concentration[idx_c - 1]) / (p->calc_b[idx_c] - p->calc_b[idx_c - 1]);
temp = k * (cnt - p->calc_b[idx_c - 1]) + p->concentration[idx_c - 1]; temp = k * (cnt - p->calc_b[idx_c - 1]) + p->concentration[idx_c - 1];
return (int32_t)temp; return concentration_neg_drift_compensation(ch, (int32_t)temp);
} }
void concentration_loop(void) void concentration_loop(void)

View File

@ -25,6 +25,7 @@ extern struct data_lookup_table_s data_lookup_table[4];
void concentration_lookup_table_copy(uint8_t ch); void concentration_lookup_table_copy(uint8_t ch);
void concentration_init(void); void concentration_init(void);
int32_t concentration_neg_drift_compensation(uint8_t ch, int32_t reading);
int32_t concentration_calc(uint8_t ch); int32_t concentration_calc(uint8_t ch);
void concentration_loop(void); void concentration_loop(void);