Battery Vehicle Society

Welcome to the BVS forums at www.batteryvehiclesociety.org.uk
* FAQ    * Search
 * Register * Login 

It is currently Sat May 25, 2013 12:23 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Computer Maths & Programming Question
PostPosted: Thu Oct 28, 2010 12:05 am 
Offline
User avatar

Joined: Sat May 26, 2007 4:50 pm
Posts: 1378
Location: North Yorkshire England
Binary maths, shifting etc etc is not my strong point so perhaps someone can give me a few pointers here for some program code.

In the below simplified sample code two hex bytes of incomming battery current data are
evaluated to give a display of current. The below code to interpret and evaluate the data works very well. The range of values the below formula encounters is HEXBYTE1 is $00-$17 and HEXBYTE2 it is $00-$7F. The application is a current sensor data stream which measures from -100 to +50A


Code:
Amps =  ((HEXBYTE1 * 128) + HEXBYTE2) - 2048   'Calculate Battery Current
   if Amps.15 = 1 then      'If Bit15 of Amps (WORD) = 1 then
   AmpSign = 45                   'Set AmpSign to 45 (-)
   else
   AmpSign = 43                    'Set AmpSign to 43 (+)
   endif
   Amps = ABS Amps
   Amps = Amps * 2500
   Amps = DIV32 512


This gives current to two decimal places when AMPS is divided by 100 and a polarity sign to indicate current flow direction. All that is fine.

Now I need to reduce the Amps by say 8-16% (That's fine) and then recreate the HEXBYTES by some reverse calculation or application of the above code. So they contain the new value and when evaluated by the above routine would give the new current and sign etc. Does that make sense? I hope somone can give me me some ideas. I have asked on the pbpro forum but it's more of a question about how to tackle it from the maths POV, not the actual mechanics of the code. Thanks

_________________
Regards Peter

Two MK1 Honda Insight's. One running 20ah A123 Lithium pack. One 8ah BetterBattery Nimh pack.
One HCH1 Civic Hybrid running 60ah A123 Lithium pack.


Top
 Profile  
 
 Post subject: Re: Computer Maths & Programming Question
PostPosted: Thu Oct 28, 2010 2:38 pm 
Offline

Joined: Thu Jan 03, 2008 1:05 pm
Posts: 73
Location: Rochdale
Woops Posted half way through.

Anyway, here we go, reverse of your code.
<pre>
Amps = Amps * 512
Amps DIV32 2500
if AmpSign == 45 then '-
Amps = 0 - Amps
else
'I don't know Pic, do nothing
endif
Amps = Amps + 2048
Byte2 = Amps & &7f 'split of HEXBYTE2
Amps = Amps >> 7 'Div by 128
Byte1 = Amps & 0x1f '
</pre>


Top
 Profile  
 
 Post subject: Re: Computer Maths & Programming Question
PostPosted: Thu Oct 28, 2010 2:51 pm 
Offline

Joined: Thu Jan 03, 2008 1:05 pm
Posts: 73
Location: Rochdale
Well I don't know what happened there. Most of my text was lost and some of the characters came out wrong.

I question the first line of your code, I think that it's wrong. If your ADC has more than 8 bits it usually fills 8 bits rather than ignoring one of the bits. Also the zero point is either at one end of the ADC range, in the middle of the ADC range or at some point that does not relate to word size. You seem to have put the zero point at the mid range of the variable that you are using, which seems odd to me.

Anyway my code reversed your maths then used bitwise operators and shifts to split off the psudo-ADC values assuming that your first line was correct.

Byte2 = Amps & $7f 'split of HEXBYTE2
Amps = Amps >> 7 'Div by 128
Byte1 = Amps & $1f '

The funny character is the ampersand. ie bitwise and (mask off some bits)


Top
 Profile  
 
 Post subject: Re: Computer Maths & Programming Question
PostPosted: Thu Oct 28, 2010 3:08 pm 
Offline
User avatar

Joined: Sat May 26, 2007 4:50 pm
Posts: 1378
Location: North Yorkshire England
Jon

Thanks for that reply. I'll study it.

The incomming data is not directly from an ADC but is from a battery management module and the output is in this weird propietary format. The first line is correct as far as we know and gives the correct current values.

_________________
Regards Peter

Two MK1 Honda Insight's. One running 20ah A123 Lithium pack. One 8ah BetterBattery Nimh pack.
One HCH1 Civic Hybrid running 60ah A123 Lithium pack.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group