0

First of all sorry for the silly question.

What i'm trying to do is capture the accelerometer data for each second for three seconds and do addition of that data, but due to lack of experience i'm finding it hard time to do it.

What I've accomplished till now is:

Get acceleration data of X,Y,X axes in real time and calculated resultant acceleration as resultant_acceleration = Math.sqrt(X^2+y^2+z^2) that is real time as well and keeps changing with time.

What i want to do is:

Find resultant_acceleration at end of first, second and third second,i.e a1,a2,a3 and return (a1+a2+a3) I'm going through hard times to write a method in java that does the task.

        public void onSensorChanged(SensorEvent event) {

   final ArrayList<Double> accln = new ArrayList<>();

    //enter your code here
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    if (!mInitialized) {
        mLastX = x;
        mLastY = y;
        mLastZ = z;

        mInitialized = true;
    } else {
        float deltaX = Math.abs(mLastX - x);
        float deltaY = Math.abs(mLastY - y);
        float deltaZ = Math.abs(mLastZ - z);
        if (deltaX < NOISE) deltaX = (float) 0.0;
        if (deltaY < NOISE) deltaY = (float) 0.0;
        if (deltaZ < NOISE) deltaZ = (float) 0.0;
        mLastX = x;
        mLastY = y;
        mLastZ = z;

       final double a =Math.sqrt((deltaX*deltaX)+(deltaY*deltaY)+(deltaZ*deltaZ));
        accln.add(a); //what i am trying to do here is collect the sample data of acceleration in an array list for 3 seconds,// so later i can calculate average acceleration

    }
    }
  • How far did you get writing the method on your own? Could you post the code you are currently trying and describe what is currently not working as it should? – Dekker1 Jul 16 '17 at 16:48
  • what is onSensorChanged method ? When it will get called ? based on its name it is a kind of event, if it will be triggered based on changes you can not guarantee the call of methon on each second then you can not access to the amount of sensor for that time, pleaase provide more detail for this problem – Sir1 Jul 17 '17 at 07:40
  • onSensorChanged method is a method of interface SensorEventListener, It is called when there is a new sensor event, Source: https://developer.android.com/reference/android/hardware/SensorEventListener.html – Andro Noob Jul 17 '17 at 07:48

3 Answers3

0

I suggest starting a timer, then when it hits 1000 milliseconds take a measurement, then at 2000 and 3000. This might help: Android timer? How-to?

0

Solved the Problem

Just use Timer class method do this every 1s

new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                                     // Just calculate your accleration here

                              }
                                                 }, 0, 1000);
Anas Aijaz
  • 352
  • 2
  • 9
0

You never mentioned at what rate you were trying to get accel data, so I just figured it would be at ~5hz (SENSOR_DELAY_NORMAL). Keep in mind this is just a hint. You could potentially get events at a faster rate if another app registers for accel sensor data at a faster rate. As for the code:

// SOMEWHERE IN YOUR APP

final ArrayList<Double> accln = new ArrayList<>(); //THIS SHOULD BE OUTSIDE YOUR CALLBACK!

final SensorEventListener eventListener = new SensorEventListener() {
                @Override
                public void onSensorChanged(SensorEvent event) {
                    //enter your code here
                    float x = event.values[0];
                    float y = event.values[1];
                    float z = event.values[2];
                    if (!mInitialized) {
                        mLastX = x;
                        mLastY = y;
                        mLastZ = z;

                        mInitialized = true;
                    } else {
                        float deltaX = Math.abs(mLastX - x);
                        float deltaY = Math.abs(mLastY - y);
                        float deltaZ = Math.abs(mLastZ - z);
                        if (deltaX < NOISE) deltaX = (float) 0.0;
                        if (deltaY < NOISE) deltaY = (float) 0.0;
                        if (deltaZ < NOISE) deltaZ = (float) 0.0;
                        mLastX = x;
                        mLastY = y;
                        mLastZ = z;

                        final double a = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY) + (deltaZ * deltaZ));
                        accln.add(a); //what i am trying to do here is collect the sample data of acceleration in an array list for 3 seconds,// so later i can calculate average acceleration

                    }
                }

                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy) {
                }
            };

SensorManager sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
Handler handler = new Handler();
Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(eventListener, accel, SensorManager.SENSOR_DELAY_NORMAL);  //~5hz of data
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        sensorManager.unregisterListener(eventListener);
    }
}, 5000);

This would register your listener and unregister 5 seconds later from the handler runnable.

Pablo Baxter
  • 2,144
  • 1
  • 17
  • 36