Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.spot.motion.util;
- import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
- import com.sun.spot.sensorboard.peripheral.IAccelerometer3DThresholdListener;
- import com.sun.spot.sensorboard.peripheral.LIS3L02AQAccelerometer;
- import com.sun.spot.util.Utils;
- /**
- * <code>AccelerometerRegulator</code>
- *
- * @author Yi Wang (Neakor)
- * @version Creation date: 20:18 10-12-2008
- * @version Modified date: 20:46 10-12-2008
- */
- public class AccelerometerRegulator implements IAccelerometer3DThresholdListener, Runnable {
- /**
- * The current sample rate.
- */
- private long rate;
- /**
- * The flag indicates if this regulator is finished.
- */
- private boolean finished;
- /**
- * The flag indicates if the current sample cycle is completed.
- */
- private boolean completed;
- /**
- * The largest acceleration value along x axis in the last cycle.
- */
- private double x;
- /**
- * The largest acceleration value along y axis in the last cycle.
- */
- private double y;
- /**
- * The largest acceleration value along z axis in the last cycle.
- */
- private double z;
- /**
- * The dominant movement axis of the last cycle.
- */
- private int dominantAxis;
- /**
- * The temporary elapsed time.
- */
- private long elapsed;
- /**
- * Constructor of <code>AccelerometerRegulator</code>.
- * @param rate The <code>Long</code> sample rate.
- */
- public AccelerometerRegulator(long rate) {
- this.rate = rate;
- this.dominantAxis = -1;
- }
- public void thresholdChanged(IAccelerometer3D accel, int axis, double low, double high, boolean relative) {
- // TODO Auto-generated method stub
- }
- public void thresholdExceeded(IAccelerometer3D accel, int axis, double val, boolean relative) {
- System.out.println("Entered");
- if(this.completed) return;
- System.out.println("Updating");
- switch(axis) {
- case LIS3L02AQAccelerometer.X_AXIS:
- if(val > this.x) this.x = val;
- break;
- case LIS3L02AQAccelerometer.Y_AXIS:
- if(val > this.y) this.y = val;
- break;
- case LIS3L02AQAccelerometer.Z_AXIS:
- if(val > this.z) this.z = val;
- break;
- }
- }
- public void run() {
- while(!this.finished) {
- while(!this.completed) {
- Utils.sleep(1);
- this.elapsed++;
- if(this.elapsed >= this.rate) this.completed = true;
- }
- double max = Math.max(Math.max(this.x, this.y), this.z);
- if(this.x == max) this.dominantAxis = 0;
- else if(this.y == max) this.dominantAxis = 1;
- else this.dominantAxis = 2;
- if(this.dominantAxis != -1) System.out.println("Moved along axis: " + this.dominantAxis);
- this.reset();
- }
- }
- private void reset() {
- this.completed = false;
- this.elapsed = 0;
- this.dominantAxis = -1;
- this.x = 0;
- this.y = 0;
- this.z = 0;
- }
- }
Add Comment
Please, Sign In to add comment