Frage

Hey Guys I am currently working on a project which will access the accelerometer and graph it how ever my code is currently not working and I am unsure of the reasoning, any help will be greatly appreciated.

The program is required to record data for 30 seconds then output a graph how ever it keeps outputting a 0 values for all 3 axis.

The Code:

public class AccelerometerData extends Activity implements SensorEventListener {

public float _x;
public float _y;
public float _z;

boolean senseA = false; 

int Time = 30;
int samplerate=10;
int sampleSize=(Time*1000)/samplerate;
public double[] X = new double[sampleSize];         
public double[] Y = new double[sampleSize];
public double[] Z = new double[sampleSize];

Sensor accelerometer;
SensorManager sm;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sm=(SensorManager)getSystemService(SENSOR_SERVICE);
    accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);

    final Timer updateTimer = new Timer();
    updateTimer.scheduleAtFixedRate(new TimerTask(){
        public void run(){
            if (senseA= true){
                for (int i = 0; i < sampleSize; i++){
                    X[i] = _x;                                          
                    Y[i] = _y;
                    Z[i] = _z;                                                  
        }

            }                                                       
        }
    }, 0, samplerate);

  }

public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
        _x = event.values[0];
        _y = event.values[1];
        _z = event.values[2];
        senseA = true;
        }

}

}

The outputted values are as follow:

AccelerometerData data= new AccelerometerData();

double[] dataX =  data.X;   

double[] dataY =  data.Y;

double[] dataZ =  data.Z;

int length = data.sampleSize;

Thank you for your time in advance

War es hilfreich?

Lösung

Your code for reading from the accelerometer looks fine.

However, you can't start the Activity with

AccelerometerData data= new AccelerometerData();

Instead, you can start the Activity with an Intent, e.g.

startActivity(new Intent(this, AccelerometerData.class));

Also, the way you store the accelerometer values looks bogus. I assume you want to store the i-th reading at the i-th position in the X,Y,Z arrays. Instead you are overwriting all elements of the arrays with the latest value.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top