Shop OBEX P1 Docs P2 Docs Learn Events
natural log function — Parallax Forums

natural log function

ArchiverArchiver Posts: 46,084
edited 2004-05-28 19:47 in General Discussion
Hi, I want to convert a voltage to a temperature and the equation I'm
using is -50*LN(voltage)+198

Can anyone help me with the code to do this equation on the stamp2
since the stamp can only do integer arithmetic I'm having a hard time.

Thanks

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-05-24 09:33
    I think what you will need to do is linearize the data between several points
    best as possible and use one of several linearized equations......Not being a
    stamp expert that is how I would start...or a giant look up table.

    I am sure some stamp experts will chime is with a better idea.

    ken

    ==========================
    Hi, I want to convert a voltage to a temperature and the equation I'm
    using is -50*LN(voltage)+198

    Can anyone help me with the code to do this equation on the stamp2
    since the stamp can only do integer arithmetic I'm having a hard time.

    Thanks


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-24 14:15
    At 02:04 AM 5/24/04 +0000, vilet_bounnam wrote:
    >Hi, I want to convert a voltage to a temperature and the equation I'm
    >using is -50*LN(voltage)+198
    >
    >Can anyone help me with the code to do this equation on the stamp2
    >since the stamp can only do integer arithmetic I'm having a hard time.
    >
    >Thanks

    You may find some of the information here to be of interest in doing math on the Basic Stamp:
    http://www.emesystems.com/BS2index.htm#math

    Regards,

    Bruce Bates
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-24 14:15
    There are a few options. One would be to do a look up table. Say your
    voltage is coming from an A/D converter with 8 bits. You could store a
    lookup table with 256 entries that simply "knows" the answer to the equation
    (of course, you still have to use integers -- maybe 152 is really 15.2,
    though).

    If you want to do floating point, look at our PAK-II, IX, and XII
    coprocessors. All have natural log and the IX and XII also have A/D
    converters. The PAK-II and IX use SHIFTIN/SHIFTOUT in a very efficient
    protocol. The PAK-XII uses RS232 and works sort of like an RPN calculator --
    very easy to use. Read more at http://www.awce.com/pak2.htm and
    http://www.awce.com/pak12.htm

    Tracy may have some other ideas on cheating LN :-)

    Regards,

    Al Williams
    AWC
    http://www.awce.com





    Original Message
    From: vilet_bounnam [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=q5R7DUK-A5tIaC2cENJYgRZV6_qc3FnWzfJRgliQsLjICyJdQeqeXgHX45dX8oo_KRUiwX6tlk0Rk5viDGh-]vilet_bounnam@y...[/url
    Sent: Sunday, May 23, 2004 9:04 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] natural log function


    Hi, I want to convert a voltage to a temperature and the equation I'm using
    is -50*LN(voltage)+198

    Can anyone help me with the code to do this equation on the stamp2 since the
    stamp can only do integer arithmetic I'm having a hard time.

    Thanks




    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.

    Yahoo! Groups Links
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-24 21:15
    How granular do you need to be? You could just encode it in ram and use a
    lookup table.

    On Mon, 24 May 2004, vilet_bounnam wrote:

    > Hi, I want to convert a voltage to a temperature and the equation I'm
    > using is -50*LN(voltage)+198
    >
    > Can anyone help me with the code to do this equation on the stamp2
    > since the stamp can only do integer arithmetic I'm having a hard time.
    >
    > Thanks
    >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and Body of the message will be ignored.
    >
    > Yahoo! Groups Links
    >
    >
    >
    >
    >
    >

    Sean T. Lamont, CTO / Chief NetNerd, Abstract Software, Inc. (ServNet)
    Seattle - Bellingham - Vancouver - Portland - Everett - Tacoma - Bremerton
    email: lamont@a... WWW: http://www.serv.net
    "Do not fear mistakes, There Are None" - Miles Davis
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-24 21:41
    I'm actually doing the table now with 32 points. The problem is that
    I'm inputing 8 channels from the max186 and currently I have 5 tables
    and am using up 98% of the EEPROM memory on the stamp.


    --- In basicstamps@yahoogroups.com, "Sean T. Lamont .lost."
    <lamont@a...> wrote:
    >
    > How granular do you need to be? You could just encode it in ram and
    use a
    > lookup table.
    >
    > On Mon, 24 May 2004, vilet_bounnam wrote:
    >
    > > Hi, I want to convert a voltage to a temperature and the equation I'm
    > > using is -50*LN(voltage)+198
    > >
    > > Can anyone help me with the code to do this equation on the stamp2
    > > since the stamp can only do integer arithmetic I'm having a hard time.
    > >
    > > Thanks
    > >
    > >
    > >
    > >
    > > To UNSUBSCRIBE, just send mail to:
    > > basicstamps-unsubscribe@yahoogroups.com
    > > from the same email address that you subscribed. Text in the
    Subject and Body of the message will be ignored.
    > >
    > > Yahoo! Groups Links
    > >
    > >
    > >
    > >
    > >
    > >
    >
    > Sean T. Lamont, CTO / Chief NetNerd, Abstract Software, Inc. (ServNet)
    > Seattle - Bellingham - Vancouver - Portland - Everett - Tacoma -
    Bremerton
    > email: lamont@a... WWW: http://www.serv.net
    > "Do not fear mistakes, There Are None" - Miles Davis
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-26 16:40
    >Can anyone help me with the (max186) code to do this equation on the stamp2
    > since the stamp can only do integer arithmetic I'm having a hard time.
    >-50*LN(voltage)+198

    If voltage is less than 5, the temperature comes out at greater than
    117. Is that equation right, you are working at high temperatures?

    The max186 will give an answer in millivolts, so the equation becomes,
    temperature = -50 * LN(millivolts/1000) + 198
    = -50 * (LN(millivolts) - LN(1000)) + 198
    = -50 * (LN(millivolts) - 6.91) + 198
    = -50 * LN(millivolts) + 544
    = -35 * LG(millivolts) + 544
    The last step converts from natural log to binary (base 2) log.
    LN(x) = 0.69315 * LG(x). All that serves to make integer values that
    the Stamp can deal with. From there you have a number of
    possibilities, including direct calculation of log base 2:
    http://www.emesystems.com/BS2math3.htm#Logcalc


    All of the compact methods, including the bitlog function and the
    table lookups, rely on the "periodic" characteristics of the
    logarithm function (as seen on log graph paper). You can separate the
    characteristic from the mantissa. The characteristic is the integer
    part of the logarithm, and the Stamp has a built-in function "NCD" to
    calculate that.


    -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-26 18:10
    Yes I am working with High temperatures, the readings will be from a
    car. I did come across your webpage Tracy and played around with
    your Log function but how do I deal with the -50 or -35 that I have
    to multiply before the LN function?

    -Thanks

    --- In basicstamps@yahoogroups.com, Tracy Allen <tracy@e...> wrote:
    > >Can anyone help me with the (max186) code to do this equation on
    the stamp2
    > > since the stamp can only do integer arithmetic I'm having a hard
    time.
    > >-50*LN(voltage)+198
    >
    > If voltage is less than 5, the temperature comes out at greater
    than
    > 117. Is that equation right, you are working at high temperatures?
    >
    > The max186 will give an answer in millivolts, so the equation
    becomes,
    > temperature = -50 * LN(millivolts/1000) + 198
    > = -50 * (LN(millivolts) - LN(1000)) + 198
    > = -50 * (LN(millivolts) - 6.91) + 198
    > = -50 * LN(millivolts) + 544
    > = -35 * LG(millivolts) + 544
    > The last step converts from natural log to binary (base 2) log.
    > LN(x) = 0.69315 * LG(x). All that serves to make integer values
    that
    > the Stamp can deal with. From there you have a number of
    > possibilities, including direct calculation of log base 2:
    > http://www.emesystems.com/BS2math3.htm#Logcalc
    >
    >
    > All of the compact methods, including the bitlog function and the
    > table lookups, rely on the "periodic" characteristics of the
    > logarithm function (as seen on log graph paper). You can separate
    the
    > characteristic from the mantissa. The characteristic is the
    integer
    > part of the logarithm, and the Stamp has a built-in function "NCD"
    to
    > calculate that.
    >
    >
    > -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-26 19:01
    Multiplication by negative numbers works fine in twos complement
    arithmetic. I.e.,
    y = -35 * x + 544

    is a perfectly good statement for the Stamp, so long as x is in the
    range of +/-933 so that the product falls in the range of +/- 32678.
    Or you can just rewrite it as
    y = 544 - (35 * x)

    -- Tracy



    >Yes I am working with High temperatures, the readings will be from a
    >car. I did come across your webpage Tracy and played around with
    >your Log function but how do I deal with the -50 or -35 that I have
    >to multiply before the LN function?
    >
    >-Thanks
    >
    >--- In basicstamps@yahoogroups.com, Tracy Allen <tracy@e...> wrote:
    >> >Can anyone help me with the (max186) code to do this equation on
    >the stamp2
    >> > since the stamp can only do integer arithmetic I'm having a hard
    >time.
    >> >-50*LN(voltage)+198
    >>
    >> If voltage is less than 5, the temperature comes out at greater
    >than
    >> 117. Is that equation right, you are working at high temperatures?
    >>
    >> The max186 will give an answer in millivolts, so the equation
    >becomes,
    >> temperature = -50 * LN(millivolts/1000) + 198
    >> = -50 * (LN(millivolts) - LN(1000)) + 198
    >> = -50 * (LN(millivolts) - 6.91) + 198
    >> = -50 * LN(millivolts) + 544
    >> = -35 * LG(millivolts) + 544
    >> The last step converts from natural log to binary (base 2) log.
    >> LN(x) = 0.69315 * LG(x). All that serves to make integer values
    >that
    >> the Stamp can deal with. From there you have a number of
    >> possibilities, including direct calculation of log base 2:
    >> http://www.emesystems.com/BS2math3.htm#Logcalc
    >>
    >>
    >> All of the compact methods, including the bitlog function and the
    >> table lookups, rely on the "periodic" characteristics of the
    >> logarithm function (as seen on log graph paper). You can separate
    >the
    >> characteristic from the mantissa. The characteristic is the
    >integer
    >> part of the logarithm, and the Stamp has a built-in function "NCD"
    >to
    >> calculate that.
    >>
    >>
    >
    > > -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-28 19:47
    I realize I'm a little late in replying to this, and you've already received some great answers
    from others, but if you're still considering alternatives I thought I should point out how
    easy this is with the Floating Point Coprocessor that Parallax recently announced (see
    Message #44641).

    The constants -50 and 198 would be loaded in your setup routines. The conversion loop
    would then look like the following:

    fA = Result 'load the 12-bit voltage reading to umFPU
    fLow = voltage ' and converts to floating point
    GOSUB Load_FloatWord
    GOSUB Fset

    GOSUB Flog 'calculate natural logarithm

    fB = ConstantM50 'multiply by -50
    GOSUB Fmultiply

    fB = Constant198 'add 198
    GOSUB Fadd

    GOSUB Ffix 'convert to integer
    GOSUB Lget ' and get result

    It's another alternative to consider.

    Regards,
    Cam


    --- In basicstamps@yahoogroups.com, "vilet_bounnam" <vilet_bounnam@y...> wrote:
    > Hi, I want to convert a voltage to a temperature and the equation I'm
    > using is -50*LN(voltage)+198
    >
    > Can anyone help me with the code to do this equation on the stamp2
    > since the stamp can only do integer arithmetic I'm having a hard time.
    >
    > Thanks
Sign In or Register to comment.