Skip to content

Commit

Permalink
Bug fixes in the way the gyro and controller stop start. Bug fixes fo…
Browse files Browse the repository at this point in the history
…r the pref buttons.
  • Loading branch information
jaydeetay committed Jul 11, 2016
1 parent 2b7c066 commit 345d440
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private void calculatePointing() {
}

calculateLocalNorthAndUpInCelestialCoords(false);
calculateLocalNorthAndUpInPhoneCoordsFromAccelAndMagFieldSensors();
calculateLocalNorthAndUpInPhoneCoordsFromSensors();

Matrix33 transform = matrixMultiply(axesMagneticCelestialMatrix, axesPhoneInverseMatrix);

Expand Down Expand Up @@ -301,7 +301,7 @@ private void calculateLocalNorthAndUpInCelestialCoords(boolean forceUpdate) {
* Calculates local North and Up vectors in terms of the phone's coordinate
* frame from the magnetic field and accelerometer sensors.
*/
private void calculateLocalNorthAndUpInPhoneCoordsFromAccelAndMagFieldSensors() {
private void calculateLocalNorthAndUpInPhoneCoordsFromSensors() {
Vector3 magneticNorthPhone;
Vector3 upPhone;
Vector3 magneticEastPhone;
Expand Down Expand Up @@ -334,31 +334,6 @@ private void calculateLocalNorthAndUpInPhoneCoordsFromAccelAndMagFieldSensors()
axesPhoneInverseMatrix = new Matrix33(magneticNorthPhone, upPhone, magneticEastPhone, false);
}

/**
* Calculates local North and Up vectors in terms of the phone's coordinate
* frame from the rotation matrix. A temporary hack till we rebuild the rendering codel.
*/
private void calculateLocalNorthAndUpInPhoneCoordsFromRotationMatrixSensor() {
// TODO(johntaylor): we can reduce the number of vector copies done in here.
Vector3 down = acceleration.copy();
down.normalize();
// Magnetic field goes *from* North to South, so reverse it.
Vector3 magneticFieldToNorth = magneticField.copy();
magneticFieldToNorth.scale(-1);
magneticFieldToNorth.normalize();
// This is the vector to magnetic North *along the ground*.
Vector3 magneticNorthPhone = addVectors(magneticFieldToNorth,
scaleVector(down, -scalarProduct(magneticFieldToNorth, down)));
magneticNorthPhone.normalize();
Vector3 upPhone = scaleVector(down, -1);
Vector3 magneticEastPhone = vectorProduct(magneticNorthPhone, upPhone);

// The matrix is orthogonal, so transpose it to find its inverse.
// Easiest way to do that is to construct it from row vectors instead
// of column vectors.
axesPhoneInverseMatrix = new Matrix33(magneticNorthPhone, upPhone, magneticEastPhone, false);
}

/**
* Updates the angle between True North and Magnetic North.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,16 @@

package com.google.android.stardroid.control;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.android.stardroid.ApplicationConstants;
import com.google.android.stardroid.util.MiscUtil;
import com.google.android.stardroid.util.SensorAccuracyReporter;
import com.google.android.stardroid.util.smoothers.ExponentiallyWeightedSmoother;
import com.google.android.stardroid.util.smoothers.PlainSmootherModelAdaptor;

Expand All @@ -40,7 +36,7 @@
* @author John Taylor
*/
public class SensorOrientationController extends AbstractController
implements OnSharedPreferenceChangeListener, SensorEventListener {
implements SensorEventListener {
// TODO(johntaylor): this class needs to be refactored to use the new
// sensor API and to behave properly when sensors are not available.

Expand Down Expand Up @@ -74,72 +70,62 @@ public SensorDampingSettings(float damping, int exponent) {
private SensorListener accelerometerSmoother;
private SensorListener compassSmoother;
private Provider<PlainSmootherModelAdaptor> modelAdaptorProvider;
private SensorAccuracyReporter accuracyReporter;
private Sensor rotationSensor;

private SharedPreferences sharedPreferences;

@Inject
SensorOrientationController(Context context, SensorAccuracyReporter accuracyReporter,
Provider<PlainSmootherModelAdaptor> modelAdaptorProvider,
SensorManager manager) {
SensorOrientationController(Provider<PlainSmootherModelAdaptor> modelAdaptorProvider,
SensorManager manager, SharedPreferences sharedPreferences) {
this.manager = manager;
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
this.accuracyReporter = accuracyReporter;
this.modelAdaptorProvider = modelAdaptorProvider;
this.rotationSensor = manager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
this.sharedPreferences = sharedPreferences;
}

@Override
public void start() {
PlainSmootherModelAdaptor modelAdaptor = modelAdaptorProvider.get();

Log.d(TAG, "Exponentially weighted smoothers used");
String dampingPreference = sharedPreferences.getString(
ApplicationConstants.SENSOR_DAMPING_PREF_KEY,
ApplicationConstants.SENSOR_DAMPING_STANDARD);
String speedPreference = sharedPreferences.getString(ApplicationConstants.SENSOR_SPEED_PREF_KEY,
ApplicationConstants.SENSOR_SPEED_STANDARD);
Log.d(TAG, "Sensor damping preference " + dampingPreference);
Log.d(TAG, "Sensor speed preference " + speedPreference);
int dampingIndex = 0;
if (ApplicationConstants.SENSOR_DAMPING_HIGH.equals(dampingPreference)) {
dampingIndex = 1;
} else if (ApplicationConstants.SENSOR_DAMPING_EXTRA_HIGH.equals(dampingPreference)) {
dampingIndex = 2;
} else if (ApplicationConstants.SENSOR_DAMPING_REALLY_HIGH.equals(dampingPreference)) {
dampingIndex = 3;
}
int sensorSpeed = SensorManager.SENSOR_DELAY_GAME;
if (ApplicationConstants.SENSOR_SPEED_SLOW.equals(speedPreference)) {
sensorSpeed = SensorManager.SENSOR_DELAY_NORMAL;
} else if (ApplicationConstants.SENSOR_SPEED_HIGH.equals(speedPreference)) {
sensorSpeed = SensorManager.SENSOR_DELAY_FASTEST;
}
accelerometerSmoother = new ExponentiallyWeightedSmoother(
modelAdaptor,
ACC_DAMPING_SETTINGS[dampingIndex].damping,
ACC_DAMPING_SETTINGS[dampingIndex].exponent);
compassSmoother = new ExponentiallyWeightedSmoother(
modelAdaptor,
MAG_DAMPING_SETTINGS[dampingIndex].damping,
MAG_DAMPING_SETTINGS[dampingIndex].exponent);

if (manager != null) {
if (sharedPreferences.getBoolean(ApplicationConstants.SHARED_PREFERENCE_EXPERIMENTAL_USE_GYRO,
false)) {
Log.d(TAG, "Using rotation sensor");
manager.registerListener(this, rotationSensor, SensorManager.SENSOR_DELAY_GAME);
manager.registerListener(
accuracyReporter, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
manager.registerListener(
accuracyReporter, manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
} else {
Log.d(TAG, "Using classic sensors");
Log.d(TAG, "Exponentially weighted smoothers used");
String dampingPreference = sharedPreferences.getString(
ApplicationConstants.SENSOR_DAMPING_PREF_KEY,
ApplicationConstants.SENSOR_DAMPING_STANDARD);
String speedPreference = sharedPreferences.getString(ApplicationConstants.SENSOR_SPEED_PREF_KEY,
ApplicationConstants.SENSOR_SPEED_STANDARD);
Log.d(TAG, "Sensor damping preference " + dampingPreference);
Log.d(TAG, "Sensor speed preference " + speedPreference);
int dampingIndex = 0;
if (ApplicationConstants.SENSOR_DAMPING_HIGH.equals(dampingPreference)) {
dampingIndex = 1;
} else if (ApplicationConstants.SENSOR_DAMPING_EXTRA_HIGH.equals(dampingPreference)) {
dampingIndex = 2;
} else if (ApplicationConstants.SENSOR_DAMPING_REALLY_HIGH.equals(dampingPreference)) {
dampingIndex = 3;
}
int sensorSpeed = SensorManager.SENSOR_DELAY_GAME;
if (ApplicationConstants.SENSOR_SPEED_SLOW.equals(speedPreference)) {
sensorSpeed = SensorManager.SENSOR_DELAY_NORMAL;
} else if (ApplicationConstants.SENSOR_SPEED_HIGH.equals(speedPreference)) {
sensorSpeed = SensorManager.SENSOR_DELAY_FASTEST;
}
accelerometerSmoother = new ExponentiallyWeightedSmoother(
modelAdaptor,
ACC_DAMPING_SETTINGS[dampingIndex].damping,
ACC_DAMPING_SETTINGS[dampingIndex].exponent);
compassSmoother = new ExponentiallyWeightedSmoother(
modelAdaptor,
MAG_DAMPING_SETTINGS[dampingIndex].damping,
MAG_DAMPING_SETTINGS[dampingIndex].exponent);
manager.registerListener(accelerometerSmoother,
SensorManager.SENSOR_ACCELEROMETER,
sensorSpeed);
SensorManager.SENSOR_ACCELEROMETER,
sensorSpeed);
manager.registerListener(compassSmoother,
SensorManager.SENSOR_MAGNETIC_FIELD,
sensorSpeed);
Expand All @@ -150,22 +136,14 @@ public void start() {

@Override
public void stop() {
Log.d(TAG, "Unregistering sensor listeners");
Log.d(
TAG, "Unregistering sensor listeners: " + accelerometerSmoother + ", "
+ compassSmoother + ", " + this);
manager.unregisterListener(accelerometerSmoother);
manager.unregisterListener(compassSmoother);
manager.unregisterListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (ApplicationConstants.SENSOR_DAMPING_PREF_KEY.equals(key) || ApplicationConstants.SENSOR_SPEED_PREF_KEY.equals(key)
|| ApplicationConstants.SHARED_PREFERENCE_EXPERIMENTAL_USE_GYRO.equals(key)) {
Log.d(TAG, "User sensor preferences changed - restarting sensor controllers");
stop();
start();
}
}

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor != rotationSensor) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setAttrs(Context context, AttributeSet attrs) {
imageOn = a.getDrawable(R.styleable.PreferencesButton_image_on);
imageOff = a.getDrawable(R.styleable.PreferencesButton_image_off);
prefKey = a.getString(R.styleable.PreferencesButton_pref_key);
defaultValue = a.getBoolean(R.styleable.PreferencesButton_default_value, false);
defaultValue = a.getBoolean(R.styleable.PreferencesButton_default_value, true);
Log.d(TAG, "Preference key is " + prefKey);
}

Expand All @@ -101,9 +101,8 @@ private void setVisuallyOnOrOff() {

private void setPreference() {
Log.d(TAG, "Setting preference " + prefKey + " to... " + isOn);
// TODO(put this on a background thread)
if (prefKey != null) {
preferences.edit().putBoolean(prefKey, isOn).commit();
preferences.edit().putBoolean(prefKey, isOn).apply();
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/whatsnew.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<string name="whats_new_text" translation_description="Description of new features in this version in html">
<![CDATA[
<h1>New in version %s</h1>
\t • Uses gyroscope for a smoother experience <br/>
\t • Gyroscope option (if your phone has one) for a smoother experience. Enable it from settings. <br/>
\t • Now in Greek! (thanks: ftsamis)<br/>
\t • Fixed Russian translations (thanks: AlexLitvino)<br/>
]]>
Expand Down

0 comments on commit 345d440

Please sign in to comment.