thecause17
Newbie
Someone asked me to look into this for them and I'm not sure how to figure this out. Apparently they are using the accelerometer to measure the number of "g's". They've tried using a few different phones and found that they seem to be capped on what they can measure. Is some cases they couldn't measure any more than 2g's. Does anyone know if this is info is accessible somehow?
I started writing a program to test what my own phone is going. I accessed and displayed the accelerometer data. I'm trying to store the display the max value of each axis but I'm doing something wrong. Here is the code. Note, that I only have worked on the x axis max at this point.
I started writing a program to test what my own phone is going. I accessed and displayed the accelerometer data. I'm trying to store the display the max value of each axis but I'm doing something wrong. Here is the code. Note, that I only have worked on the x axis max at this point.
Code:
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
final float[] mValuesMagnet = new float[3];
final float[] mValuesAccel = new float[3];
final float[] mValuesOrientation = new float[3];
final float[] mRotationMatrix = new float[9];
final double xmax = 0;
final double xmaxold = 0;
final TextView txt1 = (TextView) findViewById(R.id.textView1);
final TextView txt2 = (TextView) findViewById(R.id.textView2);
final TextView txt3 = (TextView) findViewById(R.id.textView3);
final TextView txt4 = (TextView) findViewById(R.id.textView4);
final SensorEventListener mEventListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event)
{
switch (event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
break;
}
SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
DecimalFormat precision = new DecimalFormat("0.0");
final CharSequence gravdisplayx;
final CharSequence gravdisplayy;
final CharSequence gravdisplayz;
final CharSequence xmaximum;
double gravityx;
double gravityy;
double gravityz;
gravityx = mValuesAccel[0];
gravityy = mValuesAccel[1];
gravityz = mValuesAccel[2];
double xmax = max(gravityx,xmaxold);
gravdisplayx = "X: " + precision.format(gravityx);
gravdisplayy = "Y: " + precision.format(gravityy);
gravdisplayz = "Z: " + precision.format(gravityz);
xmaximum = "X Max: " + precision.format(xmax);
txt1.setText(gravdisplayx);
txt2.setText(gravdisplayy);
txt3.setText(gravdisplayz);
txt4.setText(xmaximum);
}
;
};
setListners(sensorManager, mEventListener);
}
public static double max(double gravityx, double xmaxold)
{
double absgravx = Math.abs(gravityx);
double result;
if(absgravx > xmaxold)
{
result = absgravx;
}
else
{
result = xmaxold;
}
return result;
}
public void setListners(SensorManager sensorManager, SensorEventListener mEventListener)
{
sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
}
}