From 9b42842323c582aff9860d1476aa7103a1a6277c Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Thu, 5 Feb 2026 19:36:56 -0500 Subject: [PATCH 01/10] feat: update PowerManagement voltage calculation logic --- include/PowerManagement.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/include/PowerManagement.h b/include/PowerManagement.h index 7b5a578..4e24dd9 100644 --- a/include/PowerManagement.h +++ b/include/PowerManagement.h @@ -11,8 +11,8 @@ */ class BatteryVoltage { public: - BatteryVoltage(uint8_t adcPin, float conversionFactor) - : pin(adcPin), factor(conversionFactor) {} + BatteryVoltage(uint8_t adcPin, float factor, int numAdcBits) + : pin(adcPin), factor(factor), numAdcBits(numAdcBits) {analogReadResolution(numAdcBits);} /** * @brief Sample the ADC and convert to battery voltage. @@ -22,14 +22,14 @@ class BatteryVoltage { float readVoltage() { // Set the pin for reading pinMode(pin, INPUT); - uint32_t rawValue = 0; - rawValue = analogRead(pin); // Should give a value between 0 and 1023, not sure how to convert this to the true voltage. + uint32_t rawValue = analogRead(pin); // Should give a value between 0 and 4095 + float vRef = 3.3f; // Conversion notes -> respect to Vref you would do Vref * analogRead()/2^12. // After that you can use the voltage divider equation to get the battery voltage level - // Vref is 134.33? - // float voltage = pow((rawValue/2),12) * factor; - Serial.println(rawValue); - return rawValue; + float vPin = (static_cast(rawValue) / (static_cast(1 << numAdcBits) - 1)) * vRef; + float vBat = vPin * factor; + + return vBat; } /** @@ -45,6 +45,7 @@ class BatteryVoltage { private: uint8_t pin; float factor; + int numAdcBits; }; #endif // PowerManagement_H \ No newline at end of file From 69106921121b177fef8276fccde8a1d9dd17920c Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 12:12:24 -0500 Subject: [PATCH 02/10] fix: update comments in PowerManagement.h --- include/PowerManagement.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/include/PowerManagement.h b/include/PowerManagement.h index 4e24dd9..8fe31d0 100644 --- a/include/PowerManagement.h +++ b/include/PowerManagement.h @@ -11,21 +11,25 @@ */ class BatteryVoltage { public: + /** + * @param adcPin The ADC pin connected to the battery voltage divider. + * @param factor The scaling factor to convert the pin voltage to battery voltage. + * @param numAdcBits The resolution of the ADC (e.g., 12 for 12-bit ADC). + */ BatteryVoltage(uint8_t adcPin, float factor, int numAdcBits) : pin(adcPin), factor(factor), numAdcBits(numAdcBits) {analogReadResolution(numAdcBits);} /** * @brief Sample the ADC and convert to battery voltage. * @return Converted voltage reading. - * @note When to use: periodic health checks or brownout warnings. */ float readVoltage() { // Set the pin for reading pinMode(pin, INPUT); - uint32_t rawValue = analogRead(pin); // Should give a value between 0 and 4095 - float vRef = 3.3f; - // Conversion notes -> respect to Vref you would do Vref * analogRead()/2^12. - // After that you can use the voltage divider equation to get the battery voltage level + uint32_t rawValue = analogRead(pin); // Should give a value between 0 and 2^numAdcBits - 1 + float vRef = 3.3f; // reference voltage for the ADC on MARTHA 1.4 + + // Convert raw ADC value to voltage at the pin, then apply the factor to get battery voltage float vPin = (static_cast(rawValue) / (static_cast(1 << numAdcBits) - 1)) * vRef; float vBat = vPin * factor; From a6a300d6d91560cc6f82eebe1b9c52468332522a Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 12:58:15 -0500 Subject: [PATCH 03/10] feat: add more comments to PowerManagement --- include/PowerManagement.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/include/PowerManagement.h b/include/PowerManagement.h index 8fe31d0..c28650d 100644 --- a/include/PowerManagement.h +++ b/include/PowerManagement.h @@ -6,8 +6,18 @@ /** * @brief Simple ADC-based battery voltage helper. - * @note When to use: quick health checks of the main battery using an analog - * pin and known divider scaling. + * This class provides a way to read the battery voltage using an ADC pin and a scaling factor. + * It also includes a simple "isAlive" check to determine if the voltage is above a certain threshold. + * + * The correct pin can be found in the hardware scematics under "ADC_VOLTAGE" + * and the scaling factor can be calculated based on the voltage divider used in the hardware design. + * For MARTHA 1.4, the voltage divider is 200k and 1k5, so the factor is (200k + 1k5) / 1k5 = 134.33333. + * + * The voltage at the pin is calculated as: + * vPin = (rawValue / (2^numAdcBits - 1)) * vRef + * where rawValue is the ADC reading, numAdcBits is the resolution of the ADC, and vRef is the reference voltage for the ADC (3.3V for MARTHA 1.4). + * The battery voltage is then calculated as: + * vBat = vPin * factor */ class BatteryVoltage { public: From 404a89b3dbce3238f8606159ad6bee42f58c7758 Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 13:32:55 -0500 Subject: [PATCH 04/10] remove isAlive function from powermanagement --- include/PowerManagement.h | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/include/PowerManagement.h b/include/PowerManagement.h index c28650d..2969f1e 100644 --- a/include/PowerManagement.h +++ b/include/PowerManagement.h @@ -7,7 +7,6 @@ /** * @brief Simple ADC-based battery voltage helper. * This class provides a way to read the battery voltage using an ADC pin and a scaling factor. - * It also includes a simple "isAlive" check to determine if the voltage is above a certain threshold. * * The correct pin can be found in the hardware scematics under "ADC_VOLTAGE" * and the scaling factor can be calculated based on the voltage divider used in the hardware design. @@ -45,16 +44,6 @@ class BatteryVoltage { return vBat; } - - /** - * @brief Check whether voltage exceeds a minimal threshold. - * @return true if voltage is above the survival threshold. - * @note When to use: lightweight go/no-go checks before more detailed - * power analysis. - */ - bool isAlive(){ - return readVoltage() > .3; // Not sure what a good cutoff is - } private: uint8_t pin; From d161c7ee31ea9e57cf789823917c8460b205b986 Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 13:42:01 -0500 Subject: [PATCH 05/10] feat: add islow() to powermanagement --- include/PowerManagement.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/include/PowerManagement.h b/include/PowerManagement.h index 2969f1e..81e1cc3 100644 --- a/include/PowerManagement.h +++ b/include/PowerManagement.h @@ -24,9 +24,10 @@ class BatteryVoltage { * @param adcPin The ADC pin connected to the battery voltage divider. * @param factor The scaling factor to convert the pin voltage to battery voltage. * @param numAdcBits The resolution of the ADC (e.g., 12 for 12-bit ADC). + * @param voltageThreshold A threshold voltage for low battery checks (not used in current implementation but can be useful for future extensions). */ - BatteryVoltage(uint8_t adcPin, float factor, int numAdcBits) - : pin(adcPin), factor(factor), numAdcBits(numAdcBits) {analogReadResolution(numAdcBits);} + BatteryVoltage(uint8_t adcPin, float factor, int numAdcBits, float voltageThreshold) + : pin(adcPin), factor(factor), numAdcBits(numAdcBits), voltageThreshold(voltageThreshold) {analogReadResolution(numAdcBits);} /** * @brief Sample the ADC and convert to battery voltage. @@ -44,11 +45,21 @@ class BatteryVoltage { return vBat; } + + /** + * @brief Check whether voltage exceeds a minimal threshold. + * @return true if voltage is above the survival threshold. + * @note Could be useful for future extension, like triggering a low power mode + */ + bool isLow(){ + return readVoltage() < voltageThreshold; + } private: uint8_t pin; float factor; int numAdcBits; + float voltageThreshold; }; #endif // PowerManagement_H \ No newline at end of file From 8d01b960068931521925720c46032fb57f6bb7d4 Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 14:05:20 -0500 Subject: [PATCH 06/10] fix: include ArduinoHAL and add necessary functions for powermanagement to ArduinoHAL --- hal/ArduinoHAL.h | 12 ++++++++++++ include/PowerManagement.h | 3 +-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/hal/ArduinoHAL.h b/hal/ArduinoHAL.h index 14137e0..b41372e 100644 --- a/hal/ArduinoHAL.h +++ b/hal/ArduinoHAL.h @@ -37,6 +37,18 @@ inline void digitalWrite(int pin, int value) { //NOLINT // Do nothing } +inline void analogReadResolution(int bits) { + // Do nothing, we will just return a 12-bit value in analogRead +} + +inline uint32_t analogRead(int pin) { + // Return a dummy 12 bit value for the voltage pin, and 0 for other pins. + // This allows us to test the battery voltage reading functionality without needing a real ADC. + if (pin == ADC_VOLTAGE) { + return 4090; + } + return 0; // Default dummy value for other pins +} // millis mock which still gives us the time since the program started in milliseconds static auto program_start = std::chrono::high_resolution_clock::now(); diff --git a/include/PowerManagement.h b/include/PowerManagement.h index 81e1cc3..c79f2cf 100644 --- a/include/PowerManagement.h +++ b/include/PowerManagement.h @@ -1,8 +1,7 @@ #ifndef PowerManagement_H #define PowerManagement_H -#include "Arduino.h" -#include "pins.h" +#include "ArduinoHAL.h" /** * @brief Simple ADC-based battery voltage helper. From 55531900ecebe4bdaf3cf47f7d5c876d3a896d03 Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 14:46:07 -0500 Subject: [PATCH 07/10] add support to ArduinoHAL.h for PowerManagement testing --- hal/ArduinoHAL.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hal/ArduinoHAL.h b/hal/ArduinoHAL.h index b41372e..27d45a9 100644 --- a/hal/ArduinoHAL.h +++ b/hal/ArduinoHAL.h @@ -44,9 +44,12 @@ inline void analogReadResolution(int bits) { inline uint32_t analogRead(int pin) { // Return a dummy 12 bit value for the voltage pin, and 0 for other pins. // This allows us to test the battery voltage reading functionality without needing a real ADC. - if (pin == ADC_VOLTAGE) { + if (pin == 192) { return 4090; } + if (pin == 193) { // fake pin for testing the low voltage case + return 0; + } return 0; // Default dummy value for other pins } From 761e4644bb5d66e68a55ac4664f6ac57243090b5 Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 14:55:58 -0500 Subject: [PATCH 08/10] remove magic numbers from analogRead in ArduinoHAL --- hal/ArduinoHAL.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hal/ArduinoHAL.h b/hal/ArduinoHAL.h index 27d45a9..7cf052f 100644 --- a/hal/ArduinoHAL.h +++ b/hal/ArduinoHAL.h @@ -28,6 +28,8 @@ using String = std::string; #define INPUT 0 #define HIGH 1 #define LOW 0 +#define ADC_VOLTAGE 192 +#define ADC_VOLTAGE_LOW 193 inline void pinMode(int pin, int mode) { //NOLINT // Do nothing @@ -44,10 +46,10 @@ inline void analogReadResolution(int bits) { inline uint32_t analogRead(int pin) { // Return a dummy 12 bit value for the voltage pin, and 0 for other pins. // This allows us to test the battery voltage reading functionality without needing a real ADC. - if (pin == 192) { - return 4090; + if (pin == ADC_VOLTAGE) { + return static_cast((1 << 12) - 1); // Return full-scale value for a 12-bit ADC } - if (pin == 193) { // fake pin for testing the low voltage case + if (pin == ADC_VOLTAGE_LOW) { // fake pin for testing the low voltage case return 0; } return 0; // Default dummy value for other pins From 5ba85724cb08a04add06c4985e5872c0e703e50a Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 15:00:15 -0500 Subject: [PATCH 09/10] remove redundent pin in analogRead in ArduinoHAL --- hal/ArduinoHAL.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/hal/ArduinoHAL.h b/hal/ArduinoHAL.h index 7cf052f..68e21d8 100644 --- a/hal/ArduinoHAL.h +++ b/hal/ArduinoHAL.h @@ -49,9 +49,6 @@ inline uint32_t analogRead(int pin) { if (pin == ADC_VOLTAGE) { return static_cast((1 << 12) - 1); // Return full-scale value for a 12-bit ADC } - if (pin == ADC_VOLTAGE_LOW) { // fake pin for testing the low voltage case - return 0; - } return 0; // Default dummy value for other pins } From fd7011fa3da5d7c975f8df8709c93135a573cb42 Mon Sep 17 00:00:00 2001 From: Jackson Shepherd Date: Fri, 6 Feb 2026 15:15:01 -0500 Subject: [PATCH 10/10] remove reduntent ADC_VOLTAGE_LOW var from Ardunio.HAL --- hal/ArduinoHAL.h | 1 - 1 file changed, 1 deletion(-) diff --git a/hal/ArduinoHAL.h b/hal/ArduinoHAL.h index 68e21d8..a4da5fa 100644 --- a/hal/ArduinoHAL.h +++ b/hal/ArduinoHAL.h @@ -29,7 +29,6 @@ using String = std::string; #define HIGH 1 #define LOW 0 #define ADC_VOLTAGE 192 -#define ADC_VOLTAGE_LOW 193 inline void pinMode(int pin, int mode) { //NOLINT // Do nothing