uM-FPU V3.1 question?
Dwayne Dibbley
Posts: 63
i have the following code:
my problem is the add part does not seem increase the value of distfpu ( it should increase each loop by the value 3.196609987707373e-5 )
am i using the commands correctly?
Many Thanks
distfpu := string("0.0") 'set counter to zero repeat FPU.WriteCmdByte(FPU#_SELECTA, 1) ' select register 1 FPU.WriteCmdStr(FPU#_ATOF, string("0.8689762")) ' convert string to float in register 0 FPU.WriteCmdByte(FPU#_FSET0, 0) 'set register 1 with value of register 0 FPU.WriteCmdStr(FPU#_ATOF, string("1")) 'convert string to float in register 0 FPU.WriteCmdByte(FPU#_SELECTA, 1) 'select register 1 FPU.WriteCmdByte(FPU#_FDIVR0, 0) 'divide register 0 by register 1 value FPU.WriteCmdByte(FPU#_SELECTA, 1) 'select register 1 term.Str(FPU.ReadRaFloatAsStr(0)) 'print out result ( in this case 1.1507794 ) FPU.WriteCmdByte(FPU#_SELECTA, 1) 'select register 1 FPU.WriteCmdStr(FPU#_ATOF, string("0.00002777778")) 'convert string to float in register 0 FPU.WriteCmdByte(FPU#_FMUL0, 0) 'multiply register 1 by register 0 value FPU.WriteCmdByte(FPU#_SELECTA, 1) 'select register 1 FPU.WriteCmdStr(FPU#_ATOF, distfpu) 'convert string to float in register 0 FPU.WriteCmdByte(FPU#_FADD0, 0) 'add register 1 to register 0 FPU.WriteCmdByte(FPU#_SELECTA, 1) 'select register 1 distfpu := FPU.ReadRaFloatAsStr(0) 'now set distfpu value to the value in register 1 term.Str(distfpu) 'print out distfpu value
my problem is the add part does not seem increase the value of distfpu ( it should increase each loop by the value 3.196609987707373e-5 )
am i using the commands correctly?
Many Thanks
Comments
1. why do you convert from string to float and from float to string several times?
It would be much wiser to convert once and store to floats for reuse. Conversion will definitely add up a lot of runtime. Only convert back to string when you want to display the value.
2. Converting back to strings seems to be your problem. I'd expect that the FPU object has only one buffer for converting floats to string. And each call to FPU.ReadRaFloatAsStr will give you back the same string buffer. If you need that string again later on, you have to copy it to a safe place if you do other ..AsStr function calls in between.
The reason is, that we don't have dynamic memory allocation for the propeller. In PC systems the ...AsStr would simply allocate some new memory and use a different buffer for each conversion. Maybe this is the reason you have this missunderstanding.
sync := cnt
FPU.WriteCmdByte(FPU#_CLR, 12)
FPU.WriteCmdByte(FPU#_SELECTA, 10)
FPU.WriteCmdFloat(FPU#_FWRITEA, 0.0) ' reset trip meter
FPU.WriteCmdByte(FPU#_CLR, 10)
FPU.WriteCmdByte(FPU#_SELECTA, 10)
FPU.WriteCmdFloat(FPU#_FWRITEA, 0.00002777778) ' set 1/10th second inverval in register 10 (10hz GPS)
FPU.WriteCmdByte(FPU#_CLR, 11)
FPU.WriteCmdByte(FPU#_SELECTA, 11)
FPU.WriteCmdFloat(FPU#_FWRITEA, 0.8689762) ' set knot to miles conversion in register 11
repeat
FPU.WriteCmdByte(FPU#_SELECTA, 9)
FPU.WriteCmdStr(FPU#_ATOF, string("111.1")) 'fake gps speed in knots will be different each loop comes in as string
FPU.WriteCmd(FPU#_FSET0)
FPU.WriteCmdByte(FPU#_FDIV, 11) 'divide knots by figure in register 11 to get miles per hour
term.Str(FPU.ReadRaFloatAsStr(0)) 'debug print out miles per hour
FPU.WriteCmdByte(FPU#_FMUL, 10) ' multiply miles per hour (still in register 9) by value in register 10 to get distance in miles
FPU.WriteCmdByte(FPU#_SELECTA, 12) ' select tripmeter register 12
FPU.WriteCmdByte(FPU#_FADD, 9) 'add register 9 distance to total distance in register 12
term.Str(FPU.ReadRaFloatAsStr(0)) ' debug print out total distance
waitcnt(sync += clkfreq/10)