[mod] Increasing accuracy of VBAT via VBATSCALE

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
User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

[mod] Increasing accuracy of VBAT via VBATSCALE

Post by haydent »

So VBATSCALE as set in config.h is used to adjust VBAT (your battery voltage) to be accurate to your multimeters reading.

This is the formula:

Line 420 of Multiwii Tab 2.3

Code: Select all

#if VBAT_SMOOTH == 16
          analog.vbat = vsum / conf.vbatscale; // result is Vbatt in 0.1V steps


Line 581 of Multiwii Tab 2.2

Code: Select all

vbat = (vbatRaw*2) / conf.vbatscale; // result is Vbatt in 0.1V steps 


but with my sensor (rctimer 50 V / 90 Amps), changes of VBATSCALE even by just 1 integer (eg 20 to 21) result in a change of VBAT by several 10ths of a volt, generally equaling about 0.5v.


so there is not fine enough adjustment.

to fix this i tweaked the formula in 2.3

Code: Select all

#if VBAT_SMOOTH == 16
          analog.vbat = vsum * 6 / conf.vbatscale; // result is Vbatt in 0.1V steps


to fix this i tweaked the formula in 2.2

Code: Select all

  vbat = (vbatRaw*6) / conf.vbatscale; // result is Vbatt in 0.1V steps //haydent *12 gives finer adjustment by vbatscale


in general the larger the multiplier the finer accuracy you get with VBATSCALE, the only 'limitation' to size of multiplier is VBATSCALE is defined as a uint8 so unless you want to change that the largest you can have VBATSCALE is 127, luckily i could get away with 123 to give my voltage.

but if you need finer accuracy i imagine you could define it as a int16 the change the formula to multiply say by 200 and and use a vbatscale of say ~2099

:D
Last edited by haydent on Sat Dec 07, 2013 12:41 am, edited 5 times in total.

felixrising
Posts: 244
Joined: Sat Mar 23, 2013 12:34 am
Location: Australia

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by felixrising »

I'm using r1411 or thereabouts (looks the same as current _shared r1538). Instead of using a *16 multiplier I used a multiplier of *64 to get reasonable numbers with the same rctimer 50V/90A sensor.

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by haydent »

(i used *12) sure i said that the higher the number the finer adjustment you get but in the standard 2.2 i run, vbatscale is a int8 so 12 is as high as you can use without changing its type which i believe has been done in the release you refer to

felixrising
Posts: 244
Joined: Sat Mar 23, 2013 12:34 am
Location: Australia

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by felixrising »

It's not 2.2, but 2.2 does use uint16_t for vbatRaw(v) and vbatRawArray(vsum), same as current _shared. However vbat is a uint8_t
Current code:

Code: Select all

  #if defined(VBAT)
  case 1:
  {
      static uint8_t ind = 0;
      static uint16_t vvec[VBAT_SMOOTH], vsum;
     uint16_t v = analogRead(V_BATPIN);
      //debug[1] = v;
      #if VBAT_SMOOTH == 1
        analog.vbat = (v<<4) / conf.vbatscale; // result is Vbatt in 0.1V steps
      #else
        vsum += v;
        vsum -= vvec[ind];
        vvec[ind++] = v;
        ind %= VBAT_SMOOTH;
        #if VBAT_SMOOTH == 16
          analog.vbat = vsum / conf.vbatscale; // result is Vbatt in 0.1V steps
        #elif VBAT_SMOOTH < 16
          analog.vbat = (vsum * (16/VBAT_SMOOTH)) / conf.vbatscale; // result is Vbatt in 0.1V steps
        #else
          analog.vbat = ((vsum /VBAT_SMOOTH) * 16) / conf.vbatscale; // result is Vbatt in 0.1V steps
        #endif
      #endif
      break;
  }
  #endif // VBAT


I just changed all <<4 bitshifts to <<6 and multiply and divide by 16 to 64.. with a vbatscale of 82, my voltage comes in correct on crius aio pro v1

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by haydent »

what does << mean

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by copterrichie »

Personally, I fine using a Zener and a Cap to be the best way to measure battery voltage. There is no need to average smoothing, the Cap does this.

viewtopic.php?f=6&t=2661

jef79m
Posts: 23
Joined: Sun May 06, 2012 11:38 am

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by jef79m »

haydent wrote:what does << mean


No one here really knows. But it seems to be used as some sort of division operator.

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

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by timecop »

jef79m wrote:
haydent wrote:what does << mean


No one here really knows. But it seems to be used as some sort of division operator.


It seems to be some kind of idiotic optimization to make code less readable, while any modern compiler would substitute same operation in it's place if necessary.

Nicksdesign
Posts: 63
Joined: Sat Jan 26, 2013 6:49 am

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by Nicksdesign »

haydent wrote:what does << mean


It means 'bit shift left' . Bit-shifting is a quick way to multiply/divide by powers of two. Experienced C coders seem to love bit-shifting. Unfortunately, they also like to use it when a binary number would be much more clear. For example, 1<<3 takes 1, or 00000001 for a 8 bit number, and turns it into 00001000. It would be so much more clear if they would use B00001000! (The leading 'B' tells the compiler that the number is binary.) The code where multiple bit-shifts are used just to construct a single binary number is even much harder to understand!

Nick

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by haydent »

Personally, I fine using a Zener and a Cap to be the best way to measure battery voltage. There is no need to average smoothing, the Cap does this.


yes ive seen your thread before, but seems i already had the current meter with voltage sense, i went with that, mind you i imagine you could combine them

It means 'bit shift left'


thanks, im still not sure, but atleast i have a term to google, its alway hard googling the meaning of certain characters as often they are ignored or taken out of context

Nicksdesign
Posts: 63
Joined: Sat Jan 26, 2013 6:49 am

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by Nicksdesign »

To understand the operators, like '<<', try going to the Arduino site and looking in the language reference. There's a lot of useful information there.

http://arduino.cc/en/Reference/HomePage ... e.Extended

Nick

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

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by timecop »

Nicksdesign wrote:
haydent wrote:what does << mean


It means 'bit shift left' . Bit-shifting is a quick way to multiply/divide by powers of two. Experienced C coders seem to love bit-shifting. Unfortunately, they also like to use it when a binary number would be much more clear. For example, 1<<3 takes 1, or 00000001 for a 8 bit number, and turns it into 00001000. It would be so much more clear if they would use B00001000! (The leading 'B' tells the compiler that the number is binary.) The code where multiple bit-shifts are used just to construct a single binary number is even much harder to understand!

Nick


absofuckinglutely wrong.
setting bits by shifting is a VERY common and USEFUL practice. Anyone who's an actual programmer (as opposed to tarduino copypaster) knows what it means and understands what it does. It doesn't make compiled code larger because compiler (proper one at least, no idea about gcc) will parse these static constants and turn them into whatever numbers.

The "B0010010" whatever notation you're talking about is some tarduino shit, where they have a bits.h include file or similar that does shit like #define B00000000 (0) B00000001 (1) etc. This is NOT standard, neither is the 0b101101 type notation (again, gcc extension, other compilers don't do it).

Nicksdesign
Posts: 63
Joined: Sat Jan 26, 2013 6:49 am

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by Nicksdesign »

Common and useful generally means: that's the way it's always been done because that's the way it's always been done because that's the way it's always been done, etc.. The thing that makes it better is because doing things the way they have always been done avoids being ridiculed/punished for daring to suggest that there might be a better way.

Nick

Sebbi
Posts: 478
Joined: Sun Jul 08, 2012 1:08 am
Location: Germany
Contact:

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by Sebbi »

Don't be rude to each other ... 0b10101010 is a valid way to show which bits are set in a byte ... I prefer hex though 0xAA ... there, much clearer and shorter when you have 16 bit or 32 bit variables/registers (e.g. 0xB3AA instead of 0b1011001110101010) ;-)

Bit-shifts are for manipulation of variables ... var | 1<<3 is much better in communicating "set bit 3 to 1" as var | 0b00000100 would be AND it can be used in case of var being a 16 bit or 32 bit variable AND the 3 could also be a variable resulting in the statement var | 1<<var2 which is just not possible when using 0b00000100 or the B00000100 Arduino defines.

Nicksdesign
Posts: 63
Joined: Sat Jan 26, 2013 6:49 am

Re: [A] Increasing accuracy of VBAT via VBATSCALE

Post by Nicksdesign »

I agree that there are good uses for bit shifting. However, the choice of constructing a binary number via a sequence of bit shifted 1's and 'and's/'or's instead of a binary constant is a question of personal style and not an indication of professional qualifications.

Nick

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: [mod] Increasing accuracy of VBAT via VBATSCALE

Post by haydent »

updated OP for 2.3

Post Reply