PDA

View Full Version : Floating point math help?

Andrew C
12-13-2009, 11:46 PM
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?

Mike Green
12-13-2009, 11:53 PM
Each floating point operation involves a function call (which can be nested) like this (assuming your floating point object is called "flt"):

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

Andrew C
12-14-2009, 12:18 AM
Ok, good news is that I understand the nested function call.

Which leads to my question, what type of variable is R?

Andrew C
12-14-2009, 12:36 AM
Ok, I should have looked closer at p110 in the PEK manual! My problem lies in the debug calls. Seems my math function calls were okay.

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.

Mike Green
12-14-2009, 01:08 AM
R is just a LONG like T. Floating point values are just 32-bit values like any LONG. The Propeller Tool doesn't distinguish between floating point values and 32-bit integer values. Even floating point constants are converted into their internal representation and treated afterwards like any integer constant. If you want, look at the Wikipedia article on IEEE floating point for a description of the internal representation of floats.

Andrew C
12-14-2009, 02:19 AM
Ok, I thought I had things figured out.

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[30..26] := %01000···················· ' Set mode to "POS detector"
· ctra[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[17] := outa[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[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[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)
···· debug.str(fString.FloatToString(b))
···· Debug.Str(String("ohms"))

···· waitcnt(clkfreq/2 + cnt)

Andrew C
12-14-2009, 02:41 AM
Ok, solved another problem of my own stupidity. I changed a variable "time" to "a" and my problem was fixed. Next step is rounding the answer!

At least I am learning...

Mike Green
12-14-2009, 02:47 AM
Variables have to be declared somewhere, either in a VAR section, in a DAT section, as parameters, or as local variables.

Andrew C
12-16-2009, 09:59 AM
@Mike,

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.