Page 1 of 1
Autodetection number of Cells for Battery alarms
Posted: Tue Aug 14, 2012 9:10 pm
by Mis
Someone want this feature ?
I have routine for autodetect numbers of lipo cells. The alarms values (VBATLEVEL1_3S and so) should be changed to Volt/cell, multiwii counts number of cells (using maximum detected voltage after power-up), and use VBATLEVEL*cells calculation for low battery alarms.
BTW for more than 3S battery we can different divider because 51k/33k can meadure only up to 12.5V battery voltage. With 100k/33k we can monitor up to 6S packs. For 100k/33k divider, the VBATSCALE should be about 86.
The code size cost is less than 100 bytes and not should be a problem.
Re: Autodetection number of Cells for Battery alarms
Posted: Tue Aug 14, 2012 10:10 pm
by LuFa
of course , I want it for example
can you post your change here ?
Re: Autodetection number of Cells for Battery alarms
Posted: Tue Aug 14, 2012 11:34 pm
by Mis
No problem.
In annexCode function replace vbat code to this piece of code:
Code: Select all
#if defined(VBAT)
static uint8_t vbatTimer = 0;
static uint8_t ind = 0;
uint16_t vbatRaw = 0;
static uint16_t vbatRawArray[8];
if (! (++vbatTimer % VBATFREQ)) {
vbatRawArray[(ind++)%8] = analogRead(V_BATPIN);
for (uint8_t i=0;i<8;i++) vbatRaw += 2*vbatRawArray[i];
vbat = vbatRaw / VBATSCALE; // result is Vbatt in 0.1V steps
uint8_t c = vbat/43 + 1;
if(c > cells) cells = c;
}
if (((vbat>VBATLEVEL1*cells)
#if defined(POWERMETER)
&& ( (pMeter[PMOTOR_SUM] < pAlarm) || (pAlarm == 0) )
#endif
) || (vbat<16)) // ToLuSe
{ // VBAT ok AND powermeter ok, buzzer off
buzzerFreq = 0;
#if defined(POWERMETER)
} else if (pMeter[PMOTOR_SUM] > pAlarm) { // sound alarm for powermeter
buzzerFreq = 4;
#endif
} else if (vbat>VBATLEVEL2*cells) buzzerFreq = 1;
else if (vbat>VBATLEVEL3*cells) buzzerFreq = 2;
else buzzerFreq = 4;
#endif
And add in MultiIii.ino one global variable "static uint8_t cells = 1;"
Then change VBATT definitions in config.h:
Code: Select all
// alarm levels in volt/cell because now we have automatic cells count detection
#define VBATLEVEL1 35 // 3.5V/cell
#define VBATLEVEL2 34 // 3.4V/cell
#define VBATLEVEL3 33 // 3.3V/cell
Re: Autodetection number of Cells for Battery alarms
Posted: Wed Aug 15, 2012 5:14 am
by timecop
Why would you recalculate cell count each loop time?
I kind of liked the way it was done in baseflight months ago:
http://code.google.com/p/afrodevices/so ... sors.c#124
Re: Autodetection number of Cells for Battery alarms
Posted: Wed Aug 15, 2012 11:23 am
by Mis
dongs wrote:Why would you recalculate cell count each loop time?
Not in each loop, but %VBATFREQ - about 18ms.
The calculation is not complicated, and not waste much cpu time, and this way consume less flash memory.
Re: Autodetection number of Cells for Battery alarms
Posted: Wed Aug 15, 2012 11:28 am
by timecop
Huh, but what is the logical reason for ever recalculating battery cell count once in flight?
What if you discharge enough that your logic begins to think a 3S battery is now 2S.. (for example if you crashed and listening for battery low alarm)... Oh, right... Less flash? lol.
Re: Autodetection number of Cells for Battery alarms
Posted: Wed Aug 15, 2012 11:50 am
by Mis
dongs wrote:What if you discharge enough that your logic begins to think a 3S battery is now 2S..
No way ! Look at this piece of code:
if(c > cells) cells = c; The cells count can't decrease during flight.
Re: Autodetection number of Cells for Battery alarms
Posted: Wed Aug 15, 2012 12:01 pm
by timecop
OK sure. More problems?
No hardware divide on AVR.
You're dividing in each loop. And then multiplying.
Comparing against a precalculated value is still faster.
Shrug. Just saying.