Skip to content

Commit

Permalink
Add force disable charging method
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan-Kouba authored and eberseth committed Oct 1, 2022
1 parent 7bc9258 commit 82b4109
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
43 changes: 29 additions & 14 deletions src/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ Tracker::Tracker() :
_chargeStatus(TrackerChargeState::CHARGE_INIT),
_lowBatteryEvent(0),
_evalChargingTick(0),
_batteryChargeEnabled(true)
_batterySafeToCharge(true),
_forceDisableCharging(false)
{
_cloudConfig =
{
Expand Down Expand Up @@ -350,7 +351,7 @@ void Tracker::initBatteryMonitor() {
}

// Keep a handy variable to check on battery charge enablement
_batteryChargeEnabled = !powerConfig.isFeatureSet(SystemPowerFeature::DISABLE_CHARGING);
_batterySafeToCharge = !powerConfig.isFeatureSet(SystemPowerFeature::DISABLE_CHARGING);

// To initialize the fuel gauge so that it provides semi-accurate readings we
// want to ensure that the charging circuit is off when providing the
Expand All @@ -371,7 +372,8 @@ void Tracker::initBatteryMonitor() {
// getSoC(), or reading will not have updated yet.
delay(200);

if (_batteryChargeEnabled) {
_forceDisableCharging = _deviceConfig.disableCharging();
if (_batterySafeToCharge && !_forceDisableCharging) {
pmic.enableCharging();
}
pmic.disableWatchdog();
Expand Down Expand Up @@ -729,9 +731,7 @@ int Tracker::reset() {
return SYSTEM_ERROR_NONE;
}

int Tracker::enableCharging() {
_batteryChargeEnabled = true;

int Tracker::pmicEnableCharging() {
auto powerConfig = System.getPowerConfiguration();
if (powerConfig.isFeatureSet(SystemPowerFeature::DISABLE_CHARGING)) {
powerConfig.clearFeature(SystemPowerFeature::DISABLE_CHARGING);
Expand All @@ -741,9 +741,7 @@ int Tracker::enableCharging() {
return SYSTEM_ERROR_NONE;
}

int Tracker::disableCharging() {
_batteryChargeEnabled = false;

int Tracker::pmicDisableCharging() {
auto powerConfig = System.getPowerConfiguration();
if (!powerConfig.isFeatureSet(SystemPowerFeature::DISABLE_CHARGING)) {
powerConfig.feature(SystemPowerFeature::DISABLE_CHARGING);
Expand All @@ -753,6 +751,19 @@ int Tracker::disableCharging() {
return SYSTEM_ERROR_NONE;
}

void Tracker::forceDisableCharging(bool value) {
_forceDisableCharging = value;

if (_forceDisableCharging) {
pmicDisableCharging();
}
else {
if (_batterySafeToCharge) {
pmicEnableCharging();
}
}
}

int Tracker::setChargeCurrent(uint16_t current) {
int ret = SYSTEM_ERROR_NONE;
auto powerConfig = System.getPowerConfiguration();
Expand Down Expand Up @@ -793,11 +804,15 @@ int Tracker::chargeCallback(TemperatureChargeEvent event) {
}

// Check if anything needs to be changed for charging
if (!shouldCharge && _batteryChargeEnabled) {
disableCharging();
}
else if (shouldCharge && !_batteryChargeEnabled) {
enableCharging();
if (!shouldCharge && _batterySafeToCharge) {
_batterySafeToCharge = false;
pmicDisableCharging();
}
else if (shouldCharge && !_batterySafeToCharge) {
_batterySafeToCharge = true;
if (!_forceDisableCharging) {
pmicEnableCharging();
}
}

return SYSTEM_ERROR_NONE;
Expand Down
48 changes: 37 additions & 11 deletions src/tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
#define TRACKER_CONFIG_ENABLE_IO_SLEEP (false)
#endif

#ifndef TRACKER_CONFIG_DISABLE_CHARGING
// Enable or disable LiPo charging. Also available to the user application. See TrackerConfiguration below
#define TRACKER_CONFIG_DISABLE_CHARGING (false)
#endif

#ifndef TRACKER_CONFIG_ENABLE_FAST_LOCK
// Enable or disable faster GNSS lock based on HDOP, see TrackerConfiguration below
#define TRACKER_CONFIG_ENABLE_FAST_LOCK (false)
Expand Down Expand Up @@ -89,6 +94,7 @@ class TrackerConfiguration {
TrackerConfiguration() :
_enableIo(TRACKER_CONFIG_ENABLE_IO),
_enableIoSleep(TRACKER_CONFIG_ENABLE_IO_SLEEP),
_disableCharging(TRACKER_CONFIG_DISABLE_CHARGING),
_gnssRetryCount(TRACKER_CONFIG_GNSS_RETRY_COUNT),
_locationServiceConfig(LocationServiceConfiguration()) {
}
Expand Down Expand Up @@ -141,6 +147,27 @@ class TrackerConfiguration {
return _enableIoSleep;
}

/**
* @brief Disable or enable LiPo battery charging. Can be overridden in the user app with forceDisableCharging()
*
* @param disable Disable charging of the LiPo
* @return TrackerConfiguration&
*/
TrackerConfiguration& disableCharging(bool disable) {
_disableCharging = disable;
return *this;
}

/**
* @brief Indicate if charging is disabled or not
*
* @return true Charging is disabled
* @return false Charging is enabled
*/
bool disableCharging() const {
return _disableCharging;
}

/**
* @brief Enable or disable faster GNSS lock based on HDOP. May result in poor horizontal accuracy.
*
Expand Down Expand Up @@ -208,6 +235,7 @@ class TrackerConfiguration {
}
this->_enableIo = rhs._enableIo;
this->_enableIoSleep = rhs._enableIoSleep;
this->_disableCharging = rhs._disableCharging;
this->_gnssRetryCount = rhs._gnssRetryCount;
this->_locationServiceConfig = rhs._locationServiceConfig;

Expand All @@ -216,6 +244,7 @@ class TrackerConfiguration {
private:
bool _enableIo;
bool _enableIoSleep;
bool _disableCharging;
unsigned int _gnssRetryCount;
LocationServiceConfiguration _locationServiceConfig;
};
Expand Down Expand Up @@ -321,18 +350,12 @@ class Tracker {
}

/**
* @brief Enable battery charging
*
* @retval SYSTEM_ERROR_NONE
*/
int enableCharging();

/**
* @brief Disable battery charging
* @brief Manually force off battery charging
*
* @retval SYSTEM_ERROR_NONE
* @param value true charging will be disabled (not re-enabled by Tracker Edge)
* @param value false charging will revert to original logic (handled by Tracker Edge)
*/
int disableCharging();
void forceDisableCharging(bool value);

/**
* @brief Force battery charge current
Expand Down Expand Up @@ -426,7 +449,8 @@ class Tracker {
TrackerChargeState _chargeStatus;
unsigned int _lowBatteryEvent;
unsigned int _evalChargingTick;
bool _batteryChargeEnabled;
bool _batterySafeToCharge;
bool _forceDisableCharging;
bool _deviceMonitoring {false};

// Startup and initialization related
Expand Down Expand Up @@ -457,6 +481,8 @@ class Tracker {
void initBatteryMonitor();
bool getChargeEnabled();
void evaluateBatteryCharge();
int pmicEnableCharging();
int pmicDisableCharging();

/**
* @brief Handle OTA events from System.on() interface
Expand Down

0 comments on commit 82b4109

Please sign in to comment.