Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. /*
  2. The sensor outputs provided by the library are the raw 16-bit values
  3. obtained by concatenating the 8-bit high and low accelerometer and
  4. magnetometer data registers. They can be converted to units of g and
  5. gauss using the conversion factors specified in the datasheet for your
  6. particular device and full scale setting (gain).
  7.  
  8. Eyample: An LSM303D gives a magnetometer y axis reading of 1982 with
  9. its default full scale setting of +/- 4 gauss. The M_GN specification
  10. in the LSM303D datasheet (page 10) states a conversion factor of 0.160
  11. mgauss/LSB (least significant bit) at this FS setting, so the raw
  12. reading of -1982 corresponds to 1982 * 0.160 = 317.1 mgauss =
  13. 0.3171 gauss.
  14.  
  15. In the LSM303DLHC, LSM303DLM, and LSM303DLH, the acceleration data
  16. registers actually contain a left-aligned 12-bit number, so the lowest
  17. 4 bits are always 0, and the values should be shifted right by 4 bits
  18. (divided by 16) to be consistent with the conversion factors specified
  19. in the datasheets.
  20.  
  21. Example: An LSM303DLH gives an accelerometer Z axis reading of -16144
  22. with its default full scale setting of +/- 2 g. Dropping the lowest 4
  23. bits gives a 12-bit raw value of -1009. The LA_So specification in the
  24. LSM303DLH datasheet (page 11) states a conversion factor of 1 mg/digit
  25. at this FS setting, so the value of -1009 corresponds to -1009 * 1 =
  26. 1009 mg = 1.009 g.
  27. */
  28.  
  29. #include <Wire.h>
  30. #include <LSM303.h>
  31.  
  32. LSM303 compass;
  33.  
  34. char report[80];
  35. int start = 1,initial = 100,xMax , yMax, zMax , xMin, yMin , zMin;
  36. int xArr [100],yArr[100],zArr[100];
  37. int xArrSm [34],yArrSm[34],zArrSm[34];
  38. int movingNum =0;
  39. int average =0;
  40. int SumArr[4];
  41. int steps=0 , counter =1 , current , prev,endIndex=0 , last , lastLast, stepDist = 40, distance =0,calories = 0, velocity,weight = 62;
  42.  
  43. void setup()
  44. {
  45. Serial.begin(9600);
  46. Wire.begin();
  47. compass.init();
  48. compass.enableDefault();
  49. }
  50.  
  51. void loop()
  52. {
  53. for (int i = 1 ; i<initial && start;i++){
  54. compass.read();
  55. snprintf(report, sizeof(report), "A: %6d %6d %6d M: %6d %6d %6d",
  56. compass.a.x, compass.a.y, compass.a.z,
  57. compass.m.x, compass.m.y, compass.m.z);
  58. Serial.println(report);
  59.  
  60. xArr[i] = compass.a.x;
  61. yArr[i] = compass.a.y;
  62. zArr[i] = compass.a.z;
  63. //smooth data
  64. if (i %3 == 0 ){
  65. if(i/3 == 1 ){
  66. Serial.print("just for test about 10sec get ready start moving!!");
  67. xArrSm[i/3] = (xArr[i] + xArr[i-1] + xArr[i-2])/3;
  68. yArrSm[i/3] = (yArr[i] + yArr[i-1] + yArr[i-2])/3;
  69. zArrSm[i/3] = (zArr[i] + zArr[i-1] + zArr[i-2])/3;
  70. xMax = xArrSm[i/3], yMax= yArrSm[i/3], zMax =zArrSm[i/3], xArrSm[i/3], yMin=yArrSm[i/3] , zMin=zArrSm[i/3] ;
  71.  
  72. }else{
  73. xArrSm[i/3] = (xArr[i] + xArr[i-1] + xArr[i-2])/3;
  74. yArrSm[i/3] = (yArr[i] + yArr[i-1] + yArr[i-2])/3;
  75. zArrSm[i/3] = (zArr[i] + zArr[i-1] + zArr[i-2])/3;
  76. if (xArrSm[i/3] > xMax){
  77. xMax = xArrSm[i/3] ;
  78. }else if(xArrSm[i/3] < xMin){
  79. xMin = xArrSm[i/3];
  80. }
  81. if (yArrSm[i/3] > yMax){
  82. yMax = yArrSm[i/3];
  83. }else if(yArrSm[i/3] < yMin){
  84. yMin =yArrSm[i/3];
  85. }
  86. if (zArrSm[i/3] > zMax){
  87. zMax = zArrSm[i/3];
  88. }else if(zArrSm[i/3] < zMin){
  89. zMin = zArrSm[i/3] ;
  90. }
  91. }
  92. }
  93. endIndex = i;
  94. delay(100);
  95. }
  96. //calculate average
  97. if (start){
  98. int xDiff = xMax-xMin , yDiff = yMax-yMin , zDiff = zMax-zMin;
  99. int xSum = xMax+xMin , ySum = yMax+yMin , zSum = zMax+zMin;
  100. SumArr[1] = xSum; SumArr[2] = ySum; SumArr[3] = zSum;
  101. int maxNum = xDiff;
  102. movingNum = 1 ;
  103. if (yDiff > maxNum){
  104. maxNum = yDiff;
  105. movingNum = 2;
  106. }
  107. if (zDiff > maxNum){
  108. maxNum = zDiff;
  109. movingNum = 3 ;
  110. }
  111. average = SumArr[movingNum]/2;
  112. Serial.println("we will start right now!!");
  113. }
  114.  
  115. start = 0 ;
  116. compass.read();
  117. snprintf(report, sizeof(report), "A: %6d %6d %6d M: %6d %6d %6d",
  118. compass.a.x, compass.a.y, compass.a.z,
  119. compass.m.x, compass.m.y, compass.m.z);
  120. Serial.println(report);
  121.  
  122. if(movingNum == 1){
  123. if(counter % 3 == 0){
  124. if ( counter == 3){
  125. current = last + lastLast + compass.a.x;
  126. prev = current;
  127. } else{
  128. prev = current;
  129. current = last + lastLast + compass.a.x;
  130. }
  131. if(prev>current && prev >average && current < average){
  132. steps++;
  133. distance = stepDist * steps;
  134. Serial.print("steps updated:");
  135. Serial.println(steps);
  136. Serial.print("distance travelled updated:");
  137. Serial.println(distance);
  138. Serial.print("calories:");
  139. calories = velocity * weight/400;
  140. Serial.println(calories);
  141.  
  142. }
  143. }else if(counter % 3 == 1){
  144. last = compass.a.x;
  145. }else if(counter % 3 == 2){
  146. lastLast = compass.a.x;
  147. }
  148. counter++;
  149. }else if (movingNum == 2){
  150. if(counter % 3 == 0){
  151. if ( counter == 3){
  152. current = last + lastLast + compass.a.y;
  153. prev = current;
  154. } else{
  155. prev = current;
  156. current = last + lastLast + compass.a.y;
  157. }
  158. if(prev>current && prev >average && current < average){
  159. steps++;
  160. distance = stepDist * steps;
  161. Serial.print("steps updated:");
  162. Serial.println(steps);
  163. Serial.print("distance travelled updated:");
  164. Serial.println(distance);
  165. Serial.print("calories:");
  166. calories = velocity * weight/400;
  167. Serial.println(calories);
  168.  
  169. }
  170. }else if(counter % 3 == 1){
  171. last = compass.a.y;
  172. }else if(counter % 3 == 2){
  173. lastLast = compass.a.y;
  174. }
  175. counter++;
  176. }else if (movingNum == 3){
  177. if(counter % 3 == 0){
  178. if ( counter == 3){
  179. current = last + lastLast + compass.a.z;
  180. prev = current;
  181. } else{
  182. prev = current;
  183. current = last + lastLast + compass.a.z;
  184. }
  185. if(prev>current && prev >average && current < average){
  186. steps++;
  187. distance = stepDist * steps;
  188. Serial.print("steps updated:");
  189. Serial.println(steps);
  190. Serial.print("distance travelled updated:");
  191. Serial.println(distance);
  192. Serial.print("calories:");
  193. calories = velocity * weight/400;
  194. Serial.println(calories);
  195.  
  196. }
  197. }else if(counter % 3 == 1){
  198. last = compass.a.z;
  199. }else if(counter % 3 == 2){
  200. lastLast = compass.a.z;
  201. }
  202. counter++;
  203. }
  204. //Serial.print("x:");
  205. //Serial.println(xx);
  206.  
  207.  
  208. delay(100);
  209.  
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement