-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampleAccelerometerData.cpp
More file actions
306 lines (256 loc) · 9.82 KB
/
exampleAccelerometerData.cpp
File metadata and controls
306 lines (256 loc) · 9.82 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package ece155.uwaterloo.ca.lab4_202_19;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.util.Log;
import android.widget.TextView;
import java.io.PrintWriter;
import java.util.Arrays;
/**
* Created by Aneesh on 2017-01-31.
*/
public class AccelHandler implements SensorEventListener {
TextView output;
float[][] history;
int historyIndex;
final int SMOOTHING_FACTOR = 10;
//States and movements
private enum states{WAIT, RISE_A, DROP_B, DETERMINED}
private enum signatures{UP, DOWN, LEFT, RIGHT, UNKNOWN}
private states stateX;
private states stateY;
private signatures signatureX;
private signatures signatureY;
//Expretimentally determined threshold values for left and right gestures, for FSM
private final float[] THRESH_RIGHT = {0.6f, 2.3f};
private final float[] THRESH_LEFT = {-0.6f, -2.3f};
private final float[] THRESH_UP = {0.6f, 2.3f};
private final float[] THRESH_DOWN = {-0.6f, -2.3f};
//FSM Starts at 0 samples having been obtained initially
private final int SAMPLEDEFAULT = 30;
private int sampleCounter;
private GameLoopTask myTask;
/**
* Constructor
* @param outputView textview to output text to
*/
public AccelHandler(TextView outputView, GameLoopTask myTask) {
output = outputView;
history = new float[100][3];
historyIndex = 0;
stateX = states.WAIT;
stateY = states.WAIT;
signatureX = signatures.UNKNOWN;
signatureY = signatures.UNKNOWN;
output.setText("UNDETERMINED");
this.myTask = myTask;
}
/**
* Outputs last 100 pieces of data to csv file
*/
public void log(PrintWriter writer)
{
for(int row = 0;row < 100;row++)
{
writer.print(String.format("%f,%f,%f", history[row][0], history[row][1], history[row][2]));
writer.println();
}
}
/**
* Updates current values in main, updates max values.
* Keeps track of 100 most recent readings. (accelerometer only)
* @param se event generated by sensor change
*/
public void onSensorChanged(SensorEvent se)
{
//Shift all readings over by 1
for (int row = 0; row < 99; row++)
{
history[row][0] = history[row+1][0];
history[row][1] = history[row+1][1];
history[row][2] = history[row+1][2];
}
//Insert filtered reading
history[99][0] += (se.values[0] - history[99][0]) / SMOOTHING_FACTOR;
history[99][1] += (se.values[1]- history[99][1]) / SMOOTHING_FACTOR;
history[99][2] += (se.values[2]- history[99][2]) / SMOOTHING_FACTOR;
//Sample has been obtained
sampleCounter--;
if(sampleCounter <= 0)
{
//If a gesture has been detected
if((stateX == states.DETERMINED) || (stateY == states.DETERMINED))
{
if(signatureX == signatures.RIGHT) {
output.setText("RIGHT");
myTask.setDirection(GameLoopTask.GameDirection.RIGHT);
}
else if(signatureX == signatures.LEFT) {
output.setText("LEFT");
myTask.setDirection(GameLoopTask.GameDirection.LEFT);
}
else if(signatureY == signatures.UP) {
output.setText("UP");
myTask.setDirection(GameLoopTask.GameDirection.UP);
}
else if(signatureY == signatures.DOWN) {
output.setText("DOWN");
myTask.setDirection(GameLoopTask.GameDirection.DOWN);
}
else {
output.setText("N/A");
myTask.setDirection(GameLoopTask.GameDirection.NO_MOVEMENT);
}
}
//No gesture detected but 20 samples still up
else
{
output.setText("N/A");
}
//Reset state
stateX = states.WAIT;
stateY = states.WAIT;
sampleCounter = SAMPLEDEFAULT;
}
//Update FSM with new data reading
callFSM();
}
public void onAccuracyChanged(Sensor s, int i) {}
/**
* Contains implementation of Finite State Machine used to recognize gestures
*/
public void callFSM()
{
double deltaX = history[99][0] - history[98][0];
double deltaY = history[99][1] - history[98][1];
//X-AXIS FSM Implementation
switch(stateX)
{
case WAIT:
signatureX = signatures.UNKNOWN;
//Signal ascending (characteristic of RIGHT/UP gesture)
if(deltaX >= THRESH_RIGHT[0])
stateX = states.RISE_A;
//Signal descending (characteristic of LEFT/DOWN gesture)
else if(deltaX <= THRESH_LEFT[0])
stateX = states.DROP_B;
break;
case RISE_A:
//If signal has begun to descend
if(deltaX <= 0){
//If latest reading has exceeded required threshold value
if(history[99][0] >= THRESH_RIGHT[1]) {
stateX = states.DETERMINED;
signatureX = signatures.RIGHT;
}
else
{
stateX = states.DETERMINED;
signatureX = signatures.UNKNOWN;
}
}
break;
/*case DROP_A:
//If signal has begun to ascend
if(deltaX >= 0){
if (history[99][0] <= THRESH_RIGHT[2])
signatureX = signatures.RIGHT;
stateX = states.DETERMINED;
}
break;*/
/*case RISE_B:
//If the signal has begun to descend
if(deltaX <= 0){
if (history[99][0] >= THRESH_LEFT[2])
signatureX = signatures.LEFT;
stateX = states.DETERMINED;
}
break;*/
case DROP_B:
//If the signal has begun to rise
if(deltaX >= 0){
//If the previous reading has exceeded the threshold value
if(history[99][0] <= THRESH_LEFT[1]) {
stateX = states.DETERMINED;
signatureX = signatures.LEFT;
}
else {
stateX = states.DETERMINED;
signatureX = signatures.UNKNOWN;
}
}
break;
case DETERMINED:
//Log.d("FSM: ", "State DETERMINED " + signatureX.toString());
break;
default:
stateX = states.WAIT;
signatureX = signatures.UNKNOWN;
break;
}
//Y-AXIS FSM Implementation
switch(stateY)
{
case WAIT:
signatureY = signatures.UNKNOWN;
//Signal ascending (characteristic of RIGHT/UP gesture)
if(deltaY >= THRESH_UP[0])
stateY = states.RISE_A;
//Signal descending (characteristic of LEFT/DOWN gesture)
else if(deltaY <= THRESH_DOWN[0])
stateY = states.DROP_B;
break;
case RISE_A:
//If signal has begun to descend
if(deltaY <= 0){
//If latest reading has exceeded required threshold value
if(history[99][1] >= THRESH_UP[1]) {
stateY = states.DETERMINED;
signatureY = signatures.UP;
}
else {
stateY = states.DETERMINED;
signatureY = signatures.UNKNOWN;
}
}
break;
/* case DROP_A:
//If signal has begun to ascend
if(deltaY >= 0){
if (history[99][1] <= THRESH_UP[2])
signatureY = signatures.UP;
stateY = states.DETERMINED;
}
break;*/
/*case RISE_B:
//If the signal has begun to descend
if(deltaY <= 0){
if (history[99][1] >= THRESH_DOWN[2])
signatureY = signatures.DOWN;
stateY = states.DETERMINED;
}
break;*/
case DROP_B:
//If the signal has begun to rise
if(deltaY >= 0){
//If the previous reading has exceeded the threshold value
if(history[99][1] <= THRESH_DOWN[1]) {
stateY = states.DETERMINED;
signatureY = signatures.DOWN;
}
else {
stateY = states.DETERMINED;
signatureY = signatures.UNKNOWN;
}
}
break;
case DETERMINED:
//Log.d("FSM: ", "State DETERMINED " + signatureY.toString());
break;
default:
stateY = states.WAIT;
signatureY = signatures.UNKNOWN;
break;
}
}
}