-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPS_App.java
More file actions
345 lines (264 loc) · 11.7 KB
/
Copy pathGPS_App.java
File metadata and controls
345 lines (264 loc) · 11.7 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package lab4_202_08.uwaterloo.ca.lab4_202_08;
import android.graphics.Color;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.widget.Button;
import java.lang.Math;
import java.util.ArrayList;
import java.util.Locale;
import ca.uwaterloo.sensortoy.LineGraphView;
import mapper.*;
class RotationVectorEventListener implements SensorEventListener {
TextView output;
static double angle;
public RotationVectorEventListener(TextView outputView){
output = outputView;
}
public void onAccuracyChanged(Sensor s, int i) {}
public void onSensorChanged(SensorEvent se){
if (se.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR){
//Show the current values of rotation vector on screen
String v = String.format(Locale.getDefault(), "(%.2f, %.2f, %.2f)", se.values[0], se.values[1], se.values[2]);
String t = "Rotation Vector Values: " + v;
output.setText(t);
//create new vectors to hold values from the getRotationMatrixFromVector function and getOrientation function
float[] R = new float[9];
float[] orientation = new float[3];
SensorManager.getRotationMatrixFromVector(R,se.values);
//reorients the vectors from the rotation matrix function to allow the phone to function when being held up instead of flat
float[] RotAxis = new float[9];
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, RotAxis);
//uses getOrientation function to get angle from North in radians
SensorManager.getOrientation(RotAxis, orientation);
angle = orientation[0];
output.setText("Angle from North: " + Math.toDegrees(angle));
}
}
}
public class Lab4_202_08 extends AppCompatActivity {
LineGraphView graph;
mapper.MapView mv;
NavigationalMap map;
PointF pf = new PointF();
float yAxis = (float)18;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab4_202_08);
//adds map of room by calling function
mv = new mapper.MapView(getApplicationContext(), 1200, 1200, 68, 68);
registerForContextMenu(mv);
map = mapper.MapLoader.loadMap(getExternalFilesDir(null), "Lab-room-peninsula.svg");
mv.setMap(map);
mv.addListener(Position);
TextView ls = (TextView) findViewById(R.id.label1); //light sensor text view
ls.setText(" ");
//TextView for rotation
TextView Rot = new TextView(getApplicationContext());
Rot.setTextColor(Color.BLACK); //change colour of rotation textview
LinearLayout l = (LinearLayout) findViewById(R.id.label2); //set the linear layout orientation
l.setOrientation(LinearLayout.VERTICAL);
mv.setVisibility(View.VISIBLE);
l.addView(mv); //add map to display
//graph = new LineGraphView(getApplicationContext(),100, Arrays.asList("x", "y", "z"));
//l.addView(graph);
//graph.setVisibility(View.VISIBLE);
TextView Ac = new TextView(getApplicationContext()); //Accelerometer text view
Ac.setText("Accelerometer");
l.addView(Ac);
l.addView(Rot);
TextView directions = new TextView(getApplicationContext());
directions.setTextColor(Color.BLACK);
l.addView(directions);
Button button = (Button)findViewById(R.id.button);
//setting the colours of all the labels to black
ls.setTextColor(0xff000000);
Ac.setTextColor(0xff000000);
//Accelerometer
SensorManager sensorManager1 = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor Accelerometer = sensorManager1.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
SensorEventListener a = new AccelerometerEventListener(Ac, mv, ls);
sensorManager1.registerListener(a,Accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
//Rotation Vector
SensorManager sensorManager2 = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor RotationVector = sensorManager2.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
SensorEventListener r = new RotationVectorEventListener(Rot);
sensorManager2.registerListener(r, RotationVector, SensorManager.SENSOR_DELAY_NORMAL);
}
PositionListener Position = new PositionListener() {
@Override
public void originChanged(MapView source, PointF loc) {
mv.setUserPoint(loc);
//Path finding
PointF origin = new PointF();
origin.set(mv.getOriginPoint());
PointF destination = new PointF();
destination.set(mv.getDestinationPoint());
ArrayList<PointF> paths = new ArrayList<PointF>();
if (map.calculateIntersections(origin, destination).isEmpty()) {
paths.add(origin);
paths.add(destination);
}
else {
PointF pathOne = new PointF(origin.x, yAxis);
PointF pathTwo = new PointF(destination.x, yAxis);
paths.add(origin);
paths.add(pathOne);
paths.add(pathTwo);
paths.add(destination);
}
mv.setUserPath(paths);
}
@Override
public void destinationChanged(MapView source, PointF dest) {
mv.setDestinationPoint(dest);
PointF origin = new PointF();
origin.set(mv.getOriginPoint());
PointF destination = new PointF();
destination.set(mv.getDestinationPoint());
ArrayList<PointF> paths = new ArrayList<PointF>();
if (map.calculateIntersections(origin, destination).isEmpty()) {
paths.add(origin);
paths.add(destination);
}
else {
PointF pathOne = new PointF(origin.x, yAxis);
PointF pathTwo = new PointF(destination.x, yAxis);
paths.add(origin);
paths.add(pathOne);
paths.add(pathTwo);
paths.add(destination);
}
mv.setUserPath(paths);
}
};
@Override
public void onCreateContextMenu(ContextMenu menu , View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu , v, menuInfo);
mv.onCreateContextMenu(menu , v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item) || mv.onContextItemSelected(item);
}
class AccelerometerEventListener implements SensorEventListener {
TextView output;
TextView notice;
int count = 0; //defined a variable to count steps
int state = 0; //defined a variable to see the current state of the step
final int waiting = 0; // defined a variable equal to 0 acceleration or the "waiting" period
float[] smValues = {0, 0, 0}; //holds filtered values from accelerometer
float c = (float)2.5;
float q = (float) 0.7;
float northSteps = 0; //steps towards north (steps are negative if towards south)
float eastSteps = 0; //steps towards east (steps are negative if towards west)
long stepTime = System.currentTimeMillis();
MapView stepmap;
Button button = (Button)findViewById(R.id.button);
public AccelerometerEventListener(TextView outputView, MapView map, TextView popup) {
output = outputView;
stepmap = map;
notice = popup;
}
public void onAccuracyChanged(Sensor s, int i) {
}
public void onSensorChanged(SensorEvent se) {
if (se.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
String z = String.format(Locale.getDefault(), "(%f, %f, %f)\n", se.values[0], se.values[1], se.values[2]);
//low pass filter to smooth values
smValues[0] += (se.values[0] - smValues[0])/c;
smValues[1] += (se.values[1] - smValues[1])/c;
smValues[2] += (se.values[2] - smValues[2])/c;
//defined the finite state machine
switch(state){
case 0: if(smValues[1] >= -0.2) state = 1;
else {
state = waiting;
}
break;
case 1: if(smValues[1] > 0.2 ) state = 2;
else {
state = waiting;
}
break;
case 2: if(smValues[1] > 0.4 ) state = 2;
else if (smValues[1]<0.4){
state = 3;
}
else {
state = waiting;
}
break;
case 3: if(smValues[1] <= 0.11)state = 4;
else {
state = 1;
}
break;
//Count a step and turn the state back to waiting
case 4: if(System.currentTimeMillis() - stepTime >= 500) {
count++;
float newnorthSteps = 0;
float neweastSteps = 0;
//Code to determine direction travelled
double currentAngle = RotationVectorEventListener.angle;
if(currentAngle <= Math.PI/2 && currentAngle >= 0){
newnorthSteps += Math.abs(Math.cos(currentAngle));
neweastSteps += Math.abs(Math.sin(currentAngle));
}
if(currentAngle < 0 && currentAngle >= -Math.PI/2) {
newnorthSteps += Math.abs(Math.cos(currentAngle));
neweastSteps -= Math.abs(Math.sin(currentAngle));
}
if(currentAngle < -Math.PI/2 && currentAngle >= -Math.PI) {
newnorthSteps -= Math.abs(Math.cos(currentAngle));
neweastSteps -= Math.abs(Math.sin(currentAngle));
}
if(currentAngle <= Math.PI && currentAngle > Math.PI/2) {
newnorthSteps -= Math.abs(Math.cos(currentAngle));
neweastSteps += Math.abs(Math.sin(currentAngle));
}
northSteps += newnorthSteps;
eastSteps += neweastSteps;
PointF tp = new PointF();
tp.set(stepmap.getUserPoint());
float newX = tp.x + neweastSteps/q;
float newY = tp.y - newnorthSteps/q;
stepmap.setUserPoint(newX, newY);
if(Math.abs(newX - stepmap.getDestinationPoint().x) < 0.5 && Math.abs(newY - stepmap.getDestinationPoint().y) < 0.5) {
notice.setText("You have reached your destination!");
}
state = waiting;
}
else{
state = waiting;}
stepTime = System.currentTimeMillis();
break;
default: break;
}
//Created a button to reset number of steps and turn the state to waiting and turn all calculated values to 0
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count = 0;
state = waiting;
northSteps = 0;
eastSteps = 0;
notice.setText(" ");
}
});
//graph.addPoint(smValues); //added the accelerometer points to the graph
output.setText(String.format("Step: %d \nState: %d \nSteps to the North: %f \nSteps to the East: %f", count, state, northSteps, eastSteps));
}
}
}
}