The document provides an overview of motion and position sensors in Android development, detailing how these sensors monitor device movement and orientation. It explains the use of accelerometers, gyroscopes, and magnetometers, along with methods for determining device orientation and handling coordinate transformations. Additionally, it covers practical applications such as step counting and the use of rotation vectors for accurate orientation detection.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views33 pages
3.2 Motion and Position Sensors
The document provides an overview of motion and position sensors in Android development, detailing how these sensors monitor device movement and orientation. It explains the use of accelerometers, gyroscopes, and magnetometers, along with methods for determining device orientation and handling coordinate transformations. Additionally, it covers practical applications such as step counting and the use of rotation vectors for accurate orientation detection.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33
Advanced Android Development
Sensors
Lesson 3
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 1 Development International License 3.2 Motion and position sensors Monitor device movement or position in space
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 2 Development International License Contents
● Overview of motion and position sensors
● Determining device orientation ● Understanding device rotation ● Using motion sensors ● Using position sensors
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 3 Development International License Overview Motion and position sensors
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 4 Development International License Motion and position sensors
● Motion and position sensors monitor device
movement or position in space respectively ● Both return multi-dimensional arrays of sensor values for each SensorEvent ○ Example: Accelerometer returns acceleration force data for 3 coordinate axes (x, y, z) relative to device
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 5 Development International License Coordinate systems
● Device coordinate system : Some sensors use
device coordinate system relative to the device ○ Example: Accelerometers
● Earth coordinate system : Other sensors use
Earth coordinate system relative to Earth surface ○ Example: Magnetometer
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 6 Development International License Device coordinates (1) ● Relative to physical device regardless of device position in the world ● x is horizontal and points right ● y is vertical and points up ● z points toward outside of screen ● Negative z points behind screen This work is licensed under a Advanced Android Sensors Creative Commons Attribution 4.0 7 Development International License Device coordinates (2) ● Relative to the device screen when device is in its default orientation ● Axes are not swapped when orientation changes by rotation ● App must transform incoming sensor data to match rotation
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 8 Development International License Earth coordinates ● y points to magnetic north along Earth's surface ● x is 90 degrees from y, pointing east ● z extends up into space ● Negative z extends down into ground This work is licensed under a Advanced Android Sensors Creative Commons Attribution 4.0 9 Development International License Determining device orientation
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 10 Development International License Device orientation ● Position of device relative to Earth's coordinates (y points to magnetic north) ● Determine by using accelerometer and geomagnetic field sensor with methods in SensorManager
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 11 Development International License Components of orientation ● Azimuth ○ Angle between device's compass direction and magnetic north ● Pitch ○ Angle between plane parallel to device's screen and plane parallel to ground ● Roll ○ Angle between plane perpendicular to device's screen and plane perpendicular to ground This work is licensed under a Advanced Android Sensors Creative Commons Attribution 4.0 12 Development International License SensorManager methods ● getRotationMatrix() generates rotation matrix from accelerometer and geomagnetic field sensor ○ Translates sensor data from device coordinates to Earth coordinates ● getOrientation() uses rotation matrix to compute angles of device's orientation
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 13 Development International License Example: Determine orientation private SensorManager mSensorManager; ... // Rotation matrix based on current readings. final float[] rotationMatrix = new float[9]; mSensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading, magnetometerReading);
// Express updated rotation matrix as 3 orientation
angles. final float[] orientationAngles = new float[3]; mSensorManager.getOrientation(rotationMatrix, This work is licensed under a Advanced Android Sensors Creative Commons Attribution 4.0 14 Development International License Understandi ng device rotation
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 15 Development International License Transform coordinates for rotation If app draws views based on sensor data: ● Screen or activity coordinate system rotates with device ● Sensor coordinate system doesn't rotate ● Need to transform sensor coordinates to activity coordinates
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 16 Development International License Handle device and activity rotation 1. Query device orientation with getRotationMatrix() 2. Remap rotation matrix from sensor data to activity coordinates with remapCoordinateSystem()
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 17 Development International License Returned from getRotation() Integer constants: ● ROTATION_0: Default (portrait for phones) ● ROTATION_90: Sideways (landscape for phones) ● ROTATION_180: Upside-down (if device allows) ● ROTATION_270: Sideways in the opposite direction ● Many devices return ROTATION_90 or ROTATION_270 regardless of clockwise or counterclockwise rotation
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 18 Development International License Example: Handle device rotation (1) Use getRotation() with remapCoordinateSystem(): float[] rotationMatrix = new float[9]; boolean rotationOK = SensorManager.getRotationMatrix(rotationMatrix, null, mAccelerometerData, mMagnetometerData); // Remap matrix based on current device/activity rotation. float[] rotationMatrixAdjusted = new float[9]; This work is licensed under a Advanced Android Sensors Creative Commons Attribution 4.0 19 Development International License Example: Handle device rotation (2) switch (mDisplay.getRotation()) { case Surface.ROTATION_0: rotationMatrixAdjusted = rotationMatrix.clone(); break; case Surface.ROTATION_90: SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, rotationMatrixAdjusted); Break; // Rotation_180, Rotation_270 ...
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 20 Development International License Example: Handle device rotation (3) // Rotation_180, Rotation_270 case Surface.ROTATION_180: SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Y, rotationMatrixAdjusted); break; case Surface.ROTATION_270: SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, rotationMatrixAdjusted); break; This work is licensed under a } Advanced Android Sensors Creative Commons Attribution 4.0 International License 21 Development Using motion sensors Monitor device motion such as tilt, shake, rotation, swing
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 22 Development International License Motion sensors The movement is usually a reflection of : ● Direct user input relative to device/app (steering car in game, etc.) ● Device motion relative to Earth (device is with you while you are driving) ○ Motion sensors are used with other sensors to determine device position relative to Earth
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 23 Development International License Accelerometer ● TYPE_ACCELEROMETER measures acceleration along 3 device axes (x, y, z) including gravity ● Acceleration without gravity: use TYPE_LINEAR_ACCELERATION ● Force of gravity without acceleration: use TYPE_GRAVITY ● TYPE_GYROSCOPE measures rate of rotation (radians/second) ● For calculations This work is licensed under a see SensorEvent Advanced Android Development Sensors values Creative Commons Attribution 4.0 International License 24 Accelerometer event data Event data Description Units SensorEvent.values[0 Acceleration force along x-axis, m/s2 ] including gravity
SensorEvent.values[1 Acceleration force along y-axis, m/s2
] including gravity
SensorEvent.values[2 Acceleration force along z-axis, m/s2
] including gravity
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 25 Development International License Gravity event data Event data Description Units SensorEvent.values[0 Gravity along x- m/s2 ] axis SensorEvent.values[1 Gravity along y- m/s2 ] axis SensorEvent.values[2 gravity along z- m/s2 ] axis
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 28 Development International License Rotation-vector sensor ● TYPE_ROTATION_VECTOR provides orientation with respect to Earth coordinated as unit quaternion ● Software sensor that integrates data from accelerometer, magnetometer, and gyroscope (if available) ● Efficient and accurate way to determine device orientation ● For calculations see SensorEvent valuesThis work is licensed under a Advanced Android Sensors Creative Commons Attribution 4.0 29 Development International License Step counter and step detector ● TYPE_STEP_COUNTER measures user steps since last reboot ● To preserve battery use JobScheduler to retrieve current value from step-counter at specific interval ● TYPE_STEP_DETECTOR: hardware sensor that triggers event for each step ● Example: See the BatchStepSensor sample app This work is licensed under a Advanced Android Sensors Creative Commons Attribution 4.0 30 Development International License Using position sensors Determine device physical position on Earth
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 31 Development International License Geomagnetic (magnetometer) ● TYPE_MAGNETIC_FIELD measures strength of magnetic fields around device on each of 3 axes (x, y, z), including Earth magnetic field ● Units are in microtesla (uT) ● Find device position with respect to external world (compass)
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 32 Development International License Orientation ● TYPE_ORIENTATION deprecated in API 8
● For accurate device orientation (choose one):
○ Use getRotationMatrix() and getOrientation(), or ○ Use rotation-vector sensor with TYPE_ROTATION_VECTOR This work is licensed under a Advanced Android Sensors Creative Commons Attribution 4.0 33 Development International License What's next?
● Concept chapter: 3.2 Motion and position sensors
● Practical: 3.2 Working with sensor-based orientation
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 34 Development International License END
This work is licensed under a
Advanced Android Sensors Creative Commons Attribution 4.0 35 Development International License