Chapter 5. The Motion, Position, and Fingerprint Sensors

This chapter will introduce you to the motion, position, and fingerprint sensors. We will learn in detail about all the motion sensors (accelerometer, gyroscope, linear acceleration, gravity, and significant motion) and position sensors (magnetometer and orientation). As a learning exercise for the chapter, we will develop three small applications. The first application will detect a shake using the accelerometer sensor, the second one will tell the earth's magnetic field direction using the orientation sensor, and the third one will use the fingerprint sensor to authenticate the user.

The topics covered in this chapter are as follows:

  • Understanding the motion-based sensors (accelerometer, gyroscope, linear acceleration, gravity, and significant motion sensors)
  • Understanding the position-based sensors (magnetometer and orientation sensors)
  • Understanding the newly introduced fingerprint sensor and its supporting APIs
  • How to use the accelerometer sensor to detect the shaking of a phone
  • How to use the orientation sensor and alternative API to build a compass
  • How to use the fingerprint sensor to authenticate the user

Understanding motion-based sensors

Motion sensors are responsible for measuring acceleration forces and rotational forces acting along three axes of the phone. Motion sensors include the accelerometer, the gyroscope, gravity, linear acceleration, signification motion, the step detector, and the step counter. The detailed list of motion sensors can be found in Chapter 1, Sensor Fundamentals. In the next section, we will be discussing these sensors individually in detail. We will cover the step detector and step counter in the next chapter.

The accelerometer sensor

The accelerometer sensor determines the acceleration along the xy, and z axes, which is applied to a phone by measuring the forces acting on the phone. The measured acceleration includes both the physical acceleration (change of velocity) and the static gravity acting on the phone all the time. Accelerometer sensors are made up of the Micro Electro Mechanical System (MEMS), which is an embedded system that integrates electronic and mechanical components on a very small scale. The general principle of working of the smart phone accelerometer is based on the displacement of microscopic crystal plates, also called seismic mass, in the xy, and z directions over the microscopic crystal base structure, as shown in the following diagram. When the phone is moved because of any user-generated force, the seismic mass also moves relative to the base structure located in between the plates of seismic mass, which produces a change in capacitance. This change in capacitance is converted to force using electric circuits. Additional algorithms are applied to calibrate the accelerometer values to compensate for the temperature, bias, and scale. The value reported by the accelerometer sensor is in SI units (m/s2). When the phone is kept still without any motion, then the accelerometer sensor shows 9.81m/s2 of gravitational force acting on the phone, and when the phone is in free fall, it shows zero force acting on the phone.

The accelerometer sensor

The gyroscope sensor

The gyroscope sensor reports the rate of rotation of the device around the x, y, and axes. The gyroscope sensor works by sensing the change in capacitance between two microscopic crystal structures due to the effect of rotational forces acting on them. The general principle of working of the smart phone gyroscope is based on displacement of the microscopic crystal circular plate also called the proof mass in the x, y and z directions, over the underlying microscopic crystal base or plate, as shown in the following diagram. When the user rotates the phone, the proof mass (center plate) moves over the capacitor plates located underneath the proof mass, which produces a change in capacitance. This change in capacitance is converted to a rate of rotation using electric circuits. Additional algorithms are applied to calibrate the gyroscope values to compensate for the temperature, drift, and scale. There are two types of values given by the gyroscope sensor: calibrated and uncalibrated. The uncalibrated values are the raw values without the drift compensation and can be requested from SensorManager by specifying the TYPE_GYROSCOPE_UNCALIBRATED constant, while the calibrated values can be obtained by requesting the TYPE_GYROSCOPE constant. For most use cases, we should use the calibrated gyroscope values, and it is only if we are applying our own calibration algorithms that we should use un-calibrated values. The values are reported in units of radians per second (rad/s).

The gyroscope sensor

The gravity sensor

The gravity sensor is a software sensor and it reports the force of gravity acting along the x, y, and axes. It uses the same accelerometer sensor unit (m/s2) to report values. The gravity values are computed by removing the user-generated forces acting on the phone. It uses the accelerometer as its underlying hardware sensor.

The linear acceleration sensor

The linear acceleration sensor is a software sensor and is responsible for reporting the acceleration force acting on the x, y, and axes of the phone after excluding the gravity force. It also uses the same unit (m/s2) to report values that is used by the gravity and accelerometer sensors. Linear acceleration values are calculated by removing the force of gravity acting on each axis of the phone. It uses the accelerometer as its underlying hardware sensor. Conceptually, the accelerometer sensor values are a summation of the linear acceleration and gravity:

Accelerometer value = Linear Acceleration value + Gravity value.

The significant motion sensor

The signification motion sensor is a software sensor, and it uses the accelerometer as its underlying hardware sensor. It triggers an event every time a signification motion on the phone takes place. Walking, biking, or sitting in a moving car are examples of signification motions. It has a one-shot reporting mode and hence will be disabled automatically once it is triggered. Further details on reporting modes can be found in Chapter 1, Sensor Fundamentals. We need to request the significant motion as many times as we want to trigger it. The way to set up the significant motion sensor is slightly different than other sensors, which are done using SensorManager. The significant motion event trigger callback is set using the object of TriggerEventListener. The following is the code snippet to set up, request, and cancel the signification motion sensor:

public class SigMotionActivity extends Activity{ 
 
    private SensorManager mSensorManager; 
    private Sensor mSensor; 
    private TriggerEventListener mTriggerEventListener; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        mSensorManager = (SensorManager) 
        getSystemService(Context.SENSOR_SERVICE); 
        mSensor = mSensorManager.getDefaultSensor
       (Sensor.TYPE_SIGNIFICANT_MOTION); 
 
        mTriggerEventListener = new TriggerEventListener() { 
            @Override 
            public void onTrigger(TriggerEvent event) { 
                // Take action 
            } 
        }; 
    } 
 
    @Override 
    protected void onResume() { 
        super.onResume(); 
        mSensorManager.requestTriggerSensor
        (mTriggerEventListener, mSensor); 
    } 
 
    @Override 
    protected void onPause() { 
        super.onPause(); 
        mSensorManager.cancelTriggerSensor(mTriggerEventListener, mSensor); 
    } 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset