Port unit tests to Zephyr build system
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef DAQ_STUB_H_
|
||||
#define DAQ_STUB_H_
|
||||
|
||||
/** Data to fill adc_filtered array during unit-tests
|
||||
*/
|
||||
typedef struct {
|
||||
float solar_voltage;
|
||||
float battery_voltage;
|
||||
float dcdc_current;
|
||||
float load_current;
|
||||
float bat_temperature;
|
||||
float internal_temperature;
|
||||
} AdcValues;
|
||||
|
||||
void prepare_adc_readings(AdcValues values);
|
||||
|
||||
void prepare_adc_filtered();
|
||||
void clear_adc_filtered();
|
||||
uint32_t get_adc_filtered(uint32_t channel);
|
||||
uint16_t adc_raw_clamp(float scale, float limit);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "unity.h"
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <device.h>
|
||||
|
||||
#ifdef CONFIG_ARCH_POSIX
|
||||
#include "posix_board_if.h"
|
||||
#endif
|
||||
|
||||
#include "tests.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
void setUp (void) {}
|
||||
void tearDown (void) {}
|
||||
|
||||
} /* extern "C" */
|
||||
|
||||
void main()
|
||||
{
|
||||
int err = 0;
|
||||
|
||||
err += daq_tests();
|
||||
err += bat_charger_tests();
|
||||
err += power_port_tests();
|
||||
err += half_bridge_tests();
|
||||
err += dcdc_tests();
|
||||
err += device_status_tests();
|
||||
err += load_tests();
|
||||
|
||||
#ifdef CONFIG_CUSTOM_TESTS
|
||||
err += custom_tests();
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ARCH_POSIX
|
||||
posix_exit(err);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <unity.h>
|
||||
#include <time.h>
|
||||
|
||||
int bat_charger_tests();
|
||||
|
||||
int daq_tests();
|
||||
|
||||
int power_port_tests();
|
||||
|
||||
int half_bridge_tests();
|
||||
|
||||
int dcdc_tests();
|
||||
|
||||
int device_status_tests();
|
||||
|
||||
int load_tests();
|
||||
|
||||
// activate this via build_flags in platformio.ini or custom.ini
|
||||
#ifdef CUSTOM_TESTS
|
||||
int custom_tests();
|
||||
#endif
|
||||
@@ -0,0 +1,342 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "tests.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "setup.h"
|
||||
|
||||
static void init_structs()
|
||||
{
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100);
|
||||
charger.init_terminal(&bat_conf);
|
||||
charger.state = CHG_STATE_IDLE;
|
||||
charger.bat_temperature = 25;
|
||||
bat_terminal.bus->voltage = 14.0;
|
||||
bat_terminal.bus->voltage_filtered = 14.0;
|
||||
bat_terminal.current = 0;
|
||||
}
|
||||
|
||||
void no_start_at_high_voltage()
|
||||
{
|
||||
init_structs();
|
||||
bat_terminal.bus->voltage = bat_conf.recharge_voltage + 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state);
|
||||
}
|
||||
|
||||
void no_start_after_short_rest()
|
||||
{
|
||||
init_structs();
|
||||
charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge + 1;
|
||||
bat_terminal.bus->voltage = bat_conf.recharge_voltage - 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state);
|
||||
}
|
||||
|
||||
void no_start_outside_temperature_limits()
|
||||
{
|
||||
init_structs();
|
||||
charger.bat_temperature = bat_conf.charge_temp_max + 1;
|
||||
bat_terminal.bus->voltage = bat_conf.recharge_voltage - 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state);
|
||||
|
||||
charger.bat_temperature = bat_conf.charge_temp_min - 1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state);
|
||||
}
|
||||
|
||||
void start_if_everything_just_fine()
|
||||
{
|
||||
init_structs();
|
||||
charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1;
|
||||
bat_terminal.bus->voltage = bat_conf.recharge_voltage - 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state);
|
||||
}
|
||||
|
||||
void enter_topping_at_voltage_setpoint()
|
||||
{
|
||||
init_structs();
|
||||
charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1;
|
||||
bat_terminal.bus->voltage = bat_conf.recharge_voltage - 0.1;
|
||||
bat_terminal.bus->voltage_filtered = bat_terminal.bus->voltage;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state);
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1;
|
||||
bat_terminal.bus->voltage_filtered = bat_terminal.bus->voltage;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state);
|
||||
}
|
||||
|
||||
void topping_to_bulk_after_8h_low_power()
|
||||
{
|
||||
enter_topping_at_voltage_setpoint();
|
||||
|
||||
charger.time_state_changed = time(NULL) - 8 * 60 * 60 + 1;
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage - 0.1;
|
||||
bat_terminal.bus->voltage_filtered = bat_terminal.bus->voltage;
|
||||
bat_terminal.current = bat_conf.topping_cutoff_current + 0.1;
|
||||
bat_terminal.current_filtered = bat_terminal.current;
|
||||
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state);
|
||||
|
||||
charger.time_state_changed = time(NULL) - 8 * 60 * 60 - 1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state);
|
||||
}
|
||||
|
||||
void stop_topping_after_time_limit()
|
||||
{
|
||||
enter_topping_at_voltage_setpoint();
|
||||
|
||||
charger.target_voltage_timer = bat_conf.topping_duration - 1;
|
||||
bat_terminal.current = bat_conf.topping_cutoff_current + 0.1;
|
||||
bat_terminal.current_filtered = bat_terminal.current;
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage -
|
||||
bat_terminal.current * bat_terminal.bus->sink_droop_res + 0.1;
|
||||
bat_terminal.bus->voltage_filtered = bat_terminal.bus->voltage;
|
||||
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state);
|
||||
|
||||
charger.target_voltage_timer = bat_conf.topping_duration + 1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_FLOAT, charger.state);
|
||||
}
|
||||
|
||||
void stop_topping_at_cutoff_current()
|
||||
{
|
||||
enter_topping_at_voltage_setpoint();
|
||||
|
||||
charger.target_voltage_timer = 0;
|
||||
bat_terminal.current = bat_conf.topping_cutoff_current - 0.1;
|
||||
bat_terminal.current_filtered = bat_terminal.current;
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage -
|
||||
bat_terminal.current * bat_terminal.bus->sink_droop_res + 0.1;
|
||||
bat_terminal.bus->voltage_filtered = bat_terminal.bus->voltage;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_FLOAT, charger.state);
|
||||
}
|
||||
|
||||
void float_to_idle_for_li_ion()
|
||||
{
|
||||
enter_topping_at_voltage_setpoint();
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
|
||||
charger.time_state_changed = time(NULL) - 1;
|
||||
bat_terminal.current = bat_conf.topping_cutoff_current - 0.1;
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage -
|
||||
bat_terminal.current * bat_terminal.bus->sink_droop_res + 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state);
|
||||
}
|
||||
|
||||
void no_equalization_if_disabled()
|
||||
{
|
||||
enter_topping_at_voltage_setpoint();
|
||||
bat_conf.equalization_enabled = false;
|
||||
|
||||
// set triggers such that it would normally start
|
||||
charger.deep_dis_last_equalization =
|
||||
charger.num_deep_discharges - bat_conf.equalization_trigger_deep_cycles;
|
||||
charger.time_last_equalization =
|
||||
time(NULL) - bat_conf.equalization_trigger_days * 24*60*60;
|
||||
|
||||
charger.time_state_changed = time(NULL) - 1;
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1;
|
||||
bat_terminal.current = bat_conf.topping_cutoff_current - 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state);
|
||||
}
|
||||
|
||||
void no_equalization_if_limits_not_reached()
|
||||
{
|
||||
enter_topping_at_voltage_setpoint();
|
||||
bat_conf.equalization_enabled = true;
|
||||
|
||||
// set triggers such that they are just NOT reached
|
||||
charger.deep_dis_last_equalization =
|
||||
charger.num_deep_discharges - bat_conf.equalization_trigger_deep_cycles + 1;
|
||||
charger.time_last_equalization =
|
||||
time(NULL) - (bat_conf.equalization_trigger_days - 1) * 24*60*60;
|
||||
|
||||
charger.time_state_changed = time(NULL) - 1;
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage + 0.1;
|
||||
bat_terminal.current = bat_conf.topping_cutoff_current - 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state);
|
||||
}
|
||||
|
||||
void float_to_equalization_if_enabled_and_time_limit_reached()
|
||||
{
|
||||
enter_topping_at_voltage_setpoint();
|
||||
bat_conf.equalization_enabled = true;
|
||||
charger.time_last_equalization =
|
||||
time(NULL) - bat_conf.equalization_trigger_days * 24*60*60;
|
||||
|
||||
charger.time_state_changed = time(NULL) - 1;
|
||||
bat_terminal.current = bat_conf.topping_cutoff_current - 0.1;
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage -
|
||||
bat_terminal.current * bat_terminal.bus->sink_droop_res + 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state);
|
||||
}
|
||||
|
||||
|
||||
void float_to_equalization_if_enabled_and_deep_dis_limit_reached()
|
||||
{
|
||||
enter_topping_at_voltage_setpoint();
|
||||
bat_conf.equalization_enabled = true;
|
||||
charger.deep_dis_last_equalization =
|
||||
charger.num_deep_discharges - bat_conf.equalization_trigger_deep_cycles;
|
||||
|
||||
charger.time_state_changed = time(NULL) - 1;
|
||||
bat_terminal.current = bat_conf.topping_cutoff_current - 0.1;
|
||||
bat_terminal.bus->voltage = bat_conf.topping_voltage -
|
||||
bat_terminal.current * bat_terminal.bus->sink_droop_res + 0.1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state);
|
||||
}
|
||||
|
||||
|
||||
void stop_equalization_after_time_limit()
|
||||
{
|
||||
float_to_equalization_if_enabled_and_time_limit_reached();
|
||||
|
||||
charger.time_state_changed = time(NULL) - bat_conf.equalization_duration + 1;
|
||||
bat_terminal.bus->voltage = bat_conf.equalization_voltage -
|
||||
bat_terminal.current * bat_terminal.bus->sink_droop_res + 0.1;
|
||||
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state);
|
||||
|
||||
charger.time_state_changed = time(NULL) - bat_conf.topping_duration - 1;
|
||||
charger.charge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_FLOAT, charger.state);
|
||||
}
|
||||
|
||||
void restart_bulk_from_float_if_voltage_drops()
|
||||
{
|
||||
TEST_ASSERT(0);
|
||||
}
|
||||
|
||||
void stop_discharge_at_low_voltage()
|
||||
{
|
||||
init_structs();
|
||||
|
||||
bat_terminal.bus->voltage = 14;
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit);
|
||||
|
||||
bat_terminal.bus->voltage = bat_conf.absolute_min_voltage - 0.1;
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit);
|
||||
}
|
||||
|
||||
void stop_discharge_at_overtemp()
|
||||
{
|
||||
init_structs();
|
||||
TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit);
|
||||
|
||||
charger.bat_temperature = bat_conf.discharge_temp_max + 1;
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit);
|
||||
}
|
||||
|
||||
void stop_discharge_at_undertemp()
|
||||
{
|
||||
init_structs();
|
||||
TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit);
|
||||
|
||||
charger.bat_temperature = bat_conf.discharge_temp_min - 1;
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit);
|
||||
}
|
||||
|
||||
void restart_discharge_if_allowed()
|
||||
{
|
||||
init_structs();
|
||||
|
||||
// stop because of undervoltage
|
||||
bat_terminal.bus->voltage = bat_conf.absolute_min_voltage - 0.1;
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit);
|
||||
|
||||
// increase voltage slightly above absolute minimum
|
||||
bat_terminal.bus->voltage = bat_conf.absolute_min_voltage + 0.05;
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit);
|
||||
|
||||
// increase voltage above hysteresis voltage
|
||||
bat_terminal.bus->voltage = bat_conf.absolute_min_voltage + 0.15;
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit);
|
||||
}
|
||||
|
||||
void battery_values_propagated_to_lv_bus_int()
|
||||
{
|
||||
TEST_ASSERT(0);
|
||||
}
|
||||
|
||||
void no_soc_above_100()
|
||||
{
|
||||
TEST_ASSERT(0);
|
||||
}
|
||||
|
||||
void no_soc_below_0()
|
||||
{
|
||||
TEST_ASSERT(0);
|
||||
}
|
||||
|
||||
int bat_charger_tests()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
// startup of charging
|
||||
RUN_TEST(no_start_at_high_voltage);
|
||||
RUN_TEST(no_start_after_short_rest);
|
||||
RUN_TEST(no_start_outside_temperature_limits);
|
||||
RUN_TEST(start_if_everything_just_fine);
|
||||
|
||||
// topping
|
||||
RUN_TEST(enter_topping_at_voltage_setpoint);
|
||||
RUN_TEST(topping_to_bulk_after_8h_low_power);
|
||||
RUN_TEST(stop_topping_after_time_limit);
|
||||
RUN_TEST(stop_topping_at_cutoff_current);
|
||||
|
||||
// float
|
||||
RUN_TEST(float_to_idle_for_li_ion);
|
||||
|
||||
// equalization
|
||||
RUN_TEST(no_equalization_if_disabled);
|
||||
RUN_TEST(no_equalization_if_limits_not_reached);
|
||||
RUN_TEST(float_to_equalization_if_enabled_and_time_limit_reached);
|
||||
RUN_TEST(float_to_equalization_if_enabled_and_deep_dis_limit_reached);
|
||||
RUN_TEST(stop_equalization_after_time_limit);
|
||||
|
||||
//RUN_TEST(restart_bulk_from_float_if_voltage_drops);
|
||||
|
||||
// TODO: temperature compensation
|
||||
// TODO: current compensation
|
||||
|
||||
RUN_TEST(stop_discharge_at_low_voltage);
|
||||
RUN_TEST(stop_discharge_at_overtemp);
|
||||
RUN_TEST(stop_discharge_at_undertemp);
|
||||
RUN_TEST(restart_discharge_if_allowed);
|
||||
|
||||
//RUN_TEST(battery_values_propagated_to_lv_bus_int);
|
||||
|
||||
// ToDo: SOC calculation
|
||||
//RUN_TEST(no_soc_above_100);
|
||||
//RUN_TEST(no_soc_below_0);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "tests.h"
|
||||
#include "daq.h"
|
||||
#include "daq_stub.h"
|
||||
#include "helper.h"
|
||||
#include "setup.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static AdcValues adcval;
|
||||
|
||||
void test_adc_voltage_to_raw()
|
||||
{
|
||||
int32_t raw;
|
||||
|
||||
raw = adc_voltage_to_raw(0.0, 3300);
|
||||
TEST_ASSERT_EQUAL_INT32(0, raw);
|
||||
|
||||
raw = adc_voltage_to_raw(3.3, 3300);
|
||||
TEST_ASSERT_EQUAL_INT32(65535, raw);
|
||||
|
||||
raw = adc_voltage_to_raw(1.65, 3300);
|
||||
TEST_ASSERT_EQUAL_INT32(32767, raw);
|
||||
}
|
||||
|
||||
void test_adc_raw_to_voltage()
|
||||
{
|
||||
float voltage;
|
||||
|
||||
voltage = adc_raw_to_voltage(0, 3300);
|
||||
TEST_ASSERT_EQUAL_FLOAT(0.0, voltage);
|
||||
|
||||
voltage = adc_raw_to_voltage(65535, 3300);
|
||||
TEST_ASSERT_FLOAT_WITHIN(0.01, 3.3, voltage);
|
||||
|
||||
voltage = adc_raw_to_voltage(32767, 3300);
|
||||
TEST_ASSERT_FLOAT_WITHIN(0.01, 1.65, voltage);
|
||||
}
|
||||
|
||||
// testing only for 2 values
|
||||
void check_filtering()
|
||||
{
|
||||
// reset values
|
||||
clear_adc_filtered();
|
||||
|
||||
// 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)] = get_adc_filtered(ADC_POS(vref_mcu));
|
||||
adc_filtered_bak[ADC_POS(v_high)] = get_adc_filtered(ADC_POS(v_high));
|
||||
|
||||
// overwrite filtered values
|
||||
prepare_adc_filtered();
|
||||
|
||||
TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS(vref_mcu)), adc_filtered_bak[ADC_POS(vref_mcu)]);
|
||||
TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS(v_high)), adc_filtered_bak[ADC_POS(v_high)]);
|
||||
}
|
||||
|
||||
void check_solar_terminal_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.bus->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.bus->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.bus->voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_temperature_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.bat_temperature, round(charger.bat_temperature * 10) / 10);
|
||||
}
|
||||
|
||||
void adc_alert_lv_undervoltage_triggering()
|
||||
{
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
daq_set_lv_limits(bat_conf.absolute_max_voltage, bat_conf.absolute_min_voltage);
|
||||
prepare_adc_filtered();
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
|
||||
// undervoltage test
|
||||
adcval.battery_voltage = bat_conf.absolute_min_voltage - 0.1;
|
||||
prepare_adc_readings(adcval);
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP));
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
load.control();
|
||||
TEST_ASSERT_EQUAL(true, flags_check(&load.error_flags, ERR_LOAD_VOLTAGE_DIP));
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load.state);
|
||||
|
||||
// reset values
|
||||
adcval.battery_voltage = 13;
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
daq_update();
|
||||
|
||||
charger.discharge_control(&bat_conf);
|
||||
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)); // ToDo
|
||||
}
|
||||
|
||||
void adc_alert_lv_overvoltage_triggering()
|
||||
{
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
daq_set_lv_limits(bat_conf.absolute_max_voltage, bat_conf.absolute_min_voltage);
|
||||
prepare_adc_filtered();
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
|
||||
dcdc.state = DCDC_CONTROL_MPPT;
|
||||
|
||||
// overvoltage test
|
||||
adcval.battery_voltage = bat_conf.absolute_max_voltage + 0.1;
|
||||
prepare_adc_readings(adcval);
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_OVERVOLTAGE));
|
||||
//TEST_ASSERT_EQUAL(false, pwm_switch.active());
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_OFF, dcdc.state);
|
||||
|
||||
// reset values
|
||||
adcval.battery_voltage = 12;
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
daq_update();
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
void adc_alert_hv_overvoltage_triggering()
|
||||
{
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
daq_set_hv_limit(DT_PROP(DT_PATH(pcb), hs_voltage_max));
|
||||
prepare_adc_filtered();
|
||||
adc_update_value(ADC_POS(v_high));
|
||||
|
||||
// overvoltage test
|
||||
adcval.solar_voltage = 85.0F;
|
||||
prepare_adc_readings(adcval);
|
||||
adc_update_value(ADC_POS(v_high));
|
||||
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_DCDC_HS_OVERVOLTAGE));
|
||||
adc_update_value(ADC_POS(v_high));
|
||||
TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_DCDC_HS_OVERVOLTAGE));
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_OFF, dcdc.state);
|
||||
}
|
||||
|
||||
void adc_alert_overflow_prevention()
|
||||
{
|
||||
// try to set an alert that overflows the 12-bit ADC resolution
|
||||
uint16_t limit = adc_raw_clamp(1, UINT16_MAX + 1);
|
||||
TEST_ASSERT_EQUAL_HEX(UINT16_MAX, limit);
|
||||
}
|
||||
|
||||
/** Data acquisition tests
|
||||
*
|
||||
* Purpose: Check if raw data from 2 voltage and 2 current measurements are converted
|
||||
* to calculated voltage/current measurements of different DC buses
|
||||
*/
|
||||
int daq_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(test_adc_voltage_to_raw);
|
||||
RUN_TEST(test_adc_raw_to_voltage);
|
||||
|
||||
RUN_TEST(check_filtering);
|
||||
|
||||
// call original daq_update function
|
||||
daq_update();
|
||||
|
||||
RUN_TEST(check_solar_terminal_readings);
|
||||
RUN_TEST(check_bat_terminal_readings);
|
||||
RUN_TEST(check_load_terminal_readings);
|
||||
|
||||
//RUN_TEST(check_temperature_readings); // TODO
|
||||
|
||||
RUN_TEST(adc_alert_lv_undervoltage_triggering);
|
||||
RUN_TEST(adc_alert_lv_overvoltage_triggering);
|
||||
RUN_TEST(adc_alert_hv_overvoltage_triggering);
|
||||
RUN_TEST(adc_alert_overflow_prevention);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,476 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "tests.h"
|
||||
|
||||
#include "half_bridge.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "setup.h"
|
||||
|
||||
static void init_structs_buck(int num_batteries = 1)
|
||||
{
|
||||
dev_stat.error_flags = 0;
|
||||
hv_terminal.init_solar();
|
||||
hv_terminal.bus->voltage = 20 * num_batteries;
|
||||
hv_terminal.bus->src_voltage_intercept = 18 * num_batteries;
|
||||
hv_terminal.bus->series_multiplier = 1;
|
||||
hv_terminal.current = 0;
|
||||
hv_terminal.update_bus_current_margins();
|
||||
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100);
|
||||
charger.port = &lv_terminal;
|
||||
charger.init_terminal(&bat_conf);
|
||||
lv_terminal.bus->voltage = 14 * num_batteries;
|
||||
lv_terminal.bus->series_multiplier = num_batteries;
|
||||
lv_terminal.current = 0;
|
||||
lv_terminal.update_bus_current_margins();
|
||||
|
||||
dcdc.mode = DCDC_MODE_BUCK;
|
||||
dcdc.temp_mosfets = 25;
|
||||
dcdc.off_timestamp = 0;
|
||||
dcdc.inductor_current = 0;
|
||||
dcdc.power = 0;
|
||||
dcdc.power_prev = 0;
|
||||
dcdc.pwm_delta = 1;
|
||||
dcdc.enable = true;
|
||||
}
|
||||
|
||||
static void start_buck()
|
||||
{
|
||||
dev_stat.error_flags = 0;
|
||||
init_structs_buck();
|
||||
half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97);
|
||||
dcdc.control();
|
||||
dcdc.control();
|
||||
dcdc.control(); // call multiple times because of startup delay
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_MPPT, dcdc.state);
|
||||
}
|
||||
|
||||
static void init_structs_boost(int num_batteries = 1)
|
||||
{
|
||||
half_bridge_stop();
|
||||
|
||||
hv_terminal.init_solar();
|
||||
lv_terminal.bus->voltage = 20;
|
||||
lv_terminal.bus->src_voltage_intercept = 18;
|
||||
lv_terminal.bus->series_multiplier = 1;
|
||||
dcdc.inductor_current = 0;
|
||||
dcdc.power = 0;
|
||||
lv_terminal.update_bus_current_margins();
|
||||
|
||||
int num_cells = (num_batteries == 1) ? 10 : 5;
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_NMC, num_cells, 9);
|
||||
charger.port = &hv_terminal;
|
||||
charger.init_terminal(&bat_conf);
|
||||
hv_terminal.bus->voltage = 3.7 * num_cells * num_batteries;
|
||||
hv_terminal.bus->series_multiplier = num_batteries;
|
||||
hv_terminal.current = 0;
|
||||
hv_terminal.power = 0;
|
||||
hv_terminal.update_bus_current_margins();
|
||||
|
||||
dcdc.mode = DCDC_MODE_BOOST;
|
||||
dcdc.temp_mosfets = 25;
|
||||
dcdc.off_timestamp = 0;
|
||||
dcdc.power_prev = 0;
|
||||
dcdc.pwm_delta = 1;
|
||||
dcdc.enable = true;
|
||||
}
|
||||
|
||||
static void start_boost()
|
||||
{
|
||||
init_structs_boost();
|
||||
half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97);
|
||||
dcdc.control();
|
||||
dcdc.control();
|
||||
dcdc.control(); // call multiple times because of startup delay
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_MPPT, dcdc.state);
|
||||
}
|
||||
|
||||
void start_valid_mppt_buck()
|
||||
{
|
||||
init_structs_buck();
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_BUCK, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void start_valid_mppt_buck_dual_battery()
|
||||
{
|
||||
init_structs_buck(2);
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_BUCK, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void start_valid_mppt_boost()
|
||||
{
|
||||
init_structs_boost();
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_BOOST, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void start_valid_mppt_boost_dual_battery()
|
||||
{
|
||||
init_structs_boost(2);
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_BOOST, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_start_before_restart_delay()
|
||||
{
|
||||
init_structs_buck();
|
||||
dcdc.off_timestamp = time(NULL) - dcdc.restart_interval + 1;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
dcdc.off_timestamp = time(NULL) - dcdc.restart_interval;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_BUCK, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_start_if_dcdc_disabled()
|
||||
{
|
||||
init_structs_buck();
|
||||
dcdc.enable = false;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_start_if_dcdc_lv_voltage_low()
|
||||
{
|
||||
init_structs_buck();
|
||||
lv_terminal.bus->voltage = dcdc.ls_voltage_min - 0.5;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
// buck
|
||||
|
||||
void no_buck_start_if_bat_voltage_high()
|
||||
{
|
||||
init_structs_buck();
|
||||
lv_terminal.bus->voltage = bat_conf.topping_voltage + 0.1;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_buck_start_if_bat_chg_not_allowed()
|
||||
{
|
||||
init_structs_buck();
|
||||
lv_terminal.bus->sink_current_margin = 0;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_buck_start_if_solar_voltage_high()
|
||||
{
|
||||
init_structs_buck();
|
||||
hv_terminal.bus->voltage = dcdc.hs_voltage_max + 1;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_buck_start_if_solar_voltage_low()
|
||||
{
|
||||
init_structs_buck();
|
||||
hv_terminal.bus->voltage = 17;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
// boost
|
||||
|
||||
void no_boost_start_if_bat_voltage_high()
|
||||
{
|
||||
init_structs_boost();
|
||||
hv_terminal.bus->voltage = bat_conf.topping_voltage + 0.1;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_boost_start_if_bat_chg_not_allowed()
|
||||
{
|
||||
init_structs_boost();
|
||||
hv_terminal.bus->sink_current_margin = 0;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_boost_start_if_solar_voltage_high()
|
||||
{
|
||||
init_structs_boost();
|
||||
lv_terminal.bus->voltage = dcdc.ls_voltage_max + 1;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
void no_boost_start_if_solar_voltage_low()
|
||||
{
|
||||
init_structs_boost();
|
||||
lv_terminal.bus->voltage = 17;
|
||||
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
|
||||
}
|
||||
|
||||
// buck operation
|
||||
|
||||
void buck_increasing_power()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
void buck_derating_output_voltage_too_high()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
lv_terminal.bus->voltage = lv_terminal.bus->sink_voltage_intercept + 0.1;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_LS, dcdc.state);
|
||||
}
|
||||
|
||||
void buck_derating_output_current_too_high()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
lv_terminal.current = lv_terminal.pos_current_limit + 0.1;
|
||||
lv_terminal.update_bus_current_margins();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_LS, dcdc.state);
|
||||
}
|
||||
|
||||
void buck_derating_inductor_current_too_high()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
dcdc.inductor_current = dcdc.inductor_current_max + 0.1;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_LS, dcdc.state);
|
||||
}
|
||||
|
||||
void buck_derating_input_voltage_too_low()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
hv_terminal.bus->voltage = hv_terminal.bus->src_voltage_intercept - 0.1;
|
||||
dcdc.power = 1.2;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_HS, dcdc.state);
|
||||
}
|
||||
|
||||
void buck_derating_input_current_too_high()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
hv_terminal.current = hv_terminal.neg_current_limit - 0.1;
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_HS, dcdc.state);
|
||||
}
|
||||
|
||||
void buck_derating_temperature_limits_exceeded()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
dcdc.temp_mosfets = 81;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
}
|
||||
|
||||
void buck_stop_input_power_too_low()
|
||||
{
|
||||
start_buck();
|
||||
dcdc.power_good_timestamp = time(NULL) - 11;
|
||||
dcdc.control();
|
||||
TEST_ASSERT(half_bridge_enabled() == false);
|
||||
}
|
||||
|
||||
void buck_stop_high_voltage_emergency()
|
||||
{
|
||||
start_buck();
|
||||
lv_terminal.bus->voltage = dcdc.ls_voltage_max + 0.1;
|
||||
dcdc.control();
|
||||
TEST_ASSERT(half_bridge_enabled() == false);
|
||||
}
|
||||
|
||||
void buck_correct_mppt_operation()
|
||||
{
|
||||
start_buck();
|
||||
|
||||
dcdc.power = 5;
|
||||
dcdc.control();
|
||||
float pwm1 = half_bridge_get_duty_cycle();
|
||||
dcdc.power = 7;
|
||||
dcdc.control();
|
||||
float pwm2 = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm2 > pwm1);
|
||||
|
||||
dcdc.power = 6; // decrease power to make the direction turn around
|
||||
dcdc.control();
|
||||
float pwm3 = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm3 < pwm2);
|
||||
}
|
||||
|
||||
// boost operation
|
||||
|
||||
void boost_increasing_power()
|
||||
{
|
||||
// make sure that without changing anything, dcdc_control goes in direction of increasing power
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
}
|
||||
|
||||
void boost_derating_output_voltage_too_high()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
hv_terminal.bus->voltage = hv_terminal.bus->sink_voltage_intercept + 0.5;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power
|
||||
}
|
||||
|
||||
void boost_derating_output_current_too_high()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
hv_terminal.current = hv_terminal.pos_current_limit + 0.1;
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
void boost_derating_input_voltage_too_low()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
lv_terminal.bus->voltage = lv_terminal.bus->src_voltage_intercept - 0.1;
|
||||
dcdc.power = -1.2;
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_LS, dcdc.state);
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
void boost_derating_input_current_too_high()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
dcdc.inductor_current = lv_terminal.neg_current_limit - 0.1;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
void boost_derating_temperature_limits_exceeded()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
dcdc.temp_mosfets = 81;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
void boost_stop_input_power_too_low()
|
||||
{
|
||||
start_boost();
|
||||
dcdc.power_good_timestamp = time(NULL) - 11;
|
||||
dcdc.control();
|
||||
TEST_ASSERT(half_bridge_enabled() == false);
|
||||
}
|
||||
|
||||
void boost_stop_high_voltage_emergency()
|
||||
{
|
||||
start_boost();
|
||||
hv_terminal.bus->voltage = dcdc.hs_voltage_max + 0.1;
|
||||
dcdc.control();
|
||||
TEST_ASSERT(half_bridge_enabled() == false);
|
||||
}
|
||||
|
||||
void boost_correct_mppt_operation()
|
||||
{
|
||||
start_boost();
|
||||
|
||||
dcdc.power = -5;
|
||||
dcdc.control();
|
||||
float pwm1 = half_bridge_get_duty_cycle();
|
||||
dcdc.power = -7;
|
||||
dcdc.control();
|
||||
float pwm2 = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm2 < pwm1);
|
||||
|
||||
dcdc.power = -6; // decrease power to make the direction turn around
|
||||
dcdc.control();
|
||||
float pwm3 = half_bridge_get_duty_cycle();
|
||||
TEST_ASSERT(pwm3 > pwm2);
|
||||
}
|
||||
|
||||
int dcdc_tests()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
// 1. Check of general (re)start conditions
|
||||
|
||||
// check if initialization of test is correct and we start at all
|
||||
RUN_TEST(start_valid_mppt_buck);
|
||||
RUN_TEST(start_valid_mppt_buck_dual_battery);
|
||||
RUN_TEST(start_valid_mppt_boost);
|
||||
RUN_TEST(start_valid_mppt_boost_dual_battery);
|
||||
|
||||
RUN_TEST(no_start_before_restart_delay);
|
||||
RUN_TEST(no_start_if_dcdc_disabled);
|
||||
RUN_TEST(no_start_if_dcdc_lv_voltage_low);
|
||||
|
||||
// 2. Check startup for MPPT buck converter scenario
|
||||
|
||||
RUN_TEST(no_buck_start_if_bat_voltage_high);
|
||||
RUN_TEST(no_buck_start_if_bat_chg_not_allowed);
|
||||
RUN_TEST(no_buck_start_if_solar_voltage_high);
|
||||
RUN_TEST(no_buck_start_if_solar_voltage_low);
|
||||
|
||||
// 3. Check startup for MPPT boost converter scenario
|
||||
|
||||
RUN_TEST(no_boost_start_if_bat_voltage_high);
|
||||
RUN_TEST(no_boost_start_if_bat_chg_not_allowed);
|
||||
RUN_TEST(no_boost_start_if_solar_voltage_high);
|
||||
RUN_TEST(no_boost_start_if_solar_voltage_low);
|
||||
|
||||
// 4. Check startup for nanogrid scenario
|
||||
|
||||
// TODO
|
||||
|
||||
// 5. Check DC/DC control after being started
|
||||
|
||||
// buck mode
|
||||
RUN_TEST(buck_increasing_power);
|
||||
RUN_TEST(buck_derating_output_voltage_too_high);
|
||||
RUN_TEST(buck_derating_output_current_too_high);
|
||||
RUN_TEST(buck_derating_inductor_current_too_high);
|
||||
RUN_TEST(buck_derating_input_voltage_too_low);
|
||||
RUN_TEST(buck_derating_input_current_too_high);
|
||||
RUN_TEST(buck_derating_temperature_limits_exceeded);
|
||||
RUN_TEST(buck_stop_input_power_too_low);
|
||||
RUN_TEST(buck_stop_high_voltage_emergency);
|
||||
RUN_TEST(buck_correct_mppt_operation);
|
||||
|
||||
// boost mode
|
||||
RUN_TEST(boost_increasing_power);
|
||||
RUN_TEST(boost_derating_output_voltage_too_high);
|
||||
RUN_TEST(boost_derating_output_current_too_high);
|
||||
RUN_TEST(boost_derating_input_voltage_too_low);
|
||||
RUN_TEST(boost_derating_input_current_too_high);
|
||||
RUN_TEST(boost_derating_temperature_limits_exceeded);
|
||||
RUN_TEST(boost_stop_input_power_too_low);
|
||||
RUN_TEST(boost_stop_high_voltage_emergency);
|
||||
RUN_TEST(boost_correct_mppt_operation);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "tests.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "setup.h"
|
||||
|
||||
void reset_counters_at_start_of_day()
|
||||
{
|
||||
solar_terminal.bus->voltage = bat_terminal.bus->voltage - 1;
|
||||
|
||||
dev_stat.day_counter = 0;
|
||||
|
||||
solar_terminal.neg_energy_Wh = 10.0;
|
||||
bat_terminal.neg_energy_Wh = 3.0;
|
||||
bat_terminal.pos_energy_Wh = 4.0;
|
||||
load.pos_energy_Wh = 9.0;
|
||||
|
||||
// 5 houurs without sun
|
||||
for (int i = 0; i <= 5 * 60 * 60; i++) {
|
||||
dev_stat.update_energy();
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(10, dev_stat.solar_in_total_Wh);
|
||||
TEST_ASSERT_EQUAL(3, dev_stat.bat_dis_total_Wh);
|
||||
TEST_ASSERT_EQUAL(4, dev_stat.bat_chg_total_Wh);
|
||||
TEST_ASSERT_EQUAL(9, dev_stat.load_out_total_Wh);
|
||||
|
||||
TEST_ASSERT_EQUAL(10, solar_terminal.neg_energy_Wh);
|
||||
TEST_ASSERT_EQUAL(3, bat_terminal.neg_energy_Wh);
|
||||
TEST_ASSERT_EQUAL(4, bat_terminal.pos_energy_Wh);
|
||||
TEST_ASSERT_EQUAL(9, load.pos_energy_Wh);
|
||||
|
||||
// solar didn't come back yet
|
||||
TEST_ASSERT_EQUAL(0, dev_stat.day_counter);
|
||||
|
||||
// now solar power comes back
|
||||
solar_terminal.bus->voltage = bat_terminal.bus->voltage + 1;
|
||||
dev_stat.update_energy();
|
||||
|
||||
// day counter should be increased and daily energy counters reset
|
||||
TEST_ASSERT_EQUAL(1, dev_stat.day_counter);
|
||||
TEST_ASSERT_EQUAL(0, solar_terminal.neg_energy_Wh);
|
||||
TEST_ASSERT_EQUAL(0, bat_terminal.neg_energy_Wh);
|
||||
TEST_ASSERT_EQUAL(0, bat_terminal.pos_energy_Wh);
|
||||
TEST_ASSERT_EQUAL(0, load.pos_energy_Wh);
|
||||
}
|
||||
|
||||
void dev_stat_new_solar_voltage_max()
|
||||
{
|
||||
solar_terminal.bus->voltage = 40;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(40, dev_stat.solar_voltage_max);
|
||||
}
|
||||
|
||||
void dev_stat_new_bat_voltage_max()
|
||||
{
|
||||
bat_terminal.bus->voltage = 31;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(31, dev_stat.battery_voltage_max);
|
||||
}
|
||||
|
||||
void dev_stat_new_dcdc_current_max()
|
||||
{
|
||||
dcdc.inductor_current = 21;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(21, dev_stat.dcdc_current_max);
|
||||
}
|
||||
|
||||
void dev_stat_new_load_current_max()
|
||||
{
|
||||
load.current = 21;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(21, dev_stat.load_current_max);
|
||||
}
|
||||
|
||||
void dev_stat_solar_power_max()
|
||||
{
|
||||
solar_terminal.power = -50;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(50, dev_stat.solar_power_max_day);
|
||||
TEST_ASSERT_EQUAL(50, dev_stat.solar_power_max_total);
|
||||
}
|
||||
|
||||
void dev_stat_load_power_max()
|
||||
{
|
||||
load.power = 50;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(50, dev_stat.load_power_max_day);
|
||||
TEST_ASSERT_EQUAL(50, dev_stat.load_power_max_total);
|
||||
}
|
||||
|
||||
void dev_stat_new_mosfet_temp_max()
|
||||
{
|
||||
dcdc.temp_mosfets = 80;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(80, dev_stat.mosfet_temp_max);
|
||||
}
|
||||
|
||||
void dev_stat_new_bat_temp_max()
|
||||
{
|
||||
charger.bat_temperature = 45;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(45, dev_stat.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);
|
||||
}
|
||||
|
||||
int device_status_tests()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(reset_counters_at_start_of_day);
|
||||
|
||||
RUN_TEST(dev_stat_new_solar_voltage_max);
|
||||
RUN_TEST(dev_stat_new_bat_voltage_max);
|
||||
RUN_TEST(dev_stat_new_dcdc_current_max);
|
||||
RUN_TEST(dev_stat_new_load_current_max);
|
||||
RUN_TEST(dev_stat_solar_power_max);
|
||||
RUN_TEST(dev_stat_load_power_max);
|
||||
RUN_TEST(dev_stat_new_mosfet_temp_max);
|
||||
RUN_TEST(dev_stat_new_bat_temp_max);
|
||||
RUN_TEST(dev_stat_new_int_temp_max);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "tests.h"
|
||||
#include "half_bridge.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#define MAX_PWM_DUTY 0.97
|
||||
#define MIN_PWM_DUTY 0.1
|
||||
#define MID_PWM_DUTY ((MIN_PWM_DUTY + MAX_PWM_DUTY)/2)
|
||||
#define PWM_F_KHZ 70
|
||||
#define PWM_DEADTIME_NS 300
|
||||
|
||||
const float duty_epsilon = 0.006;
|
||||
// calculated duty cycle float may deviate +/- duty_epsilon from test target value
|
||||
|
||||
static void init_structs()
|
||||
{
|
||||
half_bridge_init(PWM_F_KHZ, PWM_DEADTIME_NS, MIN_PWM_DUTY, MAX_PWM_DUTY);
|
||||
half_bridge_stop();
|
||||
}
|
||||
|
||||
void half_bridge_set_duty_cycle_works()
|
||||
{
|
||||
half_bridge_set_duty_cycle(MID_PWM_DUTY);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MID_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
}
|
||||
|
||||
void half_bridge_starts_up()
|
||||
{
|
||||
half_bridge_set_duty_cycle(MID_PWM_DUTY);
|
||||
half_bridge_start();
|
||||
TEST_ASSERT_EQUAL(true, half_bridge_enabled());
|
||||
}
|
||||
|
||||
void half_bridge_stops()
|
||||
{
|
||||
half_bridge_set_duty_cycle(MID_PWM_DUTY);
|
||||
half_bridge_start();
|
||||
half_bridge_stop();
|
||||
TEST_ASSERT_EQUAL(false, half_bridge_enabled());
|
||||
}
|
||||
|
||||
void half_bridge_duty_limits_not_violated()
|
||||
{
|
||||
// maximum limit
|
||||
half_bridge_set_duty_cycle(1.0);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MAX_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
|
||||
// minimum limit
|
||||
half_bridge_set_duty_cycle(0.0);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MIN_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
}
|
||||
|
||||
void half_bridge_ccr_limits_not_violated()
|
||||
{
|
||||
// maximum limit
|
||||
half_bridge_set_duty_cycle(MAX_PWM_DUTY);
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() + 1);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MAX_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
|
||||
// minimum limit
|
||||
half_bridge_set_duty_cycle(MIN_PWM_DUTY);
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() - 1);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MIN_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
}
|
||||
|
||||
int half_bridge_tests()
|
||||
{
|
||||
init_structs();
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(half_bridge_set_duty_cycle_works);
|
||||
RUN_TEST(half_bridge_starts_up);
|
||||
RUN_TEST(half_bridge_stops);
|
||||
|
||||
RUN_TEST(half_bridge_duty_limits_not_violated);
|
||||
RUN_TEST(half_bridge_ccr_limits_not_violated);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "tests.h"
|
||||
#include "daq_stub.h"
|
||||
#include "daq.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "setup.h"
|
||||
|
||||
static bool output_on;
|
||||
|
||||
static void load_drv_set(bool status)
|
||||
{
|
||||
output_on = status;
|
||||
}
|
||||
|
||||
static void load_drv_init()
|
||||
{
|
||||
output_on = false;
|
||||
}
|
||||
|
||||
static void load_init(LoadOutput *l, bool on = false, int num_batteries = 1)
|
||||
{
|
||||
l->overvoltage = 14.6;
|
||||
l->current = 0;
|
||||
l->pos_current_limit = 10;
|
||||
l->bus->series_multiplier = num_batteries;
|
||||
l->bus->voltage = 14 * num_batteries;
|
||||
l->bus->sink_voltage_intercept = 14.4;
|
||||
l->bus->src_voltage_intercept = 12;
|
||||
l->bus->sink_current_margin = 10;
|
||||
l->bus->src_current_margin = -10;
|
||||
l->junction_temperature = 25;
|
||||
l->error_flags = 0;
|
||||
l->enable = true;
|
||||
|
||||
if (on) {
|
||||
load_drv_set(true);
|
||||
l->state = LOAD_STATE_ON;
|
||||
|
||||
// check if it stays on
|
||||
l->control();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, l->state);
|
||||
}
|
||||
}
|
||||
|
||||
static void control_off_to_on_if_everything_fine()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out);
|
||||
|
||||
load_out.enable = true;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
}
|
||||
|
||||
static void control_off_to_on_if_everything_fine_dual_battery()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, false, 2);
|
||||
|
||||
load_out.enable = true;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
}
|
||||
|
||||
static void control_on_to_off_shedding()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, true);
|
||||
|
||||
bus.voltage = load_out.disconnect_voltage - 0.1;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_SHEDDING, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
}
|
||||
|
||||
void control_on_to_off_overvoltage()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, true);
|
||||
|
||||
bus.voltage = bus.sink_voltage_intercept + 0.6;
|
||||
|
||||
// increase debounce counter to 1 before limit
|
||||
for (int i = 0; i < CONFIG_CONTROL_FREQUENCY; i++) {
|
||||
load_out.control();
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
|
||||
load_out.control(); // once more
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
}
|
||||
|
||||
void control_on_to_off_overvoltage_dual_battery()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, true, 2);
|
||||
|
||||
bus.voltage = (bus.sink_voltage_intercept + 0.6) * bus.series_multiplier;
|
||||
|
||||
// increase debounce counter to 1 before limit
|
||||
for (int i = 0; i < CONFIG_CONTROL_FREQUENCY; i++) {
|
||||
load_out.control();
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
|
||||
load_out.control(); // once more
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
}
|
||||
|
||||
void control_on_to_off_overcurrent()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, true);
|
||||
|
||||
// current slightly below factor 2 so that it is not switched off immediately
|
||||
load_out.current = DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max) * 1.9;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
|
||||
// almost 2x current = 4x heat generation: Should definitely trigger after waiting one time constant
|
||||
int trigger_steps = DT_PROP(DT_PATH(pcb), mosfets_tau_ja) * CONFIG_CONTROL_FREQUENCY;
|
||||
for (int i = 0; i <= trigger_steps; i++) {
|
||||
load_out.control();
|
||||
}
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
}
|
||||
|
||||
void control_on_to_off_voltage_dip()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, true);
|
||||
|
||||
load_out.stop(ERR_LOAD_VOLTAGE_DIP);
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_VOLTAGE_DIP, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
}
|
||||
|
||||
void control_on_to_off_bus_limit()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, true);
|
||||
|
||||
load_out.bus->src_current_margin = 0;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_BUS_SRC_CURRENT, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
}
|
||||
|
||||
void control_on_to_off_if_enable_false()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, true);
|
||||
|
||||
load_out.enable = false;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
}
|
||||
|
||||
void control_off_shedding_to_on_after_delay()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out);
|
||||
load_out.error_flags = ERR_LOAD_SHEDDING;
|
||||
|
||||
load_out.lvd_timestamp = time(NULL) - load_out.lvd_recovery_delay + 1;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_SHEDDING, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
|
||||
load_out.lvd_timestamp = time(NULL) - load_out.lvd_recovery_delay - 1;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
}
|
||||
|
||||
void control_off_overcurrent_to_on_after_delay()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out);
|
||||
load_out.error_flags = ERR_LOAD_OVERCURRENT;
|
||||
|
||||
load_out.oc_timestamp = time(NULL) - load_out.oc_recovery_delay + 1;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
|
||||
load_out.oc_timestamp = time(NULL) - load_out.oc_recovery_delay - 1;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
}
|
||||
|
||||
void control_off_overvoltage_to_on_at_lower_voltage()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out);
|
||||
bus.voltage = load_out.overvoltage + 0.1;
|
||||
load_out.error_flags = ERR_LOAD_OVERVOLTAGE;
|
||||
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
|
||||
bus.voltage = load_out.overvoltage - 0.1; // test hysteresis
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
|
||||
bus.voltage = load_out.overvoltage - load_out.ov_hysteresis - 0.1;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
}
|
||||
|
||||
void control_off_overvoltage_to_on_at_lower_voltage_dual_battery()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out, false, 2);
|
||||
bus.voltage = (load_out.overvoltage + 0.1) * bus.series_multiplier;
|
||||
load_out.error_flags = ERR_LOAD_OVERVOLTAGE;
|
||||
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
|
||||
bus.voltage = (load_out.overvoltage - 0.1) * bus.series_multiplier; // test hysteresis
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
|
||||
bus.voltage = (load_out.overvoltage - load_out.ov_hysteresis - 0.1) * bus.series_multiplier;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
}
|
||||
|
||||
void control_off_short_circuit_flag_reset()
|
||||
{
|
||||
DcBus bus = {};
|
||||
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init, NULL);
|
||||
load_init(&load_out);
|
||||
load_out.error_flags = ERR_LOAD_SHORT_CIRCUIT;
|
||||
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(ERR_LOAD_SHORT_CIRCUIT, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
|
||||
load_out.enable = false; // this is like a manual reset
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(0, load_out.error_flags);
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
|
||||
|
||||
load_out.enable = true;
|
||||
load_out.control();
|
||||
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
|
||||
}
|
||||
|
||||
int load_tests()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
// control tests
|
||||
RUN_TEST(control_off_to_on_if_everything_fine);
|
||||
RUN_TEST(control_off_to_on_if_everything_fine_dual_battery);
|
||||
RUN_TEST(control_on_to_off_shedding);
|
||||
RUN_TEST(control_on_to_off_overvoltage);
|
||||
RUN_TEST(control_on_to_off_overvoltage_dual_battery);
|
||||
RUN_TEST(control_on_to_off_overcurrent);
|
||||
RUN_TEST(control_on_to_off_voltage_dip);
|
||||
RUN_TEST(control_on_to_off_bus_limit);
|
||||
RUN_TEST(control_on_to_off_if_enable_false);
|
||||
|
||||
RUN_TEST(control_off_shedding_to_on_after_delay);
|
||||
RUN_TEST(control_off_overcurrent_to_on_after_delay);
|
||||
RUN_TEST(control_off_overvoltage_to_on_at_lower_voltage);
|
||||
RUN_TEST(control_off_overvoltage_to_on_at_lower_voltage_dual_battery);
|
||||
RUN_TEST(control_off_short_circuit_flag_reset);
|
||||
|
||||
// ToDo: What to do if port current is above the limit, but the hardware can still handle it?
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "tests.h"
|
||||
#include "daq_stub.h"
|
||||
#include "daq.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "setup.h"
|
||||
|
||||
static AdcValues adcval;
|
||||
|
||||
const float dcdc_current_sun = 3;
|
||||
const float load_current = 1;
|
||||
const int sun_hours = 1;
|
||||
const int night_hours = 3;
|
||||
|
||||
void energy_calculation_init()
|
||||
{
|
||||
dev_stat.solar_in_total_Wh = 0;
|
||||
dev_stat.load_out_total_Wh = 0;
|
||||
dev_stat.bat_chg_total_Wh = 0;
|
||||
dev_stat.bat_dis_total_Wh = 0;
|
||||
hv_terminal.neg_energy_Wh = 0.0;
|
||||
load.pos_energy_Wh = 0.0;
|
||||
lv_terminal.pos_energy_Wh = 0.0;
|
||||
lv_terminal.neg_energy_Wh = 0.0;
|
||||
|
||||
// set desired measurement values
|
||||
adcval.bat_temperature = 25;
|
||||
adcval.battery_voltage = 12;
|
||||
adcval.dcdc_current = dcdc_current_sun;
|
||||
adcval.internal_temperature = 25;
|
||||
adcval.load_current = load_current;
|
||||
adcval.internal_temperature = 25;
|
||||
adcval.solar_voltage = 30;
|
||||
|
||||
// insert values into ADC functions
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
daq_update();
|
||||
|
||||
for (int i = 0; i < 60*60*sun_hours; i++) {
|
||||
hv_terminal.energy_balance();
|
||||
lv_terminal.energy_balance();
|
||||
load.energy_balance();
|
||||
}
|
||||
|
||||
// disable DC/DC = solar charging
|
||||
adcval.dcdc_current = 0;
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
daq_update();
|
||||
|
||||
for (int i = 0; i < 60*60*night_hours; i++) {
|
||||
hv_terminal.energy_balance();
|
||||
lv_terminal.energy_balance();
|
||||
load.energy_balance();
|
||||
}
|
||||
}
|
||||
|
||||
void charging_energy_calculation_valid()
|
||||
{
|
||||
energy_calculation_init();
|
||||
// charging only during sun hours
|
||||
TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.bus->voltage * (dcdc_current_sun - load_current)), round(lv_terminal.pos_energy_Wh));
|
||||
}
|
||||
|
||||
void discharging_energy_calculation_valid()
|
||||
{
|
||||
energy_calculation_init();
|
||||
// discharging (sum of current) only during dis hours
|
||||
TEST_ASSERT_EQUAL_FLOAT(round(night_hours * lv_terminal.bus->voltage * adcval.load_current), round(lv_terminal.neg_energy_Wh));
|
||||
}
|
||||
|
||||
void solar_input_energy_calculation_valid()
|
||||
{
|
||||
energy_calculation_init();
|
||||
TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.bus->voltage * dcdc_current_sun), round(hv_terminal.neg_energy_Wh));
|
||||
}
|
||||
|
||||
void load_output_energy_calculation_valid()
|
||||
{
|
||||
energy_calculation_init();
|
||||
TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.bus->voltage * adcval.load_current), round(load.pos_energy_Wh));
|
||||
}
|
||||
|
||||
int power_port_tests()
|
||||
{
|
||||
energy_calculation_init();
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
// energy calculation
|
||||
RUN_TEST(charging_energy_calculation_valid);
|
||||
RUN_TEST(discharging_energy_calculation_valid);
|
||||
RUN_TEST(solar_input_energy_calculation_valid);
|
||||
RUN_TEST(load_output_energy_calculation_valid);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user