MEMSIC Dual Accel Question
Hi,
I'd like to output just brads to Excel for a marksmanship experiment that I am using the Memsic Dual Accel sensor for. Right now I am playing with the code from the website but it would be very helpful to have just the concise code. Here is what I have now:
' =========================================================================
'
'·· File...... MEMSIC2125-Dual.BS2
'·· Purpose... Memsic 2125 Accelerometer Dual-Axis to Excel
'·· Author.... Jeff Malnick
'·· E-mail.... jpmalnic@nps.edu / malnick@gmail.com
'·· Started...
'·· Updated... 12 FEB 2009
'
'·· {$STAMP BS2}
'·· {$PBASIC 2.5}
'
' =========================================================================
'
[noparse][[/noparse] Program Description ]
'Reads the accelerometer and outputs the x and y tilt axis in degrees
'to an excel spreadsheet running the paralax DAQ active x plugin
'Revised 11 FEB 2009
'
[noparse][[/noparse] I/O Definitions ]
Xin············ PIN···· 14······················ ' X input from Memsic 2125
Yin············ PIN···· 13····················· ' Y input from Memsic 2125
'
[noparse][[/noparse] Constants ]
' Set scale factor for PULSIN
Scale·········· CON···· $200··················· ' 2.0 us per unit
HiPulse········ CON···· 1······················ ' measure high-going pulse
LoPulse········ CON···· 0
DegSym········· CON···· 176···················· ' degrees symbol
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
AvgCount······· VAR···· Word
'AvgRawForce···· VAR···· Word
rawForceSub···· VAR···· Word
'n·············· VAR···· Word
sPin··········· CON···· 16······················· 'Serial Pin - P16, Programming port
Baud··········· CON···· 84······················· 'Baud mode for a rate of 9600, 8-N-1
················································· 'BS2P, BS2SX use 240 for 9600, 8-N-1
Row············ VAR···· Word····················· 'Variable to hold row data
idx············ VAR···· Nib······················ ' table index
mult··········· VAR···· Word····················· ' multiplier - whole part
frac··········· VAR···· Word····················· ' multiplier - fractional part
Main:
PAUSE 1000···································· 'Allow data communications to stabilize
SEROUT sPin,Baud,[noparse][[/noparse]CR]······························ 'Send a lone CR to ensure PLX-DAQ buffer is ready
SEROUT sPin,Baud,[noparse][[/noparse]CR,"LABEL,Time,xTilt,yTilt",CR]·· 'Label 3 columns with TIME, X, and SIN X
SEROUT sPin,Baud,[noparse][[/noparse]"CLEARDATA",CR]·················· 'Clear all data columns (A-J) in Excel
SEROUT sPin,Baud,[noparse][[/noparse]"RESETTIMER",CR]················· 'Reset Timer
DO
··· ' Send String with DATA FOR Excel
···· SEROUT sPin,Baud,[noparse][[/noparse]"DATA,TIME,", SDEC xTilt, ",", SDEC yTilt,CR]
··· ' Request last row of data
···· SEROUT sPin,Baud,[noparse][[/noparse]"ROW,GET",CR]
···· ' Accept returning data and store into Row with 200mS timeout
···· SERIN sPin, Baud,200,TimeOut,[noparse][[/noparse]DEC Row]
···· 'IF Row is OR exceeds 1000, set row back TO 2
···· IF row >= 1000 THEN SEROUT sPin,Baud,[noparse][[/noparse]"ROW,SET,2",CR]
···· Timeout:
···· ' reads G-force and Tilt
···· GOSUB Read_Tilt
···· GOSUB X_Tilt
···· GOSUB Y_Tilt
···· 'DEBUG xTilt, CR
··· ' DEBUG yTilt, CR
LOOP
'
[noparse][[/noparse] Subroutines ]
XTExit:
RETURN
X_Tilt:
··· xTilt = (xTilt.BIT15 * 13 + ABS xTilt)
··· RETURN
Y_Tilt:
··· yTilt = (yTilt.BIT15 * 13 + ABS yTilt)
··· RETURN
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 commented out
· RETURN
Arcsine:
· GOSUB Arccosine
· angle = 90 - angle
· RETURN
**********************************************
As you can see it is very messy, and as it stands I do not get accurate measurements. I simply commented out the conversion for Brads to Degrees in hopes that it would be correct, however my data shows otherwise. When the sensor is flat it reads 37 brads in the x and 39 brads in the y. My excel spreadsheet is set up to convert from Brads to radians to degrees so I don't have to concern myself with the difficulty of doing this on the sensor (I don't have the math co-processor).
All help appreciated.
Thanks
Jeff
I'd like to output just brads to Excel for a marksmanship experiment that I am using the Memsic Dual Accel sensor for. Right now I am playing with the code from the website but it would be very helpful to have just the concise code. Here is what I have now:
' =========================================================================
'
'·· File...... MEMSIC2125-Dual.BS2
'·· Purpose... Memsic 2125 Accelerometer Dual-Axis to Excel
'·· Author.... Jeff Malnick
'·· E-mail.... jpmalnic@nps.edu / malnick@gmail.com
'·· Started...
'·· Updated... 12 FEB 2009
'
'·· {$STAMP BS2}
'·· {$PBASIC 2.5}
'
' =========================================================================
'
[noparse][[/noparse] Program Description ]
'Reads the accelerometer and outputs the x and y tilt axis in degrees
'to an excel spreadsheet running the paralax DAQ active x plugin
'Revised 11 FEB 2009
'
[noparse][[/noparse] I/O Definitions ]
Xin············ PIN···· 14······················ ' X input from Memsic 2125
Yin············ PIN···· 13····················· ' Y input from Memsic 2125
'
[noparse][[/noparse] Constants ]
' Set scale factor for PULSIN
Scale·········· CON···· $200··················· ' 2.0 us per unit
HiPulse········ CON···· 1······················ ' measure high-going pulse
LoPulse········ CON···· 0
DegSym········· CON···· 176···················· ' degrees symbol
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
AvgCount······· VAR···· Word
'AvgRawForce···· VAR···· Word
rawForceSub···· VAR···· Word
'n·············· VAR···· Word
sPin··········· CON···· 16······················· 'Serial Pin - P16, Programming port
Baud··········· CON···· 84······················· 'Baud mode for a rate of 9600, 8-N-1
················································· 'BS2P, BS2SX use 240 for 9600, 8-N-1
Row············ VAR···· Word····················· 'Variable to hold row data
idx············ VAR···· Nib······················ ' table index
mult··········· VAR···· Word····················· ' multiplier - whole part
frac··········· VAR···· Word····················· ' multiplier - fractional part
Main:
PAUSE 1000···································· 'Allow data communications to stabilize
SEROUT sPin,Baud,[noparse][[/noparse]CR]······························ 'Send a lone CR to ensure PLX-DAQ buffer is ready
SEROUT sPin,Baud,[noparse][[/noparse]CR,"LABEL,Time,xTilt,yTilt",CR]·· 'Label 3 columns with TIME, X, and SIN X
SEROUT sPin,Baud,[noparse][[/noparse]"CLEARDATA",CR]·················· 'Clear all data columns (A-J) in Excel
SEROUT sPin,Baud,[noparse][[/noparse]"RESETTIMER",CR]················· 'Reset Timer
DO
··· ' Send String with DATA FOR Excel
···· SEROUT sPin,Baud,[noparse][[/noparse]"DATA,TIME,", SDEC xTilt, ",", SDEC yTilt,CR]
··· ' Request last row of data
···· SEROUT sPin,Baud,[noparse][[/noparse]"ROW,GET",CR]
···· ' Accept returning data and store into Row with 200mS timeout
···· SERIN sPin, Baud,200,TimeOut,[noparse][[/noparse]DEC Row]
···· 'IF Row is OR exceeds 1000, set row back TO 2
···· IF row >= 1000 THEN SEROUT sPin,Baud,[noparse][[/noparse]"ROW,SET,2",CR]
···· Timeout:
···· ' reads G-force and Tilt
···· GOSUB Read_Tilt
···· GOSUB X_Tilt
···· GOSUB Y_Tilt
···· 'DEBUG xTilt, CR
··· ' DEBUG yTilt, CR
LOOP
'
[noparse][[/noparse] Subroutines ]
XTExit:
RETURN
X_Tilt:
··· xTilt = (xTilt.BIT15 * 13 + ABS xTilt)
··· RETURN
Y_Tilt:
··· yTilt = (yTilt.BIT15 * 13 + ABS yTilt)
··· RETURN
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 commented out
· RETURN
Arcsine:
· GOSUB Arccosine
· angle = 90 - angle
· RETURN
**********************************************
As you can see it is very messy, and as it stands I do not get accurate measurements. I simply commented out the conversion for Brads to Degrees in hopes that it would be correct, however my data shows otherwise. When the sensor is flat it reads 37 brads in the x and 39 brads in the y. My excel spreadsheet is set up to convert from Brads to radians to degrees so I don't have to concern myself with the difficulty of doing this on the sensor (I don't have the math co-processor).
All help appreciated.
Thanks
Jeff
Comments
I am presently using the MEMSIC2125 to measure the tilt of the shoulder section of a robotic arm.
I found that sampling the output of the accelerometer for 10 - 20 interations and then taking the average resulted in fairly accurate readings.
I also found that the closer I got to the vertical (90 degrees) the less accurate the reading became. Also repeatability at close to 90 degrees was marginal.
Regards,
TCIII
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If you are going to send·a Robot·to save the world, you·better make sure it likes it the way it is!