Guest User

Untitled

a guest
May 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. package com.spot.motion.util;
  2.  
  3. import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
  4. import com.sun.spot.sensorboard.peripheral.IAccelerometer3DThresholdListener;
  5. import com.sun.spot.sensorboard.peripheral.LIS3L02AQAccelerometer;
  6. import com.sun.spot.util.Utils;
  7.  
  8. /**
  9. * <code>AccelerometerRegulator</code>
  10. *
  11. * @author Yi Wang (Neakor)
  12. * @version Creation date: 20:18 10-12-2008
  13. * @version Modified date: 20:46 10-12-2008
  14. */
  15. public class AccelerometerRegulator implements IAccelerometer3DThresholdListener, Runnable {
  16. /**
  17. * The current sample rate.
  18. */
  19. private long rate;
  20. /**
  21. * The flag indicates if this regulator is finished.
  22. */
  23. private boolean finished;
  24. /**
  25. * The flag indicates if the current sample cycle is completed.
  26. */
  27. private boolean completed;
  28. /**
  29. * The largest acceleration value along x axis in the last cycle.
  30. */
  31. private double x;
  32. /**
  33. * The largest acceleration value along y axis in the last cycle.
  34. */
  35. private double y;
  36. /**
  37. * The largest acceleration value along z axis in the last cycle.
  38. */
  39. private double z;
  40. /**
  41. * The dominant movement axis of the last cycle.
  42. */
  43. private int dominantAxis;
  44. /**
  45. * The temporary elapsed time.
  46. */
  47. private long elapsed;
  48.  
  49. /**
  50. * Constructor of <code>AccelerometerRegulator</code>.
  51. * @param rate The <code>Long</code> sample rate.
  52. */
  53. public AccelerometerRegulator(long rate) {
  54. this.rate = rate;
  55. this.dominantAxis = -1;
  56. }
  57.  
  58. public void thresholdChanged(IAccelerometer3D accel, int axis, double low, double high, boolean relative) {
  59. // TODO Auto-generated method stub
  60.  
  61. }
  62.  
  63. public void thresholdExceeded(IAccelerometer3D accel, int axis, double val, boolean relative) {
  64. System.out.println("Entered");
  65. if(this.completed) return;
  66. System.out.println("Updating");
  67. switch(axis) {
  68. case LIS3L02AQAccelerometer.X_AXIS:
  69. if(val > this.x) this.x = val;
  70. break;
  71. case LIS3L02AQAccelerometer.Y_AXIS:
  72. if(val > this.y) this.y = val;
  73. break;
  74. case LIS3L02AQAccelerometer.Z_AXIS:
  75. if(val > this.z) this.z = val;
  76. break;
  77. }
  78. }
  79.  
  80. public void run() {
  81. while(!this.finished) {
  82. while(!this.completed) {
  83. Utils.sleep(1);
  84. this.elapsed++;
  85. if(this.elapsed >= this.rate) this.completed = true;
  86. }
  87. double max = Math.max(Math.max(this.x, this.y), this.z);
  88. if(this.x == max) this.dominantAxis = 0;
  89. else if(this.y == max) this.dominantAxis = 1;
  90. else this.dominantAxis = 2;
  91. if(this.dominantAxis != -1) System.out.println("Moved along axis: " + this.dominantAxis);
  92. this.reset();
  93. }
  94. }
  95.  
  96. private void reset() {
  97. this.completed = false;
  98. this.elapsed = 0;
  99. this.dominantAxis = -1;
  100. this.x = 0;
  101. this.y = 0;
  102. this.z = 0;
  103. }
  104. }
Add Comment
Please, Sign In to add comment