Interfacing a BS2p to an ADXL210 accelerometer
cwolff
Posts: 24
Hello,
I recently picked up a ADXL210 and interfaced it to my BS2p.· I'm getting some output from the accelerometer but it's not clear to me what it means and how to turn the output into something understandable.· I have the following code cobbled together from various googled sources:
FREQ··········· VAR···· Word··················· 'variable declarations
T1x············ VAR···· Word··················· 'T1 X channel
T1y············ VAR···· Word··················· 'T1 Y channel
T2············· VAR···· Word··················· 'PWM period
COUNT 4,500,FREQ······························· 'COUNT how many cycles in 500 ms
T2=25000/(FREQ/20)····························· 'T2 is the period in µs
PULSIN 10,1,T1y································· 'READ T1y
T1y=2*T1y······································ 'convert TO µs
PULSIN 11,1,T1x································· 'READ T1x
T1x=2*T1x······································ 'convert TO µ
·DEBUG DEC5 T1x, ",", "y",DEC5 T1y
The results come out something like this;
x11692,y11024
x11684,y11024
x11688,y11026
The good news is that the values change when i move the accelerometer around.· The bad news is that i don't understand how to interpret the values [noparse]:)[/noparse]
My application is to log changes over time.· Any assistance, code snippets, or other godly advice would be greatly appreciated!!!!
Regards,
Christopher
·
I recently picked up a ADXL210 and interfaced it to my BS2p.· I'm getting some output from the accelerometer but it's not clear to me what it means and how to turn the output into something understandable.· I have the following code cobbled together from various googled sources:
FREQ··········· VAR···· Word··················· 'variable declarations
T1x············ VAR···· Word··················· 'T1 X channel
T1y············ VAR···· Word··················· 'T1 Y channel
T2············· VAR···· Word··················· 'PWM period
COUNT 4,500,FREQ······························· 'COUNT how many cycles in 500 ms
T2=25000/(FREQ/20)····························· 'T2 is the period in µs
PULSIN 10,1,T1y································· 'READ T1y
T1y=2*T1y······································ 'convert TO µs
PULSIN 11,1,T1x································· 'READ T1x
T1x=2*T1x······································ 'convert TO µ
·DEBUG DEC5 T1x, ",", "y",DEC5 T1y
The results come out something like this;
x11692,y11024
x11684,y11024
x11688,y11026
The good news is that the values change when i move the accelerometer around.· The bad news is that i don't understand how to interpret the values [noparse]:)[/noparse]
My application is to log changes over time.· Any assistance, code snippets, or other godly advice would be greatly appreciated!!!!
Regards,
Christopher
·
Comments
As a start....
If your acclerometer is set up to measure gravity, you can tilt it on the measuring axis and get a "full scale" reading (2g if that is the one you have), put it on a 45 deg angle and get a value representative of 1g (close) and get a value representative of 0g when flat.
My brief look at the device also indicates you can configure the device for a range of pulsewidth outputs.
Do you know what that pulsewidth is at 2g and 0g?
I interfaced the ADXL10 to a BS2...don't imagine much difference in coding to a BS2P (baud changes...etc..).
A Lot of my code was gleaned from what JonW. had done previously.
You'll notice a lot of debug lines that are remarked out (with the ' ).· I use them to help sort out what's going on and just left them in there.
I start by taking in the pulse reading from the sensor and averaging it based on how many samples I've gotten since the last reading.
I also start looking for the max and min values from the "reseting" point.· I believe it throws out 128 (or so) for a 'flat' reading and the number goes up when tilted one way and goes down when tilted the other way.
I then deseminate the peak value from the resting point.
We've got a separate PC running some custom software for monitoring.· I serout in to the PC with the Xavg, Yavg, Xpk, Ypk, 0 values (the 0 lets the PC software know that my stream is done).
I tried to document those code as much as possible without getting too wordy and I just noticed I've left a couple of variables in there that I didn't use ('bump')....but it does work for what we wanted (vibration/shudder of mechanical devices).
I could set up a time reference and use the sensor to determine velocity/accleration too.· Another day!
sb
here's my code:
'===========================================================
'
'File......accelerometer_text.bs2
'Purpose...ADXL210AE test program
'Author....S.Brady
[url=mailto:'E-mail....steven.brady@ec.gc.ca]'E-mail....steven.brady@ec.gc.ca[/url]
'Started...March 18, 2004
'Updated...March 29, 2004
'
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'===========================================================
'
'Program Description
'
'
' Read Pulse from Pulse Generator
' 5V level; 0.5mS Period
' Square wave
' Rset on Accel Eval Board is 68.2Kohms to set period to 0.5mS
' Using R22K on pin inputs
'
' Pinouts on Accel Eval Board
' A = Vdd· (+3 to +5.5Vdc)
' B = ST·· (Self Test input -- apply 5V for 3% deflection on readings)
' C = Yout (Y axis Pulse out)
' D = Xout (X axis Pulse out)
' E = COM· (Common/Ground)
'
' Wiring to stamp
' A connected Vdd
' B connected to Vss (Common/Ground)
' C connected to P1
' D connected to P0
' E connected to Vss
'
'
' I/O Definitions
'
Xin· PIN 0
Yin· PIN 1
'
'Constants
'
HiPulse·· CON 1
LoPulse·· CON 0
Degrees·· CON 176
BAUD····· CON 17197
'BAUD····· CON 813
seroutpin CON 16
samples·· CON 30······· 'Averaged sample count
xcenter·· CON 125······ 'Returned Value for 50%D.C.
ycenter·· CON 125
'
'Variables
'
XinRaw· VAR Word······· 'Var for X Pulsin
YinRaw· VAR Word······· 'Var for Y Pulsin
Xavg··· VAR Word······· 'Calculated Var for X over sample count
Yavg··· VAR Word······· 'Calculated Var for Y over sample count
Xmax··· VAR Byte······· 'Calculated Var for X max over sample count
Xmin··· VAR Byte······· 'Calculated Var for X min over sample count
Ymax··· VAR Byte······· 'Calculated Var for Y max over sample count
Ymin··· VAR Byte······· 'Calculated Var for Y min over sample count
xpk···· VAR Byte······· 'X peak value over sample count
ypk···· VAR Byte······· 'Y peak value over sample count
z1····· VAR Byte
z2····· VAR Byte
cnt···· VAR Byte
bump1·· VAR Byte
bump2·· VAR Byte
'
'Initialization
'
Xavg = 0
Yavg = 0
Xmax = 1
Ymax = 1
cnt = 0
'
'Program Code
'
MAIN:
DO
· GOSUB pulse
· GOSUB average
· GOSUB Xpeak
· GOSUB Ypeak
· GOSUB dataout
LOOP
END
'
'Subroutines
'
init:·························· 'Routine to re-init vars
··· Xavg = 0
··· Yavg = 0
··· Xpk = 1
··· Ypk = 1
··· Xmax = 1
··· Ymax = 1
··· xmin = 250
··· ymin = 250
··· cnt = 0
··· z1 = 0
··· z2 = 0
··· bump1 = 0
··· bump2 = 0
'· PAUSE 2000
RETURN
pulse:························· 'Routine to read-in pulses
'· DEBUG "pulse",CR
··· PULSIN Xin,HiPulse,XinRaw·· 'Read in X-axis pulse
··· PULSIN Yin,HiPulse,YinRaw·· 'Read in Y-axis pulse
RETURN
average:······················· 'Routine to determine average of readings in sample period
'· DEBUG "average",CR
··· cnt = cnt + 1·············· 'start a counter for samples number
··· Xavg = Xavg + XinRaw······· 'add each new value of Xin to previous value
··· Yavg = Yavg + YinRaw······· 'add each new value of Yin to previous value
· IF (cnt = samples) THEN······ 'when sample count is reached.....
··· Xavg = Xavg / samples······ 'divide total X readings by sample count
··· Yavg = Yavg / samples······ 'divide total Y readings by sample count
··· Xavg = Xavg MAX 250········ 'don't let Xavg be more than 250
··· Xavg = Xavg MIN 1·········· 'don't let Xavg be less than 1
··· Yavg = Yavg MAX 250········ 'don't let Yavg be more than 250
··· Yavg = Yavg MIN 1·········· 'don't let Yavg be less than 1
· ENDIF
RETURN
Xpeak:·························· 'Routine to determine Xpeak value
'· DEBUG "Xpeak",CR
··· Xmax = XinRaw MIN Xmax······ 'determine max peak value
··· xmin = XinRaw MAX xmin······ 'determine min peak value
··· IF (Xmax > xcenter) THEN···· '2 IF's to find out PEAK differences from Xcenter
'······ DEBUG "1",CR
······· z1 = Xmax - xcenter
···· ELSE
'······ DEBUG "2",CR
······· z1 = xcenter - Xmax
···· ENDIF
··· IF (xmin > xcenter) THEN
'······ DEBUG "3",CR
······· z2 = xmin - xcenter
···· ELSE
'······ DEBUG "4",CR
······· z2 = xcenter - xmin
···· ENDIF
'··· DEBUG DEC z1,",",DEC z2,CR
·· z1 = z1 MIN z2·············· 'Make z1 the bigger of the two values
·· Xpk = z1 MIN 1·············· 'Don't let Xpk be less than 1
··· IF (Xmax > xcenter) THEN
····· Xpk = Xpk + xcenter
··· ELSE
····· Xpk = xcenter - Xpk
··· ENDIF
RETURN
Ypeak:·························· 'Routine to determine Y peak value
'· DEBUG "Ypeak",CR
··· z1 = 0
··· z2 = 0
··· Ymax = YinRaw MIN Ymax······ 'determine max peak value
··· Ymin = YinRaw MAX Ymin······ 'determine min peak value
··· IF (Ymax > ycenter) THEN···· '2 IF's to find out PEAK differences from Xcenter
'······ DEBUG "1",CR
······· z1 = Ymax - ycenter
···· ELSE
'······ DEBUG "2",CR
······· z1 = ycenter - Ymax
··· ENDIF
··· IF (ymin > ycenter) THEN
'······ DEBUG "3",CR
······· z2 = ymin - ycenter
···· ELSE
'······ DEBUG "4",CR
······· z2 = ycenter - ymin
··· ENDIF
··· 'DEBUG DEC z1,",",DEC z2,CR
··· z1 = z1 MIN z2·············· 'Make z1 the bigger of the two values
··· Ypk = z1 MIN 1·············· 'Don't let Ypk be less than 1
··· IF (Ymax > ycenter) THEN
····· Ypk = Ypk + ycenter
··· ELSE
····· Ypk = ycenter - Ypk
··· ENDIF
RETURN
dataout:
··· IF (cnt = samples) THEN
···· SEROUT seroutpin,BAUD, [noparse][[/noparse]Xavg,Yavg,Xpk,Ypk,0]
'··· DEBUG DEC xavg,",",DEC yavg,",",DEC xpk,",",DEC ypk,CR
'··· DEBUG HEX xavg,",",HEX yavg,",",HEX xpk,",",HEX ypk,CR
···· GOSUB init
··· ENDIF
· RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·
Steve
http://members.rogers.com/steve.brady
http://www.geocities.com/paulsopenstage
"Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
I'm going to drop this code in and see what happens [noparse]:)[/noparse]· I'll keep you guys posted!· Thank you!!!
Hex out.· If you change the serout string to:
SEROUT seroutpin,BAUD, [noparse][[/noparse]dec Xavg, dec Yavg, dec Xpk, dec Ypk, 0]
This will at least send out "readable" data to a pc (hyperterm/procomm/etc..).
I just went over the program earlier this week for an update and couldn't figure out what was wrong with my out put stream...it was hex!
and you can see in the 'program description' what I've done for setting up the adxl itself.
BTW: we did pick up the evaluation board kit.· I also picked up a spare sensor to realize that it's a surface mount device...so, it's a good thing I got the eval board!
I'm planning on putting one of these in my Jeep for when I'm offroading.· You see some guys out there with "tilt indicators" (you see the same thing on ships -- YAW?)...anyhow, wanted to use one of these and include some GPS, temps (engine and environment) and use a multi menu system....all down the road!·
good luck!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·
Steve
http://members.rogers.com/steve.brady
http://www.geocities.com/paulsopenstage
"Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
I pulled out all the comments, ran your code and get the following results;· Any suggestions on how to interpret this data are greatly appreciated [noparse]:)[/noparse]
pulse
average
Xpeak
1
3
71,125
Ypeak
1
3
13,125
pulse
average
Xpeak
1
3
70,125
Ypeak
1
3
15,125
pulse
average
Xpeak
1
3
73,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
72,125
Ypeak
1
3
15,125
pulse
average
Xpeak
1
3
72,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
70,125
Ypeak
1
3
17,125
pulse
average
Xpeak
1
3
73,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
72,125
Ypeak
1
3
13,125
pulse
average
Xpeak
1
3
72,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
73,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
71,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
71,125
Ypeak
1
3
16,125
pulse
average
Xpeak
1
3
70,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
72,125
Ypeak
1
3
12,125
pulse
average
Xpeak
1
3
71,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
71,125
Ypeak
1
3
18,125
pulse
average
Xpeak
1
3
69,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
69,125
Ypeak
1
3
15,125
pulse
average
Xpeak
1
3
72,125
Ypeak
1
3
17,125
pulse
average
Xpeak
1
3
73,125
Ypeak
1
3
15,125
pulse
average
Xpeak
1
3
72,125
Ypeak
1
3
15,125
pulse
average
Xpeak
1
3
71,125
Ypeak
1
3
15,125
pulse
average
Xpeak
1
3
73,125
Ypeak
1
3
15,125
pulse
average
Xpeak
1
3
73,125
Ypeak
1
3
16,125
pulse
average
Xpeak
1
3
70,125
Ypeak
1
3
16,125
pulse
average
Xpeak
1
3
73,125
Ypeak
1
3
13,125
pulse
average
Xpeak
1
3
72,125
Ypeak
1
3
13,125
pulse
average
Xpeak
1
3
71,125
Ypeak
1
3
16,125
pulse
average
Xpeak
1
3
74,125
Ypeak
1
3
14,125
pulse
average
Xpeak
1
3
73,125
Ypeak
1
3
14,125
àüàüàüàü�250,250,250,250
FA,FA,FA,FA
So if i use the code below and get accx = 5837 and accy = 5518, that means the pulsewidth i'm getting off of the accelerometer is 5837 us and 5518 us accordingly?
If I rotate the accelerometer 90 degrees clockwise and counterclockwise on its Y axis i get the values of 5039 and 5969, you could probably round those to 5000 and 6000.· If I do the same on the X axis I get 5389 and 6300.· So there 'seems' to be 1000 us·possible delta·between 90 degree position of the sensor.
Therefore,·you could extrapolate that 1000 us into 11.111 per degree inclination on either axis.·· Is my logic off?· My other question is how does this pulsewidth information translate into +/- G?· Thank you!!!
Christopher
' {$STAMP BS2p}
accx· VAR· Word
accy· VAR· Word
loop:
· PULSIN 6, 1, accx··· 'read high state on pin 6
· DEBUG HOME, ? accx
· PULSIN 7, 1, accy··· 'read high state on pin 7
· DEBUG ? accy
· PAUSE 100
GOTO loop
I found the following information that differentiates a BS2p from the BS2...how does this information change the assumptions in your code?
Quick Facts
COUNT, 4, 500, FREQ line my BS2p should be more like
COUNT, 4, 3484, FREQ.
This produced a FREQ of 117, when applied to T2=25000/(Freq/20) that gives me a· PWM period of 5000us.
According to the Analog ADXL210 datasheet X-axis acceleration is calculated by (25xT1x)/T2.· So if my X-axis is reading 5840 that gives me·29.2G.· Bah that doesn't sound right if my desk is accelerating at 29.2G we have major problems
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
(I don't like that it keeps double spacing all my sentences....anyhow...)
the pulse/average/Xpeak/1/3/etc....· all those words are just to let me know that it made it to a certain function.
same goes for the Ypeak/1/3 part too!
It's the numbers separated by comma's that are the values.
Tell ya what....there's WAY too much in my program....you just need to get going and then do what you want with your data from there!
So here's my program pared down a bit.
::
'
' I/O Definitions
'
Xin· PIN 0
Yin· PIN 1
'
'Constants
'
HiPulse·· CON 1
LoPulse·· CON 0
BAUD····· CON 17197
seroutpin CON 16
samples·· CON 30······· 'Averaged sample count
xcenter·· CON 125······ 'Returned Value for 50%D.C.
ycenter·· CON 125
'
'Variables
'
XinRaw· VAR Word······· 'Var for X Pulsin
YinRaw· VAR Word······· 'Var for Y Pulsin
'
'Program Code
'
MAIN:
DO
· GOSUB pulse
· GOSUB dataout
LOOP
END
'
'Subroutines
'
pulse:························· 'Routine to read-in pulses
'· DEBUG "pulse",CR
··· PULSIN Xin,HiPulse,XinRaw·· 'Read in X-axis pulse
··· PULSIN Yin,HiPulse,YinRaw·· 'Read in Y-axis pulse
RETURN
dataout:
·· debug dec XinRaw, CR
·· debug dec YiinRaw, CR
RETURN
Ok, so from this you'll get your basic values up in your debug window.·
Set your sensor on a flat surface and see what your readings are.
Tilt your sensor to find out which is the X-axis and which is the Y-axis.
I wanted a starting point in mine...and although I don't remember what the "resting" number was when the sensor was flat, I worked it to be 250 full scale.· (taking a quick look....you should get a 50% Pulse when the sensor is flat).· As you tilt one way the number goes up and the number goes down when you go the opposite way!
Start there.
There's a calculation somewhere that tells you what you need to sort out for your pulse duration count and all (the number to expect!).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·
Steve
http://members.rogers.com/steve.brady
http://www.geocities.com/paulsopenstage
"Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
here's an application note off of Analog Devices website.
It may or may not help.
so far as the count difference between the BS2 and BS2p (correct me if I'm wrong Jon), but it'll just mean a different result.
So, you have to toss out the average/peak sections of my original program (sorry, hadn't thought of it beforehand).
You need to know your Raw limits first...then work from there.
Just taking a quick look....your values will be roughly 3.5x higher than mine.· So...where my max count was 250, yours will be about 871 or so.
anyhow..good luck!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·
Steve
http://members.rogers.com/steve.brady
http://www.geocities.com/paulsopenstage
"Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
Thanks for the help.·· The code you supplied produced results that corroborate with the raw values I generated earlier...seems like I'm on the right track!
So now i'm looking at the following values (generated from your code)
5836
5517
5835
5516
5835
5518
5837
5516
5833
5517
5834
5515
5837
5517
5835
5517
This suggests that the ADXL, in its resting position, reads about 5835 on its x-axis on 5517 on its y-axis.
If I tilt the device 90 degrees in either direction along the x axis my limits are 5390 and 6311.· If I tilt along the y axis my limits are 5000 and 5967.· So it seems there is roughly 1000 'units' of delta between 90 degree positions.· The hard part i'm having is what does this information mean in physics terms of acceleration?· I could roughly approximate that each 11 units of delta equals 1 degree of tilt (static acceleration), which makes this a great tilt meter but still leaves me unclear as to measuring G.
According to the datasheet the ADXL measures acceleration according to the ratio of T1/T2.· If the COUNT 6,3484,FREQ·and T2=25000/(FREQ/20) reveals that my T2 is 5000, and my T1/T2 = 5835/5000 at rest and 5390/5000, 5967/5000·at 90 degrees, does that mean that 1.167 = resting and 1.078 at -10G and 1.1934 at +10G?
Thank you for your patience, I'll catch up shortly [noparse]:)[/noparse]
·
I'm sure there's a calculation to figure out speed/velocity/acceleration....
I seem to be answering these at work (sshhh, don't tell anyone) so have limited access to my notes.
If you leave your sensor flat (no tilting) then run it in a straight line along it's X-axis or Y-axis, you should still see numbers change.· You are generating G-forces.· By tilting the sensor you are causing gravity to pull down on one side of the sensor...when the sensor is flat, gravity pulls down evenly and doesn't cause any differences in measurements.·
Just take a gander at that code I attached in the previous message.· It's Analog Devices Application Note 596 (AN-596).
My math skills aren't the greatest....but I THINK what they are doing is..:
Determining how many pulse counts in your selected Pulse width (resistor selected on ADXL210) -- I'm not sure where they get their formula from: T2=25000/(Freq/20)·--just remember this appnote is BS2 related so these #'s would change with yours (I'm sure!).
Just be aware there is an error on that appnote: the COUNT 8, 500,FREQ line is wrong.· There's nothing connected to Pin8...try 5 or 4 (for the x and y axis channels).
Anyhow....ADHD is kickin in!· They count the number of pulses in a 500ms period (the COUNT line above) and then I'm guessing they use this as a time basis as this gets in to the T2=25000/(FREQ/20) line·· -- the value collected from the COUNT line gets dropped in to the FREQ variable.
They then use the acceleration formula of Xaccel= (25 * T1x)/T2;· the datasheet says the ADXL210s' scale factor is 4%/g.· I don't know how these gets worked in to this formula.· The ADXL202s' scale factor is 12.5%/g which, if doubled, works out to the 25....not sure if they got these mixed up...I'm certainly mixed up!
Anyhow, the standard way of determing acceleration would seem a more 'understanding' way for me to do it, regardless of the fact that it's a much bigger PIA!!
sorry for the overly VERBOSE reply....ppl dropping by to chat amid writing this has left me....lost really! haha
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·
Steve
http://members.rogers.com/steve.brady
http://www.geocities.com/paulsopenstage
"Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
just notice in your original post:
If you note in your original program....you are doing this:
T1x=2*T1x········-- which says it's converting to uS.· Well, divide the 11692 in half and you get the values you are getting now (~5846).
So, what period do you have your sensor set to?· 1ms or 2ms??· or a half?
EAch pulse count on your RAW reading is equal to 286uS (BS2P) so that must mean that hmmm...(did I mention I hate math!)...1.66mS....??
not sure if that makes sense....what resistor value you using?· got a scope?·
alright....boss is looking at me funny..bakc to work!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·
Steve
http://members.rogers.com/steve.brady
http://www.geocities.com/paulsopenstage
"Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."