Shop OBEX P1 Docs P2 Docs Learn Events
Converting a DEBUG DEC to a numerical value — Parallax Forums

Converting a DEBUG DEC to a numerical value

worleybirdworleybird Posts: 5
edited 2013-03-16 21:44 in BASIC Stamp
I purchased a Memsic 2125 Dual-Axis Accelerometer and I’m having a programming problem on the BS2e. The accelerometer works fine but I’m having trouble converting the DEBUG output to a numerical variable.

This line of code was taken from MEMSIC2125-Dual.BS2 I downloaded from Parallax:
DEBUG "X Tilt.... ", (xTilt.BIT15 * 13 + " "), DEC ABS xTilt, DegSym, CLREOL

The part I want to convert to a numerical value is: (xTilt.BIT15 * 13 + " "), DEC ABS xTilt

I understand that: (xTilt.BIT15 * 13 + " ") gives me a space or – (Minus sign)

How do I convert: (xTilt.BIT15 * 13 + " "), DEC ABS xTilt to a numerical variable that is positive or negative?

Thanks
Worleybird

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-03-16 13:04
    xTilt is already a variable that contains a signed value. The DEBUG statement just converts the signed value to a series of characters for display purposes. The complex part is due to the program needing a space or a minus sign in front of the displayed value while DEC , by itself, normally produces a minus sign if the value is negative and no sign if the value is positive and that's not what the author wanted. What are you trying to do?
  • worleybirdworleybird Posts: 5
    edited 2013-03-16 14:22
    project.jpg
    Next weekend we are going to have WV’s first STEM (Science, Technology, Engineering, and Math) Festival. It is going to be held at Potomac State College. I’m an advocate of Science Fairs, Social Studies Fairs, Math Fairs, anything to motivate our youth. Since I am on the Governor’s STEM council and a manager at the local Walmart my store will have an exhibit at the fair (manned by Associates and/or their dependents).

    Our activity for the children that attend is - Objective: The participant will be able to determine the height of an object if given the distance they are from the base of the object using the Angle of elevation method. Approach: Using a protractor, string, washer, soda straw, and tape the participant will assemble a basic clinometer. They will also be given a tangent chart, worksheet, and the distance to the base of the object.

    Since we are going to be in the gym I am going to have a military drone (12ft) wingspan suspended from the ceiling (lights flashing and all). That should get their attention. Well I should have left well enough alone. One of the kids suggested using a computer to measure the angle, and I of course agreed. My daughters used basic stamps in their projects (8 years ago) so I had about 6 BS2e’s. The kids built the display pictured above. The accelerometer is mounted inside the helicopter, and the helicopter is mounted on a small tri-pod. I don’t know if you can see it or not but the string on the protractor says 10 degrees yet the read out on the display says 13 degrees. I can off-set the output earlier in the program, but the offset isn’t constant, so I feel the quickest solution is to have a look-up table in the program.
    566 x 550 - 88K
  • Mike GreenMike Green Posts: 23,101
    edited 2013-03-16 15:46
    3 degrees isn't much (around 1%) and there's probably a degree or so of slop in the mounting setup as well as the accelerometer maybe not being quite on axis. How about rounding to the nearest 5 degree mark? A lookup table for corrections is also a good idea.
  • worleybirdworleybird Posts: 5
    edited 2013-03-16 16:33
    In some positions the readings are as much as 14 degrees off. The readings are predictable so that is the reason I feel a look-up table is the way to go. I agree it has everything to do with the construction of the display. I still need a way to have a positive or negative number to” look up”.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-03-16 18:14
    You need a LOOKDOWN working with a LOOKUP.

    The lookdown looks for a value from the lookdown table and a match gives it an index for the lookup.

    The index then points to a value in the lookup table that has a value of tilt offset accordingly.

    Here is an example that offsets a single digit positive number by +10, should help you get started.

    When it comes to your own code you may have to use two lookup lookdowns that depend on a positive or negative value as a condition.
    xTilt   VAR    Word
    index  VAR     Byte
    
    DO
    DEBUGIN DEC1 xTilt
    LOOKDOWN  xTilt  , [1, 9, 3, 4, 2, 6, 7, 8, 5], index
    LOOKUP    index , [11, 19, 13, 14, 12, 16, 17, 18, 15], xTilt
    DEBUG DEC xTilt ,CR
    LOOP
    

    Jeff T.
  • worleybirdworleybird Posts: 5
    edited 2013-03-16 19:37
    I still do not know the numerical value of xTilt.

    Below is a listing of the program I am working with.
    About half way down I have added <<<<<<<<<<<<>>>>>>>>>>> at the end of two lines of code. These lines print (DEBUG) the value of xTilt. Is it possible to convert what is "printed" as the value of xTilt to a numerical value? Keep in mind (xTilt.BIT15 * 13 + " ") places a space or - in front of the number. So I need to also know when it is pos or neg.



    ' =========================================================================
    '
    ' File...... MEMSIC2125-Dual.BS2
    ' Purpose... Memsic 2125 Accelerometer Dual-Axis Demo
    ' Author.... (C) 2003-2004 Parallax, Inc -- All Rights Reserved
    ' E-mail.... support@parallax.com
    ' Started...
    ' Updated... 07 SEP 2004
    '
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    ' =========================================================================


    '
    [ Program Description ]
    '
    ' Read the pulse outputs from a Memsic 2125 accelerometer and converts to
    ' G-force and tilt angle.
    '
    ' g = ((t1 / 10 ms) - 0.5) / 12.5%
    '
    ' Tilt = ARCSIN(g)
    '
    ' Refer to Memsic documentation (AN-00MX-007.PDF) for details on g-to-tilt
    ' conversion and considerations.
    '
    ' www.memsic.com


    '
    [ Revision History ]


    '
    [ I/O Definitions ]

    Xin PIN 8 ' X input from Memsic 2125
    Yin PIN 9 ' Y input from Memsic 2125


    '
    [ Constants ]

    ' Set scale factor for PULSIN

    #SELECT $STAMP
    #CASE BS2, BS2E
    Scale CON $200 ' 2.0 us per unit
    #CASE BS2SX
    Scale CON $0CC ' 0.8 us per unit
    #CASE BS2P
    Scale CON $0C0 ' 0.75 us per unit
    #CASE BS2PE
    Scale CON $1E1 ' 1.88 us per unit
    #ENDSELECT

    HiPulse CON 1 ' measure high-going pulse
    LoPulse CON 0

    DegSym CON 176 ' degrees symbol


    '
    [ Variables ]

    xRaw VAR Word ' pulse from Memsic 2125
    xmG VAR Word ' g force (1000ths)
    xTilt VAR Word ' tilt angle

    yRaw VAR Word
    ymG VAR Word
    yTilt VAR Word

    disp VAR Byte ' displacement (0.0 - 0.99)
    angle VAR Byte ' tilt angle


    '
    [ EEPROM Data ]


    '
    [ Initialization ]

    Setup:
    PAUSE 250 ' let DEBUG window open
    DEBUG "Memsic 2125 Accelerometer", CR,
    "
    "


    '
    [ Program Code ]

    Main:
    DO
    GOSUB Read_Tilt ' reads G-force and Tilt

    ' display results

    DEBUG CRSRXY, 0, 3
    DEBUG "X Input... ",
    DEC (xRaw / 1000), ".", DEC3 xRaw, " ms",
    CLREOL, CR,
    "G Force... ", (xmG.BIT15 * 13 + " "),
    DEC (ABS xmG / 1000), ".", DEC3 (ABS xmG), " g",
    CLREOL, CR,
    "X Tilt.... ", (xTilt.BIT15 * 13 + " "), '<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>
    DEC ABS xTilt, DegSym, CLREOL '<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>

    DEBUG CRSRXY, 0, 7
    DEBUG "Y Input... ",
    DEC (yRaw / 1000), ".", DEC3 yRaw, " ms",
    CLREOL, CR,
    "G Force... ", (ymG.BIT15 * 13 + " "),
    DEC (ABS ymG / 1000), ".", DEC3 (ABS ymG), " g",
    CLREOL, CR,
    "Y Tilt.... ", (yTilt.BIT15 * 13 + " "),
    DEC ABS yTilt, DegSym, CLREOL

    PAUSE 200 ' update about 5x/second
    LOOP
    END


    '
    [ Subroutines ]

    Read_G_Force:
    PULSIN Xin, HiPulse, xRaw ' read pulse output
    xRaw = xRaw */ Scale ' convert to uSecs
    xmG = ((xRaw / 10) - 500) * 8 ' calc 1/1000 g
    PULSIN Yin, HiPulse, yRaw
    yRaw = yRaw */ Scale
    ymG = ((yRaw / 10) - 500) * 8
    RETURN


    Read_Tilt:
    GOSUB Read_G_Force
    disp = ABS xmG / 10 MAX 99 ' x displacement
    GOSUB Arcsine
    xTilt = angle * (-2 * xmG.BIT15 + 1) ' fix sign
    disp = ABS ymG / 10 MAX 99 ' y displacement
    GOSUB Arcsine
    yTilt = angle * (-2 * ymG.BIT15 + 1) ' fix sign
    RETURN


    ' Trig routines courtesy Tracy Allen, PhD. (www.emesystems.com)

    Arccosine:
    disp = disp */ 983 / 3 ' normalize input to 127
    angle = 63 - (disp / 2) ' approximate angle
    DO ' find angle
    IF (COS angle <= disp) THEN EXIT
    angle = angle + 1
    LOOP
    angle = angle */ 360 ' convert brads to degrees
    RETURN


    Arcsine:
    GOSUB Arccosine
    angle = 90 - angle
    RETURN
  • SapphireSapphire Posts: 496
    edited 2013-03-16 21:44
    xTilt is a number, the DEC ABS xTilt just converts it to printable characters. What you have to be concerned about is the sign of that number, and that's what xTilt.BIT15 is indicating.

    Using Unsoundcode's idea, you will first determine the sign of xTilt by checking to see if BIT15 is set. If it is, the number is negative. Then use the ABS function to get a positive xTilt that will be used in the LOOKDOWN table.

    Example:
    IF xTilt.BIT15 = 0 THEN        ' positive xTilt
      LOOKDOWN xTilt,[ #, #, #, #, #, #], index  ' get index from xTilt
      LOOKUP index,[ #, #, #, #, #, #], result   ' get result from index
      DEBUG DEC result,CR          ' show positive result
    ELSE                           ' negative xTilt
      LOOKDOWN ABS xTilt,[ #, #, #, #, #, #], index  ' get index from ABS xTilt
      LOOKUP index,[ #, #, #, #, #, #], result       ' get result from index
      DEBUG "-",DEC result,CR      ' show negative result
    ENDIF
    

    where #, #, #, is the list of values that you are going to have in your LOOKDOWN and LOOKUP table. Result will then hold the corrected xTilt value you want. The DEBUG will probably need more formatting for your display, and is just a placeholder as you build the list of values to see the result.
Sign In or Register to comment.