Autodetection number of Cells for Battery alarms

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
Mis
Posts: 203
Joined: Fri Apr 01, 2011 12:23 am

Autodetection number of Cells for Battery alarms

Post 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.

LuFa
Posts: 160
Joined: Fri Jan 27, 2012 7:56 pm

Re: Autodetection number of Cells for Battery alarms

Post by LuFa »

of course , I want it for example :)
can you post your change here ?

Mis
Posts: 203
Joined: Fri Apr 01, 2011 12:23 am

Re: Autodetection number of Cells for Battery alarms

Post 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

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: Autodetection number of Cells for Battery alarms

Post 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

Mis
Posts: 203
Joined: Fri Apr 01, 2011 12:23 am

Re: Autodetection number of Cells for Battery alarms

Post 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.

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: Autodetection number of Cells for Battery alarms

Post 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.

Mis
Posts: 203
Joined: Fri Apr 01, 2011 12:23 am

Re: Autodetection number of Cells for Battery alarms

Post 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.

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: Autodetection number of Cells for Battery alarms

Post 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.

Post Reply