Page 1 of 1

Voltage measuring

Posted: Sun Sep 01, 2013 10:55 pm
by Benik3
Hi,
I'm new in MultiWii but I have a question, how the measuring of Voltage is made? Exactly I mean how the VBATSCALE work...
Because I found it not very comfortable...

Why not to use the two values of used resistors as voltage divider?
Calculation will be made this way:
UR2=AD*5/1023
Vbat=UR2*(R1+R2)/R2

Where UR2 is voltage on the resistor from multiwii to ground, AD is value from A/D converter in decimal, Vbat is real voltage of battery (shown e.g. in GUI) and R1+R2 are the two used resistors.
I think that if you measure the resistors, you can get immediately very accurate measuring...

Of course there could be voltage*10 as now and maybe also *10 or *100 the number from resistor voltage divider for not using fraction.
Or simply use float...
Error in measuring will be probably made by wrong values of resistor, so there could be variable Vbat=UR2*((R1+R2)/R2+correction)

What do you think? Thanks :geek:

Re: Voltage measuring

Posted: Sun Sep 01, 2013 11:33 pm
by Hamburger
Advantage?
With current implementation, for correct vbatscale you get the actual voltage in vbat.

Re: Voltage measuring

Posted: Tue Sep 03, 2013 3:28 pm
by Benik3
As I said, advantage is, that you dont need to find the vbatscale...

Re: Voltage measuring

Posted: Tue Sep 03, 2013 3:35 pm
by timecop
Hamburger wrote:Advantage?
With current implementation, for correct vbatscale you get the actual voltage in vbat.


rofl.
yeah, I totally agree with OP, this bip bip bip "vbatscale" needs to bip bip bip DIE.
resistor divider values are known
adcref voltage is known
lsb/mV for adc is known.
just bip bip bip calculate it PROPERLY instead of using bip bip bip magic numbers / dividers / whatever the bip bip bip.
And you actually get CORRECT VALUES from that formula, too.
Hamburger, do you ever bip bip bip stop for a second and think about doing something correctly in mwc, or just perpetuate bip bip bip fail + shortcuts for everything?

For an example of this properly done, take a look at baseflight:
https://code.google.com/p/afrodevices/s ... sors.c#121

Re: Voltage measuring

Posted: Tue Sep 03, 2013 3:41 pm
by copterrichie
On this one, I have to say, I dumped the vbatscale some time ago. When upgrading to a new version, I just edited that out and place my own code there. vbatscale does need to go.

Re: Voltage measuring

Posted: Tue Sep 03, 2013 5:58 pm
by Hamburger
Benik3 wrote:As I said, advantage is, that you dont need to find the vbatscale...

So instead of vbatscale you must find what you called correction?

Re: Voltage measuring

Posted: Tue Sep 03, 2013 6:06 pm
by Hamburger
timecop wrote:Hamburger, do you ever fucking stop for a second and think about doing something correctly in mwc, or just perpetuate fucking fail + shortcuts for everything?

I did not implement the vbat measuring in the first place. As this is an open souce project you could well supply a patch with the one and only implementation yourself.
What other 'fucking fail+shortcuts for everything' did you find and can provide the one and only true solution?

Re: Voltage measuring

Posted: Tue Sep 03, 2013 9:07 pm
by Mis
resistor divider values are known

Yes, but with 5% tolerance.

adcref voltage is known

Theoretically yes, but fuc..g atmels have big initial vref tolerance. Really.

5% tolerance give about 0.5V readout error. Not important ? Calibration is stupid ? OK, start killing your LiPo's now.

Re: Voltage measuring

Posted: Tue Sep 03, 2013 9:37 pm
by copterrichie
There are 1% resistors used in precision measurements. P.S. What is the problem with using an ohm meter and measuring the actually resistance?

Re: Voltage measuring

Posted: Wed Sep 04, 2013 5:21 am
by timecop
Mis wrote:
resistor divider values are known

Yes, but with 5% tolerance.

People still use 5% resistors?

adcref voltage is known

Theoretically yes, but fuc..g atmels have big initial vref tolerance. Really.

People still use bip bip bip atmels?

5% tolerance give about 0.5V readout error. Not important ? Calibration is stupid ? OK, start killing your LiPo's now.


OK, so what's the bip bip bip problem? How does "vbatscale" solve the problem of "calibration" except instead of dealing with numbers you can CORRECT (i.e. known resistor divider values and a known forumula) instead you have some RANDOM bip bip bip NUMBER THAT DOESNT MEAN ANYTHING.

Re: Voltage measuring

Posted: Wed Sep 04, 2013 11:04 am
by crazyal
I finally found that vbat "bullshit" in the code... what the fuck do you have to smoke to implement something like that :)

Code: Select all

analog.vbat = (v<<4) / conf.vbatscale;  // result is Vbatt in 0.1V steps

seriously I don't see how this even remotely makes sense..
Replace it with something like this:

Code: Select all

analog.vbat = (v * conf.vbatscale) / 1023;  // result is Vbatt in 0.1V steps

whereas conf.vbatscale is the maximum voltage you can measure with your voltage divider.
It is calculated like this: conf.vbatscale = (R1+R2)/R1 * vref * 10
for example if you have a divider with R1=1kOhm, R2=10kOhm and vref is 5V then conf.vbatscale is 550
this value can also be easily tuned to match tolerances, just set it higher or lower to match the voltage you measure with a voltmeter.

Re: Voltage measuring

Posted: Wed Sep 04, 2013 8:13 pm
by Mis
Ekhem, the conf.vbatscale variable is 8-bit type...

Re: Voltage measuring

Posted: Wed Sep 04, 2013 8:54 pm
by crazyal
Mis wrote:Ekhem, the conf.vbatscale variable is 8-bit type...

then use a 16-bit type for the sake of readability. Tha'll only cost a few cycles..
but I forgot you need to obfuscate the code so can still run on a 8-bit cpu..
btw all those << or >> instructions could be replaced by *2^n or /2^n. The compiler still generates the same asm code and the c-code is more readable.

Re: Voltage measuring

Posted: Wed Sep 04, 2013 9:20 pm
by copterrichie
Just Dump it. Good Lord.

Re: Voltage measuring

Posted: Thu Sep 05, 2013 10:35 am
by Benik3
Crazyal: Yes! That's exactly what I wrote in OP :)
Did you already test it in multiwii?

BTW in most cases the resistors are named R1 for the one from Vcc to measure pin and R2 for the one from pin to GND...
I don't know why also this is switched in the comment in multiwii code...

Hamburger: You can simply measure the resistance with multimeter, I suppose that probably everyone, who build multicopter this way, have some....

Thanks to all for interest :)

Re: Voltage measuring

Posted: Thu Sep 05, 2013 1:37 pm
by Hamburger
When I see some working code for MWii for current arduino based hardware which works with accuracy without any empirical correction factored in then it will stir my interest.

Re: Voltage measuring

Posted: Thu Sep 05, 2013 2:14 pm
by copterrichie
And this in my opinion is why there is very little progress on the project.

This is very SIMPLE basic stuff, just imagine when it comes to more complicated issues. Good Lord.

Re: Voltage measuring

Posted: Thu Sep 05, 2013 2:27 pm
by timecop
Hamburger wrote:When I see some working code for MWii for current arduino based hardware which works with accuracy without any empirical correction factored in then it will stir my interest.


What the actual fuck?
How is
a) val * 16 / scale different from
b) val * scale / 1023
?????????
Are you saying the a) is somehow more ACCURATE than b)?
It's EXACTLY same thing, except in case of a) you're dealing with a RANDOM number to "tune" whereas in b) you've got a number that makes perfect SENSE ( result of a known formula of resistor divider ).

Re: Voltage measuring

Posted: Thu Sep 05, 2013 2:41 pm
by Hamburger
You are free to ignore what mis said on precision and accuracy.
I do not understand how all that fucking from your comments relates here and prefer to ignore it from hereon.
Have fun. Out.

Re: Voltage measuring

Posted: Thu Sep 05, 2013 3:16 pm
by copterrichie
Just an FYI, I think measuring Battery voltage using the Balance plug is far better. The battery provides some filtering itself, adding a Cap between R1 and R2 improves this filtering but keep in mind, there is about a .5 voltage drop when the motors are running.

Re: Voltage measuring

Posted: Thu Sep 05, 2013 3:25 pm
by crazyal
vbatpatch.zip
(303 Bytes) Downloaded 217 times
;)

Re: Voltage measuring

Posted: Thu Sep 05, 2013 3:31 pm
by Benik3
Thanks, but in the patch i see that you only changed the vbatscale to 16-bit, what about the rest?

Re: Voltage measuring

Posted: Thu Sep 05, 2013 3:54 pm
by crazyal
that was just making a point :P
here is the real one, untested tough.

Code: Select all

Index: MultiWii.cpp
===================================================================
--- MultiWii.cpp   (revision 1563)
+++ MultiWii.cpp   (working copy)
@@ -410,23 +410,15 @@
   case 1:
   {
       static uint8_t ind = 0;
-      static uint16_t vvec[VBAT_SMOOTH], vsum;
+      static uint16_t 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
+        analog.vbat = (v * conf.vbatscale) / 1023; // 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
+        vsum -= vsum / VBAT_SMOOTH;
+        analog.vbat = (vsum * conf.vbatscale) / (1023 * VBAT_SMOOTH);
       #endif
       break;
   }
Index: types.h
===================================================================
--- types.h   (revision 1563)
+++ types.h   (working copy)
@@ -172,7 +172,7 @@
     int16_t failsafe_throttle;
   #endif
   #ifdef VBAT
-    uint8_t vbatscale;
+    uint16_t vbatscale;
     uint8_t vbatlevel_warn1;
     uint8_t vbatlevel_warn2;
     uint8_t vbatlevel_crit;

Re: Voltage measuring

Posted: Sat Sep 07, 2013 9:50 pm
by Benik3
Thanks, but I don't see the main change to conf.vbatscale = (R1+R2)/R1 * vref * 10 and the definitions...

Sorry, but I'm not expert in programming, otherwise I made it already myself :D

BTW what is for vsum and VBAT_SMOOTH?

Re: Voltage measuring

Posted: Sun Sep 08, 2013 4:17 pm
by Benik3
Another reason to rework it.
Today I wanted to test the development multiwii code with new PID, i used VBATSCALE from v2.2 and what you think? It showed 25,5V!!!

Re: Voltage measuring

Posted: Sun Sep 08, 2013 7:47 pm
by crazyal
Benik3 wrote:Thanks, but I don't see the main change to conf.vbatscale = (R1+R2)/R1 * vref * 10 and the definitions...

Sorry, but I'm not expert in programming, otherwise I made it already myself :D

BTW what is for vsum and VBAT_SMOOTH?


+ in the patch file means line added
- means line removed..
so conf.vbatscale is (R1+R2)/R1 * vref * 10
the smoothing part needs less memory that way and still works the same.

Re: Voltage measuring

Posted: Sun Sep 08, 2013 7:57 pm
by Benik3
Oh, now I get how you mean it :)
I thought, that you will implement the formula (R1+R2)/R1 * vref * 10 into the code, so user will change only R2= and R1=...
Never mind, I will try it :)

The SMOOTH is for eliminating quick changes in voltage?

Re: Voltage measuring

Posted: Sun Sep 08, 2013 8:05 pm
by crazyal
imho having R1,R2 and vref in the code isn't that easy to implement efficiently and the so called developers of multiwii probably won't like it.
yep SMOOTH does that, as the name suggests

Re: Voltage measuring

Posted: Sun Sep 08, 2013 8:17 pm
by Benik3
OK, but I really don't know, why is vbat_smooth in config.h under section ESC calibration...

Also for me it looks, that you have switched logic of the smooth, I mean there is, that if VBAT_SMOOTH==1, then it read vbat directly from the A/D, if it's anything else, it use smoothing...
I expected that if I set VBAT_SMOOTH to 1, ti will use smoothing, otherwise (e.g. while it's 0) it read it directly...

Re: Voltage measuring

Posted: Wed Oct 02, 2013 3:58 am
by timecop
Hey, why hasn't this been merged into main trunk yet???
This gotta go in before 2.3 release