Merge pull request #73 from LibreSolar/adc-alerts
ADC alerts for faster over/undervoltage detection
This commit is contained in:
+100
-19
@@ -19,6 +19,7 @@
|
||||
#endif
|
||||
|
||||
#include "main.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include "adc_dma.h"
|
||||
#include "pcb.h" // contains defines for pins
|
||||
@@ -59,7 +60,7 @@ float load_current_offset;
|
||||
// for ADC and DMA
|
||||
volatile uint16_t adc_readings[NUM_ADC_CH] = {0};
|
||||
volatile uint32_t adc_filtered[NUM_ADC_CH] = {0};
|
||||
//volatile int num_adc_conversions;
|
||||
volatile AdcAlert adc_alerts[NUM_ADC_CH] = {0};
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
|
||||
@@ -76,6 +77,50 @@ void calibrate_current_sensors()
|
||||
load_current_offset = -(float)(((adc_filtered[ADC_POS_I_LOAD] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_LOAD / 1000.0;
|
||||
}
|
||||
|
||||
void adc_update_value(unsigned int pos)
|
||||
{
|
||||
// low pass filter with filter constant c = 1/16
|
||||
// y(n) = c * x(n) + (c - 1) * y(n-1)
|
||||
|
||||
#if FEATURE_PWM_SWITCH == 1
|
||||
if (pos == ADC_POS_V_SOLAR || pos == ADC_POS_I_SOLAR) {
|
||||
// only read input voltage and current when switch is on or permanently off
|
||||
if (pwm_switch.signal_high() || pwm_switch.active() == false) {
|
||||
adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST);
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
// adc_readings: 12-bit ADC values left-aligned in uint16_t
|
||||
adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST);
|
||||
}
|
||||
|
||||
adc_alerts[pos].debounce_ms++;
|
||||
if (adc_alerts[pos].callback_upper != NULL &&
|
||||
adc_readings[pos] > adc_alerts[pos].upper_limit)
|
||||
{
|
||||
if (adc_alerts[pos].debounce_ms > 1) {
|
||||
// create function pointer and call function
|
||||
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts[pos].callback_upper);
|
||||
alert();
|
||||
}
|
||||
}
|
||||
else if (adc_alerts[pos].callback_lower != NULL &&
|
||||
adc_readings[pos] < adc_alerts[pos].lower_limit)
|
||||
{
|
||||
if (adc_alerts[pos].debounce_ms > 1) {
|
||||
void (*alert)(void) = reinterpret_cast<void(*)()>(adc_alerts[pos].callback_lower);
|
||||
alert();
|
||||
}
|
||||
}
|
||||
else if (adc_alerts[pos].debounce_ms > 0) {
|
||||
// reset debounce ms counter only if already close to triggering to allow setting negative
|
||||
// values to specify a one-time inhibit delay
|
||||
adc_alerts[pos].debounce_ms = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void update_measurements()
|
||||
{
|
||||
@@ -185,6 +230,59 @@ void update_measurements()
|
||||
// else: keep previous setting
|
||||
}
|
||||
|
||||
void high_voltage_alert()
|
||||
{
|
||||
// disable any sort of input
|
||||
#if FEATURE_DCDC_CONVERTER
|
||||
dcdc.emergency_stop();
|
||||
#endif
|
||||
#if FEATURE_PWM_SWITCH
|
||||
pwm_switch.emergency_stop();
|
||||
#endif
|
||||
// do not use enter_state function, as we don't want to wait entire recharge delay
|
||||
charger.state = CHG_STATE_IDLE;
|
||||
|
||||
dev_stat.set_error(ERR_BAT_OVERVOLTAGE);
|
||||
|
||||
print_error("High voltage alert, ADC reading: %d limit: %d\n",
|
||||
adc_readings[ADC_POS_V_BAT], adc_alerts[ADC_POS_V_BAT].upper_limit);
|
||||
}
|
||||
|
||||
void low_voltage_alert()
|
||||
{
|
||||
// disable load output
|
||||
load.emergency_stop(LOAD_STATE_OFF_OVERCURRENT);
|
||||
charger.port->neg_current_limit = 0;
|
||||
|
||||
dev_stat.set_error(ERR_BAT_UNDERVOLTAGE);
|
||||
|
||||
print_error("Low voltage alert, ADC reading: %d limit: %d\n",
|
||||
adc_readings[ADC_POS_V_BAT], adc_alerts[ADC_POS_V_BAT].lower_limit);
|
||||
}
|
||||
|
||||
void adc_alert_inhibit(int adc_pos, int timeout_ms)
|
||||
{
|
||||
// set negative value so that we get a final debouncing of this timeout + the original
|
||||
// delay in the alert function (currently only waiting for 2 samples = 2 ms)
|
||||
adc_alerts[adc_pos].debounce_ms = -timeout_ms;
|
||||
}
|
||||
|
||||
void adc_set_lv_alerts(float upper, float lower)
|
||||
{
|
||||
int vcc = VREFINT_VALUE * VREFINT_CAL /
|
||||
(adc_filtered[ADC_POS_VREF_MCU] >> (4 + ADC_FILTER_CONST));
|
||||
|
||||
// LV side (battery) overvoltage alert
|
||||
adc_alerts[ADC_POS_V_BAT].upper_limit =
|
||||
(uint16_t)((upper * 1000 / (ADC_GAIN_V_BAT) * 4096.0 / vcc)) << 4;
|
||||
adc_alerts[ADC_POS_V_BAT].callback_upper = (void *) &high_voltage_alert;
|
||||
|
||||
// LV side (battery) undervoltage alert
|
||||
adc_alerts[ADC_POS_V_BAT].lower_limit =
|
||||
(uint16_t)((lower * 1000 / (ADC_GAIN_V_BAT) * 4096.0 / vcc)) << 4;
|
||||
adc_alerts[ADC_POS_V_BAT].callback_lower = (void *) &low_voltage_alert;
|
||||
}
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
void dma_setup()
|
||||
@@ -231,26 +329,9 @@ extern "C" void DMA1_Channel1_IRQHandler(void)
|
||||
{
|
||||
if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) // Test if transfer completed on DMA channel 1
|
||||
{
|
||||
// low pass filter with filter constant c = 1/16
|
||||
// y(n) = c * x(n) + (c - 1) * y(n-1)
|
||||
#ifdef CHARGER_TYPE_PWM
|
||||
for (unsigned int i = 0; i < NUM_ADC_CH; i++) {
|
||||
if (i == ADC_POS_V_SOLAR || i == ADC_POS_I_SOLAR) {
|
||||
// only read input voltage and current when switch is on or permanently off
|
||||
if (GPIOB->IDR & GPIO_PIN_1 || pwm_switch_enabled() == false) {
|
||||
adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST);
|
||||
}
|
||||
}
|
||||
else {
|
||||
adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST);
|
||||
}
|
||||
adc_update_value(i);
|
||||
}
|
||||
#else
|
||||
for (unsigned int i = 0; i < NUM_ADC_CH; i++) {
|
||||
// adc_readings: 12-bit ADC values left-aligned in uint16_t
|
||||
adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt registers
|
||||
}
|
||||
|
||||
@@ -28,6 +28,17 @@
|
||||
|
||||
#define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST)
|
||||
|
||||
/** Struct to definie upper and lower limit alerts for any ADC channel
|
||||
*/
|
||||
typedef struct {
|
||||
//bool enable = false; ///< Can be used to temporarily disable this alert
|
||||
void *callback_upper = NULL; ///< Function to be called when limits are exceeded
|
||||
void *callback_lower = NULL; ///< Function to be called when limits are exceeded
|
||||
uint16_t upper_limit = UINT16_MAX; ///< ADC reading for upper limit
|
||||
uint16_t lower_limit = 0; ///< ADC reading for lower limit
|
||||
int debounce_ms = 0; ///< Milliseconds delay for triggering alert
|
||||
} AdcAlert;
|
||||
|
||||
/** Sets offset to actual measured value, i.e. sets zero current point.
|
||||
*
|
||||
* All input/output switches and consumers should be switched off before calling this function
|
||||
@@ -50,4 +61,22 @@ void adc_setup(void);
|
||||
*/
|
||||
void dma_setup(void);
|
||||
|
||||
/** Read, filter and check raw ADC readings stored by DMA controller
|
||||
*/
|
||||
void adc_update_value(unsigned int pos);
|
||||
|
||||
/** Set lv side (battery) voltage limits where an alert should be triggered
|
||||
*
|
||||
* @param upper Upper voltage limit
|
||||
* @param lower Lower voltage limit
|
||||
*/
|
||||
void adc_set_lv_alerts(float upper, float lower);
|
||||
|
||||
/** Add an inhibit delay to the alerts to disable it temporarily
|
||||
*
|
||||
* @param adc_pos The position of the ADC measurement channel
|
||||
* @param timeout_ms Timeout in milliseconds
|
||||
*/
|
||||
void adc_alert_inhibit(int adc_pos, int timeout_ms);
|
||||
|
||||
#endif /* ADC_DMA */
|
||||
|
||||
@@ -351,6 +351,7 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
full = false;
|
||||
dev_stat.clear_error(ERR_BAT_CHG_OVERTEMP);
|
||||
dev_stat.clear_error(ERR_BAT_CHG_UNDERTEMP);
|
||||
dev_stat.clear_error(ERR_BAT_OVERVOLTAGE);
|
||||
enter_state(CHG_STATE_BULK);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -137,6 +137,7 @@ int Dcdc::check_start_conditions()
|
||||
hvs->voltage > hs_voltage_max || // also critical for buck mode because of ringing
|
||||
lvs->voltage > ls_voltage_max ||
|
||||
lvs->voltage < ls_voltage_min ||
|
||||
dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE) ||
|
||||
time(NULL) < (off_timestamp + restart_interval))
|
||||
{
|
||||
return 0; // no energy transfer allowed
|
||||
@@ -269,6 +270,13 @@ void Dcdc::test()
|
||||
}
|
||||
}
|
||||
|
||||
void Dcdc::emergency_stop()
|
||||
{
|
||||
half_bridge_stop();
|
||||
state = DCDC_STATE_OFF;
|
||||
off_timestamp = time(NULL);
|
||||
}
|
||||
|
||||
void Dcdc::self_destruction()
|
||||
{
|
||||
print_error("Charge controller self-destruction called!\n");
|
||||
|
||||
@@ -88,6 +88,12 @@ public:
|
||||
*/
|
||||
void test();
|
||||
|
||||
/** Fast emergency stop function
|
||||
*
|
||||
* May be called from an ISR which detected overvoltage / overcurrent conditions
|
||||
*/
|
||||
void emergency_stop();
|
||||
|
||||
/** Prevent overcharging of battery in case of shorted HS MOSFET
|
||||
*
|
||||
* This function switches the LS MOSFET continuously on to blow the battery input fuse. The
|
||||
|
||||
+10
-1
@@ -260,7 +260,9 @@ void LoadOutput::state_machine()
|
||||
switch (state) {
|
||||
case LOAD_STATE_DISABLED:
|
||||
if (enable == true) {
|
||||
if (port->pos_current_limit > 0) {
|
||||
if (port->pos_current_limit > 0 &&
|
||||
!dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE))
|
||||
{
|
||||
switch_set(true);
|
||||
state = LOAD_STATE_ON;
|
||||
}
|
||||
@@ -407,3 +409,10 @@ void LoadOutput::control()
|
||||
debounce_counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void LoadOutput::emergency_stop(uint16_t next_state)
|
||||
{
|
||||
switch_set(false);
|
||||
leds_flicker(LED_LOAD);
|
||||
state = next_state;
|
||||
}
|
||||
|
||||
@@ -80,6 +80,14 @@ public:
|
||||
*/
|
||||
void control();
|
||||
|
||||
/** Fast emergency stop function
|
||||
*
|
||||
* May be called from an ISR which detected overvoltage / overcurrent conditions
|
||||
*
|
||||
* @param next_state Select state the load should go to
|
||||
*/
|
||||
void emergency_stop(uint16_t next_state = LOAD_STATE_DISABLED);
|
||||
|
||||
uint16_t state; ///< Current state of load output switch
|
||||
uint16_t usb_state; ///< Current state of USB output
|
||||
|
||||
|
||||
@@ -165,6 +165,10 @@ int main()
|
||||
|
||||
load.state_machine();
|
||||
|
||||
// update regularly to cover changed battery configurations
|
||||
adc_set_lv_alerts(bat_conf.voltage_absolute_max * charger.num_batteries,
|
||||
bat_conf.voltage_absolute_min * charger.num_batteries);
|
||||
|
||||
eeprom_update();
|
||||
|
||||
leds_update_1s();
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include "config.h"
|
||||
#include "pcb.h"
|
||||
|
||||
#include "adc_dma.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h> // for time(NULL) function
|
||||
|
||||
@@ -108,6 +110,11 @@ void pwm_signal_stop()
|
||||
_pwm_active = false;
|
||||
}
|
||||
|
||||
bool pwm_signal_high()
|
||||
{
|
||||
return (GPIOB->IDR & GPIO_PIN_1);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// dummy functions for unit tests
|
||||
@@ -117,6 +124,7 @@ void pwm_signal_duty_cycle_step(int delta) {;}
|
||||
void pwm_signal_init_registers(int freq_Hz) {;}
|
||||
void pwm_signal_start(float pwm_duty) {;}
|
||||
void pwm_signal_stop() {;}
|
||||
bool pwm_signal_high() { return false; }
|
||||
|
||||
#endif
|
||||
|
||||
@@ -125,6 +133,11 @@ bool PwmSwitch::active()
|
||||
return _pwm_active;
|
||||
}
|
||||
|
||||
bool PwmSwitch::signal_high()
|
||||
{
|
||||
return pwm_signal_high();
|
||||
}
|
||||
|
||||
PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int)
|
||||
{
|
||||
terminal = pwm_terminal;
|
||||
@@ -192,12 +205,20 @@ void PwmSwitch::control()
|
||||
&& time(NULL) > (off_timestamp + restart_interval)
|
||||
&& enabled == true)
|
||||
{
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 100 ms
|
||||
adc_alert_inhibit(ADC_POS_V_BAT, 100);
|
||||
pwm_signal_start(1);
|
||||
printf("PWM charger start.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PwmSwitch::emergency_stop()
|
||||
{
|
||||
pwm_signal_stop();
|
||||
off_timestamp = time(NULL);
|
||||
}
|
||||
|
||||
float PwmSwitch::get_duty_cycle()
|
||||
{
|
||||
return pwm_signal_get_duty_cycle();
|
||||
|
||||
+13
-1
@@ -41,12 +41,24 @@ public:
|
||||
*/
|
||||
void control();
|
||||
|
||||
/** Read the on/off status of the PWM switch
|
||||
/** Fast emergency stop function
|
||||
*
|
||||
* May be called from an ISR which detected overvoltage / overcurrent conditions
|
||||
*/
|
||||
void emergency_stop();
|
||||
|
||||
/** Read the general on/off status of PWM switching
|
||||
*
|
||||
* @returns true if on
|
||||
*/
|
||||
bool active();
|
||||
|
||||
/** Read the current high or low state of the PWM signal
|
||||
*
|
||||
* @returns true if high, false if low
|
||||
*/
|
||||
bool signal_high();
|
||||
|
||||
/** Read the currently set duty cycle
|
||||
*
|
||||
* @returns Duty cycle between 0.0 and 1.0
|
||||
|
||||
+15
-7
@@ -22,14 +22,22 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern uint16_t adc_readings[NUM_ADC_CH];
|
||||
extern uint32_t adc_filtered[NUM_ADC_CH];
|
||||
|
||||
|
||||
void prepare_adc_readings(adc_values_t values)
|
||||
void prepare_adc_readings(AdcValues values)
|
||||
{
|
||||
adc_filtered[ADC_POS_VREF_MCU] = (uint32_t)(1.224 / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_V_SOLAR] = (uint32_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_V_BAT] = (uint32_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_I_DCDC] = (uint32_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_I_LOAD] = (uint32_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_readings[ADC_POS_VREF_MCU] = (uint16_t)(1.224 / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS_V_SOLAR] = (uint16_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS_V_BAT] = (uint16_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS_I_DCDC] = (uint16_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS_I_LOAD] = (uint16_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << 4;
|
||||
}
|
||||
|
||||
void prepare_adc_filtered()
|
||||
{
|
||||
// initialize also filtered values
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
adc_filtered[i] = adc_readings[i] << ADC_FILTER_CONST;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -25,6 +25,8 @@ typedef struct {
|
||||
float load_current;
|
||||
float bat_temperature;
|
||||
float internal_temperature;
|
||||
} adc_values_t;
|
||||
} AdcValues;
|
||||
|
||||
void prepare_adc_readings(adc_values_t values);
|
||||
void prepare_adc_readings(AdcValues values);
|
||||
|
||||
void prepare_adc_filtered();
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
|
||||
#include "tests.h"
|
||||
#include "adc_dma.h"
|
||||
#include "adc_dma_stub.h"
|
||||
|
||||
#include "main.h"
|
||||
|
||||
static adc_values_t adcval;
|
||||
|
||||
void check_solar_terminal_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current / adcval.solar_voltage * adcval.battery_voltage,
|
||||
-round(hv_terminal.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_bat_terminal_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_terminal.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current - adcval.load_current,
|
||||
round(lv_terminal.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_load_terminal_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load_terminal.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_lv_bus_int_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(dcdc_lv_port.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(dcdc_lv_port.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_temperature_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.bat_temperature, round(charger.bat_temperature * 10) / 10);
|
||||
}
|
||||
|
||||
/** ADC conversion test
|
||||
*
|
||||
* Purpose: Check if raw data from 2 voltage and 2 current measurements are converted
|
||||
* to calculated voltage/current measurements of different DC buses
|
||||
*/
|
||||
void adc_tests()
|
||||
{
|
||||
adcval.bat_temperature = 25;
|
||||
adcval.battery_voltage = 12;
|
||||
adcval.dcdc_current = 3;
|
||||
adcval.internal_temperature = 25;
|
||||
adcval.load_current = 1;
|
||||
adcval.solar_voltage = 30;
|
||||
prepare_adc_readings(adcval);
|
||||
|
||||
// call original update_measurements function
|
||||
update_measurements();
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(check_solar_terminal_readings);
|
||||
RUN_TEST(check_bat_terminal_readings);
|
||||
RUN_TEST(check_load_terminal_readings);
|
||||
RUN_TEST(check_lv_bus_int_readings);
|
||||
|
||||
//RUN_TEST(check_temperature_readings); // TODO
|
||||
|
||||
UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
|
||||
#include "tests.h"
|
||||
#include "adc_dma.h"
|
||||
#include "adc_dma_stub.h"
|
||||
|
||||
#include "main.h"
|
||||
|
||||
static AdcValues adcval;
|
||||
|
||||
extern uint32_t adc_filtered[NUM_ADC_CH];
|
||||
extern AdcAlert adc_alerts[NUM_ADC_CH];
|
||||
|
||||
// testing only for 2 values
|
||||
void check_filtering()
|
||||
{
|
||||
// reset values
|
||||
adc_filtered[ADC_POS_VREF_MCU] = 0;
|
||||
adc_filtered[ADC_POS_V_SOLAR] = 0;
|
||||
|
||||
// collect 1000 samples to update filtered values
|
||||
for (int s = 0; s < 1000; s++) {
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
adc_update_value(i);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t adc_filtered_bak[NUM_ADC_CH];
|
||||
adc_filtered_bak[ADC_POS_VREF_MCU] = adc_filtered[ADC_POS_VREF_MCU];
|
||||
adc_filtered_bak[ADC_POS_V_SOLAR] = adc_filtered[ADC_POS_V_SOLAR];
|
||||
|
||||
// overwrite filtered values
|
||||
prepare_adc_filtered();
|
||||
|
||||
TEST_ASSERT_EQUAL(adc_filtered[ADC_POS_VREF_MCU], adc_filtered_bak[ADC_POS_VREF_MCU]);
|
||||
TEST_ASSERT_EQUAL(adc_filtered[ADC_POS_V_SOLAR], adc_filtered_bak[ADC_POS_V_SOLAR]);
|
||||
}
|
||||
|
||||
void check_solar_terminal_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current / adcval.solar_voltage * adcval.battery_voltage,
|
||||
-round(hv_terminal.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_bat_terminal_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_terminal.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current - adcval.load_current,
|
||||
round(lv_terminal.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_load_terminal_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load_terminal.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_lv_bus_int_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(dcdc_lv_port.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(dcdc_lv_port.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_temperature_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.bat_temperature, round(charger.bat_temperature * 10) / 10);
|
||||
}
|
||||
|
||||
void adc_alert_undervoltage_triggering()
|
||||
{
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
adc_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
|
||||
prepare_adc_filtered();
|
||||
adc_update_value(ADC_POS_V_BAT);
|
||||
|
||||
// undervoltage test
|
||||
adcval.battery_voltage = bat_conf.voltage_absolute_min - 0.1;
|
||||
prepare_adc_readings(adcval);
|
||||
adc_update_value(ADC_POS_V_BAT);
|
||||
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE));
|
||||
adc_update_value(ADC_POS_V_BAT);
|
||||
TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE));
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state);
|
||||
|
||||
// reset values
|
||||
adcval.battery_voltage = 13;
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
update_measurements();
|
||||
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE));
|
||||
}
|
||||
|
||||
void adc_alert_overvoltage_triggering()
|
||||
{
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
adc_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min);
|
||||
prepare_adc_filtered();
|
||||
adc_update_value(ADC_POS_V_BAT);
|
||||
|
||||
dcdc.state = DCDC_STATE_MPPT;
|
||||
|
||||
// overvoltage test
|
||||
adcval.battery_voltage = bat_conf.voltage_absolute_max + 0.1;
|
||||
prepare_adc_readings(adcval);
|
||||
adc_update_value(ADC_POS_V_BAT);
|
||||
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
|
||||
adc_update_value(ADC_POS_V_BAT);
|
||||
TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
|
||||
TEST_ASSERT_EQUAL(false, pwm_switch.active());
|
||||
TEST_ASSERT_EQUAL(DCDC_STATE_OFF, dcdc.state);
|
||||
|
||||
// reset values
|
||||
adcval.battery_voltage = 12;
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
update_measurements();
|
||||
|
||||
charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
|
||||
}
|
||||
|
||||
/** ADC conversion test
|
||||
*
|
||||
* Purpose: Check if raw data from 2 voltage and 2 current measurements are converted
|
||||
* to calculated voltage/current measurements of different DC buses
|
||||
*/
|
||||
void adc_tests()
|
||||
{
|
||||
adcval.bat_temperature = 25;
|
||||
adcval.battery_voltage = 12;
|
||||
adcval.dcdc_current = 3;
|
||||
adcval.internal_temperature = 25;
|
||||
adcval.load_current = 1;
|
||||
adcval.solar_voltage = 30;
|
||||
prepare_adc_readings(adcval);
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(check_filtering);
|
||||
|
||||
// call original update_measurements function
|
||||
update_measurements();
|
||||
|
||||
RUN_TEST(check_solar_terminal_readings);
|
||||
RUN_TEST(check_bat_terminal_readings);
|
||||
RUN_TEST(check_load_terminal_readings);
|
||||
RUN_TEST(check_lv_bus_int_readings);
|
||||
|
||||
//RUN_TEST(check_temperature_readings); // TODO
|
||||
|
||||
RUN_TEST(adc_alert_undervoltage_triggering);
|
||||
RUN_TEST(adc_alert_overvoltage_triggering);
|
||||
|
||||
UNITY_END();
|
||||
}
|
||||
@@ -108,6 +108,7 @@ void dev_stat_new_bat_temp_max()
|
||||
|
||||
void dev_stat_new_int_temp_max()
|
||||
{
|
||||
dev_stat.int_temp_max = 20;
|
||||
dev_stat.internal_temp = 22;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(22, dev_stat.int_temp_max);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include "main.h"
|
||||
|
||||
static adc_values_t adcval;
|
||||
static AdcValues adcval;
|
||||
|
||||
const float dcdc_current_sun = 3;
|
||||
const float load_current = 1;
|
||||
@@ -38,6 +38,7 @@ void energy_calculation_init()
|
||||
|
||||
// insert values into ADC functions
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
update_measurements();
|
||||
|
||||
for (int i = 0; i < 60*60*sun_hours; i++) {
|
||||
@@ -49,6 +50,7 @@ void energy_calculation_init()
|
||||
// disable DC/DC = solar charging
|
||||
adcval.dcdc_current = 0;
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
update_measurements();
|
||||
|
||||
for (int i = 0; i < 60*60*night_hours; i++) {
|
||||
|
||||
Reference in New Issue
Block a user