Floating point math help?
Andrew C
Posts: 14
Hi all,
I am new to working with the prop. I have the ed kit, and worked through several labs. I understand how to call another object in separate code, and define the variables within SPIN.
I am attempting to us the floating point math routines, but I am stuck. I've tried to understand the calls from FloatDemo on the object exchange, but something is going wrong.
Will someone help me with this basic problem:
I have calibrated a simple Resistance-Time circuit, where the formula is:
R=1.5279*T+36.1526
I want to solve for R, but I am unable to write the SPIN code for this using the floating point math routines. Anyone feel like working with me on this?
Thanks in advance!
I am new to working with the prop. I have the ed kit, and worked through several labs. I understand how to call another object in separate code, and define the variables within SPIN.
I am attempting to us the floating point math routines, but I am stuck. I've tried to understand the calls from FloatDemo on the object exchange, but something is going wrong.
Will someone help me with this basic problem:
I have calibrated a simple Resistance-Time circuit, where the formula is:
R=1.5279*T+36.1526
I want to solve for R, but I am unable to write the SPIN code for this using the floating point math routines. Anyone feel like working with me on this?
Thanks in advance!
Comments
R := flt.FAdd(flt.FMul(1.5279,T),36.1526)
Do remember that you will have to call "flt.start" during your program's initialization if you use one of the Float32 objects. If you use FloatMath, there isn't a "flt.start".
Post Edited (Mike Green) : 12/13/2009 4:59:07 PM GMT
Which leads to my question, what type of variable is R?
I do appreciate your help, Mike. I've lurked for a long time, and all (well, all I've read) your posts have been very helpful.
The next problem I have is I don't know how to convert·the "a" variable to a floating number. It was never defined as a variable, and I think that's my problem. I have tried·declaring·"a" as·a byte,·word and long, but then the value becomes "0". I also tried to assign a value of·"1.0"·at the beginning of the program with no luck.·I'm not sure where to go·next.
···· a := (phsa - 624) #> 0···············
····
···· ' Display Result·································
···· Debug.Str(String(13, "time = "))
···· Debug.str(fString.FloatToString(a))
···· Debug.Str(String(" which correlates to "))
···· b := flt.FAdd(flt.FMul(1.5179, a), 361526)
···· debug.str(fString.FloatToString(b))
···· Debug.Str(String("ohms"))
During the compile, I get an error message "Expected an instruction or variable"· with the "a" highlighted.
(In code below, I have fMath and fString instead of flt, and not nested)
Here's the full code I've slightly modified from the fundamentals lab·(TestRcDecay) source code:
CON
··
· _clkmode = xtal1 + pll16x················· ' System clock → 80 MHz
· _xinfreq = 5_000_000
· CR = 13
·
OBJ
··
· Debug: "FullDuplexSerialPlus"············· ' Use with Parallax Serial Terminal to
························································ ·' display values
· fMath: "Float32Full"···························· ' Use floating math routines
· fString: "FloatString"··························· ' Use floating math to String routines
·
var
·····
PUB Init
· 'Start serial communication, and wait 2 s for connection to Parallax Serial Terminal.
·
· Debug.Start(31, 30, 0, 9600)
··· waitcnt(clkfreq * 2 + cnt)
· ' Configure counter module.
· ctra[noparse][[/noparse]30..26] := %01000···················· ' Set mode to "POS detector"
· ctra[noparse][[/noparse]5..0] := 17·························· ' Set APIN to 17 (P17)
· frqa := 1································· ' Increment phsa by 1 for each clock tick
· main······································ ' Call the Main method
PUB Main | time
'' Repeatedly takes and displays P17 RC decay measurements.
·· fMath.start······································ ' start the floating math cog!
· repeat
···· ' Charge RC circuit.
···· dira[noparse][[/noparse]17] := outa[noparse][[/noparse]17] := 1·············· ' Set pin to output-high
···· waitcnt(clkfreq/100_000 + cnt)········· ' Wait for circuit to charge
·····
···· ' Start RC decay measurement.· It's automatic after this...
···· phsa~·································· ' Clear the phsa register
···· dira[noparse][[/noparse]17]~······························ ' Pin to input stops charging circuit
···· ' Optional - do other things during the measurement.
···· Debug.tx(Debug#CLS)
···· Debug.str(String(CR, CR, "Working on other tasks", CR))
···· Debug.str(string(CR))
·········
···· repeat 22
······ Debug.tx(".")
······ waitcnt(clkfreq/60 + cnt)·······
···· ' Measurement has been ready for a while.· Adjust ticks between phsa~ & dira[noparse][[/noparse]17]~.
···· a := (phsa - 624) #> 0···············
····
···· ' Display Result·································
···· Debug.Str(String(13, "time = "))
···· debug.str(fString.FloatToString(a))
···· Debug.Str(String(" which correlates to "))
···· b := fMath.FMul(1.5179, a)
···· c := fMath.FAdd(b, 36.1526)
···· debug.str(fString.FloatToString(b))
···· Debug.Str(String("ohms"))
···· waitcnt(clkfreq/2 + cnt)
At least I am learning...
Thanks again. You got me started on the right track. I·needed to take a big step back, and work things through in my head. With a little help from Graham, I think I have this straightened out!
I started another post, included the simpler code in case somebody gets stuck where I did.
http://forums.parallax.com/showthread.php?p=865249
So I am playing with the Parallax Serial Terminal, and Floating Point math.
I attached a few notes to help other folks out.