Example usage for android.hardware SensorManager AXIS_MINUS_Z

List of usage examples for android.hardware SensorManager AXIS_MINUS_Z

Introduction

In this page you can find the example usage for android.hardware SensorManager AXIS_MINUS_Z.

Prototype

int AXIS_MINUS_Z

To view the source code for android.hardware SensorManager AXIS_MINUS_Z.

Click Source Link

Document

see #remapCoordinateSystem

Usage

From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.BusStopDetailsFragment.java

/**
 * Update the direction needle so that it is pointing towards the bus stop,
 * based on the device location and the direction it is facing.
 *///  ww w.j av a 2  s  .  c  o m
private void updateDirectionNeedle() {
    // We need values for location, the accelerometer and magnetometer to
    // continue.
    if (lastLocation == null || accelerometerValues == null || magnetometerValues == null) {
        // Make sure the needle isn't showing.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        recycleNeedleBitmapIfNotNull(null);

        return;
    }

    // Calculating the rotation matrix may fail, for example, if the device
    // is in freefall. In that case we cannot continue as the values will
    // be unreliable.
    if (!SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerValues, magnetometerValues)) {
        return;
    }

    // The screen rotation was obtained earlier.
    switch (screenRotation) {
    // There's lots of information about this elsewhere, but briefly;
    // The values from the sensors are in the device's coordinate system
    // which may be correct if the device is in its natural orientation,
    // but it needs to be remapped if the device is rotated.
    case Surface.ROTATION_0:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                rotationMatrix);
        break;
    case Surface.ROTATION_90:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X,
                rotationMatrix);
        break;
    case Surface.ROTATION_180:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X,
                SensorManager.AXIS_MINUS_Z, rotationMatrix);
        break;
    case Surface.ROTATION_270:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Z, SensorManager.AXIS_X,
                rotationMatrix);
        break;
    }

    // Get the X, Y and Z orientations, which are in radians. Covert this
    // in to degrees East of North.
    SensorManager.getOrientation(rotationMatrix, headings);
    double heading = Math.toDegrees(headings[0]);

    // If there's a GeomagneticField value, then adjust the heading to take
    // this in to account.
    if (geoField != null) {
        heading -= geoField.getDeclination();
    }

    // The orientation is in the range of -180 to +180. Convert this in to
    // a range of 0 to 360.
    final float bearingTo = distance[1] < 0 ? distance[1] + 360 : distance[1];

    // This is the heading to the bus stop.
    heading = bearingTo - heading;

    // The above calculation may come out as a negative number again. Put
    // this back in to the range of 0 to 360.
    if (heading < 0) {
        heading += 360;
    }

    // This 'if' statement is required to prevent a crash during device
    // rotation. It ensured that the Fragment is still part of the Activity.
    if (isAdded()) {
        // Get the arrow bitmap from the resources.
        final Bitmap needleIn = BitmapFactory.decodeResource(getResources(), R.drawable.heading_arrow);
        // Get an identity matrix and rotate it by the required amount.
        final Matrix m = new Matrix();
        m.setRotate((float) heading % 360, (float) needleIn.getWidth() / 2, (float) needleIn.getHeight() / 2);
        // Apply the rotation matrix to the Bitmap, to create a new Bitmap.
        final Bitmap needleOut = Bitmap.createBitmap(needleIn, 0, 0, needleIn.getWidth(), needleIn.getHeight(),
                m, true);

        // Recycle the needle read in if it's not the same as the rotated
        // needle.
        if (needleIn != needleOut) {
            needleIn.recycle();
        }

        // This Bitmap needs to be converted to a Drawable type.
        final BitmapDrawable drawable = new BitmapDrawable(getResources(), needleOut);
        // Set the new needle to be on the right hand side of the TextView.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        recycleNeedleBitmapIfNotNull(needleOut);
    } else {
        // If the Fragment is not added to the Activity, then make sure
        // there's no needle.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        recycleNeedleBitmapIfNotNull(null);
    }
}

From source file:com.dragon4.owo.ar_trace.ARCore.MixView.java

public void onSensorChanged(SensorEvent evt) {
    try {//from   ww w  .  j a v a 2  s. c  o  m
        //         killOnError();

        // ?? , ?  ???  ? ? ? 
        if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            grav[0] = evt.values[0];
            grav[1] = evt.values[1];
            grav[2] = evt.values[2];

            augScreen.postInvalidate(); // ? 
        } else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            mag[0] = evt.values[0];
            mag[1] = evt.values[1];
            mag[2] = evt.values[2];

            augScreen.postInvalidate(); // ? 
        }

        //  ? 
        SensorManager.getRotationMatrix(RTmp, I, grav, mag);
        //  (?)
        SensorManager.remapCoordinateSystem(RTmp, SensorManager.AXIS_X, SensorManager.AXIS_MINUS_Z, Rot);

        //  ? ? 
        tempR.set(Rot[0], Rot[1], Rot[2], Rot[3], Rot[4], Rot[5], Rot[6], Rot[7], Rot[8]);

        //       ,  
        finalR.toIdentity();
        finalR.prod(m4);
        finalR.prod(m1);
        finalR.prod(tempR);
        finalR.prod(m3);
        finalR.prod(m2);
        finalR.invert();

        // ? ?  ? ??   ...
        histR[rHistIdx].set(finalR);
        rHistIdx++;
        if (rHistIdx >= histR.length)
            rHistIdx = 0;

        smoothR.set(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f);
        for (int i = 0; i < histR.length; i++) {
            smoothR.add(histR[i]);
        }
        smoothR.mult(1 / (float) histR.length);

        synchronized (mixContext.rotationM) {
            mixContext.rotationM.set(smoothR);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:org.mixare.MixViewActivity.java

public void onSensorChanged(SensorEvent evt) {
    try {/*from  w  ww .  j  av a2 s . c o  m*/
        if (getMixViewData().getSensorGyro() != null) {

            if (evt.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
                getMixViewData().setGyro(evt.values);
            }

            if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                getMixViewData().setGrav(
                        getMixViewData().getGravFilter().lowPassFilter(evt.values, getMixViewData().getGrav()));
            } else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                getMixViewData().setMag(
                        getMixViewData().getMagFilter().lowPassFilter(evt.values, getMixViewData().getMag()));
            }
            getMixViewData().setAngle(getMixViewData().getMagFilter().complementaryFilter(
                    getMixViewData().getGrav(), getMixViewData().getGyro(), 30, getMixViewData().getAngle()));

            SensorManager.getRotationMatrix(getMixViewData().getRTmp(), getMixViewData().getI(),
                    getMixViewData().getGrav(), getMixViewData().getMag());
        } else {
            if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                getMixViewData().setGrav(evt.values);
            } else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                getMixViewData().setMag(evt.values);
            }
            SensorManager.getRotationMatrix(getMixViewData().getRTmp(), getMixViewData().getI(),
                    getMixViewData().getGrav(), getMixViewData().getMag());
        }

        //         augmentedView.postInvalidate();
        hudView.postInvalidate();
        projectedMapView.postInvalidate();

        int rotation = Compatibility.getRotation(this);

        if (rotation == 1) {
            SensorManager.remapCoordinateSystem(getMixViewData().getRTmp(), SensorManager.AXIS_X,
                    SensorManager.AXIS_MINUS_Z, getMixViewData().getRot());
        } else {
            SensorManager.remapCoordinateSystem(getMixViewData().getRTmp(), SensorManager.AXIS_Y,
                    SensorManager.AXIS_MINUS_Z, getMixViewData().getRot());
        }
        getMixViewData().getTempR().set(getMixViewData().getRot()[0], getMixViewData().getRot()[1],
                getMixViewData().getRot()[2], getMixViewData().getRot()[3], getMixViewData().getRot()[4],
                getMixViewData().getRot()[5], getMixViewData().getRot()[6], getMixViewData().getRot()[7],
                getMixViewData().getRot()[8]);

        getMixViewData().getFinalR().toIdentity();
        getMixViewData().getFinalR().prod(getMixViewData().getM4());
        getMixViewData().getFinalR().prod(getMixViewData().getM1());
        getMixViewData().getFinalR().prod(getMixViewData().getTempR());
        getMixViewData().getFinalR().prod(getMixViewData().getM3());
        getMixViewData().getFinalR().prod(getMixViewData().getM2());
        getMixViewData().getFinalR().invert();

        getMixViewData().getHistR()[getMixViewData().getrHistIdx()].set(getMixViewData().getFinalR());

        int histRLenght = getMixViewData().getHistR().length;

        getMixViewData().setrHistIdx(getMixViewData().getrHistIdx() + 1);
        if (getMixViewData().getrHistIdx() >= histRLenght)
            getMixViewData().setrHistIdx(0);

        getMixViewData().getSmoothR().set(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f);
        for (int i = 0; i < histRLenght; i++) {
            getMixViewData().getSmoothR().add(getMixViewData().getHistR()[i]);
        }
        getMixViewData().getSmoothR().mult(1 / (float) histRLenght);

        MixContext.getInstance().updateSmoothRotation(getMixViewData().getSmoothR());

        //         float[] orientation = new float[3];
        //            float[] rTemp = getMixViewData().getRTmp();
        //         SensorManager.getOrientation(rTemp, orientation);
        //            Log.d("PROJECT_MAP", "Sensor Angles");
        //         Log.d("PROJECT_MAP", orientation[0] + ", " + orientation[1] + ", " + orientation[2]);
        //            Log.d("PROJECT_MAP", "Rotation Matrix Before Smoothing");
        //            Log.d("PROJECT_MAP", rTemp[0] + ", " + rTemp[1] + ", " + rTemp[2]);
        //            Log.d("PROJECT_MAP", rTemp[3] + ", " + rTemp[4] + ", " + rTemp[5]);
        //            Log.d("PROJECT_MAP", rTemp[6] + ", " + rTemp[7] + ", " + rTemp[8]);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}