PIC floating point questions

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

PIC floating point questions

Post by hyperion007 »

Hi,

I have searched the forums and tested for hours and hours but I can't get this sorted so I need help :)

Doing some floating point calculations on a PIC18F26K80 and I need higher precision I think.

What I am trying to do, shown in below example:
X=C*Z
Y=((a1*X)+(a2(X^2))+(a3(X^3))+(a4(X^4))+(a5(X^5))+(a6(X^6))+(a7(X^7))+(a8(X^8))+(a9(X^9)))

Constants:

C: 0.041276

a1: 25.08355
a2: 0.07860106
a3: -0.2503131
a4: 0.0831527
a5: -0.01228034
a6: 0.0009804036
a7: -0.0000441303
a8: 0.000001057734
a9: -0.00000001052755

Variables:

X =
Y =
Z =

---------
So for Z=20 we will get:
X = 0.82552
Y = 19.627236792

Final value of Y could be presented with only two decimal points.
---------

Is there any way to do this on a 8bit PIC?

Thanks.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

So no one knows then? ;)

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: PIC floating point questions

Post by kersing »

May-be it is not clear what your issue is... Could you be more specific about what you are experiencing compared to what you are expecting? May-be post a flowchart that is supposed to work but does not and provide some information on what fails?
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC floating point questions

Post by Benj »

Hello,

Are you entering that calculation into a single line? If so then the compiler may struggle especially with an 8 bit device.

Probably worth breaking the calculation down into a number of manageable steps, then testing each step as you go on the hardware to make sure it is giving you what you expect.

The float to string function uses a hard coded definition to define the size of the output number of decimal points. This can be overridden by tweaking the C code depending on your version of FC.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

Thanks guys,

Here I was thinking that my explanation was stellar :)

I'll post an FC example when I get home.

What I am trying to do is implement NIST coefficients for K-type thermocouples

Snippet from their website:
************************************
* This section contains coefficients for type K thermocouples for
* the two subranges of temperature listed below. The coefficients
* are in units of °C and mV and are listed in the order of constant
* term up to the highest order. The equation below 0 °C is of the form
* E = sum(i=0 to n) c_i t^i.
*
* The equation above 0 °C is of the form
* E = sum(i=0 to n) c_i t^i + a0 exp(a1 (t - a2)^2).
*
* Temperature Range (°C)
* -270.000 to 0.000
* 0.000 to 1372.000
************************************
name: reference function on ITS-90
type: K
temperature units: °C
emf units: mV
range: -270.000, 0.000, 10
0.000000000000E+00
0.394501280250E-01
0.236223735980E-04
-0.328589067840E-06
-0.499048287770E-08
-0.675090591730E-10
-0.574103274280E-12
-0.310888728940E-14
-0.104516093650E-16
-0.198892668780E-19
-0.163226974860E-22
range: 0.000, 1372.000, 9
-0.176004136860E-01
0.389212049750E-01
0.185587700320E-04
-0.994575928740E-07
0.318409457190E-09
-0.560728448890E-12
0.560750590590E-15
-0.320207200030E-18
0.971511471520E-22
-0.121047212750E-25
exponential:
a0 = 0.118597600000E+00
a1 = -0.118343200000E-03
a2 = 0.126968600000E+03



************************************
* This section contains coefficients of approximate inverse
* functions for type K thermocouples for the subranges of
* temperature and voltage listed below. The range of errors of
* the approximate inverse function for each subrange is also given.
* The coefficients are in units of °C and mV and are listed in
* the order of constant term up to the highest order.
* The equation is of the form t_90 = d_0 + d_1*E + d_2*E^2 + ...
* + d_n*E^n,
* where E is in mV and t_90 is in °C.
*
* Temperature Voltage Error
* range range range
* (°C) (mV) (° C)
* -200. to 0. -5.891 to 0.000 -0.02 to 0.04
* 0. to 500. 0.000 to 20.644 -0.05 to 0.04
* 500. to 1372. 20.644 to 54.886 -0.05 to 0.06
********************************************************
Inverse coefficients for type K:

Temperature -200. 0. 500.
Range: 0. 500. 1372.

Voltage -5.891 0.000 20.644
Range: 0.000 20.644 54.886

0.0000000E+00 0.000000E+00 -1.318058E+02
2.5173462E+01 2.508355E+01 4.830222E+01
-1.1662878E+00 7.860106E-02 -1.646031E+00
-1.0833638E+00 -2.503131E-01 5.464731E-02
-8.9773540E-01 8.315270E-02 -9.650715E-04
-3.7342377E-01 -1.228034E-02 8.802193E-06
-8.6632643E-02 9.804036E-04 -3.110810E-08
-1.0450598E-02 -4.413030E-05 0.000000E+00
-5.1920577E-04 1.057734E-06 0.000000E+00
0.0000000E+00 -1.052755E-08 0.000000E+00

Error -0.02 -0.05 -0.05
Range: 0.04 0.04 0.06

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

Ok I would really need some help here.

The results I am getting are not correct.

For example:
X = -0.00051920577*(0.919^8)

In MS Excel:
X = -0.00026415789872013

In FC V6:
T = pow (0.919,8)
T = T * -0.00051920577
Temperature_String = FloatToString$ (T)

Print String (Temperature_String) to LCD gives X = 12.807572


Doing it this way in FC V6:
T = (-0.00051920577 * (pow (0.919,8)))
Temperature_String = FloatToString$ (T)

Print String (Temperature_String) to LCD gives X = 0.281664

Don't know what to do now :(

AndrewC
Flowcode v5 User
Posts: 28
Joined: Wed Oct 02, 2013 12:16 pm
Has thanked: 7 times
Been thanked: 7 times
Contact:

Re: PIC floating point questions

Post by AndrewC »

I'm not sure if I have misunderstood but is this what you are trying to do?
Attachments
Temp.fcfx
(5.22 KiB) Downloaded 311 times

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

Thank you! I don't know why, but now I got it to actually work.

a_d1 = 25.173462
a_d2 = -1.1662878
a_d3 = -1.0833638
a_d4 = -0.8977354
a_d5 = -0.37342377
a_d6 = -0.086632643
a_d7 = -0.010450598
a_d8 = -0.00051920577
mV_TC = -1.637
T = ((a_d1 * mV_TC) + (a_d2 * (pow (mV_TC,2))) + (a_d3 * (pow (mV_TC,3))) + (a_d4 * (pow (mV_TC,4))) + (a_d5 * (pow (mV_TC,5))) + (a_d6 * (pow (mV_TC,6))) + (a_d7 * (pow (mV_TC,7))) + (a_d8 * (pow (mV_TC,8))))

Temp = FloatToString$ (T)

LCD_PrintString(Temp)

Shows "43.003540" on the display :)

Thanks!

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

Actually, it simulates fine, but it doesn't work on hardware... :(

It's the long T=((a_d1*...... bit that is causing the problems (I think, as the mV_TC is correct when I print it on the LCD hardware)

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Has thanked: 72 times
Been thanked: 177 times
Contact:

Re: PIC floating point questions

Post by Kenrix2 »

Glad you tried that in hardware, I was curious if it would really work. Maybe Benj's suggestion is worth trying. Does anyone know the V5 equivalent to V6's pow(x,y)? Although pow is a reserved word in V5 I can't get the syntax right. Maybe I just have to use x*x if y is 2 and x*x*x if y = 3?

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

This made it work:

mV_TC = 0.939
T = (b_d1 * mV_TC)
S = pow (mV_TC,2)
T = T + (b_d2 * S)
S = pow (mV_TC,3)
T = T + (b_d3 * S)
S = pow (mV_TC,4)
T = T + (b_d4 * S)
S = pow (mV_TC,5)
T = T + (b_d5 * S)
S = pow (mV_TC,6)
T = T + (b_d6 * S)
S = pow (mV_TC,7)
T = T + (b_d7 * S)
S = pow (mV_TC,8)
T = T + (b_d8 * S)
S = pow (mV_TC,9)
T = T + (b_d9 * S)
mV_TC_String = FloatToString$ (mV_TC)

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

fround isn't working when I try to compile

fround( x , y )

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

Can anyone else please verify that the fround function is not working?

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: PIC floating point questions

Post by hyperion007 »

Still an outstanding question as I still have no explanation to why I can't use this math function: fround(x,y)

GSM_MAX31855_PIC18F4550.c(1590:12): error: unknown identifier 'fround'
GSM_MAX31855_PIC18F4550.c(1590:12): error: failed to generate expression
GSM_MAX31855_PIC18F4550.c(1590:12): error: invalid operand 'fround(FCV_T, 2)'
GSM_MAX31855_PIC18F4550.c(1590:10): error: failed to generate expression
C:\Program Files (x86)\Flowcode 6\compilers\pic\boostc\boostc_18F.exe reported error code 1


Changing to:
GSM_MAX31855_PIC18F4550.c(1590:12): error: invalid operand 'fround(2322.022304, 2)'

instead of using the variable FCV_T made no difference.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC floating point questions

Post by Benj »

Hello,

Looks like the fround function is not supported in BoostC. I would put the question to the guys at Sourceboost and see if they can explain why the function is not in their compiler. Things like cos, tan, atan etc would also be very nice. Float support in general without having to use these weird non standard float functions would be amazing.

The AVR, ARM and dsPIC tool chains all support the function.

Post Reply