|
|
@@ -0,0 +1,124 @@ |
|
|
|
package com.example.ueberwachungssystem; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
import android.hardware.Sensor; |
|
|
|
import android.hardware.SensorEvent; |
|
|
|
import android.hardware.SensorEventListener; |
|
|
|
import android.hardware.SensorManager; |
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
|
|
|
|
|
|
|
|
public class Accelerometer implements Runnable, SensorEventListener { |
|
|
|
|
|
|
|
Logger logger; |
|
|
|
private volatile boolean running = false; |
|
|
|
public SensorManager sensorManager; |
|
|
|
private int sensorType = Sensor.TYPE_ACCELEROMETER; |
|
|
|
private Sensor accelerometer; |
|
|
|
private Context context; |
|
|
|
private Thread thread; |
|
|
|
private String threadName = "Accelerometer Thread"; |
|
|
|
double[] gravity = new double[3]; |
|
|
|
double[] linear_acceleration = new double[3]; |
|
|
|
private TextView textViewWorkerThread; |
|
|
|
|
|
|
|
// In constructor pass Context from MainActivity in Accelerometer class |
|
|
|
public Accelerometer(Context context, TextView textViewWorkerThread){ |
|
|
|
this.context = context; |
|
|
|
this.textViewWorkerThread = textViewWorkerThread; |
|
|
|
} |
|
|
|
// Passing Activity's instance as argument on worker thread |
|
|
|
AppCompatActivity activity; |
|
|
|
public Accelerometer(AppCompatActivity activity){ |
|
|
|
this.activity = activity; |
|
|
|
} |
|
|
|
|
|
|
|
private void print (final String s){ |
|
|
|
activity.runOnUiThread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
textViewWorkerThread.setText(s); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
while (running) { |
|
|
|
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); |
|
|
|
if (sensorManager.getSensorList(sensorType).size() == 0) { |
|
|
|
logger.log("Es gibt den gewünschten Sensor nicht"); |
|
|
|
accelerometer = null; |
|
|
|
} else { |
|
|
|
accelerometer = sensorManager.getSensorList(sensorType).get(0); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
void start(){ |
|
|
|
logger.log("Starting" + threadName); |
|
|
|
running = true; |
|
|
|
thread = new Thread(this); |
|
|
|
thread.setName(threadName); |
|
|
|
thread.start(); |
|
|
|
logger.log("alright, " + threadName + " started!"); |
|
|
|
|
|
|
|
// register the sensor before using // |
|
|
|
sensorManager.registerListener((SensorEventListener) context, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); |
|
|
|
logger.log("Accelerometer, du bist dran!"); |
|
|
|
} |
|
|
|
|
|
|
|
void stop() { |
|
|
|
// unregister from the sensor to stop using it // |
|
|
|
sensorManager.unregisterListener((SensorEventListener) context, accelerometer); |
|
|
|
logger.log("Das reicht Accelerometer, komm zurück!"); |
|
|
|
if (!running){ |
|
|
|
logger.log(threadName + " not running."); |
|
|
|
} else { |
|
|
|
logger.log("Stopping " + threadName); |
|
|
|
running = false; |
|
|
|
while (true) { |
|
|
|
try { |
|
|
|
thread.join(); |
|
|
|
logger.log("..." + threadName + " stopped"); |
|
|
|
break; |
|
|
|
} catch (InterruptedException e){ |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onSensorChanged(SensorEvent event) { |
|
|
|
// Highpass filter to filter out gravity influence |
|
|
|
final double alpha = 0.8; |
|
|
|
|
|
|
|
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; |
|
|
|
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; |
|
|
|
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2]; |
|
|
|
linear_acceleration[0] = event.values[0] - gravity[0]; |
|
|
|
|
|
|
|
linear_acceleration[1] = event.values[1] - gravity[1]; |
|
|
|
linear_acceleration[2] = event.values[2] - gravity[2]; |
|
|
|
|
|
|
|
String sb = "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 |
|
|
|
"\nlin_x=" + linear_acceleration[0] + |
|
|
|
"\nlin_x=" + linear_acceleration[1] + |
|
|
|
"\nlin_x=" + linear_acceleration[2]; |
|
|
|
|
|
|
|
logger.clearLog(); |
|
|
|
logger.log(sb); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) { |
|
|
|
|
|
|
|
} |
|
|
|
} |