Converting a DEBUG DEC to a numerical value
worleybird
Posts: 5
I purchased a Memsic 2125 Dual-Axis Accelerometer and Im having a programming problem on the BS2e. The accelerometer works fine but Im 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
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
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.
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.
Jeff T.
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
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:
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.