Shop OBEX P1 Docs P2 Docs Learn Events
Newbie Float problems — Parallax Forums

Newbie Float problems

Daniel Lloyd-WoodDaniel Lloyd-Wood Posts: 14
edited 2009-03-08 20:12 in Propeller 1
Hello,

I am having some float problems that are slowly driving me insane so I hope someone can help out or they'll be carting me off in a padded van!

I am making a method for reading in servo pulses using counter registers. I have it all theoretically working. I just can't get it to output to the terminal correctly. This is the code:

CON
 _CLKMODE            = XTAL1 + PLL16X
 _XINFREQ            = 5_000_000
 
OBJ
  fstr: "FloatString"                                   'Float to String object
  debug: "FullDuplexSerialPlus"                         'Terminal object   
                                          
PUB controlLoop | markPos, markNeg, pulseWidth, pulseRaw

  outa[noparse][[/noparse]9..10]~~
 
  ctra[noparse][[/noparse]30..26] := %01010                                'setup ctra for POSEDGE detect
  ctra[noparse][[/noparse]8..0] := 24
  frqa~
  phsa~
 
  ctrb[noparse][[/noparse]30..26] := %01110                                'setup ctrb for NEGEDGE detect
  ctrb[noparse][[/noparse]8..0] := 24
  frqb~
  phsb~
 
  debug.start(31, 30, 0, 57600)                         'start terminal for debugging messages
  dira[noparse][[/noparse]9..10]~~
  waitcnt(clkfreq * 2 + cnt)
  dira[noparse][[/noparse]9..10]~                                   


  
  repeat
    frqa := 1                                           'Start counters
    frqb := 1
 
    repeat until phsa == 1                              'Mark time when positive edge is detected
      markPos := cnt
    repeat until phsb == 1                              'Mark time when negative edge is detected
      markNeg := cnt
 
    frqa~                                               'Reset counters
    phsa~
    frqb~
    phsb~
 
    pulseRaw := ((markPos - markNeg)* -1)
    pulseWidth := pulseRaw / 80000                      'Calculate pulse width in ms
    
    debug.tx(CLS)                                       'Send results to terminal
    debug.str(string("markPos: "))
    debug.dec(markPos)
    debug.tx(CR)
    debug.str(string("markNeg: "))
    debug.dec(markNeg)
    debug.tx(CR)
    debug.str(string("pulseRaw: "))
    debug.dec(pulseRaw)
    debug.tx(CR)
    debug.str(string("pulseWidth: "))
    debug.str(fstr.FloatToString(pulseWidth))
    debug.tx(CR)
 
    waitcnt(clkfreq + cnt)

·This is an example of the output I get:

markPos: 1730228749
markNeg: 1730360925
pulseRaw: 132176
pulseWidth: 1.401298e-45

The marking of the positive (markPos)·and negative (markNeg)·edge works and the difference between them (pulseRaw)·is·calculated correctly. The·problem occurs·when·I try to calculate·the result in milliseconds (pulseWidth).

If, on a calculator,·I take 132176 and divide it by 80,000 I get 1.65 which is the value I am expecting. i.e. the pulse width·I sent from my servo tester.

If I do the same in spin and then use the floatString object to convert it to a string for the terminal I get 1.401298e-45 no matter what pulseRaw is.

Anybody got any ideas? I'm all out.

Thanks,

Dan

Comments

  • AleAle Posts: 2,363
    edited 2009-03-08 19:59
    The problem you are having has to do qith the fact that spin only knows integer operations. To do floting point you need somethinng like the float32 package or some other code like the described here : propeller.wikispaces.org/MATH.

    You are treating pulseWidth as it were a float when it really is an integer.

    I hope it solves your issue
  • Daniel Lloyd-WoodDaniel Lloyd-Wood Posts: 14
    edited 2009-03-08 20:12
    Thanks Ale.

    That's got it. I was under the wrong impression that the prop stored floats that were calculated from two integers. Now I think about it, it wasn't a very logical assumption. I've implemented FloatMath and it's working fine now.

    Thanks again,

    Dan
Sign In or Register to comment.