|
|
@@ -1,21 +1,45 @@ |
|
|
|
package com.example.ueberwachungssystem; |
|
|
|
|
|
|
|
import static java.lang.Math.sqrt; |
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
import android.hardware.Sensor; |
|
|
|
import android.hardware.SensorEvent; |
|
|
|
import android.hardware.SensorEventListener; |
|
|
|
import android.hardware.SensorManager; |
|
|
|
import android.os.Bundle; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import androidx.annotation.Nullable; |
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
import java.util.ArrayDeque; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.Deque; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.OptionalDouble; |
|
|
|
import java.util.Queue; |
|
|
|
|
|
|
|
public class Beschleunigungssensor extends AppCompatActivity implements SensorEventListener |
|
|
|
{ |
|
|
|
private Logger logger; |
|
|
|
private SensorManager sensorManager; |
|
|
|
private int sensorType = Sensor.TYPE_ACCELEROMETER; |
|
|
|
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; |
|
|
|
private Sensor sensor; |
|
|
|
|
|
|
|
//Matrizen für Datenverarbeitung: |
|
|
|
double[] linear_acceleration = new double[3]; |
|
|
|
int numberOfValues = 100; |
|
|
|
double[][] calibrationMatrix = new double[3][numberOfValues]; |
|
|
|
boolean alarm = false; |
|
|
|
//Preallocate memory for the data of each axis of the acceleration sensor |
|
|
|
double x; |
|
|
|
double y; |
|
|
|
double z; |
|
|
|
double betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z) |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onCreate(Bundle savedInstanceState) |
|
|
|
{ |
|
|
@@ -44,7 +68,7 @@ public class Beschleunigungssensor extends AppCompatActivity implements SensorEv |
|
|
|
super.onResume(); |
|
|
|
if(sensor != null) |
|
|
|
{ |
|
|
|
if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME)) |
|
|
|
if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)) |
|
|
|
{ |
|
|
|
logger.log("Wir haben uns beim Sensor angemeldet."); |
|
|
|
} |
|
|
@@ -65,15 +89,106 @@ public class Beschleunigungssensor extends AppCompatActivity implements SensorEv |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void checkAlarm (SensorEvent event) { |
|
|
|
x = event.values[0]; |
|
|
|
y = event.values[1]; |
|
|
|
z = event.values[2]; |
|
|
|
betrag = sqrt(x*x + y*y + z*z); |
|
|
|
double schwelle = 1.5; |
|
|
|
|
|
|
|
if (!alarm) { |
|
|
|
if (betrag > schwelle) { |
|
|
|
logger.log("Betragswert über Schwelle erkannt, Alarm wird gleich angeschaltet"); |
|
|
|
alarm = true; |
|
|
|
logger.log("Alarm an"); |
|
|
|
logger.log("Betrag: " + betrag + event.timestamp); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (betrag < schwelle) { |
|
|
|
logger.log("Alarm ist noch an; Neuer Betragswert unter Schwellwert: " + betrag); |
|
|
|
alarm = false; |
|
|
|
logger.log("Alarm wieder ausgeschaltet"); |
|
|
|
} else { |
|
|
|
logger.log("Betragswert immer noch über Schwellwert: " + betrag + "; Alarm bleibt an."); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
int i = 0; |
|
|
|
boolean stopp_mal = false; |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onSensorChanged(SensorEvent event) |
|
|
|
{ |
|
|
|
String sb = "t=" + event.timestamp + |
|
|
|
"\nx=" + event.values[0] + // Wert legend: x = 0.04 |
|
|
|
checkAlarm(event); |
|
|
|
|
|
|
|
//Alter Code, erstmal wild mit if statement ausgeschlossen |
|
|
|
if (stopp_mal) { |
|
|
|
if (i<numberOfValues) { |
|
|
|
calibrationMatrix[0][i] = event.values[0]; |
|
|
|
calibrationMatrix[1][i] = event.values[1]; |
|
|
|
calibrationMatrix[2][i] = event.values[2]; |
|
|
|
i++; |
|
|
|
} |
|
|
|
|
|
|
|
double sumX = 0; |
|
|
|
double sumY = 0; |
|
|
|
double sumZ = 0; |
|
|
|
for (int j = 0; j<numberOfValues;j++){ |
|
|
|
sumX += calibrationMatrix[0][j]; |
|
|
|
sumY += calibrationMatrix[1][j]; |
|
|
|
sumZ += calibrationMatrix[2][j]; |
|
|
|
} |
|
|
|
|
|
|
|
double averageX = sumX/numberOfValues; |
|
|
|
double averageY = sumY/numberOfValues; |
|
|
|
double averageZ = sumZ/numberOfValues; |
|
|
|
|
|
|
|
String sb2 = "t=" + event.timestamp + |
|
|
|
"\nx=" + event.values[0] + // Wert liegend: x = 0.04 |
|
|
|
"\ny=" + event.values[1] + // Wert liegend: y = 0.44 |
|
|
|
"\nz=" + event.values[2]; // Wert liegend: z = 9.90 = Erdbeschleunigung --> Wenn Ausrichtung unbekannt ist, müsste kalibrierung bei Start der Bewegungsüberwachung vorgenommen werden |
|
|
|
logger.clearLog(); |
|
|
|
logger.log(sb); |
|
|
|
"\nz=" + event.values[2] + // Wert liegend: z = 9.90 = Erdbeschleunigung |
|
|
|
"\nlin_x=" + linear_acceleration[0] + |
|
|
|
"\nlin_x=" + linear_acceleration[1] + |
|
|
|
"\nlin_x=" + linear_acceleration[2] + |
|
|
|
"\naverageX=" + averageX + |
|
|
|
"\naverageY=" + averageY + |
|
|
|
"\naverageZ=" + averageZ + |
|
|
|
"\ni=" + i; |
|
|
|
|
|
|
|
double schwelle = 5.0; |
|
|
|
|
|
|
|
if (alarm = false) { |
|
|
|
if (averageX + schwelle < event.values[0]) { |
|
|
|
alarm = true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (alarm = false) { |
|
|
|
if (averageY + schwelle < event.values[1]) { |
|
|
|
alarm = true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (alarm = false) { |
|
|
|
if (averageZ + schwelle < event.values[2]) { |
|
|
|
alarm = true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (alarm = true) { |
|
|
|
if (averageX + schwelle > event.values[0]) { |
|
|
|
alarm = false; |
|
|
|
} |
|
|
|
if (averageY + schwelle > event.values[1]) { |
|
|
|
alarm = false; |
|
|
|
} |
|
|
|
if (averageZ + schwelle > event.values[2]) { |
|
|
|
alarm = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logger.clearLog(); |
|
|
|
logger.log(sb2); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@@ -81,3 +196,20 @@ public class Beschleunigungssensor extends AppCompatActivity implements SensorEv |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Sample code for deque usage. Might be not useful. |
|
|
|
class MovingAverage { |
|
|
|
int size, windowSum = 0, count = 0; |
|
|
|
Deque queue = new ArrayDeque<Integer>(); |
|
|
|
public MovingAverage(int size) { |
|
|
|
this.size = size; |
|
|
|
} |
|
|
|
public double next(int val) { |
|
|
|
++count; |
|
|
|
// calculate the new sum by shifting the window |
|
|
|
queue.add(val); |
|
|
|
int tail = count > size ? (int)queue.poll() : 0; |
|
|
|
windowSum = windowSum - tail + val; |
|
|
|
return windowSum * 1.0 / Math.min(size, count); |
|
|
|
} |
|
|
|
} |