Implemented load management

This commit is contained in:
Thomas Kolb 2016-09-03 16:57:33 +02:00
parent 8ed36f6770
commit f909f26ea7

View file

@ -24,6 +24,7 @@
#define TIM_CH_BOOTSTRAP TIM_OC2
#define MAX_SLEEP_TIME 3600
#define MAX_SLEEP_TIME_LOW_VOLTAGE 300
enum OperState {
Bootstrap,
@ -67,7 +68,6 @@ static void init_gpio(void)
// GPIO for load activation
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO15);
gpio_set(GPIOA, GPIO15);
}
static void init_clock(void)
@ -493,6 +493,16 @@ static void mpp_run(
}
}
static void load_on(void)
{
gpio_set(GPIOA, GPIO15);
}
static void load_off(void)
{
gpio_clear(GPIOA, GPIO15);
}
int main(void)
{
uint32_t cpuload = 0;
@ -501,6 +511,7 @@ int main(void)
char msg[128];
char number[FXP_STR_MAXLEN];
uint8_t sentSomething = 0;
uint8_t loadInitialized = 0;
int32_t pwm = 0;
@ -539,6 +550,15 @@ int main(void)
// input voltage must exceed output voltage by this value to leave idle mode
fxp_t WAKEUP_OFFSET_VOLTAGE = fxp_from_float(1.0f);
// switch off load below LOAD_OFF_THRESHOLD to protect the battery; when the
// battery recovers above LOAD_ON_THRESHOLD the load is switched on again.
//
// If the battery voltage is below LOAD_LOW_VOLTAGE_THRESHOLD, the battery
// voltage is monitored more closely during idle mode.
fxp_t LOAD_ON_THRESHOLD = fxp_from_float(13.000f);
fxp_t LOAD_OFF_THRESHOLD = fxp_from_float(11.200f);
fxp_t LOAD_LOW_VOLTAGE_THRESHOLD = fxp_from_float(11.600f);
// Calculated values
//fxp_t VIN_SCALE = fxp_from_float(3.3f * (100 + 12.4f) / 12.4f / 4095.0f);
//fxp_t VOUT_SCALE = fxp_from_float(3.3f * (100 + 12.0f) / 12.0f / 4095.0f);
@ -639,6 +659,22 @@ int main(void)
power_state.power_avg = fxp_mult(power_state.vout_avg, power_state.current_avg);
// load management
if(timebase_ms >= 100) {
if(!loadInitialized && power_state.vout_avg > LOAD_OFF_THRESHOLD) {
load_on();
loadInitialized = 1;
}
if(loadInitialized) {
if(power_state.vout_avg < LOAD_OFF_THRESHOLD) {
load_off();
} else if(power_state.vout_avg > LOAD_ON_THRESHOLD) {
load_on();
} /* else current state is kept */
}
}
// Main FSM
if(timebase_ms >= 1000) {
switch(operState) {
@ -832,8 +868,14 @@ int main(void)
time_in_state = 9900; // run the voltage test again
sleep_time *= 2;
if(sleep_time > MAX_SLEEP_TIME) {
sleep_time = MAX_SLEEP_TIME;
if(power_state.vout_avg > LOAD_LOW_VOLTAGE_THRESHOLD) {
if(sleep_time > MAX_SLEEP_TIME) {
sleep_time = MAX_SLEEP_TIME;
}
} else {
if(sleep_time > MAX_SLEEP_TIME_LOW_VOLTAGE) {
sleep_time = MAX_SLEEP_TIME_LOW_VOLTAGE;
}
}
force_display_update_time = timebase_ms + 10;