-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpedometer.cpp
155 lines (133 loc) · 4.98 KB
/
pedometer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* pedometer.cpp
* use accelerometer to make a pedometer
*
* Copyright (c) 2014 seeed technology inc.
* Website : www.seeed.cc
* Author : lawliet zou
* Create Time: March 2014
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "pedometer.h"
void Pedometer::sensorInit()
{
adxl.powerOn();
//set activity/ inactivity thresholds (0-255)
adxl.setActivityThreshold(75); //62.5mg per increment
adxl.setInactivityThreshold(75); //62.5mg per increment
adxl.setTimeInactivity(10); // how many seconds of no activity is inactive?
//look of activity movement on this axes - 1 == on; 0 == off
adxl.setActivityX(1);
adxl.setActivityY(1);
adxl.setActivityZ(1);
//look of inactivity movement on this axes - 1 == on; 0 == off
adxl.setInactivityX(1);
adxl.setInactivityY(1);
adxl.setInactivityZ(1);
//look of tap movement on this axes - 1 == on; 0 == off
adxl.setTapDetectionOnX(0);
adxl.setTapDetectionOnY(0);
adxl.setTapDetectionOnZ(1);
//set values for what is a tap, and what is a double tap (0-255)
adxl.setTapThreshold(50); //62.5mg per increment
adxl.setTapDuration(15); //625us per increment
adxl.setDoubleTapLatency(80); //1.25ms per increment
adxl.setDoubleTapWindow(200); //1.25ms per increment
//set values for what is considered freefall (0-255)
adxl.setFreeFallThreshold(7); //(5 - 9) recommended - 62.5mg per increment
adxl.setFreeFallDuration(45); //(20 - 70) recommended - 5ms per increment
//setting all interrupts to take place on int pin 1
//I had issues with int pin 2, was unable to reset it
adxl.setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_FREE_FALL_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT, ADXL345_INT1_PIN );
//register interrupt actions - 1 == on; 0 == off
adxl.setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
adxl.setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
adxl.setInterrupt( ADXL345_INT_FREE_FALL_BIT, 1);
adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT, 1);
adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
}
void Pedometer::init()
{
sensorInit();
stepCount = 0;
updateModelAxis();
_curr_val = 0;
}
void Pedometer::updateModelAxis(void)
{
int x,y,z;
int16_t sum[MAX_AXIS] = {0};
for(int i = 0; i < SAMPLING_MODEL_NUMBER; i++){
adxl.readXYZ(&x, &y, &z);
sum[X_AXIS] += abs(x);
sum[Y_AXIS] += abs(y);
sum[Z_AXIS] += abs(z);
}
sum[X_AXIS] /= SAMPLING_MODEL_NUMBER;
sum[Y_AXIS] /= SAMPLING_MODEL_NUMBER;
sum[Z_AXIS] /= SAMPLING_MODEL_NUMBER;
_model_axis = sum[X_AXIS] >= sum[Y_AXIS]?X_AXIS:Y_AXIS;
_model_axis = sum[_model_axis] >= sum[Z_AXIS]?_model_axis:Z_AXIS;
_model_val = sum[_model_axis];
_model_ratio = (_model_val+MODEL_STANDARD_VALUE/2)/MODEL_STANDARD_VALUE;
}
void Pedometer::getValue(void)
{
int tmp_val[MAX_AXIS];
_curr_val = 0;
for(int i = 0; i < MAX_WINDOW; i++){
adxl.readXYZ(&tmp_val[X_AXIS], &tmp_val[Y_AXIS], &tmp_val[Z_AXIS]);
_curr_val += abs(tmp_val[_model_axis]);
}
_curr_val /= MAX_WINDOW;
_curr_val = (_curr_val + _model_ratio/2)/_model_ratio;
}
void Pedometer::getValidValue(void)
{
do{
getValue();
}while((abs(_curr_val-_model_val) <= 2) || (_curr_val == _last_val));
_last_val = _curr_val;
}
void Pedometer::stepCalc()
{
unsigned long timerStart,timerEnd;
timerStart = millis();
timerEnd = 100 + timerStart;//one step is more than 100ms
getValidValue();
if(_curr_val >= 23){
while(1){
getValidValue();
if(_curr_val <= 17){
if(millis() > timerEnd) {
stepCount++;
}
break;
}
}
}
}