I am trying out to detect a rotation, not a screen orientation change.
I only got a false on getRotationMatrix().
I found a solution here but it didn't work for me.
#Override
public void onSensorChanged(SensorEvent event) {
// This method will be called when the accelerometer values are changing.
if (event == null || event.values.length == 0) throw new IllegalArgumentException();
else {
// Handle the events for which we registered
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
Log.i(TAG, "detected Accelerometer");
mValuesAccel = event.values;
break;
case Sensor.TYPE_MAGNETIC_FIELD:
Log.i(TAG, "detected Magneticfield");
mValuesMagnet = event.values;
break;
}
Log.i(TAG, ""+ SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet));
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
mRotationListener.onChange(mValuesOrientation);
}
}
Check if your device has a compass.
If there's no compass then your mValuesMagnet is always empty and as a result getRotationMatrix returns false.
Related
I am working on an Android app wherein I want to scroll a large image horizontally. I used the accelerometer (Sensor.TYPE_ACCELEROMETER) and magnetic field (Sensor.TYPE_MAGNETIC_FIELD) data to get the angle of rotation. This data being to frequent infested with noise I am not able to implement a smooth motion effect.
#Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_MAGNETIC_FIELD:
mags = event.values.clone();
break;
case Sensor.TYPE_ACCELEROMETER:
accels = event.values.clone();
break;
}
if (mags != null && accels != null) {
gravity = new float[16];
boolean success = SensorManager.getRotationMatrix(gravity, null, accels, mags);
if (success) {
float[] outGravity = new float[16];
SensorManager.remapCoordinateSystem(gravity, SensorManager.AXIS_X, SensorManager.AXIS_Z, outGravity);
SensorManager.getOrientation(outGravity, values);
rollingAverage[0] = roll(rollingAverage[0], values[0]);
rollingAverage[1] = roll(rollingAverage[1], values[1]);
rollingAverage[2] = roll(rollingAverage[2], values[2]);
azimuth = Math.toDegrees(values[0]);
pitch = Math.toDegrees(values[1]);
roll = Math.toDegrees(values[2]);
mags = null;
accels = null;
double diffRoll = lastRoll - roll;
double diffPitch = lastPitch - pitch;
long curTime = System.currentTimeMillis();
if (Math.abs(diffRoll) >= 2) {
if (diffRoll > 0)
imageView.panLeft();
else
imageView.panRight();
lastRoll = roll;
}
}
}
}
Any ideas on achieving this using other methods?
You have to implement sensor fusion techniques based on Kalman filter or other filters. You can use open source libraries if needed. Refer this bitbucket repository. If you want to do yourself, read the tutorial.
I am trying to adapt a low pass filter from this site:
Low Pass Filter
My problem is that I do not know what is the goal of below line:
int rotation = Compatibility.getRotation(this);
I have googled a lot in order to search information about this but without no luck. Anyone knows what it does?
See below piece of code:
#Override
public void onSensorChanged(SensorEvent evt) {
if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
gravSensorVals = lowPass(evt.values.clone(), gravSensorVals);
} else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
magSensorVals = lowPass(evt.values.clone(), magSensorVals);
}
if (gravSensorVals != null && magSensorVals != null) {
SensorManager.getRotationMatrix(RTmp, I, gravSensorVals, magSensorVals);
int rotation = Compatibility.getRotation(this);
if (rotation == 1) {
SensorManager.remapCoordinateSystem(RTmp, SensorManager.AXIS_X, SensorManager.AXIS_MINUS_Z, Rot);
} else {
SensorManager.remapCoordinateSystem(RTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_Z, Rot);
}
SensorManager.getOrientation(Rot, results);
UIARView.azimuth = (float)(((results[0]*180)/Math.PI)+180);
UIARView.pitch = (float)(((results[1]*180/Math.PI))+90);
UIARView.roll = (float)(((results[2]*180/Math.PI)));
radarMarkerView.postInvalidate();
}
}
From android documentation: here, it will check the device orientation. So value of one is ROTATION_90. device rotated 90 degrees counter clockwise.
My app is locked into portrait orientation only, however in one fragment I have a camera preview where I would like to rotate captured images based on the device orientation. I believe that because my app is portrait only, the following code always logs zero.
Display display = ((WindowManager)getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
Log.i(TAG, "Rotation: " + rotation );
Is it possible to get the actual orientation of the device while locking the app to portrait?
I am targeting android 4.0+ so I'm not concerned if the solution won't work on older devices.
you could implement a SensorEventListener, then look at the Roll in onSensorChanged:
#Override
public void onSensorChanged(SensorEvent event) {
synchronized(this)
{
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mAccelerometerValues = event.values;
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mGeomageneticValues = event.values;
}
if ((mAccelerometerValues != null) && (mGeomageneticValues != null))
{
boolean success = SensorManager.getRotationMatrix(R, I, mAccelerometerValues, mGeomageneticValues);
if (success)
{
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR,orientation);
mYaw = orientation[0] * MathX.toDegreesF;
mPitch = orientation[1] * MathX.toDegreesF;
mRoll = orientation[2] * MathX.toDegreesF;
String sText = String.format("a:%1.4f\np:%1.4f\nr:%1.4f", yaw,pitch,roll);
}
}
}
}
In my 2d game I have the following code which is responsible for game entity control (flying plane). It all seems to be working fine when it comes for phones, but unfortunately I've getting some information that on Android tablets the steering is completely unreliable (axis are messed up, or it doesn't work at all). Unfortunately I don't have a tablet of my own, so I cannot investigate it closer. So.. what's wrong with the following code? (for the clarity I put only the code related to sensors)
// ...
private float[] accelerometerValues;
private float[] magneticFieldValues;
private float[] R;
private float[] I;
private float[] outR;
private float[] sensorValues;
private Sensor accelerometer;
private Sensor magneticField;
// ...
// ... sensor initialization
sensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
if(sensorManager == null)
return;
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
// ...
// ... onPause() sensor is being unregistered
public void onResume() {
if(!sensorManager.registerListener(sensorListener, accelerometer, SensorManager.SENSOR_DELAY_GAME) ||
!sensorManager.registerListener(sensorListener, magneticField, SensorManager.SENSOR_DELAY_GAME))
// ...
// ...
sensorListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
public void onSensorChanged(android.hardware.SensorEvent event) {
synchronized(InputMgr.this) {
switch(event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, accelerometerValues, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, magneticFieldValues, 0, 3);
break;
}
}
}
};
// ...
// used somewhere in the game
public void getSensorValues(float values[]) {
synchronized(InputMgr.this) {
SensorManager.getRotationMatrix(R, I, accelerometerValues, magneticFieldValues);
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_X, outR);
SensorManager.getOrientation(outR, sensorValues);
System.arraycopy(sensorValues, 0, values, 0, sensorValues.length);
}
}
This wont cheer you up much but I have a similar problem. I can see a TYPE_MAGNETIC_FIELD sensor, I can add a an event listener to it but I never get any data from it. Other sensors work fine. This is on a Galaxy Tab 7.
onSensorChanged(SensorEvent event) never fires a case Sensor.TYPE_MAGNETIC_FIELD:
As such I get no data from the Magnetic Field Sensor.
if you solve it let us know =)
On devices whose default orientation is landscape (-> most tablets), the sensor values are kind of 'wrong' (I don't know why). So you need to catch those devices and remap your Rotation Matrix.
To check whether the matrix needs to be remapped, you can use this code:
public boolean needToRemapOrientationMatrix;
// compute once (e.g. in onCreate() of your Activity):
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int orientation;
if(display.getWidth() < display.getHeight()) orientation = Configuration.ORIENTATION_PORTRAIT;
else if(display.getWidth() > display.getHeight()) orientation = Configuration.ORIENTATION_LANDSCAPE;
else orientation = Configuration.ORIENTATION_SQUARE;
int rotation = display.getRotation();
needToRemapOrientationMatrix =
(orientation==Configuration.ORIENTATION_LANDSCAPE && (rotation==Surface.ROTATION_0 || rotation==Surface.ROTATION_180)) ||
(orientation==Configuration.ORIENTATION_PORTRAIT && (rotation==Surface.ROTATION_90 || rotation==Surface.ROTATION_270));
And when you read the sensor values, remap the matrix if needed:
public void getSensorValues(float values[]) {
synchronized(InputMgr.this) {
SensorManager.getRotationMatrix(R, I, accelerometerValues, magneticFieldValues);
if(needToRemapOrientationMatrix)
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, R);
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_X, outR);
SensorManager.getOrientation(outR, sensorValues);
System.arraycopy(sensorValues, 0, values, 0, sensorValues.length);
}
}
This worked for me, I hope it helps.
I'm getting my phone orientation with help of
SensorManager.getOrientation
but the results are very unstable, something like +-8 degrees, is there some good way fo filtering the results?
this is how I get the values:
public void onSensorChanged(SensorEvent event)
{
switch (event.sensor.getType ()){
case Sensor.TYPE_ACCELEROMETER:
aValues = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mValues = event.values.clone();
break;
}
float[] R = new float[16];
float[] orientationValues = new float[3];
if( aValues == null || mValues == null )
return;
if( !SensorManager.getRotationMatrix (R, null, aValues, mValues) )
return;
float[] outR = new float[16];
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X, outR);
SensorManager.getOrientation (outR, orientationValues);
orientationValues[0] = (float)Math.toDegrees (orientationValues[0]);
orientationValues[1] = (float)Math.toDegrees (orientationValues[1]);
orientationValues[2] = (float)Math.toDegrees (orientationValues[2]);
}
Just buffer the values by calculating the average (there are many different calculation possibilities: http://en.wikipedia.org/wiki/Average#Types ). Also consider that the more you buffer the slower the app will react to changes!