diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index 930d124..37f61c4 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -41,6 +41,10 @@ AnalogOut ref_i_dcdc(PIN_REF_I_DCDC); #endif +#ifdef PIN_TEMP_INT_PD +DigitalInOut temp_pd(PIN_TEMP_INT_PD); +#endif + float dcdc_current_offset; float load_current_offset; @@ -53,6 +57,7 @@ volatile uint32_t adc_filtered[NUM_ADC_CH] = {0}; extern Serial serial; extern log_data_t log_data; +extern float mcu_temp; void calibrate_current_sensors(dcdc_t *dcdc, load_output_t *load) { @@ -101,17 +106,20 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load // https://www.embeddedrelated.com/showarticle/91.php float v_temp, rts; + float bat_temp = 25.0; #ifdef PIN_ADC_TEMP_BAT // battery temperature calculation v_temp = ((adc_filtered[ADC_POS_TEMP_BAT] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096; // voltage read by ADC (mV) - rts = 10000 * v_temp / (vcc - v_temp); // resistance of NTC (Ohm) + rts = NTC_SERIES_RESISTOR * v_temp / (vcc - v_temp); // resistance of NTC (Ohm) // Temperature calculation using Beta equation for 10k thermistor // (25°C reference temperature for Beta equation assumed) - bat->temperature = 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C + bat_temp = 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C #endif + detect_battery_temperature(bat, bat_temp); + #ifdef PIN_ADC_TEMP_FETS // MOSFET temperature calculation v_temp = ((adc_filtered[ADC_POS_TEMP_FETS] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096; // voltage read by ADC (mV) @@ -119,12 +127,10 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load dcdc->temp_mosfets = 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C #endif -/* // internal MCU temperature - uint16_t adcval = (adc_filtered[ADC_POS_TEMP_MCU] >> (4 + ADC_FILTER_CONST)); - meas->temp_int = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) / (TSENSE_CAL2 - TSENSE_CAL1) * (adcval - TSENSE_CAL1) + TSENSE_CAL1_VALUE; + uint16_t adcval = (adc_filtered[ADC_POS_TEMP_MCU] >> (4 + ADC_FILTER_CONST)) * vcc / VREFINT_VALUE; + mcu_temp = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) / (TSENSE_CAL2 - TSENSE_CAL1) * (adcval - TSENSE_CAL1) + TSENSE_CAL1_VALUE; //printf("TS_CAL1:%d TS_CAL2:%d ADC:%d, temp_int:%f\n", TS_CAL1, TS_CAL2, adcval, meas->temp_int); -*/ if (ls->voltage > log_data.battery_voltage_max) { log_data.battery_voltage_max = ls->voltage; @@ -173,6 +179,60 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load //} } +void detect_battery_temperature(battery_state_t *bat, float bat_temp) +{ +#ifdef PIN_TEMP_INT_PD + + // state machine for external sensor detection + enum temp_sense_state { + TSENSE_STATE_CHECK, + TSENSE_STATE_CHECK_WAIT, + TSENSE_STATE_MEASURE, + TSENSE_STATE_MEASURE_WAIT + }; + static temp_sense_state ts_state = TSENSE_STATE_CHECK; + + static int sensor_change_counter = 0; + + switch (ts_state) { + case TSENSE_STATE_CHECK: + if (bat_temp < -50) { + bat->ext_temp_sensor = false; + temp_pd.output(); + temp_pd = 0; + ts_state = TSENSE_STATE_CHECK_WAIT; + } + else { + bat->ext_temp_sensor = true; + ts_state = TSENSE_STATE_MEASURE; + } + break; + case TSENSE_STATE_CHECK_WAIT: + sensor_change_counter++; + if (sensor_change_counter > 5) { + sensor_change_counter = 0; + ts_state = TSENSE_STATE_MEASURE; + } + break; + case TSENSE_STATE_MEASURE: + bat->temperature = bat->temperature * 0.8 + bat_temp * 0.2; + //printf("Battery temperature: %.2f (%s sensor)\n", bat_temp, (bat->ext_temp_sensor ? "external" : "internal")); + temp_pd.input(); + ts_state = TSENSE_STATE_MEASURE_WAIT; + break; + case TSENSE_STATE_MEASURE_WAIT: + sensor_change_counter++; + if (sensor_change_counter > 10) { + sensor_change_counter = 0; + ts_state = TSENSE_STATE_CHECK; + } + break; + } +#else + bat->temperature = bat_temp; +#endif +} + void dma_setup() { diff --git a/src/adc_dma.h b/src/adc_dma.h index 8728f54..7a5d0de 100644 --- a/src/adc_dma.h +++ b/src/adc_dma.h @@ -32,6 +32,10 @@ */ void calibrate_current_sensors(dcdc_t *dcdc, load_output_t *load); +/** Detects if external temperature sensor is attached, otherwise takes internal sensor + */ +void detect_battery_temperature(battery_state_t *bat, float bat_temp); + /** Updates structures with data read from ADC */ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, power_port_t *hs, power_port_t *ls); diff --git a/src/battery.cpp b/src/battery.cpp index 487e74a..4370573 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -187,6 +187,7 @@ void battery_state_init(battery_state_t *bat_state) { bat_state->num_batteries = 1; // initialize with only one battery in series bat_state->soh = 100; // assume new battery + bat_state->temperature = 25.0; } //---------------------------------------------------------------------------- diff --git a/src/battery.h b/src/battery.h index db0905c..e2a77a7 100644 --- a/src/battery.h +++ b/src/battery.h @@ -181,6 +181,7 @@ typedef struct int num_batteries; ///< Used for automatic 12V/24V battery detection at start-up (can be 1 or 2 only) float temperature; ///< Battery temperature in °C from ext. temperature sensor (if existing) + bool ext_temp_sensor; ///< True if external temperature sensor was detected float chg_day_Wh; ///< Cumulated energy in charge direction of today (Wh) float dis_day_Wh; ///< Cumulated energy in discharge direction of today (Wh) diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 4cc2a89..e431935 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -45,6 +45,7 @@ extern bool pub_usb_enabled; extern uint32_t timestamp; float latitude; // can be read out via GSM module for some network operators float longitude; +float mcu_temp; /** Data Objects * @@ -117,8 +118,12 @@ const data_object_t data_objects[] = { {0x72, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_port.current), "Bat_A"}, {0x73, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(load.current), "Load_A"}, {0x74, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(bat_state.temperature), "Bat_degC"}, - {0x75, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(dcdc.temp_mosfets), "Internal_degC"}, - {0x76, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.chg_state), "ChgState"}, + {0x75, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_BOOL, 1, (void*) &(bat_state.ext_temp_sensor), "BatTempExt"}, + {0x76, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(mcu_temp), "MCU_degC"}, +#ifdef PIN_ADC_TEMP_FETS + {0x77, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(dcdc.temp_mosfets), "FETs_degC"}, +#endif + {0x78, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.chg_state), "ChgState"}, // others {0x90, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 0, (void*) &(latitude), "Latitude"}, diff --git a/src/main.cpp b/src/main.cpp index fee20a4..790b8c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -144,7 +144,7 @@ int main() load_state_machine(&load, ls_port.input_allowed); - //eeprom_update(); + eeprom_update(); interfaces_process_1s(); toggle_led_blink(); diff --git a/src/pcbs/pcb_ls_010.h b/src/pcbs/pcb_ls_010.h index b8d4411..35be0db 100644 --- a/src/pcbs/pcb_ls_010.h +++ b/src/pcbs/pcb_ls_010.h @@ -81,6 +81,7 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { // typical value for Semitec 103AT-5 thermistor: 3435 #define NTC_BETA_VALUE 3435 +#define NTC_SERIES_RESISTOR 10000.0 #define ADC_GAIN_V_BAT (110.0 / 10 ) // battery voltage divider 100k + 10k #define ADC_GAIN_V_SOLAR (105.6 / 5.6) // solar voltage divider: 100k + 5.6k diff --git a/src/pcbs/pcb_pwm_01.h b/src/pcbs/pcb_pwm_01.h index 6b2d94b..775e711 100644 --- a/src/pcbs/pcb_pwm_01.h +++ b/src/pcbs/pcb_pwm_01.h @@ -43,6 +43,8 @@ #define PIN_REF_I_DCDC PA_4 +#define PIN_TEMP_INT_PD PA_8 + enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; // assignment LED numbers on PCB to their meaning @@ -73,6 +75,7 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { // typical value for Semitec 103AT-5 thermistor: 3435 #define NTC_BETA_VALUE 3435 +#define NTC_SERIES_RESISTOR 10400.0 #define ADC_GAIN_V_BAT (132 / 12) #define ADC_GAIN_V_SOLAR (1 + 120/12 + 120/8.2)