PDA

View Full Version : Strange Math Problem (Resolved)



Greg LaPolla
04-02-2009, 05:38 AM
I found this code in another thread for Steinhart Hart




PRI Calc_StHart (A,B,C,Resistance) : Kelvin | B_x_Rlog , C_x_Rlog , Rcubed, Rlog, Addit

G.start ' Start math32

Rlog := G.log(Resistance) ' Take the log of the Resistance
B_x_Rlog := G.FMul(B,Rlog) ' Multiply B to log of R

RCubed := G.FMul(Rlog,Rlog) ' Cube the log of R by R * R * R
RCubed := G.FMul(Rlog,RCubed) ' Finish the Cube

C_x_Rlog := G.FMul(RCubed,C) ' Multiply C to log the cubed log R

Addit := G.Fadd(C_x_Rlog,B_x_Rlog)
Addit := G.Fadd(Addit, A) ' Add all A,B,C together
Kelvin := G.Fdiv(one,addit) ' Kelvin = 1/ all added

G.stop




It always returns a negative number.

I put the above problem in mathcad(see attachment) and it calculates properly. Using the same numbers in the prop yields an answer of
-9.549217


Edit - Forgot to attach picture

Post Edited (Greg LaPolla) : 4/2/2009 2:54:06 AM GMT

Mike Green
04-02-2009, 05:53 AM
How are you providing A, B, C, Resistance? If one or more are not floating point values, that could explain what's happening.

Greg LaPolla
04-02-2009, 05:57 AM
Here is how it is done




CON

Coeff1 = 0.0009461261 ' Steinhart Constant
Coeff2 = 0.00022226704 ' Steinhart Constant
Coeff3 = 0.000000105745286 ' Steinhart Constant

R1 = 10000

repeat
ohms := ((4095 * R1) / ADC.GetData(0)) - R1
temp := Calc_StHart(Coeff1,Coeff2,Coeff3,ohms)
updateLcd(ohms, temp)
waitcnt(clkfreq / 5 + cnt)

Mike Green
04-02-2009, 06:31 AM
Well, there you go ... The whole "ohms" calculation is in integer arithmetic. You may want to do that calculation in floating point or, at least, convert it to floating point.

R1_Float = 10000.0
R1_4095 = 4095.0 * R1_Float

and

ohms := FSub(FDiv(R1_4095,FFloat(ADC.GetData(0))),R1_Float )

Post Edited (Mike Green) : 4/1/2009 11:40:51 PM GMT

Greg LaPolla
04-02-2009, 06:33 AM
Yup I discovered that after I posted the reply. I am reworking the steinhart routing to convert everything to float first.

Thanks for the heads up!


Greg

Greg LaPolla
04-02-2009, 07:24 AM
I have reworked the steinhart and now I have a new strange math problem




PRI Calc_StHart (A,B,C,Resistance) : Fht | B_x_Rlog , C_x_Rlog , Rcubed, Rlog, Addit, R_Float, Kelvin, Celsius

G.start ' Start math32

Rlog := G.log(G.FFloat(Resistance)) ' Take the log of the Resistance
B_x_Rlog := G.FMul(B,Rlog) ' Multiply B to log of R

RCubed := G.FMul(Rlog,Rlog) ' Cube the log of R by R * R * R
RCubed := G.FMul(Rlog,RCubed) ' Finish the Cube

C_x_Rlog := G.FMul(RCubed,C) ' Multiply C to log the cubed log R

Addit := G.Fadd(C_x_Rlog,B_x_Rlog)
Addit := G.Fadd(Addit, A) ' Add all A,B,C together

Kelvin := G.Fdiv(one,addit) ' Kelvin = 1/ all added

Celsius := G.Fsub(Kelvin,273.15) ' Convert to Celsius

Fht := G.FMul(Celsius,1.8) ' Convert to Farenheight
Fht := G.Fadd(Fht,32)

G.stop




The last calculation ( Fht := G.Fadd(Fht,32)) will not run.

TreeLab
04-02-2009, 07:48 AM
Change 32 to 32.0

Cheers!
Paul Rowntree

Greg LaPolla
04-02-2009, 07:49 AM
That did the trick. Thanks