Shop OBEX P1 Docs P2 Docs Learn Events
Strange Math Problem (Resolved) — Parallax Forums

Strange Math Problem (Resolved)

Greg LaPollaGreg LaPolla Posts: 323
edited 2009-04-02 00:49 in Propeller 1
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
625 x 986 - 74K

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-01 22:53
    How are you providing A, B, C, Resistance? If one or more are not floating point values, that could explain what's happening.
  • Greg LaPollaGreg LaPolla Posts: 323
    edited 2009-04-01 22:57
    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 GreenMike Green Posts: 23,101
    edited 2009-04-01 23:31
    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 LaPollaGreg LaPolla Posts: 323
    edited 2009-04-01 23:33
    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 LaPollaGreg LaPolla Posts: 323
    edited 2009-04-02 00:24
    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.
  • TreeLabTreeLab Posts: 138
    edited 2009-04-02 00:48
    Change 32 to 32.0

    Cheers!
    Paul Rowntree
  • Greg LaPollaGreg LaPolla Posts: 323
    edited 2009-04-02 00:49
    That did the trick. Thanks
Sign In or Register to comment.