PDA

View Full Version : Math Help Please



markcadcam
- 20th May 2008, 14:15
Hi all,

I’m using an 18F452 Pic for a Dynamometer project. I have an amplifier feeding the a/d input on the Pic 0-5 volts. A count of 70 equals a foot-pound of torque. The problem I’m having is the torque is going to be less than 1 foot-pound and the RPM’s will be low most of the time. This means that the output and the formula will need to have decimal points in it.

Formula for Horse Power is: Horse Power = RPM x Torque (In Foot Pounds) / 5252

The example below is for 1500 RPM’s at a ½ pound of torque.
Example: (1500 rpm’s) x (35 x .0143) / 5252 = .1428 H.P.

What would be the best way to approach the Math for this application?

Thanks,
Mark

skimask
- 20th May 2008, 14:24
Example: (1500 rpm’s) x (35 x .0143) / 5252 = .1428 H.P.
What would be the best way to approach the Math for this application?
Use LONG's...to keep the precision...and you'll want it to keep it simple-ish.
Instead of .0143, which PBP won't handle, use 143.
1500 x 35 x 143 / 5252 = 1428
When displaying, just put a decimal point in front of this bunch.
Ok, RPMs are 8000 and torque is 300...
8000 x 35 x 300 / 5252 = 15993
Divide by 10000, display that number, put a decimal point, get the remainder (modulus), display that number

markcadcam
- 20th May 2008, 15:22
Thanks! I think that will work.

Mark

markcadcam
- 20th May 2008, 23:25
When I use the example below I get a results of 1019 instead of 15993.

Ok, RPMs are 8000 and torque is 300...
8000 x 35 x 300 / 5252 = 15993
Divide by 10000, display that number, put a decimal point, get the remainder (modulus), display that number

My code is below:

Include "modedefs.bas"
define OSC 20
define LOADER_USED 1 ' bootloader

RPM VAR WORD
STRAIN VAR WORD
TORQUE VAR WORD
A VAR WORD
C VAR WORD


A = 5252


RPM = 8000
STRAIN = 35
TORQUE = 300


C = RPM * STRAIN * TORQUE
C = DIV32 A

SEROUT PORTC.5,N2400,[12]
SEROUT PORTC.5,N2400,[".",#C," HP ",10,13,14]

STOP

Mark

BrianT
- 21st May 2008, 04:24
I think your problem is a result of the triple multiplication. None of the PBP examples I have seen allow this.

PBP does allow
x = a*b
y = div32 c

Your constant, 5252, has 6 factors, namely 1, 2, 4, 13, 26, and 52.

Break the equation in two with divisors say 52 and 101 and see how you go. Beware the integer division truncation problem where 201/101 = 1.

The new LONG variable type (PBP 2.50 and up) MIGHT solve your problem but only after some gymnastics as the LONG type appears to allow big multiplicands but retains integer division so you probably need to scale all your numbers UP by factors of 100 or so.

HTH
BrianT

markcadcam
- 21st May 2008, 11:38
Thanks Brian.

Mark

skimask
- 21st May 2008, 15:33
That's why I said to use LONG variable types in post #2.


Include "modedefs.bas"
define OSC 20
define LOADER_USED 1 ' bootloader
RPM VAR WORD : STRAIN VAR WORD : TORQUE VAR WORD : A VAR WORD : C VAR LONG
A = 5252 : RPM = 8000
STRAIN = 35 : TORQUE = 300 : C = A * RPM * STRAIN * TORQUE
SEROUT PORTC.5,N2400,[12,".",#C," HP ",10,13,14]
END
Even that might overflow a 31bit signed variable.
A fits in 13 bits
RPM might take up to 14 bits
Strain (in your example) another 6 bits
Torque (in your example) another 9 bits
Multiply those all together and you could get a 42 bit answer.
What do you suppose your MAXimum values would be for each variable?

markcadcam
- 21st May 2008, 15:52
RPM Max = 12,000
STRAIN Max = 210
TORQUE Max = 143

I have version 2.44 of PicBasic Pro. Will 'LONG' work with this version? Should I upgrade?

Mark

skimask
- 21st May 2008, 16:21
RPM Max = 12,000
STRAIN Max = 210
TORQUE Max = 143

I have version 2.44 of PicBasic Pro. Will 'LONG' work with this version? Should I upgrade?

Mark

Upgrade yes... Check the website for more info...

12000 x 5252 x 210 x 143 = 1,892,610,720,000
Way too big for long's...
Like BrianT said, you'll have to figure out how to do some mathematic gymnastics to keep your numbers low enough to fit in 31/32 bits...
Or you could do it the old fashioned way, like back in 2nd grade...one number at a time, carry the result if needed...and so on and so on...

I guess it's up to you to figure out how much accuracy you really need/want...

markcadcam
- 21st May 2008, 16:40
Divide by 5252, not multiply it.

RPM = 12000
STRAIN = 210
TORQUE = 140
VALUE = 5252

A = STRAIN * TORQUE
B = RPM
C= A * B
C= C DIV32 VALUE

skimask
- 21st May 2008, 16:54
Divide by 5252, not multiply it.
I knew that :D
Ok,
12000 x 210 x 143 = 360,360,000 which will fit into 31/32 bits.
but that result x 100 won't...so...
x100 /5252 is the same as x25 /1313
rpm x strain x torque / 1313 x 25 = result (with decimal point shifted over)
assuming you've used LONG variables.

markcadcam
- 21st May 2008, 17:01
Where is the x100 coming from?

skimask
- 21st May 2008, 17:35
Where is the x100 coming from?
Another...'OH YEAH' moment just happened.
You've already got the 'torque' value multiplied up for the decimal point.
Disregard the stuff about x100/5252.

12000 x 210 x 143 = 360,360,000 which will fit into 31/32 bits even when getting maximum values.
Divide that by 5252 and you get 68613, which is 100x too much, right?
So divide that by 100, gives you 686, take the remainder (modulus) and you get 13.
Should be easy enough to display that (until you get to the leading zeros...PBP CAN handle that too if you read far enough into the LCDOUT and/or SEROUT section of the manual).

markcadcam
- 21st May 2008, 19:00
I think I got it now. Thanks for the help.

Mark