Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LSM303.h>
  3. #include <LiquidCrystal.h>
  4.  
  5. #define SIZE 50
  6. #define WINDOW_SIZE 4
  7.  
  8. LSM303 compass;
  9. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  10. int running_min[3], running_max[3], last_a[3], curr_a[3];
  11.  
  12. // The previous 4 readings, used to find the current reading which is their average.
  13. // Use idx as an index.
  14. int sumX, sumY, sumZ, idx;
  15. int xSamples[4], ySamples[4], zSamples[4];
  16.  
  17. int samplingCounter, steps;
  18. int pre_steps = 0;
  19.  
  20. double stride = 0;
  21. double height = 170;
  22. double steps_to_strides[] = {height/5.0, height/5.0, height/5.0, height/4.0, height/3.0, height/2.0, height/1.2, height, height, 1.2 * height};
  23. double dist = 0;
  24. double velocity = 0;
  25. double calories = 0;
  26. double weight = 60;
  27.  
  28. unsigned long prevMillis;
  29.  
  30. unsigned long time;
  31.  
  32. // Unused.
  33. //int badFlag;
  34.  
  35. int dynamicPrecision[3], dynamicThreshold[3];
  36.  
  37. void reInit(){
  38.   samplingCounter = 0;
  39.  
  40.   int p2pX = running_max[0] - running_min[0];
  41.   int p2pY = running_max[1] - running_min[1];
  42.   int p2pZ = running_max[2] - running_min[2];
  43.  
  44.   //badFlag = 0;
  45.  
  46.   dynamicPrecision[0] = p2pX / 10;
  47.   dynamicPrecision[1] = p2pY / 10;
  48.   dynamicPrecision[2] = p2pZ / 10;
  49.   //Serial.print("Dynamic Precision : ");
  50.   //Serial.println(dynamicPrecision);
  51.  
  52.   int avgX = (running_max[0] + running_min[0]) / 2;
  53.   int avgY = (running_max[1] + running_min[1]) / 2;
  54.   int avgZ = (running_max[2] + running_min[2]) / 2;
  55.  
  56.   dynamicThreshold[0] = avgX;
  57.   dynamicThreshold[1] = avgY;
  58.   dynamicThreshold[2] = avgZ;
  59.  
  60.   running_min[0] = running_min[1] = running_min[2] = 32767;
  61.   running_max[0] = running_max[1] = running_max[2] = -32768;
  62. }
  63.  
  64. bool check_valid_read() {
  65.   return abs(sumX/4-curr_a[0]) >= dynamicPrecision[0] | abs(sumY/4-curr_a[1]) >= dynamicPrecision[1]
  66.           | abs(sumZ/4-curr_a[2]) >= dynamicPrecision[2];
  67. }
  68.  
  69. bool read(){
  70.   sumX -= xSamples[idx];
  71.   sumY -= ySamples[idx];
  72.   sumZ -= zSamples[idx];
  73.  
  74.   compass.read();
  75.  
  76.   xSamples[idx] = compass.a.x;
  77.   ySamples[idx] = compass.a.y;
  78.   zSamples[idx] = compass.a.z;
  79.  
  80.   sumX += xSamples[idx];
  81.   sumY += ySamples[idx];
  82.   sumZ += zSamples[idx];
  83.  
  84.   if(!check_valid_read())
  85.     return false;
  86.  
  87.   idx = (idx + 1) % 4;
  88.  
  89.   for(int i = 0; i < 3; ++i)
  90.     last_a[i] = curr_a[i];
  91.  
  92.   curr_a[0] = sumX / 4;
  93.   curr_a[1] = sumY / 4;
  94.   curr_a[2] = sumZ / 4;
  95.  
  96.   for(int i = 0; i < 3; ++i){
  97.     running_min[i] = min(running_min[i], curr_a[i]);
  98.     running_max[i] = max(running_max[i], curr_a[i]);
  99.   }
  100.  
  101.   /*
  102.   curr_a[0] = compass.a.x;
  103.   curr_a[1] = compass.a.y;
  104.   curr_a[2] = compass.a.z;
  105.   */
  106.  
  107.   //Serial.println("readings");
  108.   Serial.print(curr_a[0]);
  109.   Serial.print(" ");
  110.   Serial.print(curr_a[1]);
  111.   Serial.print(" ");
  112.   Serial.println(curr_a[2]);
  113.  
  114.   return true;
  115. }
  116.  
  117. bool checkStep(){
  118.   if(!read())
  119.       return false;
  120.      
  121.   unsigned long currTime = millis();
  122.   if(currTime - time < 200) return true;
  123.   time = currTime;
  124.  
  125.   for(int i = 0; i < 3; ++i){
  126.     int d = abs(last_a[i] - curr_a[i]);
  127.     if(d >= dynamicPrecision[i] && last_a[i] > dynamicThreshold[i] && curr_a[i] < dynamicThreshold[i]){
  128.        ++steps;
  129.        break;
  130.     }
  131.   }
  132.   if (millis() - prevMillis > 2000){
  133.     prevMillis = millis();
  134.     stride = steps_to_strides[min(9, steps - pre_steps)];
  135.     dist += stride;
  136.     velocity = (steps - pre_steps) * stride;
  137.     if (velocity == 0){
  138.       calories += weight / 1800.0; // calories burnt while resting
  139.     }
  140.     else {
  141.       calories += velocity * weight / 400.0;
  142.     }
  143.     pre_steps = steps;
  144.   }
  145.   return true;
  146. }
  147.  
  148. void setup(){
  149.  
  150.   Serial.begin(9600);
  151.   Wire.begin();
  152.   compass.init();
  153.   compass.enableDefault();
  154.   lcd.begin(16,2);
  155.   prevMillis = millis();
  156.  
  157.   running_min[0] = running_min[1] = running_min[2] = 32767;
  158.   running_max[0] = running_max[1] = running_max[2] = -32768;
  159.   for(int i = 0; i < 20; ++i) read();
  160.   reInit();
  161. }
  162.  
  163. void loop(){
  164.   if(samplingCounter == 20) reInit();
  165.   samplingCounter += checkStep();
  166.   //Serial.print("steps :");
  167.   //Serial.println(steps);
  168.  
  169.   lcd.setCursor(0,0);
  170.   lcd.print("Cnt:");
  171.   lcd.print(steps);
  172.   lcd.print(' ');
  173.   lcd.print("D:");
  174.   lcd.print(dist);
  175.   lcd.setCursor(0,1);
  176.   lcd.print("V:");
  177.   lcd.print(velocity);
  178.   lcd.print(' ');
  179.   lcd.print("C:");
  180.   lcd.print(calories);
  181.  
  182.   delay(100);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement