Searching \ for ' Can you crack this formula with a PIC ?' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Can you crack this formula with a PIC ?'.

No exact or substring matches. trying for part
'[PICLIST] Can you crack this formula with a PIC ?'
2000\10\26@122207 by

It's a formula to calculate water vapour pressure given a temperature in Deg C

float calculateWaterWVP(float Temp)
{
float a,tempInK,c,d,e,wvp;

tempInK = Temp + 273.15;
a = -6096.9385 * (1/tempInK);
c = -2.711193 * 0.01 * tempInK;
d = 1.673952 * 0.00001 * (tempInK * tempInK);
e = 2.433502 * (log(tempInK)/log(10)/0.434294509);

wvp = exp(a + 21.2409642 + c + d + e);
return wvp;

}  */

If you can do this, you get a medal !

Regards,
Darren

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

On Thu, 26 Oct 2000, Darren Logan wrote:

> It's a formula to calculate water vapour pressure given a temperature in Deg C
>
>  float calculateWaterWVP(float Temp)
>  {
>     float a,tempInK,c,d,e,wvp;
>
>     tempInK = Temp + 273.15;
>     a = -6096.9385 * (1/tempInK);
>     c = -2.711193 * 0.01 * tempInK;
>     d = 1.673952 * 0.00001 * (tempInK * tempInK);
>     e = 2.433502 * (log(tempInK)/log(10)/0.434294509);
>
>
>     wvp = exp(a + 21.2409642 + c + d + e);
>     return wvp;
>
>  }  */
>
> If you can do this, you get a medal !

Darren,

The easiest way to solve this is with a lookup table (with at least first order
interpolation). However, before anyone can propose a solution you'll need to
answer this question: how accurate does it have to be? How accurate is the
temperature?

Scott

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

Darren,

I did it.

#include <pic.h>
#include <math.h>

float calculateWaterWVP(float Temp)
{
float a,tempInK,c,d,e,wvp;

tempInK = Temp + 273.15;
a = -6096.9385 * (1/tempInK);
c = -2.711193 * 0.01 * tempInK;
d = 1.673952 * 0.00001 * (tempInK * tempInK);
e = 2.433502 * (log(tempInK)/log(10)/0.434294509);

wvp = exp(a + 21.2409642 + c + d + e);
return wvp;

}

void main (void)
{
calculateWaterWVP (5);
}

-------------------------------------------------------
> cd .
> picc -16F876 -E -G -O -Zg1 -L-N temp.c

F:\>picc -16F876 -E -G -O -Zg1 -L-N temp.c
HI-TECH C COMPILER (Microchip PIC) V7.86 PL3
Andy Kunz, Montana Design

Memory Usage Map:

Program ROM   \$0000 - \$0054  \$0055 (    85) words
Program ROM   \$0209 - \$07FF  \$05F7 (  1527) words
\$064C (  1612) words total Program ROM

Bank 0 RAM    \$0020 - \$0058  \$0039 (    57) bytes
Bank 0 RAM    \$0070 - \$007B  \$000C (    12) bytes
\$0045 (    69) bytes total Bank 0 RAM

F:\>

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

>
>Program ROM   \$0000 - \$0054  \$0055 (    85) words
>Program ROM   \$0209 - \$07FF  \$05F7 (  1527) words
>                              \$064C (  1612) words total Program ROM
>
>Bank 0 RAM    \$0020 - \$0058  \$0039 (    57) bytes
>Bank 0 RAM    \$0070 - \$007B  \$000C (    12) bytes
>                              \$0045 (    69) bytes total Bank 0 RAM

How much of that is system overhead?
IOW: if

void Main(void)
{

}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

It's a formula to calculate water vapour pressure given a temperature in Deg C

float calculateWaterWVP(float Temp)
{
float a,tempInK,c,d,e,wvp;

tempInK = Temp + 273.15;
a = -6096.9385 * (1/tempInK);
c = -2.711193 * 0.01 * tempInK;
d = 1.673952 * 0.00001 * (tempInK * tempInK);
e = 2.433502 * (log(tempInK)/log(10)/0.434294509);

wvp = exp(a + 21.2409642 + c + d + e);
return wvp;

}  */

If you can do this, you get a medal !

(Accuracy of result = 1% or better)

Regards,
Darren

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

main() is pretty tiny, only a few words (< 10 I bet).

Startup (erases all RAM to 0, only a few bytes)
FP Lib (most of the code)
Subroutine (a significant but much smaller chunk).

Andy

David VanHorn <dvanhornCEDAR.NET> on 10/26/2000 01:08:42 PM

Please respond to pic microcontroller discussion list <PICLISTMITVMA.MIT.EDU>

To:      PICLISTMITVMA.MIT.EDU

cc:      (bcc: Andrew Kunz/TDI_NOTES)

Subject: Re: Can you crack this formula with a PIC ?

>
>Program ROM   \$0000 - \$0054  \$0055 (    85) words
>Program ROM   \$0209 - \$07FF  \$05F7 (  1527) words
>                              \$064C (  1612) words total Program ROM
>
>Bank 0 RAM    \$0020 - \$0058  \$0039 (    57) bytes
>Bank 0 RAM    \$0070 - \$007B  \$000C (    12) bytes
>                              \$0045 (    69) bytes total Bank 0 RAM

How much of that is system overhead?
IOW: if

void Main(void)
{

}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

At 01:26 PM 10/26/00 -0400, you wrote:
>main() is pretty tiny, only a few words (< 10 I bet).
>
>
>Startup (erases all RAM to 0, only a few bytes)
>FP Lib (most of the code)
>Subroutine (a significant but much smaller chunk).

I don't use C on pics, so I don't know how much overhead to expect, but if
you had a number from an empty "main" to compare to, then it would be more
meaningful. Still, it looks to be a pretty expensive function, implemented
that way.

1% accuracy would imply that a lookup table with 255 entries would seem to
be a lot smaller, and no doubt faster.  (Depends also how you define that
1% :)

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

And what is the range of the temperature.

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

{Original Message removed}
>I don't use C on pics, so I don't know how much overhead to expect, but if
>you had a number from an empty "main" to compare to, then it would be more
>meaningful. Still, it looks to be a pretty expensive function, implemented
>that way.

< 50 bytes

>1% accuracy would imply that a lookup table with 255 entries would seem to
>be a lot smaller, and no doubt faster.  (Depends also how you define that
>1% :)

A floating point (4 byte) table would require 1024 bytes, plus lookup overhead,
and would be limited to 255 discrete inputs.

His was for a floating-point input (4 bytes) so a PIC wouldn't have adequate
table space.

But it still might be adequate for a table spanning 100 degrees.

Andy

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

At 02:30 PM 10/26/00 -0400, you wrote:
> >I don't use C on pics, so I don't know how much overhead to expect, but if
> >you had a number from an empty "main" to compare to, then it would be more
> >meaningful. Still, it looks to be a pretty expensive function, implemented
> >that way.
>
>< 50 bytes

I misunderstood then, you said (I thought) that the empty main would be
pretty small (I was thinking tens of bytes) and the compiled routine was
something like 1600?

> >1% accuracy would imply that a lookup table with 255 entries would seem to
> >be a lot smaller, and no doubt faster.  (Depends also how you define that
> >1% :)
>
>A floating point (4 byte) table would require 1024 bytes, plus lookup
>and would be limited to 255 discrete inputs.

Hmm.. I don't know how "granular" the temperature input can be, but over
any range, a 255 entry table introduces roughly 1/2% error. (rounding)

Output value would then be one of 255 different numbers, which could be
some constant, plus a scaled byte from the table. Harder to say what the
absolute accuracy here would be, but it seems to be on the order of 1/2% also.

>His was for a floating-point input (4 bytes) so a PIC wouldn't have adequate
>table space.

That's computing to a much higher accuracy than he said he needed though.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

At 01:08 PM 10/26/00 EDT, you wrote:
>It's a formula to calculate water vapour pressure given a temperature in
Deg C
{Quote hidden}

How nonlinear is this function OVER THE RANGE OF INTEREST?

How much resolution do you need (for display, it may be unacceptable
to have poor resolution, even if the accuracy is ok)?

Since this is a SISO function of temperature, I'd be tempted to use
either a lookup table with linear interpolation or a polynomial fit to
the analytical function above. Using 16-32 bit fixed point integer
math, depending.

Best regards,

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Spehro Pefhany --"it's the network..."            "The Journey is the reward"
speffinterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Contributions invited->The AVR-gcc FAQ is at: http://www.bluecollarlinux.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

As Scott mentioned, you need first to describe the input, the output, the
precision, and the range.  You could easily perform this exact calculation in a
C compiler (A good one with a math library and 32 or 64 bit floating point
numbers), but it would take a bit of time during operation.

But let's say instead that you need it to be faster.  If you define your input
to be within a range, define an acceptable error, and know how good your input
is, then you can take several shortcuts.

If you need it to be really fast, or you have lots of code space available, use
a lookup table.  Assume your sensor doesn't give you any better resolution than
.1 degree celsius, and you're only making these measurements between 0 and 100
degrees celsius.  You would only need a 1000 entry lookup table to get your
answer, if you can fit it into a byte.  If you needed 16 bit output, you would
use a 2000 entry table, and it would still take fewer than 20 instruction cycles
to get your answer.  It wouldn't surprise my too much if a 16-bit math library
including LOG and EXP took that much code space anyway, so a table would be your
best bet.

A lot of good code revolves around developing your own scales.  Many coders use
0-256 to indicate angle, rather than 0 to 359.  They lose resolution that they
don't need.  If it gives you enough information, you can translate 0 to 100
degrees celsius to 0 to 256 Degrees Darren.  Translate WVP values of 611 to
101419 to an easier to deal with 0-65536 Logans.  Now re-write the equation to
accept 0-256 degrees Darren and give 0-65536 Logans, make a table and off you
go.

Using excel, I see that a reasonable approximation of your equation on these two
new scales would be y = -1E-09x^6 + 2E-06x^5 + 0.0001x^4 + 0.0189x^3 + 0.8347x^2
+ 27.411x - 28.536.  You can use your favorite spreadsheet to generate number
for a table, or write a program to do so.

Darren Logan wrote:
{Quote hidden}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

On Thu, 26 Oct 2000, M. Adam Davis wrote:

> As Scott mentioned, you need first to describe the input, the output, the
> precision, and the range.  You could easily perform this exact calculation in a
> C compiler (A good one with a math library and 32 or 64 bit floating point
> numbers), but it would take a bit of time during operation.
>
> But let's say instead that you need it to be faster.  If you define your input
> to be within a range, define an acceptable error, and know how good your input
> is, then you can take several shortcuts.
>
> If you need it to be really fast, or you have lots of code space available, use
> a lookup table.  Assume your sensor doesn't give you any better resolution than
> .1 degree celsius, and you're only making these measurements between 0 and 100
> degrees celsius.  You would only need a 1000 entry lookup table to get your

Actually, I bet you'd need far fewer than a 1000 entry table. It depends on the
linearity of the function as Spehro suggests. For example, the sine wave
functions on my web page use a 16 entry table and still achieve a +/- 1 count
resolution (for an 8 bit result). However, the symmetry properties allow only a
quarter of the sine wave to be include. So effectively, there are 64
samples. Then linear interpolation is used to get the rest of the accuracy.

If the function is highly non-linear yet relatively monotonic, then a small look
table is still adequate. (For example, the table entries could store the
deviations from an easy-to-calculate function that closely approximates the real
function). If the function is oscillatory (which I doubt it is considering how
physical things generally don't have a oscillatory responses to temperature)
then it would be much more difficult to use a look-up table. However it's not
impossible...

Scott

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

> How nonlinear is this function OVER THE RANGE OF INTEREST?
Very non-linear, exponential!

This one for temps above freezing. Note temps in Cel.
VaporPressure := 6.11*Power(10,(T*7.5)/(T+237.3))

this one for temps below freezing IF you want the frost point!
VaporPressure := 6.11*Power(10,(T*9.5)/(T+265.5))

These are pretty good over the meteorlogical range of temps (-40 to 50
Cel.)

BTW, the vapor pressure is no better than the accuracy of the
temperature
measurement!

Jim Korman

Spehro Pefhany wrote:
{Quote hidden}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

Darren, this is not the answer you are looking for but I designed a PIC16x
(40-pin chip) based weather station several years ago. I needed to do the
Dew Point, Heat Index, and other equations which are incredibly complicated
and subjective. Since I was already implementing a bus-based system using
a 512KBye SRAM to store sensor data, I did the calculations in Excel, wrote
a simple C program to download the data tables to the PIC, and used simple
fixed-point math in the PIC to deal with it all.

- Tom

At 12:20 PM 10/26/00 EDT, Darren Logan wrote:
>It's a formula to calculate water vapour pressure given a temperature in
Deg C
{Quote hidden}

------------------------------------------------------------------------
Tom Handley
New Age Communications
Since '75 before "New Age" and no one around here is waiting for UFOs ;-)

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use listservmitvma.mit.edu?body=SET%20PICList%20DIGEST

I suppose the output was a single tri-color LED which signified whether to wear
a t-shirt, long-sleeved shirt, light jacket or coat... ;-)

Tom Handley wrote:
{Quote hidden}

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use listservmitvma.mit.edu?body=SET%20PICList%20DIGEST

Hi,

I was impressed by HiTech C's compilation of this. But.

1% is doable with 8 bits, i.e. table output can be on 8 bits. Input
accuracy does not relate to this linearly however. In particular you lose
precision in the log operation. So your input precision could be 10% to
obtain output 1% under certain circumstances.

On the other hand, I'd run the little program:

for(temp = MIN_TEMP; temp <= MAX_TEMP; temp += TEMP_STEP)
printf("%-10f\t%-10f\n",i,calculateWaterWVP(temp));

to obtain a pretty table that will speak volumes about what you need to
do. You can modify the program to calculate the (output) table values
themselves and to obtain the required input precision for each table
domain (the precision will vary because of your nonlinear dependency -
esp. the log operation). The easiest way to do this is to have a variable
float temp0 = MIN_TEMP-1; and for each calculated value display only if
the reduced (to 1%) value is different from temp0, then set temp0 so the
calculated temp. This will remove all the intermediary results from the
table, giving the domain boundaries for the table.

In the end you will likely end up with a table that may be largish
depending on your temperature domain or use several interpolation points,
as Scott has said. This being said, maybe you need to purchase HITECH C ?
;-)

Peter

PS: I do not work for them.
PS2: Of course you run this on a PC not on a PIC.

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use listservmitvma.mit.edu?body=SET%20PICList%20DIGEST

Hehehe, actually here in Oregon you just assume rain but look out
the window just in case. I use an accurate commercial rain gage when
all I need is to measure the depth of the puddles out back ;-)

- Tom

At 01:13 PM 10/27/00 -0400, Adam wrote:
>I suppose the output was a single tri-color LED which signified whether to
wear
>a t-shirt, long-sleeved shirt, light jacket or coat... ;-)
>
>
>Tom Handley wrote:
>>
>>    Darren, this is not the answer you are looking for but I designed a
PIC16x
>> (40-pin chip) based weather station several years ago. I needed to do the
>> Dew Point, Heat Index, and other equations which are incredibly complicated
>> and subjective. Since I was already implementing a bus-based system using
>> a 512KBye SRAM to store sensor data, I did the calculations in Excel, wrote
>> a simple C program to download the data tables to the PIC, and used simple
>> fixed-point math in the PIC to deal with it all.
>>
>>    - Tom

------------------------------------------------------------------------
Tom Handley
New Age Communications
Since '75 before "New Age" and no one around here is waiting for UFOs ;-)

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestmitvma.mit.edu

Could you tell us please the range of Temp ?
Regards,
Imre

On Thu, 26 Oct 2000, Darren Logan wrote:

{Quote hidden}

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestmitvma.mit.edu

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...