-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
216 lines (164 loc) · 7.15 KB
/
MainActivity.java
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
package com.dead.acctivi_classification;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.dead.acctivi_classification.distanceAlgorithm.DistanceAlgorithm;
import com.dead.acctivi_classification.distanceAlgorithm.EuclideanDistance;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Thread.sleep;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private final int REQUEST_CODE = 101;
private DistanceAlgorithm[] distanceAlgorithms = {new EuclideanDistance()};
private Classifier classifier;
private List<DataPoint> listDataPoint = new ArrayList<>();
private List<DataPoint> listDataPointOriginal = new ArrayList<>();
RunTimeCalculations calculations = new RunTimeCalculations();
private TextView activity;
private SensorManager mSensorManager;
private Sensor mSensorAccelero;
// TextViews to display current sensor values
private TextView mTextSensorGyro;
private Button btStart, btStop;
private int K;
private double spRatio;
private ArrayList<Float> dataX = new ArrayList<Float>();
private ArrayList<Float> dataY = new ArrayList<Float>();
private ArrayList<Float> dataZ = new ArrayList<Float>();
//float avgX, avgY, avgZ, varX, varY, varZ, sdX, sdY, sdZ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
btStart = (Button) findViewById(R.id.btStart);
btStop = (Button) findViewById(R.id.btStop);
activity = (TextView) findViewById(R.id.textViewZ);
//mSensorGyro = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mSensorAccelero = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
K = 11;
spRatio = 0.8;
String sensor_error = getResources().getString(R.string.error_no_sensor);
if (mSensorAccelero == null) {
mTextSensorGyro.setText(sensor_error);
}
classifier = new Classifier();
populateList();
runClassifier();
classifier.addTrainData();
btStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start();
Toast.makeText(getBaseContext(), "Data Recording Started", Toast.LENGTH_LONG).show();
//classifier.classify();
//activity.setText("Accuracy = " + classifier.getAccuracy());
//getDelay(300);
// here we have to give the calculated run time values
}
});
btStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stop();
}
});
}
/*
When ever the array list contains 10 items, it should do the calculatons. BUt this only happens once it doesn't loop back.
*/
@Override
public void onSensorChanged(SensorEvent event) {
int sensorType = event.sensor.getType();
float currentValueY = event.values[1];
float currentValueZ = event.values[2];
activity.setText("Please Wait......");
dataY.add(currentValueY);
dataZ.add(currentValueZ);
// when array contains 10 data this block should work but it doesn't.
if (dataY.size() == 10) {
activity.setText("Calculating......");
try {
sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mSensorManager.unregisterListener(this);
float avgY = calculations.findAverage(dataY);
float varY = calculations.findVariance(dataY, avgY);
float sdY = calculations.findStandardDeviation(varY);
float avgZ = calculations.findAverage(dataZ);
float varZ = calculations.findVariance(dataY, avgZ);
float sdZ = calculations.findStandardDeviation(varZ);
Category category = classifier.predictNew(avgY, varY, sdY, avgZ, varZ, sdZ);
activity.setText(category.toString());
dataZ.clear();
dataY.clear();
try {
//set time in mili
sleep(5000);
//Toast.makeText(getBaseContext(), "Sleeping....", Toast.LENGTH_LONG).show();
//
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
start();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private void populateList() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getAssets().open
("analised.csv")));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] point = line.split(",");
double mY = Double.parseDouble(point[0]);
double vY = Double.parseDouble(point[1]);
double sdY = Double.parseDouble(point[2]);
double mZ = Double.parseDouble(point[3]);
double vZ = Double.parseDouble(point[4]);
double sdZ = Double.parseDouble(point[5]);
int category = Integer.parseInt(point[6]);
DataPoint dataPoint = new DataPoint(mY, vY, sdY, mZ, vZ, sdZ, Category.values()[category]);
listDataPointOriginal.add(new DataPoint(dataPoint));
listDataPoint.add(dataPoint);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
void runClassifier() {
classifier.reset();
classifier.setDistanceAlgorithm(distanceAlgorithms[0]);
classifier.setK(11);
classifier.setSplitRatio(0.8);
classifier.setListDataPoint(listDataPoint);
classifier.splitData();
listDataPoint.clear();
listDataPoint.addAll(classifier.getListTestData());
listDataPoint.addAll(classifier.getListTrainData());
}
public void start() {
if (mSensorAccelero != null) {
mSensorManager.registerListener(this, mSensorAccelero, SensorManager.SENSOR_DELAY_NORMAL);
}
}
public void stop() {
mSensorManager.unregisterListener(this);
Toast.makeText(getBaseContext(), "Data Recording Stopped", Toast.LENGTH_LONG).show();
}
}