aboutsummaryrefslogtreecommitdiff
path: root/spaghetti-monster
diff options
context:
space:
mode:
authorSelene ToyKeeper2020-07-31 20:53:12 -0600
committerSelene ToyKeeper2020-07-31 20:53:12 -0600
commite9e65c8e8ac8b94745ab1c1ed3567bf9a07e3ff4 (patch)
tree7990da359a597e9dcfabf5af6b693fb28459c424 /spaghetti-monster
parentfixed bug: sunset timer didn't adjust MCU clock speed at low levels, (diff)
downloadanduril-e9e65c8e8ac8b94745ab1c1ed3567bf9a07e3ff4.tar.gz
anduril-e9e65c8e8ac8b94745ab1c1ed3567bf9a07e3ff4.tar.bz2
anduril-e9e65c8e8ac8b94745ab1c1ed3567bf9a07e3ff4.zip
added a voltage calibration / correction function, 7C from battcheck to configure
Diffstat (limited to 'spaghetti-monster')
-rw-r--r--spaghetti-monster/anduril/battcheck-mode.c29
-rw-r--r--spaghetti-monster/anduril/battcheck-mode.h5
-rw-r--r--spaghetti-monster/anduril/config-default.h4
-rw-r--r--spaghetti-monster/anduril/load-save-config-fsm.h3
-rw-r--r--spaghetti-monster/anduril/load-save-config.c6
-rw-r--r--spaghetti-monster/fsm-adc.c14
-rw-r--r--spaghetti-monster/fsm-adc.h5
7 files changed, 64 insertions, 2 deletions
diff --git a/spaghetti-monster/anduril/battcheck-mode.c b/spaghetti-monster/anduril/battcheck-mode.c
index c468332..3b4a144 100644
--- a/spaghetti-monster/anduril/battcheck-mode.c
+++ b/spaghetti-monster/anduril/battcheck-mode.c
@@ -35,6 +35,7 @@ uint8_t battcheck_state(Event event, uint16_t arg) {
set_state(off_state, 0);
return MISCHIEF_MANAGED;
}
+
#if defined(USE_BEACON_MODE)
// 2 clicks: beacon mode
else if (event == EV_2clicks) {
@@ -48,9 +49,37 @@ uint8_t battcheck_state(Event event, uint16_t arg) {
return MISCHIEF_MANAGED;
}
#endif
+
+ #ifdef USE_VOLTAGE_CORRECTION
+ // 7 clicks: voltage config mode
+ else if (event == EV_7clicks) {
+ push_state(voltage_config_state, 0);
+ return MISCHIEF_MANAGED;
+ }
+ #endif
+
return EVENT_NOT_HANDLED;
}
+#ifdef USE_VOLTAGE_CORRECTION
+// the user can adjust the battery measurements... on a scale of 1 to 13
+// 1 = subtract 0.30V
+// 2 = subtract 0.25V
+// ...
+// 7 = no effect (add 0V)
+// 8 = add 0.05V
+// ...
+// 13 = add 0.30V
+void voltage_config_save() {
+ uint8_t foo = config_state_values[0];
+ if (foo) voltage_correction = config_state_values[0];
+}
+
+uint8_t voltage_config_state(Event event, uint16_t arg) {
+ return config_state_base(event, arg, 1, voltage_config_save);
+}
+#endif // #ifdef USE_VOLTAGE_CORRECTION
+
#endif
diff --git a/spaghetti-monster/anduril/battcheck-mode.h b/spaghetti-monster/anduril/battcheck-mode.h
index f57fa74..8ddf31d 100644
--- a/spaghetti-monster/anduril/battcheck-mode.h
+++ b/spaghetti-monster/anduril/battcheck-mode.h
@@ -22,5 +22,10 @@
uint8_t battcheck_state(Event event, uint16_t arg);
+#ifdef USE_VOLTAGE_CORRECTION
+void voltage_config_save();
+uint8_t voltage_config_state(Event event, uint16_t arg);
+#endif
+
#endif
diff --git a/spaghetti-monster/anduril/config-default.h b/spaghetti-monster/anduril/config-default.h
index c68e869..0957a29 100644
--- a/spaghetti-monster/anduril/config-default.h
+++ b/spaghetti-monster/anduril/config-default.h
@@ -86,6 +86,10 @@
#define BATTCHECK_VpT
//#define BATTCHECK_8bars // FIXME: breaks build
//#define BATTCHECK_4bars // FIXME: breaks build
+// allow the user to calibrate the voltage readings?
+// (adjust in 0.05V increments from -0.30V to +0.30V)
+// (1 = -0.30V, 2 = -0.25V, ... 7 = 0V, ... 13 = +0.30V)
+#define USE_VOLTAGE_CORRECTION
// enable beacon mode
#define USE_BEACON_MODE
diff --git a/spaghetti-monster/anduril/load-save-config-fsm.h b/spaghetti-monster/anduril/load-save-config-fsm.h
index d9c5f4c..c7c4c8d 100644
--- a/spaghetti-monster/anduril/load-save-config-fsm.h
+++ b/spaghetti-monster/anduril/load-save-config-fsm.h
@@ -60,6 +60,9 @@ typedef enum {
therm_ceil_e,
therm_cal_offset_e,
#endif
+ #ifdef USE_VOLTAGE_CORRECTION
+ voltage_correction_e,
+ #endif
#ifdef USE_INDICATOR_LED
indicator_led_mode_e,
#endif
diff --git a/spaghetti-monster/anduril/load-save-config.c b/spaghetti-monster/anduril/load-save-config.c
index 7268dc9..8360ee4 100644
--- a/spaghetti-monster/anduril/load-save-config.c
+++ b/spaghetti-monster/anduril/load-save-config.c
@@ -60,6 +60,9 @@ void load_config() {
therm_ceil = eeprom[therm_ceil_e];
therm_cal_offset = eeprom[therm_cal_offset_e];
#endif
+ #ifdef USE_VOLTAGE_CORRECTION
+ voltage_correction = eeprom[voltage_correction_e];
+ #endif
#ifdef USE_INDICATOR_LED
indicator_led_mode = eeprom[indicator_led_mode_e];
#endif
@@ -114,6 +117,9 @@ void save_config() {
eeprom[therm_ceil_e] = therm_ceil;
eeprom[therm_cal_offset_e] = therm_cal_offset;
#endif
+ #ifdef USE_VOLTAGE_CORRECTION
+ eeprom[voltage_correction_e] = voltage_correction;
+ #endif
#ifdef USE_INDICATOR_LED
eeprom[indicator_led_mode_e] = indicator_led_mode;
#endif
diff --git a/spaghetti-monster/fsm-adc.c b/spaghetti-monster/fsm-adc.c
index 45a4297..96c4af3 100644
--- a/spaghetti-monster/fsm-adc.c
+++ b/spaghetti-monster/fsm-adc.c
@@ -114,7 +114,12 @@ static inline uint8_t calc_voltage_divider(uint16_t value) {
// use 9.7 fixed-point to get sufficient precision
uint16_t adc_per_volt = ((ADC_44<<5) - (ADC_22<<5)) / (44-22);
// shift incoming value into a matching position
- uint8_t result = ((value>>1) / adc_per_volt) + VOLTAGE_FUDGE_FACTOR;
+ uint8_t result = ((value>>1) / adc_per_volt)
+ + VOLTAGE_FUDGE_FACTOR
+ #ifdef USE_VOLTAGE_CORRECTION
+ + voltage_correction - 7
+ #endif
+ ;
return result;
}
#endif
@@ -262,7 +267,12 @@ static inline void ADC_voltage_handler() {
// calculate actual voltage: volts * 10
// ADC = 1.1 * 1024 / volts
// volts = 1.1 * 1024 / ADC
- voltage = ((uint16_t)(2*1.1*1024*10)/(measurement>>6) + VOLTAGE_FUDGE_FACTOR) >> 1;
+ voltage = ((uint16_t)(2*1.1*1024*10)/(measurement>>6)
+ + VOLTAGE_FUDGE_FACTOR
+ #ifdef USE_VOLTAGE_CORRECTION
+ + voltage_correction - 7
+ #endif
+ ) >> 1;
#endif
// if low, callback EV_voltage_low / EV_voltage_critical
diff --git a/spaghetti-monster/fsm-adc.h b/spaghetti-monster/fsm-adc.h
index cc33264..56583bb 100644
--- a/spaghetti-monster/fsm-adc.h
+++ b/spaghetti-monster/fsm-adc.h
@@ -52,6 +52,11 @@ void adc_deferred(); // do the actual ADC-related calculations
static inline void ADC_voltage_handler();
uint8_t voltage = 0;
+#ifdef USE_VOLTAGE_CORRECTION
+// same 0.05V units as fudge factor,
+// but 7 is neutral, and the expected range is from 1 to 13
+uint8_t voltage_correction = 7;
+#endif
#ifdef USE_LVP
void low_voltage();
#endif
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.