Shop OBEX P1 Docs P2 Docs Learn Events
Decimal conversion — Parallax Forums

Decimal conversion

Commander_BobCommander_Bob Posts: 17
edited 2007-04-16 04:14 in BASIC Stamp
I need to calculate the speed of sound using the Ds1620 digital thermometer. The formula is [font=ComicSansMS,Bold size=2]

Speed of sound = [font=ComicSansMS,Bold size=2]331.5 [/font]+([font=ComicSansMS,Bold size=2]0.6[/font]

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-15 22:35
    You can't enter it into a Stamp because the Stamp does all arithmetic with 16-bit integers where this formula uses floating point. Tracy Allen's website has some good information on fixed point and multiple precision arithmetic (www.emesystems.com). One of the things you can do is to convert these values to fixed point with one decimal point like "speed of sound = 3315 + (06 * TC / 10)" where TC is in tenths of a degree. The extra factor of 10 compensates for the fact that multiplying tenths by tenths gives hundreths. The resulting speed of sound is in tenths of a meter/second.
  • Commander_BobCommander_Bob Posts: 17
    edited 2007-04-15 23:02
    I just did
    Speed of sound = ( 3315 + ( 6 * degc) / 10)
    and it worked.
    Also how did they do it in the ping demo program? They have
    Scale CON $200 ' raw x 2.00 = uS
    and
    rawDist = rawDist */ Scale
    How does that work?
  • Commander_BobCommander_Bob Posts: 17
    edited 2007-04-15 23:04
    opps I ment when they use
    Scale CON $0CD ' raw x 0.80 = uS
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-15 23:11
    Have a look at the "*/" operator description in the PBasic manual. Basically, it scales the data by 256. Notice that the constant "Scale" is 2 x 256.
  • Commander_BobCommander_Bob Posts: 17
    edited 2007-04-15 23:43
    I have another problem. I have the speed of sound in M/S but I need it in CM/uS and when I convert it I need to keep the decmal for best accuracy. I am getting answers like 28 us / CM but in the demo program they use 29.034us/cm and that 0.034 is probably a lot of difference. What formula could I use to get the distance with the Ping)))?
    uS for return * what?
    Thanks
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-16 00:01
    Do have a look at the EMEsystems website, particularly this page: www.emesystems.com/BS2math1.htm.

    To get cm from meters, you need to multiply by 100. To get us from seconds, you need to multiply by 1,000,000.
    To get cm/us from ms/s, that means multiplying by 100/1000000 = 1/10000 or dividing by 10,000. In doing this
    on the Stamp while maintaining the maximum accuracy, you need to work this in from the beginning of the calculation,
    not just use a single formula. It will help you to write down all the steps, like a "pseudo-program". Read the PING
    documentation and the Nuts and Volts articles on it. The "Smart Sensors" tutorial has a whole chapter on using the PING
    and working with the speed of sound. Have a look: www.parallax.com/dl/docs/prod/sic/SmartSensors-v1.0.pdf.
  • Commander_BobCommander_Bob Posts: 17
    edited 2007-04-16 01:04
    Thanks I was looking for a sight like that. I think I will be able to get it to work.
  • Commander_BobCommander_Bob Posts: 17
    edited 2007-04-16 01:27
    I understand how to multiply by fractions now but is there a way to store my result in a fraction? Would multiplying everything by 256 and using the */ work?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-16 01:35
    The issue is that you have only a 16-bit word to store your values in. The range is 00000 to 65535 or (if signed) -32768 to 32767. If you multiply everything by 256, that leaves a range of 000 to 255 plus a fraction or -128 to 127 plus a fraction. That may work fine, but it may not give you enough precision for what you're doing. The important thing is to figure out the appropriate precision for each value in your program and program fo that.
  • Commander_BobCommander_Bob Posts: 17
    edited 2007-04-16 04:14
    I finally did it. After an entire day of work (well sorta I played with my new 3 axis accelerometer a lot) I did it. This is a mod of the demo program that uses the temperature to convert to speed of sound and then uses the ping))) to mesure distance. I tested it with a tape mesure and it was dead on each CM.
    Thanks for all your help

    ' =========================================================================
    '
    ' File....... Ping_Demo.BS2
    ' Purpose.... Demo Code for Parallax Ping Sonar Sensor
    ' Author..... Jon Williams -- Parallax, Inc.
    ' E-mail..... jwilliams@parallax.com
    ' Started....
    ' Updated.... 08 JUN 2005
    '
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    ' =========================================================================


    '
    [noparse][[/noparse] Program Description ]
    '
    ' This program demonstrates the use of the Parallax Ping Sonar sensor and
    ' converting the raw measurement to English (inches) and Metric (cm) units.
    '
    ' Sonar Math:
    '
    ' At sea level sound travels through air at 1130 feet per second. This
    ' equates to 1 inch in 73.746 uS, or 1 cm in 29.034 uS).
    '
    ' Since the Ping sensor measures the time required for the sound wave to
    ' travel from the sensor and back. The result -- after conversion to
    ' microseconds for the BASIC Stamp module in use -- is divided by two to
    ' remove the return portion of the echo pulse. The final raw result is
    ' the duration from the front of the sensor to the target in microseconds.


    '
    [noparse][[/noparse] Revision History ]


    '
    [noparse][[/noparse] I/O Definitions ]

    Ping PIN 15


    '
    [noparse][[/noparse] Constants ]

    #SELECT $STAMP
    #CASE BS2, BS2E
    Trigger CON 5 ' trigger pulse = 10 uS
    Scale CON $200 ' raw x 2.00 = uS
    #CASE BS2SX, BS2P, BS2PX
    Trigger CON 13
    Scale CON $0CD ' raw x 0.80 = uS
    #CASE BS2PE
    Trigger CON 5
    Scale CON $1E1 ' raw x 1.88 = uS
    #ENDSELECT



    IsHigh CON 1 ' for PULSOUT
    IsLow CON 0


    '
    [noparse][[/noparse] Variables ]

    rawDist VAR Word ' raw measurement
    inches VAR Word
    cm VAR Word
    rawToCM VAR Word


    '
    [noparse][[/noparse] EEPROM Data ]


    '
    [noparse][[/noparse] Initialization ]

    Reset:
    DEBUG CLS, ' setup report screen
    "Parallax Ping Sonar ", CR,
    "=====================", CR,
    CR,
    "Time (uS)..... ", CR,
    "Centimeters... "


    x VAR Byte ' General purpose variable, byte.
    degC VAR Byte ' Variable to hold degrees Celsius.
    SpdSnd VAR Word 'Speed of sound.
    '========================================================================================
    'Constants
    '========================================================================================
    SndCvn CON 6 'Speed of sound converter.
    SpdSn0C CON 3315 'Speed of sound at 0 C.
    '========================================================================================
    'Initializing
    '========================================================================================
    ' Note: DS1620 has been preprogrammed for mode 2.
    OUTS=%0000000000000000 ' Define the initial state of all pins,
    'FEDCBA9876543210
    DIRS=%1111111111111111 ' as low outputs.
    FREQOUT 0, 20, 3800 ' Beep to signal that it is running.
    HIGH 6 ' Select the DS1620.
    SHIFTOUT 8, 7, LSBFIRST, [noparse][[/noparse]238] ' Send the "start conversions" command.
    LOW 6 ' Do the command.
    '========================================================================================
    'Main
    '========================================================================================
    DO ' Going to display once per second.
    HIGH 6 ' Select the DS1620.
    SHIFTOUT 8, 7, LSBFIRST, [noparse][[/noparse]170] ' Send the "get data" command.
    SHIFTIN 8, 7, LSBPRE, [noparse][[/noparse]x] ' Get the data.
    LOW 6 ' End the command.
    degC = x / 2 ' Convert the data to degrees C.
    SpdSnd = (3315 + (6 * degC) / 10 )
    rawtocm = spdsnd */ 1678
    GOSUB Get_Sonar ' get sensor valu
    cm = rawDist ** RawToCm ' convert to centimeters

    DEBUG CRSRXY, 15, 3, ' update report screen
    DEC rawDist, CLREOL,
    CRSRXY, 15, 4,
    DEC cm, CLREOL


    PAUSE 100
    LOOP
    END


    '
    [noparse][[/noparse] Subroutines ]

    ' This subroutine triggers the Ping sonar sensor and measures
    ' the echo pulse. The raw value from the sensor is converted to
    ' microseconds based on the Stamp module in use. This value is
    ' divided by two to remove the return trip -- the result value is
    ' the distance from the sensor to the target in microseconds.

    Get_Sonar:
    Ping = IsLow ' make trigger 0-1-0
    PULSOUT Ping, Trigger ' activate sensor
    PULSIN Ping, IsHigh, rawDist ' measure echo pulse
    rawDist = rawDist */ Scale ' convert to uS
    rawDist = rawDist / 2 ' remove return trip
    RETURN
Sign In or Register to comment.