diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-08-28 13:26:53 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-08-28 13:26:53 +1000 |
| commit | 3a0c42f9240eedfbc6a1e94ad3a59c52664fb5b5 (patch) | |
| tree | 0505db40de6fceaf5829548ef86f4cb53b739bcb /src/drivers/battery.cpp | |
| parent | a1327763ab70dbf4996e032dd227de368f78f4ad (diff) | |
| download | tangara-fw-3a0c42f9240eedfbc6a1e94ad3a59c52664fb5b5.tar.gz | |
Move battery measurement to its own class
Diffstat (limited to 'src/drivers/battery.cpp')
| -rw-r--r-- | src/drivers/battery.cpp | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/src/drivers/battery.cpp b/src/drivers/battery.cpp deleted file mode 100644 index 1755fd64..00000000 --- a/src/drivers/battery.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2023 jacqueline <me@jacqueline.id.au> - * - * SPDX-License-Identifier: GPL-3.0-only - */ - -#include "battery.hpp" -#include <cstdint> - -#include "esp_adc/adc_cali.h" -#include "esp_adc/adc_cali_scheme.h" -#include "esp_adc/adc_oneshot.h" -#include "hal/adc_types.h" - -namespace drivers { - -/* - * Battery voltage, in millivolts, at which the battery charger IC will stop - * charging. - */ -static const uint32_t kFullChargeMilliVolts = 4200; - -/* - * Battery voltage, in millivolts, at which *we* will consider the battery to - * be completely discharged. This is intentionally higher than the charger IC - * cut-off and the protection on the battery itself; we want to make sure we - * finish up and have everything unmounted and snoozing before the BMS cuts us - * off. - */ -static const uint32_t kEmptyChargeMilliVolts = 3200; // BMS limit is 3100. - -static const adc_bitwidth_t kAdcBitWidth = ADC_BITWIDTH_12; -static const adc_unit_t kAdcUnit = ADC_UNIT_1; -// Max battery voltage should be a little over 2V due to our divider, so we need -// the max attenuation to properly handle the full range. -static const adc_atten_t kAdcAttenuation = ADC_ATTEN_DB_11; -// Corresponds to SENSOR_VP. -static const adc_channel_t kAdcChannel = ADC_CHANNEL_0; - -Battery::Battery() { - adc_oneshot_unit_init_cfg_t unit_config = { - .unit_id = kAdcUnit, - }; - ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_config, &adc_handle_)); - - adc_oneshot_chan_cfg_t channel_config = { - .atten = kAdcAttenuation, - .bitwidth = kAdcBitWidth, - }; - ESP_ERROR_CHECK( - adc_oneshot_config_channel(adc_handle_, kAdcChannel, &channel_config)); - - // calibrate - // TODO: compile-time assert our scheme is available - adc_cali_line_fitting_config_t calibration_config = { - .unit_id = kAdcUnit, - .atten = kAdcAttenuation, - .bitwidth = kAdcBitWidth, - }; - ESP_ERROR_CHECK(adc_cali_create_scheme_line_fitting( - &calibration_config, &adc_calibration_handle_)); - - UpdatePercent(); -} - -Battery::~Battery() { - adc_cali_delete_scheme_line_fitting(adc_calibration_handle_); - ESP_ERROR_CHECK(adc_oneshot_del_unit(adc_handle_)); -} - -auto Battery::Millivolts() -> uint32_t { - // GPIO 34 - int raw = 0; - ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, kAdcChannel, &raw)); - - int voltage = 0; - ESP_ERROR_CHECK( - adc_cali_raw_to_voltage(adc_calibration_handle_, raw, &voltage)); - - // Voltage divider halves the battery voltage to get it into the ADC's range. - return voltage * 2; -} - -auto Battery::UpdatePercent() -> bool { - auto old_percent = percent_; - // FIXME: So what we *should* do here is measure the actual real-life - // time from full battery -> empty battery, store it in NVS, then rely on - // that. If someone could please do this, it would be lovely. Thanks! - uint32_t mV = std::max(Millivolts(), kEmptyChargeMilliVolts); - percent_ = static_cast<uint_fast8_t>(std::min<double>( - std::max<double>(0.0, mV - kEmptyChargeMilliVolts) / - (kFullChargeMilliVolts - kEmptyChargeMilliVolts) * 100.0, - 100.0)); - return old_percent != percent_; -} - -} // namespace drivers |
