Voltage measuring

Post Reply
User avatar
Benik3
Posts: 25
Joined: Mon Aug 26, 2013 1:06 pm
Contact:

Voltage measuring

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

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: Voltage measuring

Post by Hamburger »

Advantage?
With current implementation, for correct vbatscale you get the actual voltage in vbat.

User avatar
Benik3
Posts: 25
Joined: Mon Aug 26, 2013 1:06 pm
Contact:

Re: Voltage measuring

Post by Benik3 »

As I said, advantage is, that you dont need to find the vbatscale...

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

Re: Voltage measuring

Post 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

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

Re: Voltage measuring

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

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: Voltage measuring

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

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: Voltage measuring

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

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

Re: Voltage measuring

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

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

Re: Voltage measuring

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

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

Re: Voltage measuring

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

crazyal
Posts: 84
Joined: Tue Sep 04, 2012 11:25 pm

Re: Voltage measuring

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

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

Re: Voltage measuring

Post by Mis »

Ekhem, the conf.vbatscale variable is 8-bit type...

crazyal
Posts: 84
Joined: Tue Sep 04, 2012 11:25 pm

Re: Voltage measuring

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

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

Re: Voltage measuring

Post by copterrichie »

Just Dump it. Good Lord.

User avatar
Benik3
Posts: 25
Joined: Mon Aug 26, 2013 1:06 pm
Contact:

Re: Voltage measuring

Post 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 :)

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: Voltage measuring

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

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

Re: Voltage measuring

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

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

Re: Voltage measuring

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

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: Voltage measuring

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

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

Re: Voltage measuring

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

crazyal
Posts: 84
Joined: Tue Sep 04, 2012 11:25 pm

Re: Voltage measuring

Post by crazyal »

vbatpatch.zip
(303 Bytes) Downloaded 89 times
;)

User avatar
Benik3
Posts: 25
Joined: Mon Aug 26, 2013 1:06 pm
Contact:

Re: Voltage measuring

Post by Benik3 »

Thanks, but in the patch i see that you only changed the vbatscale to 16-bit, what about the rest?

crazyal
Posts: 84
Joined: Tue Sep 04, 2012 11:25 pm

Re: Voltage measuring

Post 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;
Attachments
vbat.zip
(624 Bytes) Downloaded 85 times

User avatar
Benik3
Posts: 25
Joined: Mon Aug 26, 2013 1:06 pm
Contact:

Re: Voltage measuring

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

User avatar
Benik3
Posts: 25
Joined: Mon Aug 26, 2013 1:06 pm
Contact:

Re: Voltage measuring

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

crazyal
Posts: 84
Joined: Tue Sep 04, 2012 11:25 pm

Re: Voltage measuring

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

User avatar
Benik3
Posts: 25
Joined: Mon Aug 26, 2013 1:06 pm
Contact:

Re: Voltage measuring

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

crazyal
Posts: 84
Joined: Tue Sep 04, 2012 11:25 pm

Re: Voltage measuring

Post 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

User avatar
Benik3
Posts: 25
Joined: Mon Aug 26, 2013 1:06 pm
Contact:

Re: Voltage measuring

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

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

Re: Voltage measuring

Post by timecop »

Hey, why hasn't this been merged into main trunk yet???
This gotta go in before 2.3 release

Post Reply