diff --git a/Inc/config.h b/Inc/config.h index fad7b88..6ddd017 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -8,6 +8,10 @@ #define DC_CUR_LIMIT 15 // Motor DC current limit in amps. it does not disable motors, it is a soft current limit. +// Battery voltage calibration: connect power source. use multimeter to measure real voltage and write it to BAT_CALIB_REAL_VOLTAGE. watch UART on one of the sensor board cables. write value nr 4 to BAT_CALIB_ADC. make and flash firmware. you can verify voltage on UART debug value 5 (devide it by 100.0 to get calibrated voltage). +#define BAT_CALIB_REAL_VOLTAGE 42.0 // input voltage measured by multimeter +#define BAT_CALIB_ADC 1667 // adc-value measured by mainboard (value nr 4 on UART debug output) + #define BAT_LOW_LVL1 36.0 // gently beeps at this voltage level. ~3.6V/cell #define BAT_LOW_LVL2 33.0 // your battery is almost empty. Charge now! ~3.3V/cell #define BAT_LOW_DEAD 31.0 // undervoltage lockout. ~3.1V/cell diff --git a/Inc/defines.h b/Inc/defines.h index 93c17c6..7bda19a 100644 --- a/Inc/defines.h +++ b/Inc/defines.h @@ -123,8 +123,7 @@ #define DELAY_TIM_FREQUENCY_US 1000000 -#define MOTOR_AMP_CONV_DC_AMP 0.02 -#define ADC_BATTERY_VOLT 0.02647435897435897435897435897436 +#define MOTOR_AMP_CONV_DC_AMP 0.02 // A per bit (12) on ADC. #define MILLI_R (R * 1000) #define MILLI_PSI (PSI * 1000) diff --git a/Src/bldc.c b/Src/bldc.c index fb5d1af..0560cdf 100644 --- a/Src/bldc.c +++ b/Src/bldc.c @@ -161,8 +161,8 @@ void DMA1_Channel1_IRQHandler() { return; } - if (buzzerTimer % 100 == 0) { - batteryVoltage = batteryVoltage * 0.999 + ((float)adc_buffer.batt1 * ADC_BATTERY_VOLT) * 0.001; + if (buzzerTimer % 100 == 0) { // because you get float rounding errors if it would run every time + batteryVoltage = batteryVoltage * 0.999 + ((float)adc_buffer.batt1 * ((float)BAT_CALIB_REAL_VOLTAGE / (float)BAT_CALIB_ADC)) * 0.001; } diff --git a/Src/main.c b/Src/main.c index c36b837..d775fb0 100644 --- a/Src/main.c +++ b/Src/main.c @@ -217,8 +217,8 @@ int main(void) { #endif setScopeChannel(2, (int)speedR); setScopeChannel(3, (int)speedL); - // setScopeChannel(4, (int)); - // setScopeChannel(5, (int)); + setScopeChannel(4, (int)adc_buffer.batt1); // for battery voltage calibration + setScopeChannel(5, (int)(batteryVoltage * 100.0f)); // for verifying battery voltage calibration // setScopeChannel(6, (int)); // setScopeChannel(7, (int));