Beschleunigungssensor: Gyroscope und kalibrierung
This commit is contained in:
parent
4c0c4416fc
commit
a111765677
@ -9,6 +9,9 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.GreenWatch">
|
android:theme="@style/Theme.GreenWatch">
|
||||||
|
<activity
|
||||||
|
android:name=".Beschleunigungssensor"
|
||||||
|
android:exported="false" />
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
|
@ -1,47 +1,139 @@
|
|||||||
package com.example.greenwatch;
|
package com.example.greenwatch;
|
||||||
|
|
||||||
import android.content.Context;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import android.hardware.Sensor;
|
import android.hardware.Sensor;
|
||||||
import android.hardware.SensorEvent;
|
import android.hardware.SensorEvent;
|
||||||
import android.hardware.SensorEventListener;
|
import android.hardware.SensorEventListener;
|
||||||
import android.hardware.SensorManager;
|
import android.hardware.SensorManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public class Beschleunigungssensor extends AppCompatActivity implements SensorEventListener {
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Beschleunigungssensor extends AppCompatActivity implements SensorEventListener{
|
||||||
|
|
||||||
private SensorManager bsmanager;
|
private SensorManager bsmanager;
|
||||||
private int sensorType = Sensor.TYPE_ACCELEROMETER;
|
private ArrayList<Float> Gesamt_be;
|
||||||
private Sensor Beschleunigungssensor;
|
private int sensorType = Sensor.TYPE_GYROSCOPE, zaehler_runde =0, listen_groesse = 10000;
|
||||||
|
private Sensor sens, sensor_l;
|
||||||
|
private float x_value = 0, y_value, z_value, Vorbesetzung = 0, gesamt_runde =0;
|
||||||
|
private double Schwellwert = 0.5, mittelwertsumme = 0.0, Offset;
|
||||||
|
String Daten, Warnung, Zeitstempel;
|
||||||
|
TextView Daten_Bsensor, Warnung_Bsensor, test;
|
||||||
|
private boolean toggle = true, ts_setzen = true, kalibrieren;
|
||||||
|
|
||||||
public Beschleunigungssensor() {
|
@Override
|
||||||
bsmanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
if(bsmanager.getSensorList(sensorType).size()==0)
|
super.onCreate(savedInstanceState);
|
||||||
{
|
setContentView(R.layout.activity_beschleunigungssensor);
|
||||||
//logger.log("Es gibt den gewünschten Sensor
|
|
||||||
//nicht");
|
Daten_Bsensor = (TextView) findViewById(R.id.DatenBsensor);
|
||||||
Beschleunigungssensor = null;
|
Warnung_Bsensor = (TextView) findViewById(R.id.WarnungBsensor);
|
||||||
|
test = (TextView) findViewById(R.id.test);
|
||||||
|
|
||||||
|
bsmanager = (SensorManager) getSystemService(SENSOR_SERVICE);
|
||||||
|
if (bsmanager.getSensorList(Sensor.TYPE_GYROSCOPE).size() == 0) {
|
||||||
|
sens = null;
|
||||||
|
Daten = "kein B Sensor voranden";
|
||||||
|
} else {
|
||||||
|
sens = bsmanager.getSensorList(sensorType).get(0);
|
||||||
}
|
}
|
||||||
else
|
Gesamt_be = new ArrayList<Float>();
|
||||||
{
|
for (int i = 0; i < listen_groesse; i++){
|
||||||
Beschleunigungssensor = bsmanager.getSensorList(sensorType).get(0);
|
Gesamt_be.add(Vorbesetzung);
|
||||||
}
|
}
|
||||||
|
kalibrieren = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getTimestamp(){
|
||||||
|
Long tslong = System.currentTimeMillis();
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy, HH:mm:ss");
|
||||||
|
Date date = new Date(tslong);
|
||||||
|
String result = sdf.format(date);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSensorChanged(SensorEvent event) {
|
public void onSensorChanged(SensorEvent event) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("t=").append(event.timestamp)
|
sb.append("x=").append(event.values[0])//.append(event.timestamp)
|
||||||
.append("\nx=").append(event.values[0])
|
|
||||||
.append("\ny=").append(event.values[1])
|
.append("\ny=").append(event.values[1])
|
||||||
.append("\nz=").append(event.values[2]);
|
.append("\nz=").append(event.values[2]);
|
||||||
|
Daten = sb.toString();
|
||||||
|
Daten_Bsensor.setText(Daten);
|
||||||
|
gesamt_runde = event.values[0] + event.values[1] + event.values[2];
|
||||||
|
Gesamt_be.remove(zaehler_runde);
|
||||||
|
Gesamt_be.add(zaehler_runde, gesamt_runde);
|
||||||
|
mittelwertsumme = 0.0;
|
||||||
|
//Mittelwert des Arrays berechnen
|
||||||
|
for (int i = 0; i < listen_groesse; i++){
|
||||||
|
if (Gesamt_be.get(i) < 0){
|
||||||
|
mittelwertsumme += (Gesamt_be.get(i) * (-1));
|
||||||
|
}else {
|
||||||
|
mittelwertsumme += Gesamt_be.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mittelwertsumme = mittelwertsumme/listen_groesse;
|
||||||
|
if (kalibrieren == true){
|
||||||
|
Offset = mittelwertsumme;
|
||||||
|
kalibrieren = false;
|
||||||
|
}
|
||||||
|
mittelwertsumme = mittelwertsumme - Offset;
|
||||||
|
StringBuilder testsb = new StringBuilder();
|
||||||
|
testsb.append(mittelwertsumme);
|
||||||
|
String tests = testsb.toString();
|
||||||
|
test.setText(tests);
|
||||||
|
|
||||||
|
if((mittelwertsumme > Schwellwert ) & (ts_setzen == true)){
|
||||||
|
Zeitstempel = getTimestamp();
|
||||||
|
StringBuilder ts = new StringBuilder();
|
||||||
|
ts.append("Warnung: ")
|
||||||
|
.append(Zeitstempel)
|
||||||
|
.append("\n")
|
||||||
|
.append(mittelwertsumme);
|
||||||
|
Warnung = ts.toString();
|
||||||
|
Warnung_Bsensor.setText(Warnung);
|
||||||
|
ts_setzen = false;
|
||||||
|
}else if((mittelwertsumme > Schwellwert) & (ts_setzen == false)){
|
||||||
|
} else if ((mittelwertsumme < Schwellwert) & (ts_setzen == false)) {
|
||||||
|
Warnung_Bsensor.setText("");
|
||||||
|
ts_setzen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (zaehler_runde < (listen_groesse -1)){
|
||||||
|
zaehler_runde++;
|
||||||
|
}else {
|
||||||
|
zaehler_runde = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Einstellen um bei Pausen keine Werte zu speichern -> höhere Priorität auf großen Bewegungen
|
||||||
|
//Vektor verlängern um Pausen geringer zu gewichten
|
||||||
|
//Kalibrierung: Vektor einmal füllen -> Durchschnitt berechnen,
|
||||||
|
//bei Schwellwert Ruhe Erwartungswert abziehen
|
||||||
|
@Override
|
||||||
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
if (sens != null) {
|
||||||
|
if (bsmanager.registerListener(this, sens, SensorManager.SENSOR_DELAY_GAME)) {
|
||||||
|
Daten = "Wir haben uns beim Sensor angemeldet";
|
||||||
|
} else {
|
||||||
|
Daten = "Das anmelden hat nicht geklappt";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
|
protected void onPause() {
|
||||||
// if Abfrage ob values sich verändert haben
|
super.onPause();
|
||||||
}
|
if (sens != null) {
|
||||||
|
bsmanager.unregisterListener(this, sens);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,13 +2,40 @@ package com.example.greenwatch;
|
|||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
private Button Beschleunigungssensor;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
final Button Beschleunigungssensor = (Button) findViewById(R.id.beschleunigungssensor);
|
||||||
|
Beschleunigungssensor.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
Intent intent = new Intent(MainActivity.this, Beschleunigungssensor.class);
|
||||||
|
startActivity(intent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
}
|
}
|
||||||
}
|
}
|
32
app/src/main/res/layout/activity_beschleunigungssensor.xml
Normal file
32
app/src/main/res/layout/activity_beschleunigungssensor.xml
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
tools:context=".Beschleunigungssensor">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/DatenBsensor"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/WarnungBsensor"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/test"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="" />
|
||||||
|
</LinearLayout>
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -6,13 +6,42 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<TextView
|
<LinearLayout
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="match_parent"
|
||||||
android:text="Hello World!"
|
android:orientation="vertical">
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintLeft_toLeftOf="parent"
|
<TextView
|
||||||
app:layout_constraintRight_toRightOf="parent"
|
android:id="@+id/textView"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="TextView"
|
||||||
|
tools:layout_editor_absoluteX="171dp"
|
||||||
|
tools:layout_editor_absoluteY="186dp" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView2"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="TextView" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/beschleunigungssensor"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Beschleunigungssensor" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/aufnahme"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Aufnahme Start" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/wiedergabe"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Wiedergabe" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
x
Reference in New Issue
Block a user