1 / 16

Programming the Android Platform

Sensors. Programming the Android Platform. Sensors. Hardware devices that take measurements of the physical environment Some examples 3 -axis Accelerometer 3 -axis Magnetic field sensor Temperature sensor Proximity sensor Orientation sensor Light sensor. SensorManager.

ronli
Download Presentation

Programming the Android Platform

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sensors Programming the Android Platform

  2. Sensors • Hardware devices that take measurements of the physical environment • Some examples • 3-axis Accelerometer • 3-axis Magnetic field sensor • Temperature sensor • Proximity sensor • Orientation sensor • Light sensor

  3. SensorManager • System service that manages device’s sensors • Get instance with • getSystemService(Context.SENSOR_SERVICE) • Access specific sensor with • SensorManager. getDefaultSensor(int type)

  4. Some Sensor Type Constants • TYPE_ACCELEROMETER • TYPE_GRAVITY • TYPE_GYROSCOPE • TYPE_LIGHT • YPE_MAGNETIC_FIELD • TYPE_PRESSURE • TYPE_PROXIMITY • TYPE_TEMPERATURE

  5. SensorEvent • Represents a Sensor event • Holds Sensor-specific data • e.g., sensor's type, the time-stamp, accuracy & measurement data

  6. Sensor Coordinate System • When device is lying flat, face-up on a table, axes run • X – right to left • Y – top to bottom • Z – up to down • Coordinate system does not depend on device orientation (portrait vs. landscape)

  7. SensorEventListener • Interface for SensorEvent callbacks • void onAccuracyChanged(Sensor sensor, int accuracy) • Called when the accuracy of a sensor has changed. • void onSensorChanged(SensorEvent event) • Called when sensor values have changed.

  8. Registering for SensorEvents • Use SensorManager to register/unregister for SensorEvents • public booleanregisterListener (SensorEventListener listener, Sensor sensor, int rate) • Registers a SensorEventListener for the given sensor. • public void unregisterListener (SensorEventListener listener, Sensor sensor) • Unregisters a listener for the sensors with which it is registered.

  9. Accelerometer Example public class SensorShowValuesActivity extends Activity implements SensorEventListener{ … public void onCreate(BundlesavedInstanceState) { … mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE); mAccelerometer=mSensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); }

  10. Accelerometer Example (cont.) … protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { mSensorManager.unregisterListener(this); super.onPause(); }

  11. Accelerometer Example (cont.) … public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { long actualTime = System.currentTimeMillis(); if (actualTime - mLastUpdate > 500) { mLastUpdate= actualTime; float x = event.values[0], y = event.values[1],z= event.values[2]; xView.setText(String.valueOf(x)); yView.setText(String.valueOf(y)); zView.setText(String.valueOf(z)); … } }

  12. Filtering Accelerometer • When device is lying flat, face-up on a table, accelerometer ideally reports the following forces • x – ≈ 0 m/s2 • y – ≈ 0 m/s2 • z – ≈ 9.81 m/s2 • But these values will vary from natural movements, non-flat surfaces, noise, etc.

  13. Filtering Accelerometer (cont.) • Two common data transforms • Low-pass filters • Deemphasize transient force changes • Emphasize constant force components • e.g., for a bubble level • High-pass filters • Emphasizetransient force changes • Deemphasize constant force components • e.g., for a game controller

  14. Filtering Accelerometer (cont.) mAlpha = 0.9f; // simple low-pass filter float lowPass(float current, float filtered) { return mAlpha * current + (1.0f - mAlpha) * filtered; } // simple high-pass filter float highPass(float current, float last, float filtered) { return mAlpha * (filtered + current - last); }

  15. Filtering Accelerometer (cont.) x= event.values[0]; y= event.values[1]; z= event.values[2]; mLowPassX= lowPass(x, mLowPassX); mLowPassY = lowPass(y, mLowPassY); mLowPassZ = lowPass(z, mLowPassZ); mHighPassX = highPass(x, mLastX, mHighPassX); mHighPassY = highPass(y, mLastY, mHighPassY); mHighPassZ = highPass(z, mLastZ, mHighPassZ); mLastX = x; mLastY = y; mLastZ = z;

  16. Lab Assignment

More Related