Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bg.free.skill.myapplication;
- import android.content.Context;
- import android.content.SharedPreferences;
- 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.text.format.DateFormat;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import java.util.Date;
- public class MainActivity extends AppCompatActivity implements SensorEventListener, StepListener {
- private TextView textView; // textview
- private StepDetector simpleStepDetector; // Detector import
- private SensorManager sensorManager; //manager import
- private Sensor accel; // sensor detect
- private static final String TEXT_NUM_STEPS = ""; // prosto prazen string
- private int numSteps; // broq na stupkite ++;
- TextView TvSteps;// Steps
- TextView TvSteps2; //fix
- Button BtnStart; //start
- Button BtnStop; //stop
- float kl = 0; //Kilometri
- private int save;
- public static final String nom = "mysettings";
- public static final String nim = "countera";
- public static final String nim1 = "counterb";
- private SharedPreferences spleef;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- spleef = getSharedPreferences(nom, Context.MODE_PRIVATE);
- sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- simpleStepDetector = new StepDetector();
- simpleStepDetector.registerListener(this);
- BtnStart = (Button) findViewById(R.id.btn_start);
- BtnStop = (Button) findViewById(R.id.btn_stop);
- BtnStart.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- sensorManager.registerListener(MainActivity.this, accel, SensorManager.SENSOR_DELAY_FASTEST); // registrira
- numSteps = save;
- }
- });
- BtnStop.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- sensorManager.unregisterListener(MainActivity.this);
- save = numSteps;
- }
- });
- }
- protected void onResume(){
- super.onResume();
- if (spleef.contains(nim)) {
- save = spleef.getInt(nim, save);
- kl = spleef.getFloat(nim1, kl);
- numSteps = save;
- TvSteps = (TextView) findViewById(R.id.tv_steps);
- TvSteps2 = (TextView) findViewById(R.id.tv_steps2);
- TvSteps.setText(TEXT_NUM_STEPS + numSteps);
- TvSteps2.setText((float)Math.round(kl * 1000) / 1000.0 +" km");
- }
- }
- protected void onPause() {
- super.onPause();
- SharedPreferences.Editor editor = spleef.edit();
- save = numSteps;
- editor.putInt(nim, save);
- editor.putFloat(nim1, kl);
- editor.apply();
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
- @Override
- public void onSensorChanged(SensorEvent event) {
- if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
- simpleStepDetector.updateAccel(
- event.timestamp, event.values[0], event.values[1], event.values[2]);
- }
- }
- @Override
- public void step(long timeNs) {
- TvSteps = (TextView) findViewById(R.id.tv_steps);
- TvSteps2 = (TextView) findViewById(R.id.tv_steps2);
- numSteps++;
- kl += 0.001;
- TvSteps.setText(TEXT_NUM_STEPS + numSteps);
- TvSteps2.setText((float)Math.round(kl * 1000) / 1000.0 +" km");
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- StepListener
- ------------
- package bg.free.skill.myapplication;
- public interface StepListener {
- public void step(long timeNs);
- //Slushq za stupka kato executva TimeNz v mainActivity!
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- SensorFilter
- ------------
- package bg.free.skill.myapplication;
- public class SensorFilter {
- private SensorFilter() {
- }
- public static float sum(float[] array) {
- float retval = 0;
- for (int i = 0; i < array.length; i++) {
- retval += array[i];
- }
- return retval;
- }
- public static float[] cross(float[] arrayA, float[] arrayB) {
- float[] retArray = new float[3];
- retArray[0] = arrayA[1] * arrayB[2] - arrayA[2] * arrayB[1];
- retArray[1] = arrayA[2] * arrayB[0] - arrayA[0] * arrayB[2];
- retArray[2] = arrayA[0] * arrayB[1] - arrayA[1] * arrayB[0];
- return retArray;
- }
- public static float norm(float[] array) {
- float retval = 0;
- for (int i = 0; i < array.length; i++) {
- retval += array[i] * array[i];
- }
- return (float) Math.sqrt(retval);
- }
- public static float dot(float[] a, float[] b) {
- float retval = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
- return retval;
- }
- public static float[] normalize(float[] a) {
- float[] retval = new float[a.length];
- float norm = norm(a);
- // Tova super vajno ne barai i sant ot nego!
- for (int i = 0; i < a.length; i++) {
- retval[i] = a[i] / norm;
- }
- return retval;
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- StepDetector
- ------------
- package bg.free.skill.myapplication;
- public class StepDetector {
- private static final int ACCEL_RING_SIZE = 50;
- private static final int VEL_RING_SIZE = 10;
- // dont touch ec_kirka
- private static final float STEP_THRESHOLD = 35f;
- private static final int STEP_DELAY_NS = 250000000;
- private int accelRingCounter = 0;
- private float[] accelRingX = new float[ACCEL_RING_SIZE];
- private float[] accelRingY = new float[ACCEL_RING_SIZE];
- private float[] accelRingZ = new float[ACCEL_RING_SIZE];
- private int velRingCounter = 0;
- private float[] velRing = new float[VEL_RING_SIZE];
- private long lastStepTimeNs = 0;
- private float oldVelocityEstimate = 0;
- private StepListener listener;
- public void registerListener(StepListener listener) {
- this.listener = listener;
- }
- public void updateAccel(long timeNs, float x, float y, float z) {
- float[] currentAccel = new float[3];
- currentAccel[0] = x;
- currentAccel[1] = y;
- currentAccel[2] = z;
- accelRingCounter++;
- accelRingX[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[0];
- accelRingY[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[1];
- accelRingZ[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[2];
- float[] worldZ = new float[3];
- worldZ[0] = SensorFilter.sum(accelRingX) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
- worldZ[1] = SensorFilter.sum(accelRingY) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
- worldZ[2] = SensorFilter.sum(accelRingZ) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
- float normalization_factor = SensorFilter.norm(worldZ);
- worldZ[0] = worldZ[0] / normalization_factor;
- worldZ[1] = worldZ[1] / normalization_factor;
- worldZ[2] = worldZ[2] / normalization_factor;
- float currentZ = SensorFilter.dot(worldZ, currentAccel) - normalization_factor;
- velRingCounter++;
- velRing[velRingCounter % VEL_RING_SIZE] = currentZ;
- float velocityEstimate = SensorFilter.sum(velRing);
- if (velocityEstimate > STEP_THRESHOLD && oldVelocityEstimate <= STEP_THRESHOLD
- && (timeNs - lastStepTimeNs > STEP_DELAY_NS)) {
- listener.step(timeNs);
- lastStepTimeNs = timeNs;
- }
- // Nz zashot ne raboti bez nego ama...
- oldVelocityEstimate = velocityEstimate;
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement