PWM switch: Clean up comments and add F suffixes

This commit is contained in:
Martin Jäger
2020-12-13 11:04:56 +01:00
parent 76ee3dc28f
commit 4f4bdc70ae
2 changed files with 56 additions and 32 deletions
+9 -17
View File
@@ -38,16 +38,8 @@ bool PwmSwitch::signal_high()
PwmSwitch::PwmSwitch(DcBus *dc_bus) :
PowerPort(dc_bus)
{
off_timestamp = -10000; // start immediately
// calibration parameters
offset_voltage_start = 2.0; // V charging switched on if Vsolar > Vbat + offset
restart_interval = 60; // s When should we retry to start charging after low solar power cut-off?
// period stored in nanoseconds
pwm_signal_init_registers(1000 * 1000 * 1000 / PWM_PERIOD);
enable = true; // enable charging in general
}
void PwmSwitch::test()
@@ -60,7 +52,7 @@ void PwmSwitch::test()
else if (!pwm_active() && enable == true) {
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
adc_upper_alert_inhibit(ADC_POS(v_low), 50);
pwm_signal_start(0.9);
pwm_signal_start(0.9F);
LOG_INF("PWM test mode start.");
}
}
@@ -68,19 +60,19 @@ void PwmSwitch::test()
void PwmSwitch::control()
{
if (pwm_active()) {
if (current < -0.1) {
if (current < -0.1F) {
power_good_timestamp = uptime(); // reset the time
}
if (neg_current_limit == 0
|| (uptime() - power_good_timestamp > 10) // low power since 10s
|| current > 0.5 // discharging battery into solar panel --> stop
|| bus->voltage < 9.0 // not enough voltage for MOSFET drivers anymore
|| current > 0.5F // discharging battery into solar panel --> stop
|| bus->voltage < 9.0F // not enough voltage for MOSFET drivers anymore
|| enable == false)
{
pwm_signal_stop();
off_timestamp = uptime();
LOG_INF("PWM charger stop, current = %d mA", (int)(current * 1000));
LOG_INF("PWM charger stop, current = %d mA", (int)(current * 1000.0F));
}
else if (bus->voltage > bus->sink_control_voltage() // bus voltage above target
|| current < neg_current_limit // port current limit exceeded
@@ -90,11 +82,11 @@ void PwmSwitch::control()
// the gate driver switch-off time is quite high (fall time around 1ms), so very short
// on or off periods (duty cycle close to 0 and 1) should be avoided
if (pwm_signal_get_duty_cycle() > 0.95) {
if (pwm_signal_get_duty_cycle() > 0.95F) {
// prevent very short off periods
pwm_signal_set_duty_cycle(0.95);
pwm_signal_set_duty_cycle(0.95F);
}
else if (pwm_signal_get_duty_cycle() < 0.05) {
else if (pwm_signal_get_duty_cycle() < 0.05F) {
// prevent very short on periods and switch completely off instead
pwm_signal_stop();
off_timestamp = uptime();
@@ -107,7 +99,7 @@ void PwmSwitch::control()
else {
// increase power (if not yet at 100% duty cycle)
if (pwm_signal_get_duty_cycle() > 0.95) {
if (pwm_signal_get_duty_cycle() > 0.95F) {
// prevent very short off periods and switch completely on instead
pwm_signal_set_duty_cycle(1);
}
+47 -15
View File
@@ -1,13 +1,14 @@
/*
* Copyright (c) 2018 Martin Jäger / Libre Solar
*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright (c) 2018 Martin Jäger / Libre Solar
*/
#ifndef PWM_SWITCH_H
#define PWM_SWITCH_H
/** @file
/**
* @file
*
* @brief PWM charger MOSFET switch control functions
*/
@@ -19,7 +20,8 @@
#include "power_port.h"
/** PWM charger type
/**
* PWM charger type
*
* Contains all data belonging to the PWM switching sub-component.
*/
@@ -28,11 +30,13 @@ class PwmSwitch : public PowerPort
public:
PwmSwitch(DcBus *dc_bus);
/** Main control function for the PWM switching algorithm
/**
* Main control function for the PWM switching algorithm
*/
void control();
/** Test mode for PWM switch
/**
* Test mode for PWM switch
*
* Sets duty cycle to 90% and listens to enable/disable signal
*/
@@ -47,31 +51,59 @@ public:
*/
void stop();
/** Read the general on/off status of PWM switching
/**
* 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
/**
* 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
/**
* Read the currently set duty cycle
*
* @returns Duty cycle between 0.0 and 1.0
*/
float get_duty_cycle();
float ext_voltage; ///< Voltage measurement at terminal (external)
/**
* Voltage measurement at terminal (external, usually solar panel voltage)
*/
float ext_voltage;
bool enable; ///< Can be used to disable the PWM power stage
float offset_voltage_start; ///< Offset voltage of solar panel vs. battery to start charging (V)
int restart_interval; ///< Interval to wait before retrying charging after low solar power cut-off (s)
time_t off_timestamp; ///< Time when charger was switched off last time
time_t power_good_timestamp; ///< Last time the current through the switch was above minimum
/**
* Enable switch, true by default. Can be used to completely disable the PWM power stage.
*/
bool enable = true;
/**
* Offset voltage of solar panel vs. battery to start charging (V)
*/
float offset_voltage_start = 2.0F;
/**
* Interval to wait before retrying charging after low solar power cut-off or overvoltage
* event (s)
*/
int restart_interval = 60;
/**
* Time when charger was switched off last time
*
* Initialized with large negative value to start immediately after reset.
*/
time_t off_timestamp = -10000;
/**
* Last time the current through the switch was above minimum
*/
time_t power_good_timestamp;
};
#endif