Shop OBEX P1 Docs P2 Docs Learn Events
floating point help — Parallax Forums

floating point help

grouchygrouchy Posts: 16
edited 2009-08-12 02:25 in Propeller 1
New to the prop, but I love it.

I couldn't find "propeller for dummies", so I need to ask..

what's wrong with my code?

I'm reading 2 encoders and want to display position·in inches, but the display is crazy on the negative side.
What am I doing wrong?



'···· read an encoder, display the count '
'
CON
· _CLKMODE····· = XTAL1 + PLL16X·······················
· _XINFREQ····· = 5_000_000
··
OBJ
· encoder : "quadrature_encoder"··
· lcd : "serial_lcd"
· fs· : "FloatString"
· flt· : "floatmath"
··
VAR
· long encpos[noparse][[/noparse]4]··························· 'Create array for two encoders (plus delta positions)
· long scale
···
PUB main
· scale:=10300··························· ····· 'pulses per inch
· encoder.start(0, 2, 2, @encpos)········ 'start 2-encoders (pin, #of enc., #of delta enc.,bufferaddr)···························
· lcd.init(7,9600,2)························ ··· 'lcd on·pin 7


· repeat
·····
···· lcd.cls···
····
···· lcd.str(string("X:· "))········································· ' encoder 1
···· lcd.str(fs.FloatToString(flt.fdiv(encpos[noparse][[/noparse]0],scale)))·· ' show count/scaling, ok positive, nuts negative·····
····
···· lcd.move(1,2)
···· lcd.str(string("Y:· "))··········································' encoder 2
···· lcd.dec(encpos[noparse][[/noparse]1])···································· ······ ' show encoder absolute count, this is ok
····
·
···· waitcnt(8_000_000+cnt)····


Rudy



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rudy's CNC Freeware·· http://www.enter.net/~schleinkofer

Comments

  • TimmooreTimmoore Posts: 1,031
    edited 2009-08-12 02:05
    fdiv only works on float number. scale and encpos are integers. To fix
    scale := 10300.0 will put scale into float format
    encpos you can't do that because encoder returns int, to convert flt.float(encpos[noparse][[/noparse]0]) will convert int to float
  • grouchygrouchy Posts: 16
    edited 2009-08-12 02:25
    thanks, works great



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Rudy's CNC Freeware·· http://www.enter.net/~schleinkofer
Sign In or Register to comment.