Floating point math - Help.
reppig
Posts: 35
I took a class on the propeller about years ago and put it aside until now. I have a project that needs it. We fly weather balloons that has a GPS on board. We would like to take the GPS data - convert X, Y, and Z to Az and El so we can automatically point our receive antennas. I know the math works I have verified it on excel and in C sharp on a computer. However, as I am starting to debug the program the answers are not right. I am not sure were to start. Below is just the testing of the first function call. The answers I should get are: .003352811, .996647189, and 6356752.314. I am getting: 995867349, 1065296965, and 1254227489.
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal freq. = 80 MHz
_xinfreq = 5_000_000 'Extenal Crystall Freq.
{{********* WGS-84 Constants **********}}
earth_rad = 6378137.0 'Radius of a spherical model of the Earth
flattening = 298.2572236 'Flattening - spherical model of the earth
VAR
long efg[3], efg_gs[3], efg_tgt[3] 'efg coords of ground station and target(balloon
long xyz[3], rae[3], GS_Llh[3] 'xyz or range,az,el or ground station lat, long, height
OBJ
f : "Float32Full"
DBG : "FullDuplexSerialPlus"
PUB Main | ok1, ok2
WAITCNT(CLKFREQ * 6 + CNT)
'Start FullDuplexSerialPlus Driver for debug. The Driver will launch a
'COG for serial communication with Parallax Serial Terminal
oK1 := DBG.Start(31, 30, 0, 57600)
'Start Float32Full
oK2 := f.Start 'Connection pins are defined in the driver
IF NOT (oK1 AND oK2) 'Some error occured
IF oK1 'We have at least the debug terminal
DBG.Str(STRING(10, 13))
DBG.Str(STRING("Some error occurred. Check System!", 10, 13))
DBG.Stop
IF oK2
f.Stop
REPEAT
Geo_B
repeat
{********** Geo_B & Geo_E2 are Functions to do a Geodetic solution **********}
PUB Geo_B | temp 'Geo_B = (earth_rad * (1.0 - (1.0 / flattening)))
DBG.Str(STRING("Geo_B sub = "))
temp := f.FDiv(1.0, flattening) 'Geo_B = (earth_rad * (1.0 - temp))
DBG.Dec(temp)
DBG.Str(STRING(10, 13))
temp := f.FSub(1.0, temp) 'Geo_B = (earth_rad * temp)
DBG.Dec(temp)
DBG.Str(STRING(10, 13)
temp := f.FMul(earth_rad, temp)
DBG.Dec(temp)
DBG.Str(STRING(10, 13))
result := temp 'Return Geo_B
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal freq. = 80 MHz
_xinfreq = 5_000_000 'Extenal Crystall Freq.
{{********* WGS-84 Constants **********}}
earth_rad = 6378137.0 'Radius of a spherical model of the Earth
flattening = 298.2572236 'Flattening - spherical model of the earth
VAR
long efg[3], efg_gs[3], efg_tgt[3] 'efg coords of ground station and target(balloon
long xyz[3], rae[3], GS_Llh[3] 'xyz or range,az,el or ground station lat, long, height
OBJ
f : "Float32Full"
DBG : "FullDuplexSerialPlus"
PUB Main | ok1, ok2
WAITCNT(CLKFREQ * 6 + CNT)
'Start FullDuplexSerialPlus Driver for debug. The Driver will launch a
'COG for serial communication with Parallax Serial Terminal
oK1 := DBG.Start(31, 30, 0, 57600)
'Start Float32Full
oK2 := f.Start 'Connection pins are defined in the driver
IF NOT (oK1 AND oK2) 'Some error occured
IF oK1 'We have at least the debug terminal
DBG.Str(STRING(10, 13))
DBG.Str(STRING("Some error occurred. Check System!", 10, 13))
DBG.Stop
IF oK2
f.Stop
REPEAT
Geo_B
repeat
{********** Geo_B & Geo_E2 are Functions to do a Geodetic solution **********}
PUB Geo_B | temp 'Geo_B = (earth_rad * (1.0 - (1.0 / flattening)))
DBG.Str(STRING("Geo_B sub = "))
temp := f.FDiv(1.0, flattening) 'Geo_B = (earth_rad * (1.0 - temp))
DBG.Dec(temp)
DBG.Str(STRING(10, 13))
temp := f.FSub(1.0, temp) 'Geo_B = (earth_rad * temp)
DBG.Dec(temp)
DBG.Str(STRING(10, 13)
temp := f.FMul(earth_rad, temp)
DBG.Dec(temp)
DBG.Str(STRING(10, 13))
result := temp 'Return Geo_B
Comments
For example, the first thing I see is this:
temp contains a floating point number, but DBG.Dec is an integer operation. So temp will print wrong even though it is likely correct. The F32 object has a print method to convert floating point to text.
F32 has the same features as Float32Full but only requires one cog and is faster (win/win).
It would help if you used code tags when posting code. Follow this link.
Duane