Advertisement
gogo92111

Program Code!

Dec 13th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. package bg.free.skill.myapplication;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.hardware.Sensor;
  8. import android.hardware.SensorEvent;
  9. import android.hardware.SensorEventListener;
  10. import android.hardware.SensorManager;
  11. import android.text.format.DateFormat;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15.  
  16. import java.util.Date;
  17.  
  18.  
  19. public class MainActivity extends AppCompatActivity implements SensorEventListener, StepListener {
  20. private TextView textView; // textview
  21. private StepDetector simpleStepDetector; // Detector import
  22. private SensorManager sensorManager; //manager import
  23. private Sensor accel; // sensor detect
  24. private static final String TEXT_NUM_STEPS = ""; // prosto prazen string
  25. private int numSteps; // broq na stupkite ++;
  26. TextView TvSteps;// Steps
  27. TextView TvSteps2; //fix
  28. Button BtnStart; //start
  29. Button BtnStop; //stop
  30. float kl = 0; //Kilometri
  31. private int save;
  32. public static final String nom = "mysettings";
  33. public static final String nim = "countera";
  34. public static final String nim1 = "counterb";
  35. private SharedPreferences spleef;
  36.  
  37.  
  38.  
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42.  
  43. setContentView(R.layout.activity_main);
  44. spleef = getSharedPreferences(nom, Context.MODE_PRIVATE);
  45.  
  46.  
  47. sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  48. accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  49. simpleStepDetector = new StepDetector();
  50. simpleStepDetector.registerListener(this);
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. BtnStart = (Button) findViewById(R.id.btn_start);
  61. BtnStop = (Button) findViewById(R.id.btn_stop);
  62.  
  63.  
  64.  
  65. BtnStart.setOnClickListener(new View.OnClickListener() {
  66.  
  67. @Override
  68. public void onClick(View arg0) {
  69.  
  70.  
  71. sensorManager.registerListener(MainActivity.this, accel, SensorManager.SENSOR_DELAY_FASTEST); // registrira
  72. numSteps = save;
  73. }
  74. });
  75.  
  76.  
  77. BtnStop.setOnClickListener(new View.OnClickListener() {
  78.  
  79. @Override
  80. public void onClick(View arg0) {
  81.  
  82. sensorManager.unregisterListener(MainActivity.this);
  83. save = numSteps;
  84. }
  85. });
  86.  
  87.  
  88.  
  89. }
  90. protected void onResume(){
  91. super.onResume();
  92. if (spleef.contains(nim)) {
  93. save = spleef.getInt(nim, save);
  94. kl = spleef.getFloat(nim1, kl);
  95. numSteps = save;
  96.  
  97. TvSteps = (TextView) findViewById(R.id.tv_steps);
  98. TvSteps2 = (TextView) findViewById(R.id.tv_steps2);
  99. TvSteps.setText(TEXT_NUM_STEPS + numSteps);
  100. TvSteps2.setText((float)Math.round(kl * 1000) / 1000.0 +" km");
  101. }
  102. }
  103.  
  104. protected void onPause() {
  105. super.onPause();
  106. SharedPreferences.Editor editor = spleef.edit();
  107. save = numSteps;
  108. editor.putInt(nim, save);
  109. editor.putFloat(nim1, kl);
  110.  
  111. editor.apply();
  112.  
  113.  
  114.  
  115. }
  116.  
  117.  
  118. @Override
  119. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  120. }
  121.  
  122. @Override
  123. public void onSensorChanged(SensorEvent event) {
  124. if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
  125. simpleStepDetector.updateAccel(
  126. event.timestamp, event.values[0], event.values[1], event.values[2]);
  127. }
  128. }
  129.  
  130. @Override
  131. public void step(long timeNs) {
  132. TvSteps = (TextView) findViewById(R.id.tv_steps);
  133. TvSteps2 = (TextView) findViewById(R.id.tv_steps2);
  134. numSteps++;
  135. kl += 0.001;
  136.  
  137. TvSteps.setText(TEXT_NUM_STEPS + numSteps);
  138. TvSteps2.setText((float)Math.round(kl * 1000) / 1000.0 +" km");
  139. }
  140.  
  141.  
  142. }
  143. ---------------------------------------------------------------------------------------------------------------------------------------
  144. StepListener
  145. ------------
  146. package bg.free.skill.myapplication;
  147.  
  148. public interface StepListener {
  149.  
  150. public void step(long timeNs);
  151. //Slushq za stupka kato executva TimeNz v mainActivity!
  152.  
  153. }
  154. ---------------------------------------------------------------------------------------------------------------------------------------
  155. SensorFilter
  156. ------------
  157. package bg.free.skill.myapplication;
  158.  
  159. public class SensorFilter {
  160.  
  161. private SensorFilter() {
  162. }
  163.  
  164. public static float sum(float[] array) {
  165. float retval = 0;
  166. for (int i = 0; i < array.length; i++) {
  167. retval += array[i];
  168. }
  169. return retval;
  170. }
  171.  
  172. public static float[] cross(float[] arrayA, float[] arrayB) {
  173. float[] retArray = new float[3];
  174. retArray[0] = arrayA[1] * arrayB[2] - arrayA[2] * arrayB[1];
  175. retArray[1] = arrayA[2] * arrayB[0] - arrayA[0] * arrayB[2];
  176. retArray[2] = arrayA[0] * arrayB[1] - arrayA[1] * arrayB[0];
  177. return retArray;
  178. }
  179.  
  180. public static float norm(float[] array) {
  181. float retval = 0;
  182. for (int i = 0; i < array.length; i++) {
  183. retval += array[i] * array[i];
  184. }
  185. return (float) Math.sqrt(retval);
  186. }
  187.  
  188.  
  189. public static float dot(float[] a, float[] b) {
  190. float retval = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  191. return retval;
  192. }
  193.  
  194. public static float[] normalize(float[] a) {
  195. float[] retval = new float[a.length];
  196. float norm = norm(a);
  197. // Tova super vajno ne barai i sant ot nego!
  198. for (int i = 0; i < a.length; i++) {
  199. retval[i] = a[i] / norm;
  200. }
  201. return retval;
  202. }
  203.  
  204. }
  205. ---------------------------------------------------------------------------------------------------------------------------------------
  206. StepDetector
  207. ------------
  208. package bg.free.skill.myapplication;
  209.  
  210. public class StepDetector {
  211.  
  212. private static final int ACCEL_RING_SIZE = 50;
  213. private static final int VEL_RING_SIZE = 10;
  214.  
  215. // dont touch ec_kirka
  216. private static final float STEP_THRESHOLD = 35f;
  217.  
  218. private static final int STEP_DELAY_NS = 250000000;
  219.  
  220. private int accelRingCounter = 0;
  221. private float[] accelRingX = new float[ACCEL_RING_SIZE];
  222. private float[] accelRingY = new float[ACCEL_RING_SIZE];
  223. private float[] accelRingZ = new float[ACCEL_RING_SIZE];
  224. private int velRingCounter = 0;
  225. private float[] velRing = new float[VEL_RING_SIZE];
  226. private long lastStepTimeNs = 0;
  227. private float oldVelocityEstimate = 0;
  228.  
  229. private StepListener listener;
  230.  
  231. public void registerListener(StepListener listener) {
  232. this.listener = listener;
  233. }
  234.  
  235.  
  236. public void updateAccel(long timeNs, float x, float y, float z) {
  237. float[] currentAccel = new float[3];
  238. currentAccel[0] = x;
  239. currentAccel[1] = y;
  240. currentAccel[2] = z;
  241.  
  242.  
  243. accelRingCounter++;
  244. accelRingX[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[0];
  245. accelRingY[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[1];
  246. accelRingZ[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[2];
  247.  
  248. float[] worldZ = new float[3];
  249. worldZ[0] = SensorFilter.sum(accelRingX) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
  250. worldZ[1] = SensorFilter.sum(accelRingY) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
  251. worldZ[2] = SensorFilter.sum(accelRingZ) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
  252.  
  253. float normalization_factor = SensorFilter.norm(worldZ);
  254.  
  255. worldZ[0] = worldZ[0] / normalization_factor;
  256. worldZ[1] = worldZ[1] / normalization_factor;
  257. worldZ[2] = worldZ[2] / normalization_factor;
  258.  
  259. float currentZ = SensorFilter.dot(worldZ, currentAccel) - normalization_factor;
  260. velRingCounter++;
  261. velRing[velRingCounter % VEL_RING_SIZE] = currentZ;
  262.  
  263. float velocityEstimate = SensorFilter.sum(velRing);
  264.  
  265. if (velocityEstimate > STEP_THRESHOLD && oldVelocityEstimate <= STEP_THRESHOLD
  266. && (timeNs - lastStepTimeNs > STEP_DELAY_NS)) {
  267. listener.step(timeNs);
  268. lastStepTimeNs = timeNs;
  269. }
  270. // Nz zashot ne raboti bez nego ama...
  271. oldVelocityEstimate = velocityEstimate;
  272. }
  273. }
  274. ---------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement