8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
1/16
AN-900
APPLICATION NOTEOneTechnologyWayP.O.Box9106Norwood,MA02062-9106,U.S.A.Tel:781.329.4700Fax:781.461.3113www.analog.com
Enhancing the Performance of Pedometers Using a Single Accelerometerby Jim Scarlett
Rev. 0 | Page 1 of 16
The AN-602 application note examined the use of an Analog
Devices, Inc. accelerometer to make a simple but relatively
accurate pedometer. However, newer devices have been
introduced that allow the use of accelerometers in more cost-
sensitive applications. Thus, applications such as pedometers
are finding themselves in many consumer devices such as
cellular handsets.
Given this trend, a closer examination was made of pedome-
ters using a single accelerometer. The AN-602 technique was
implemented in an attempt to duplicate its results. Though thealgorithm performed well, the same accuracy was not duplicated.
In particular, there was greater variation than expected from
person to person, as well as when one person used a different
pace and stride length. This led to an investigation of potential
improvements to the algorithm used in AN-602.
The tests were done using two different pedometer test boards,
both utilizing an ADuC7020 ARM7 controller. One setup was
a combination of the ADuC7020 microcontroller and ADXL322
accelerometer evaluation boards, with an added 16 2 char-
acter LCD display. The other was a custom board using the
ADuC7020 and an ADXL330 3-axis accelerometer, again with
a 16 2 character LCD display. See Figure 5for the customboard schematic.
AN-602 METHOD
The underlying reasoning in the AN-602 method is based on
the principle that the vertical bounce in an individuals step is
directly correlated to that persons stride length (see Figure 1).
Because the angles and are equal, the stride can be shown to
be a multiple of the maximum vertical displacement. Differences
in an individuals leg length are accounted for, given that for the
same angles the vertical displacement would be greater or
smaller for taller or shorter people.
Using an accelerometer, however, gives changes in acceleration
rather than displacement. These acceleration measurements must
be converted to a distance before they can be used. With the
limited computing power available in the AN-602 setup, a
simple formula was used to approximate the double integralneeded for the conversion. With plenty of processing power
available, an attempt was made to do the discrete integrals
directly for this experiment.
A simple method was chosen to calculate the integrals. After
each step was determined, all of the acceleration samples
within that step were added to obtain a set of velocity samples.
The velocity samples for each step were normalized such that
the final sample was zero. They were then added together to
get a value for the displacement.
This technique looked promising initially, because measured
distances were relatively consistent for one subject walking
a course multiple times. However, the variance problem fromperson to person was exacerbated, as was the variance for one
subject at different paces. This led to an investigation of
whether the problem is with the model itself.
06617-001
BOUNCE
RIGHT
LEG
LEFT
LEG
LEFT
LEG
HIP
RIGHT
LEG
Figure 1. Vertical Movement of Hip While Walking
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
2/16
AN-900
Rev. 0 | Page 2 of 16
UNDERSTANDING THE MODEL
This model has two primary conditions. First, it assumes that
the foot actually makes contact with the ground at a single
point. Second, it assumes that the impact of each foot on the
ground is perfectly elastic. Of course, neither of these is the
case. The question arose of whether this can explain the largevariations encountered. Based on this experiment, it is safe to
state that it does explain much of the variation.
To understand this, it helps to look at the measured acceleration
values over several steps, shown in Figure 2. Different sources of
spring in one persons step are shown on the data.
340
290
240
190
140
1
06617-002
15 29 43 57 71 85 99 113 127 141 155 169
HEEL-TO-BALL BALL-TO-TOE
TOE UP
HEEL DOWN ACCEL
ACCELLERATION
Figure 2. Acceleration Graph for Subject 1 at a Normal Pace
Figure 2demonstrates the problems encountered trying to
translate acceleration measurements into an accurate distance
calculation. Methods that take into account the peak-to-peak
change (or even those that integrate the data) run into troublewith this type of data. The cause of this difficulty is the variation
in spring in the steps of different people, or in the steps of one
person using different paces from one measurement to another.
Figure 3shows the same subject with a longer and faster stride.
The peak-to-peak acceleration difference is larger, and the
various spring points look different. Thus, the amount of data
representing spring data vs. the amount representing real data is
different compared with Figure 2. However, the algorithm only
sees a set of acceleration measurements and does not note the
context of those measurements. The problem, therefore, is how
to remove the effect of the spring in a subjects step without
removing useful data.
1
06617-003
15 29 43 57 71 85 99 113 127 141 155 169
ACCELERATION
500
0
1
450
400
350
300
250
200
150
100
50
ACCEL
Figure 3. Acceleration Graph for Subject 1 at a Fast Pace
There are some important differences between the two plots.
The bottom part of the curve for each step is slightly narrower
in Figure 3and the tops of the curves are more consistent (fewer
distinctive peaks). These differences result in a higher averagevalue of the samples compared to the minimum and maximum
sample values.
For comparison purposes, review the data plot for a different
individual in Figure 4. The stride length is very similar to that of
Subject 1 in Figure 2. However, the data itself looks very different.
1
06617-004
15 29 43 57 71 85 99 113 127 141 155 169
ACCELERATION
50
1
400
350
300
250
200
150
100
SERIES 1
Figure 4. Acceleration Graph for Subject 2 at a Normal Pace
The stride of Subject 2 has a great deal more spring in it than
that of Subject 1 (shown in Figure 2). Yet both sets of data
represent roughly the same distance walked. Calculating distance
solely on the peak values gives widely varying results. Using asimple double integration suffers from the same problem.
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
3/16
AN-900
Rev. 0 | Page 3 of 16
SOLVING THE PROBLEM OF SPRING
All efforts to come up with a decent solution to this problem
using straightforward calculations had the same problems.
This led to a series of attempts to normalize the data in a way
that eliminated the spring, but all these attempts proved unsuc-
cessful. The main reason seemed to be that they required someknowledge of the context of the data. In actual use, however,
the system has no idea what is going on outside; all it has are
data points. The solution needs to be able to operate on the
data without context.
A possible solution to this problem began to emerge. It was
noted earlier how the data changed when going from a slower
to faster pace. There is less apparent variation due to the spring
with a longer, quicker stride. The result was a higher average for
the sample points, with respect to the data minima and maxima.
Visually, it is a little difficult to be sure of this, given the amount
of bounce in the steps shown in Figure 4. But calculations showed
that the average-vs.-peak values are very similar to those inFigure 2. Therefore, a possible simple algorithm was used to
determine the distance walked. It is
)(
)(
minmax
minavgkd
= (1)
where:
dis the distance calculated.
kis a constant multiplier.
maxis the maximum acceleration value measured within this step.
minis the minimum acceleration value.
avgis the average acceleration value for the step.
Equation 1 is completed for each step, as determined by a
different step-finding algorithm. The step-finding algorithm
uses an 8-point moving average to smooth the data. It searches
for a maximum peak, followed by a minimum. A step is counted
when the moving average crosses the zero point, which is the
overall average for the step. The data used in the distance algo-
rithm takes into account the 4-point latency of the moving
average.
This simple solution held up well for Subject 1 over various
stride lengths. It also did reasonably well with additional subjects.
But some subjects produced distances that varied as much as
10% from the average measured distance for the group. This
was not within the 7.5% error band that was targeted for an
uncalibrated measurement. Another solution was needed.
The ratio used in the last test seemed to reflect the differences
in the spring of different subjects steps. It made sense to try
combining the two methods examined here. Going back to
the original idea of using a double integral, this ratio was used
as a correction factor to remove the spring data from the calcu-
lation. The resulting formula is therefore
= )(*
)(
)(avgaccel
minavg
minmaxkd (2
where accelrepresents all measured acceleration values for
the step.
This algorithm held up well for a variety of subjects and paces,
with all variation within approximately 6%. The algorithm lends
itself to easy calibration for a specific individual/pace by adjusting
the Multiplier k. There is also provision within the listed code
to perform an average on the stride length to smooth out step-
to-step variation. The results noted here did not include the use
of this averaging.
In this experiment, only the X- and Y-axes were used. The
3-axis accelerometer was chosen for flexibility, and two axes
were found to be adequate for the task. An ADXL323 could
be used in place of the ADXL330. The same layout can be used
for both because the pin configuration is identical except for the
Z-axis output.
These experiments concentrated on achieving good results for
the pedometers distance measurement. There was no extensive
work done on the step-counting algorithm beyond ensuring it
works well while walking (and running), with measured steps
within 1 to 2 steps of the actual number over hundreds of steps
taken. However, it is possible that a simple algorithm can be
fooled with nonwalking motions. Improvements can be made
in that area, such as the time-window function described in
AN-602. The object is to ignore steps that are outside the
expected time window, while retaining the ability to adapt
when the subject changes pace.
SUMMARY
This application note represents the results of a single set of
experiments attempting to gain decent performance from a
simple pedometer that uses a single accelerometer. Some of
the barriers to gaining that performance have been reviewed.
The final results have met the stated accuracy goals, with the
added possibility of improved accuracy with calibration. Although
greater accuracy can be obtained with a more complex system
(for instance, with multiple accelerometers), the algorithm
provided in this application note is an excellent starting point
for simple, low cost applications.
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
4/16
AN-900
Rev. 0 | Page 4 of 16
06617-005
Figure 5. Custom Board Schematic
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
5/16
AN-900
Rev. 0 | Page 5 of 16
06617-006
Figure 6. Custom Board Schematic (Continued)
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
6/16
AN-900
Rev. 0 | Page 6 of 16
ADuC7020 C CODE
The following three files contain the code used to implement this algorithm on the test boards using an ADuC7020 microcontroller.
MAIN.C
The file main.cis used to initialize the device:
/ *********************************************************************
Aut hor : J Scarl et t
Dat e : Nov 2006
Fi l es : mai n. c, di spl ay.c, ped. c
Har dwar e : ADuC7020
Descr i pti on : I mpl ement s a si mpl e pedometer based on appl i cati on not e AN- 602
*********************************************************************/
#i ncl ude
/ / Functi on Prot ot ype wi t h requi red f uncti on at t ri but e.
ext ern voi d Moni t or_Functi on( voi d) ;
extern voi d Di spl ay_I ni t (voi d) ;
/ / *********************************************************************
/ / Mai n Funct i on f or ADuC7020 Pedometer essenti al l y per f orms st art up f unct i ons
/ / *********************************************************************
i nt mai n ( voi d)
{
POWKEY1 = 0x01;
POWCON = 0x06; / / set t o 653kHz cor e cl ock
POWKEY2 = 0xF4;
REFCON = 0x2; / / use ext ernal r ef erence
/ / ( connect ed to VDD)
/ / *********************************************************************
/ / I ni t i al i ze Per i phera ls
/ / *********************************************************************
/ / GPI O Conf i gur ati on
GP4DAT = 0x04000000; / / P4. 2 conf i gur ed as an out put .
/ / LED i s t ur ned on
GP0CON = 0x00000000; / /
GP0DAT = 0xE0000000; / / 0. 7, 0. 6, and 0. 5 are out put s
/ / 0. 7 = E, 0. 5 = R/ W*, 0. 6 = RS
GP1DAT = 0xFF000000; / / Al l P1 pi ns are out put s
ADCCON = 0x20; / / Turn ADC on but do not enabl e
Di spl ay_I ni t( ) ; / / f ound i n F i l e di spl ay. c
Moni t or_Functi on( ) ; / / f ound i n Fi l e ped. c/ / *********************************************************************
/ / Mai n Loop
/ / *********************************************************************
whi l e( 1)
{
}
} / / mai n( )
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
7/16
AN-900
Rev. 0 | Page 7 of 16
DISPLAY.C
The file display.ccontains all functions that access the display:
/ / F i l e "di spl ay. c"
/ / perf orms al l LCD di spl ay i nt erf ace f uncti ons
#i ncl ude
ext ern char st epbcd[ 6] ; / / f ound i n Fi l e ped. c
ext ern char di stbcd[ 6] ; / / t hi s t oo
voi d Di spl ay_I ni t( voi d);
voi d di spl ay_dat a( voi d) ;
voi d di spl ay_dat a_cl ear( voi d) ;
voi d char_l oad( i nt RS, i nt dat a) ;
voi d del ay( unsi gned i nt cycl es);
i nt r everse_data(i nt dat a) ;
voi d Di spl ay_I ni t( ){
/ / used once t o i ni t i al i ze di spl ay and wri t e
/ / t he "St eps" and "Di st ance" headers
i nt r s , data;
/ / Di spl ay i ni t i al i zat i on
rs = 0; / / no RAM access yet
dat a = 0x30; / / f uncti on set : 2- l i ne mode, di spl ay off
char_ l oad( rs, dat a);
dat a = 0x38; / / f uncti on set : 2- l i ne mode, di spl ay off
char_ l oad( rs, dat a);
data = 0x08; / / di spl ay of f , cursor of f , bl i nk of f
char_ l oad( rs, dat a);
dat a = 0x01; / / cl ear di spl ay
char_ l oad( rs, dat a);
del ay( 49) ; / / ~1. 5 ms addi t i onal del ay i s r equi r ed
data = 0x06; / / i ncr ement mode, no shi f t
char_ l oad( rs, dat a);
data = 0x0C; / / di spl ay on, cursor of f , bl i nk of f
char_ l oad( rs, dat a);
data = 0x80; / / set dat a addr ess t o home, j ust t o be sure
char_ l oad( rs, dat a);
r s = 1; / / now wr i t i ng t o data RAM
data = 0x53; / / st art of sequence t o send
char_l oad(r s, data); / / "Steps Di stance" f or t i t l e l i ne
data = 0x74; / / "t "
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
8/16
AN-900
Rev. 0 | Page 8 of 16
char_l oad( rs, data);
dat a = 0x65; / / "e"
char_l oad( rs, data);
dat a = 0x70; / / "p"
char_l oad( rs, data);
dat a = 0x73; / / "s"
char_l oad( rs, data);
data = 0x20; / / " " t i mes 3
char_l oad( rs, data);
char_l oad( rs, data);
char_l oad( rs, data);
data = 0x44; / / "D"
char_l oad( rs, data);
dat a = 0x69; / / " i "
char_l oad( rs, data);
dat a = 0x73; / / "s"
char_l oad( rs, data);
dat a = 0x74; / / "t "
char_l oad( rs, data);
dat a = 0x61; / / "a"
char_l oad( rs, data);
dat a = 0x6E; / / "n"
char_l oad( rs, data);
dat a = 0x63; / / "c"
char_l oad( rs, data);
dat a = 0x65; / / "e"
char_l oad( rs, data);
} / / Di spl ay_ I ni t ( )
voi d di spl ay_data()
{
/ / di spl ays t he data contai ned i n stepbcd[ ] & di st bcd[ ]
/ / begi nni ng at t he f i r st and ni nt h charact ers
/ / on r ow 2 of t he di spl ay
i nt i , r s , data, zero;
r s = 0; / / want t o set addr ess, not data
dat a = 0xC0; / / st art of second l i ne
char_l oad( rs, data);
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
9/16
AN-900
Rev. 0 | Page 9 of 16
r s = 1;
zero = 0;
f or ( i =5; i >=0; i - - ) / / di spl ay steps
{
i f ( ( stepbcd[ i ] > 0) | | ( zero ==1) ) / / suppress l eadi ng zeroes,
{ / / but not embedded zer oes
zero = 1;
data = 0x30 | st epbcd[ i ] ; / / numbers on di spl ay character t abl e
/ / begi n at 0x30
char_l oad ( rs, dat a) ;
} / / i f
} / / f o r
rs = 0; / / set address
data = 0xC8; / / ni nth character of second l i ne
char_ l oad( rs, dat a);
r s = 1;
zero = 0;
f or ( i =5; i >=0; i - - ) / / di spl ay di stance
{
i f ( ( di stbcd[ i ] > 0) | | (zero ==1))
{
zero = 1;
data = 0x30 | di stbcd[ i ] ;
char_l oad ( rs, dat a) ;
} / / i f
} / / f o r
} / / di spl ay_dat a( )
voi d di spl ay_dat a_cl ear( voi d)
{
/ / used t o cl ear di spl ay dat a f i el d
/ / bef or e new measur ement
i nt i , r s , data;
rs = 0; / / want t o set address
dat a = 0xC0; / / start of second l i ne
char_ l oad( rs, dat a);
r s = 1;
data =0x20;
f or ( i =0; i
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
10/16
AN-900
Rev. 0 | Page 10 of 16
/ / si gnal t i mi ng assumes a cor e cl ock < 4MHz
/ / del ay at end i s ~61us, t o al l ow wr i t e to compl ete
data = r everse_data(dat a); / / boar d l ayout di ctat ed r eversi ng MSB/ LSB
GP1CLR = 0x00FF0000; / / ensure cl ean sl ate f or next charact er
GP1SET = dat a
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
11/16
AN-900
Rev. 0 | Page 11 of 16
PED.C
The fileped.ccontains those functions used by the pedometer algorithm:
/ / f i l e "ped. c "
/ / per f orms pedometer and mi sc f unct i ons
#i ncl ude
#i ncl ude
#i ncl ude
/ / Functi on Protot ype & var i abl es
char st epbcd[ 6];
char di stbcd[ 6] ;
char st epf l ag;
f l oat str i de, avgstr i de, accel _dat[ 50];
f l oat maxavg, mi navg, accel _avg, vel oci t y, di spl ace;
f l oat di stance;
i nt steps;
voi d Moni t or_Functi on( voi d) ;
voi d get_sampl e(unsi gned i nt *xdat, unsi gned i nt *ydat, unsi gned i nt *zdat ) ;
char I sSt ep( f l oat avg, f l oat ol davg) ;
voi d di spl ay_prep( voi d) ;
l ong i nt bi n_t o_bcd( l ong i nt bi n_no) ;
ext ern voi d di spl ay_dat a( voi d) ; / / f ound i n Fi l e di spl ay.c
ext ern voi d di spl ay_dat a_cl ear( voi d) ; / / f ound i n Fi l e di spl ay.c
/ / f uncti ons
voi d Moni t or_Functi on( )
{
char f l ag;
unsi gned i nt xdat, ydat, zdat;
i nt i , cycl e_count , t ot _sampl es, avgconst = 1, l at ency = 4, avgl en = 8;
f l oat r ssdat , newmax, newmi n, ol davg, newavg, avgt hresh=1. 0;
f l oat wal kf udge = 0. 0249;
f l ag = 0;
T1CON = 0; / / t urn of f i nt er val t i mer and cl ear any I RQ
T1CLRI = 0;
whi l e (1)
{
i f ( I RQSI G & XI RQ0_BI T) / / XI RQ0 but t on has been pressed
{
whi l e(GP0DAT & 0x00010); / / wai t f or XI RQ t o be l ow agai n
i f ( ! f l ag)
{
T1CON = 0; / / t urn of f i nt er val t i mer
T1CLRI = 0; / / cl ear any t i mer I RQ
stepf l ag = 2;
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
12/16
AN-900
Rev. 0 | Page 12 of 16
maxavg = - 10000. 0;
mi navg = 10000. 0;
newmax = - 10000. 0;
newmi n = 10000. 0;
ol davg = 0. 0;
newavg = 0. 0;
cycl e_count = 0;
t ot_sampl es = 0;
steps = 0;
di st ance = 0. 0;
accel _avg = 0. 0;
vel oc i ty = 0. 0;
di spl ace = 0. 0;
avgstr i de = 0. 0;
di spl ay_data_cl ear( ); / / c l ear ol d data f romdi spl ay
f l ag = 1;
T1LD = 1092; / / ~30 Hz sampl e r at e
T1CON = 0x2C0; / / 32. 768 kHz cl ock, t i mer on,
/ / peri odi c mode
} / / i f not runni ng, start .
} // l ook f or st op but t on
i f ( GP2DAT & 0x01)
{
whi l e( GP2DAT & 0x01) ;
f l ag = 0;
} / / i f runni ng, stop
i f ( ( ( I RQSI G & GP_TI MER_BI T) && ( f l ag) ) ! = 0) / / wai t f or ti meout
/ / and f l ag
{T1CLRI = 0;
i f ( t ot_sampl es > 7) / / subt ract f i rst sampl e i n sl i di ng boxcar avg
{
ol davg = newavg;
newavg - = accel _dat [ cycl e_count - avgl en] ;
} / / i f
get_ sampl e(&xdat, &ydat, &zdat ) ; / / get data f r omaccel erometer
xdat - = 8192; / / subt r act Zero g val ue
ydat - = 8192;
rssdat = sqrt (( f l oat) (xdat*xdat + ydat *ydat) / 16. 0); / / vector sum
accel _dat [ cycl e_count ] = r ssdat; / / pl ace cur r ent sampl e data i n buf f er
newavg += r ssdat ; / / add new sampl e t o sl i di ng boxcar avg
i f ( ( abs( newavg- ol davg) ) < avgt hresh)
newavg = ol davg;
i f ( r ssdat > newmax)
newmax = r ssdat;
i f ( r ssdat < newmi n)
newmi n = r ssdat ;
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
13/16
AN-900
Rev. 0 | Page 13 of 16
t ot _sampl es++;
cycl e_count ++; / / i ncrement count of sampl es i n cur r ent st ep
i f (t ot_sampl es > 8)
{
i f ( I sSt ep( newavg, ol davg) )
{
f or ( i = l at ency; i < (cycl e_count - l atency); i ++)
accel _avg += accel _dat [ i ] ;
accel _avg /= ( cycl e_count - avgl en) ;
f or ( i = l at ency; i < (cycl e_count - l atency); i ++)
{
vel oci ty += (accel _dat[ i ] - accel _avg);
di spl ace += vel oci t y;
} / / create i nt egr ati on and doubl e i nt egr ati on
/ / cal cul ate str i de l ength
st r i de = di spl ace * ( newmax - newmi n) / ( accel _avg - newmi n);
st r i de = sqrt (abs(str i de)) ;
/ / use appr opr i ate const ant t o get st r i de l engt h
str i de *= wal kfudge;
/ / generat e exponent i al average of st r i de l ength t o smoot h data
i f ( s teps < 2)
avgstr i de = str i de;
el se
avgstr i de = ( ( avgconst - 1) *avgstr i de + str i de) / avgconst ;
st eps++;di st ance += avgst r i de;
/ / need al l data used i n cal cul ati ng newavg
f or ( i = 0; i < avgl en; i ++)
accel _dat[ i ] = accel _dat[ cycl e_count + i - avgl en];
cycl e_count = avgl en;
newmax = - 10000. 0;
newmi n = 10000. 0;
maxavg = - 10000. 0;
mi navg = 10000. 0;
accel _avg = 0;vel oc i ty = 0;
di spl ace = 0;
di spl ay_prep( ) ;
di spl ay_dat a( ) ;
/ / t emporar y
i f ( GP4DAT & 0x04) / / t oggl e LED t o r ef l ect st ep
GP4CLR = 0x040000;
el se
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
14/16
AN-900
Rev. 0 | Page 14 of 16
GP4SET = 0x040000;
} / / we have a new st ep
} / / enough sampl es t o st art checki ng f or st ep ( need at l east 8)
} / / i f t i meout
} / / cont i nual l oop
} / / Moni t or_Functi on( )
voi d get_ sampl e(unsi gned i nt *xdat, unsi gned i nt *ydat, unsi gned i nt *zdat )
{
/ / gets new sampl es f or x, y, z axes
/ / sums t oget her 4 measurments t o get average
i nt i ;
*xdat = 0;
*ydat = 0;
*zdat = 0;
f or ( i =0; i > 16) & 0xFFF) ; / / dat a i s i n bi t s 16 - 27, so shi f t i s necessar y
ADCCP = 1; / / y axi s
i ++;ADCCON = 0xA3;
whi l e ( ! ( ADCSTA) ) ;
*ydat += ( ( ADCDAT >> 16) & 0xFFF) ;
ADCCP = 2; / / z axi s
i ++;
ADCCON = 0xA3;
whi l e ( ! ( ADCSTA) ) ;
*zdat += ( ( ADCDAT >> 16) & 0xFFF) ;
} / / f o r
} / / get_sampl e()
char I sSt ep( f l oat avg, f l oat ol davg)
{
/ / t hi s f unct i on att empt s t o deter mi ne when a st ep i s compl ete
f l oat step_t hresh = 5. 0; / / used t o prevent noi se f r om f ool i ng t he al gori t hm
i f (st epf l ag == 2)
{
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
15/16
AN-900
Rev. 0 | Page 15 of 16
i f ( avg > (ol davg + step_thresh))
stepf l ag = 1;
i f ( avg < ( ol davg - st ep_t hresh) )
stepf l ag = 0;
return 0;
} / / f i rst t i me t hrough t hi s functi on
i f ( stepf l ag == 1)
{
i f ( ( maxavg > mi navg) && ( avg >
( ( maxavg+mi navg)/ 2) ) &&
( ol davg < ( ( maxavg+mi navg/ 2)) ) )
ret urn 1;
i f ( avg < ( ol davg - st ep_t hresh) )
{
stepf l ag = 0;
i f ( ol davg > maxavg)
maxavg = ol davg;
} / / sl ope has t urned down
return 0;
} / / sl ope has been up
i f ( stepf l ag == 0)
{
i f ( avg > (ol davg + step_thresh))
{
stepf l ag = 1;
i f ( ol davg < mi navg)
mi navg = ol davg;
} / / sl ope has t urned up
return 0;} / / sl ope has been down
return 0;
} / / I sStep()
voi d di spl ay_prep( )
{
i nt i ;
l ong i nt t emp;
/ / conver t st eps t o BCD val ues f or sendi ng t o di spl ay
t emp = st eps;
t emp = bi n_t o_bcd( t emp) ; / / f unct i on t o conver t bi nary
f or ( i =0; i > 4;
} / / f o r
/ / conver t di st ance t o BCD val ues f or sendi ng t o di spl ay
8/11/2019 Enhancing the Performance of Pedometers Using a Single Accelerometer
16/16
AN-900
Rev. 0 | Page 16 of 16
temp = (l ong i nt )( di stance); / / convert f l oat to l ong i nt
t emp = bi n_t o_bcd( t emp);
f or ( i =0; i > 4;
} / / f o r
} / / di spl ay_prep( )
l ong i nt bi n_t o_bcd( l ong i nt bi n_no)
{
i nt i ;
l ong i nt di vi sor , mul t i pl i er, bcd_no, t emp;
di vi sor = 100000;
mul t i pl i er = 1048576;
bcd_no = 0;
t emp = 0;
i f ( bi n_no > 999999)
bi n_no = 999999;
f or ( i =0; i > 4;
} / / f o r
r etur n bcd_no;
} / / bi n_t o_bcd( )
2007 Analog Devices, Inc. All rights reserved. Trademarks andregistered trademarks are the property of their respective owners.
AN06617-0-7/07(0)