Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors

Similar presentations


Presentation on theme: "Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors"— Presentation transcript:

1 Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors
Date: Feb 7, 2017

2 References (study these)

3 Sensors Overview Motion Sensors Position Sensors Environmental Sensors

4 Examples Motion Sensors Position Sensors Environmental Sensors
Measure acceleration and rotational forces. e.g., accelerometers, gravity sensors, gyroscopes, and rotational vector sensors. Position Sensors Measure the physical position of a device e.g., orientation sensors and magnetometers Environmental Sensors Measure various environmental parameters e.g., barometers, photometers, and thermometers.

5 Types of Sensors Sensor HW/SW Notes Use TYPE_ACCELEROMETER HW
(Ax, Ay, Az) + g; ms2 Shake, Tilt TYPE_AMBIENT_TEMPERATURE 0C TYPE_GRAVITY SW/HW g = (gx, gy, gz); ms2 TYPE_GYROSCOPE ω = (ωx, ωy, ωz) rad/s Spin, Turn TYPE_LIGHT Illumination; lx Brightness control TYPE_LINEAR_ACCELERATION (Ax, Ay, Az); no g; ms2 TYPE_MAGNETIC_FIELD (Bx, By, Bz) µT Compass TYPE_ORIENTATION SW 3 axis rotation; matrix Device position TYPE_PRESSURE hPa or mbar Air pressure TYPE_PROXIMITY cm; distant from screen Phone position TYPE_RELATIVE_HUMIDITY humidity (%) Dew point TYPE_ROTATION_VECTOR Device’s rotation vector TYPE_TEMPERATURE

6 Android Sensor Framework
Check Availability What sensors do I have? Get Information e.g., maximum range, manufacturer, power requirements, and resolution. Get Values e.g., raw sensor data, minimum rate. Register/unregister for Sensor Events

7 Four Relevant Classes SensorManager: Sensor:
Create an instance of the sensor service. Accessing and listing sensors. Registering and unregistering sensor event listeners. Acquiring orientation information. Report sensor accuracy, set data acquisition rates, and calibrate sensors. Sensor: Create an instance of a specific sensor. Determine a sensor's capabilities.

8 Four Relevant Classes SensorEvent SensorEventListener
Carries information about a sensor event: Raw data, sensor type, accuracy, timestamp SensorEventListener Two callback methods that receive notifications: onSensorChanged() and onAccuracyChanged()

9 Working with Sensors Step 1: Get the SensorManger from system services. Step 2: Get the Sensor (or a list) from the manager. private SensorManager sm; private Sensor s; private List<Sensor> l; sm = (SensorManager) getSystemService (Context.SENSOR_SERVICE); l = sm.getSensorList(Sensor.TYPE_ALL); s = sm.getDefaultSensor(Sensor.TYPE_LIGHT); Returns “null” if no proximity sensor

10 Working with Sensors Step 3 (optional): Get information about a Sensor. s.getName(); s.getMaximumRange(); s.getResolution(); s.getMinDelay(); s.getPower(); s.getVendor(); s.getVersion();

11 Working with Sensors Step 4: Implement and Register SensorEventListener public class MainActivity extends AppCompatActivity implements SensorEventListener{ private SensorManager sm; private Sensor s; private List<Sensor> l; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); l = sm.getSensorList(Sensor.TYPE_ALL); s = sm.getDefaultSensor(Sensor.TYPE_LIGHT); sm.registerListener(this, s, ); } @Override public void onSensorChanged(SensorEvent event) { } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }

12 Code Practice: Show a list of available sensors on the device.
Get Gravity Sensor readings. Report them at 1 second interval. Now, add another sensor, e.g., Light Sensor. Report light sensor at 0.5 second interval.

13 Good Practices: Check sensor before using.
Declare <uses-feature> in the manifest. Unregister at onPause(), register at onResume(). if(sm.getDefaultSensor(Sensor.TYPE_LIGHT) == null) { //Display sensor not available message. } <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" /> @Override protected void onPause() { super.onPause(); sm.unregisterListener(this); protected void onResume() { super.onResume(); sm.registerListener(this, s, ); }

14 Good Practices: Don’t block onSensorChanged()
Choose sensor delays carefully. @Override public void onSensorChanged(SensorEvent event) { //Not a place to solve an NP-hard problem here*. } *it’s a joke


Download ppt "Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors"

Similar presentations


Ads by Google