! Sensors in Android devices. ! Motion sensors. ! Accelerometer. ! Gyroscope. ! Supports various sensor related tasks

Similar documents
Android Sensors. CPRE 388 Fall 2015 Iowa State University

Sensors & Motion Sensors in Android platform. Minh H Dang CS286 Spring 2013

Android Sensor Programming. Weihong Yu

Using the Android Sensor API

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL II)

Objective. Android Sensors. Sensor Manager Sensor Types Examples. Page 2

Android Sensors. XI Jornadas SLCENT de Actualización Informática y Electrónica

E0-245: ASP. Lecture 16+17: Physical Sensors. Dipanjan Gope

CS 403X Mobile and Ubiquitous Computing Lecture 6: Maps, Sensors, Widget Catalog and Presentations Emmanuel Agu

App Development for Smart Devices. Lec #5: Android Sensors

Using Sensors on the Android Platform. Andreas Terzis Android N00b

ELET4133: Embedded Systems. Topic 15 Sensors

Android Framework. How to use and extend it

Obsoleted chapter from The Busy Coder's Guide to Advanced Android Development

Programming Mobile Applications with Android

Android app development course

06 Team Project: Android Development Crash Course; Project Introduction

Android Programming Lecture 18: Menus Sensors 11/11/2011

HP TouchPad Sensor Setup for Android

Developing Sensor Applications on Intel Atom Processor-Based Android* Phones and Tablets

Module 1: Sensor Data Acquisition and Processing in Android

Pedometer Project 1 Mr. Michaud /

Lab 1 (Reading Sensors & The Android API) Week 3

CSE476 Mobile Application Development. Yard. Doç. Dr. Tacha Serif Department of Computer Engineering Yeditepe University

Android Concepts and Programming TUTORIAL 1

ECE 455/555 Embedded System Design. Android Programming. Wei Gao. Fall

Designing An Android Sensor Subsystem Pitfalls and Considerations

Lecture 07: Work and Kinetic Energy. Physics 2210 Fall Semester 2014

How to Convert 3-Axis Directions and Swap X-Y Axis of Accelerometer Data within Android Driver by: Gang Chen Field Applications Engineer

Android Development Tutorial. Nikhil Yadav CSE40816/ Pervasive Health Fall 2011

Develop a Hello World project in Android Studio Capture, process, store, and display an image. Other sensors on Android phones

Motion Sensing with mcube igyro Delivering New Experiences for Motion Gaming and Augmented Reality for Android Mobile Devices

Unit 4 Practice Test: Rotational Motion

SENSORS ON ANDROID PHONES. Indian Institute of Technology Kanpur Commonwealth of Learning Vancouver

Mobile App Tutorial Animation with Custom View Class and Animated Object Bouncing and Frame Based Animation

WEARIT DEVELOPER DOCUMENTATION 0.2 preliminary release July 20 th, 2013

Tegra Android Accelerometer Whitepaper

AUTOMATIC HUMAN FREE FALL DETECTION USING ANDROID

Sensors and Cellphones

Android. Mobile Computing Design and Implementation. Application Components, Sensors. Peter Börjesson

STATIC AND KINETIC FRICTION

Experiment: Static and Kinetic Friction

Android Programming: 2D Drawing Part 1: Using ondraw

Physics 40 Lab 1: Tests of Newton s Second Law

PHY231 Section 2, Form A March 22, Which one of the following statements concerning kinetic energy is true?

Performance issues in writing Android Apps

Using Extensions or Cordova Plugins in your RhoMobile Application Darryn

Rotation: Moment of Inertia and Torque

The BSN Hardware and Software Platform: Enabling Easy Development of Body Sensor Network Applications

AP Physics Circular Motion Practice Test B,B,B,A,D,D,C,B,D,B,E,E,E, m/s, 0.4 N, 1.5 m, 6.3m/s, m/s, 22.9 m/s

Chapter 3.8 & 6 Solutions

Internal Services. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr. Rajiv Ramnath

Data in seismology: networks, instruments, current problems

PHY231 Section 1, Form B March 22, 2012

Fitness Motion Recognition

Orbital Mechanics. Angular Momentum

Affdex SDK for Android. Developer Guide For SDK version 1.0

Specialized Android APP Development Program with Java (SAADPJ) Duration 2 months

3600 s 1 h. 24 h 1 day. 1 day

EDUH SPORTS MECHANICS

VELOCITY, ACCELERATION, FORCE

GRADE 8 SCIENCE INSTRUCTIONAL TASKS. Gravity

Kathy Au Billy Yi Fan Zhou Department of Electrical and Computer Engineering University of Toronto { kathy.au, billy.zhou }@utoronto.

On Quiz: Change #2 to 9/23

Using the Adafruit Unified Sensor Driver. Created by Kevin Townsend

COEFFICIENT OF KINETIC FRICTION

Forces. Definition Friction Falling Objects Projectiles Newton s Laws of Motion Momentum Universal Forces Fluid Pressure Hydraulics Buoyancy

Angular Velocity vs. Linear Velocity

C B A T 3 T 2 T What is the magnitude of the force T 1? A) 37.5 N B) 75.0 N C) 113 N D) 157 N E) 192 N

AS COMPETITION PAPER 2008

ELDERLY SUPPORT - ANDROID APPLICATION FOR FALL DETECTION AND TRACKING TEJITHA RUDRARAJU. B.E, Anna University, India, 2011 A REPORT

Graduate presentation for CSCI By Janakiram Vantipalli ( Janakiram.vantipalli@colorado.edu )

Centripetal force, rotary motion, angular velocity, apparent force.

KomliMobile Android SDK

Center of Gravity. We touched on this briefly in chapter 7! x 2

Use the following information to deduce that the gravitational field strength at the surface of the Earth is approximately 10 N kg 1.

Android and OpenGL. Android Smartphone Programming. Matthias Keil. University of Freiburg

A DECISION TREE BASED PEDOMETER AND ITS IMPLEMENTATION ON THE ANDROID PLATFORM

Making Android Motion Applications Using the Unity 3D Engine

State Newton's second law of motion for a particle, defining carefully each term used.

ENGINEERING COUNCIL DYNAMICS OF MECHANICAL SYSTEMS D225 TUTORIAL 1 LINEAR AND ANGULAR DISPLACEMENT, VELOCITY AND ACCELERATION

MMI 2: Mobile Human- Computer Interaction Android

AN APPLYING OF ACCELEROMETER IN ANDROID PLATFORM FOR CONTROLLING WEIGHT

Creating a List UI with Android. Michele Schimd

Acceleration due to Gravity

Experiment 4 ~ Newton s Second Law: The Atwood Machine

Android on Intel Course App Development - Advanced

Basic Principles of Inertial Navigation. Seminar on inertial navigation systems Tampere University of Technology

MTw Development Kit WIRELESS MOTION TRACKERS

Transcription:

CSC 472 / 372 Mobile Application Development for Android Prof. Xiaoping Jia School of Computing, CDM DePaul University xjia@cdm.depaul.edu @DePaulSWEng Outline Sensors in Android devices Motion sensors Accelerometer Gyroscope 2 Sensors Convert a wide range of physical measurements into digital signals. Android devices may include several types of sensors Motion sensors Measure the acceleration, linear and rotational, of the device Position sensors Measure the position and orientation of the device, e.g., geo location, proximity Environment sensors Measure environmental parameters, e.g., ambient temperature and pressure, illumination, and humidity, etc. Android Sensor Framework Supports various sensor related tasks Determine the availability and capability of sensors Acquire raw data from sensors at a given sampling rate Monitor sensor changes using sensor listeners 3 4

Sensor Types Sensors in Android can be hardware-based or software-based Hardware-based sensors are physical components built into a device You may acquire the raw data e.g., accelerometer, gyroscope Software-based sensors derive data from one or more hardware-based sensors a.k.a. virtual sensors, or synthetic sensors, e.g., gravity Use the same API as the hardware-based sensors Why Motion Sensors? Your device is aware of its precise motion within its environment Controls are no longer limited to the UI widgets on the screen Opens the door to a new world of gaming possibilities other creative uses 5 6 The Accelerometer A hardware-based sensor measures accelerations (in m/s 2 ) in each of the three physical axes. Acceleration is the rate of change of velocity. Acceleration of a device = Earth Gravity + User Acceleration Present in early Android devices API available since Android 1.5 (API level 3) Low power consumption Responsive Noisy Acceleration Data The accelerometer measures the acceleration of the device (A d ) The relation to forces applied to the sensor (F s ) A d = - F s / mass The forces include the force of gravity Newton s Second Law of Motion (1687) F = m a 1 N = 1 kg m/s 2 Sir Isaac Newton 1642 1726 7 10

Gravity When the device is still, the accelerometer reading is g 9.81m/s2 Average Earth gravitational acceleration at sea level The Gyroscope Galileo Galilei 1564 1642 Separate the gravity User acceleration Ad = - g - F / mass Two software-based sensors A device for measuring and maintaining orientation A hardware-based sensor measures the rotation rate (in radian/s) around each of the three physical axes Gravity Earth gravitational acceleration Linear Acceleration Acceleration sans gravity Radian: standard unit of angular measure 1 rad = 180 / π 57.2958 11 The Gyroscope Gyroscope invented by Léon Foucault in 1852. 13 Senor Coordinate System MEMS gyroscope (micro-electromechanical system) API available since Android 2.3 (API level 9) Responsive Precise Inherent bias drift Standard 3-axis coordinate system Relative to the device's screen in the default orientation Never changes when the device is rotated. 14 The coordinate system for 2-D graphics rotates according to the device screen orientation. 15

Sensor Manager A system service that manages all sensors Useful methods: Determine availability of sensors getsensorlist(type) Access an instance of a specific type of sensor getdefaultsensor(type) Determine the capability and attribute of a sensor getresolution() getmaximumrange() getrower() Minimum delay in sensing data, i.e., sampling rate getmindelay() Monitoring Sensor Data Implement the callback methods in the Sensor Event Listener interface onsensorchanged() onaccuracychanged() Register the listener and specify a sampling rate Normal ( 5fps), UI ( 17fps), Game ( 50fps) Unregister the listener when unused or paused The system will not disable sensors automatically when the screen turns off. 16 17 Sensor Data The current reading of the sensors are delivered in the values array of the Sensor Event For acceleration data values[0] acceleration along the X axis Values[1] acceleration along the Y axis Values[2] acceleration along the Z axis For rotation data (gyroscope) values[0] rate of rotation around the X axis Values[1] rate of rotation around the Y axis Values[2] rate of rotation around the Z axis The Motion Sensor App Demonstrate the use of motion sensors Acquire data from five sensors Accelerometer Gravity Linear acceleration Gyroscope Gyroscope uncelebrated Display sensor data textually and graphically 18 19

The Motion Sensors App Screen Orientation public class MyView extends View { private SensorManager sensormanager; sensormanager; private int[] sensortypes = { Arrays for: sensors, Sensor.TYPE_ACCELEROMETER, listeners, and Sensor.TYPE_GRAVITY, Sensor.TYPE_LINEAR_ACCELERATION, current readings for Sensor.TYPE_GYROSCOPE, each sensor, 3 axes. Sensor.TYPE_GYROSCOPE_UNCALIBRATED }; private Sensor[] Sensor[] sensors sensors == new new Sensor[5]; Sensor[5]; private private private SensorEventListener[] SensorEventListener[] listeners listeners == new new SensorEventListener[5]; SensorEventListener[5]; private float[][] sensordata = new float[5][3]; private float[][] sensordata = new float[5][3]; int[] colors = { }; private String[] labels = { "ACCELEROMETER, "GRAVITY, }; For apps using motion sensors, we want to fix the screen orientation in the Android Manifest <?xml version="1.0" encoding="utf-8"?> <manifest > <application > <activity android:name=".myactivity" android:label="@string/app_name" android:screenorientation="portrait" android:screenorientation="portrait <intent-filter> </intent-filter> </activity> </application> </manifest> > 20 22 The Constructors Initialize the Sensors private void initsensors() { sensormanager = (SensorManager) getcontext().getsystemservice(context.sensor_service); getcontext().getsystemservice(context.sensor_service); public class MyView extends View { public MyView(Context context) { super(context); initsensors(); public MyView(Context context, AttributeSet attrs) { super(context, attrs); initsensors(); private float[][][] sensordatahistory = new float[200][5][3]; private int start = 0; for (int s = 0; s < 5; s++) { final float[] data = sensordata[s]; sensors[s] sensors[s] = = sensormanager.getdefaultsensor(sensortypes[s]); sensormanager.getdefaultsensor(sensortypes[s]); if (sensors[s] = null) { listeners[s] = new SensorEventListener() { listeners[s] = new SensorEventListener() { @Override public public void void onsensorchanged(sensorevent onsensorchanged(sensorevent event) event) {{ @Override for (int (int ii == 0; 0; ii << 3; 3; i++) i++) data[i] data[i] == event.values[i]; event.values[i]; for invalidate(); invalidate(); @Override public void onaccuracychanged(sensor sensor, @Override public void onaccuracychanged(sensor sensor, int accuracy) { int accuracy) { }; }; Position of the earliest historic data Store historic sensor data in a circular array. 200 historic data points. 25 26

The Motion Sensors App Register & Unregister the Listeners Displaying the Sensor Data, 1/3 void resume() { for (int s = 0; s < 5; s++) { if (listeners[s] = null) sensormanager.registerlistener(listeners[s],sensors[s], sensors[s], sensormanager.registerlistener(listeners[s], SensorManager.SENSOR_DELAY_NORMAL); SensorManager.SENSOR_DELAY_NORMAL); void pause() { for (int s = 0; s < 5; s++) { if (listeners[s] = null) sensormanager.unregisterlistener(listeners[s]); sensormanager.unregisterlistener(listeners[s]); @Override protected void ondraw(canvas canvas) { canvas.drawcolor(color.white); paint.setantialias(true); Display paint.setcolor(color.black); the sensor paint.settypeface(typeface.default); data textually. paint.settextsize(30); for (int s = 0; s < 5; s++) { String msg = String.format("x=%f y=%f z=%f", sensordata[s][0], sensordata[s][1], sensordata[s][2]); paint.setcolor(colors[s]); canvas.drawtext(labels[s], 20, 50 + 60 * s, paint); paint.setcolor(color.black); canvas.drawtext(msg, 50, 80 + 60 * s, paint); 27 29 The Motion Sensors App The Motion Sensors App Displaying the Sensor Data, 2/3 Displaying the Sensor Data, 3/3 @Override protected void ondraw(canvas canvas) { Display textual data. for (int s = 0; s < 5; s++) { for (int i = 0; i < 3; i++) sensordatahistory[start][s][i] = sensordata[s][i]; start = ++start % 200; Draw historical graph. Add the latest sensor data to historic data array. Drop the earliest data point. 31 @Override protected void ondraw(canvas canvas) { paint.setstyle(paint.style.stroke); for (int s = 0; s < 5; s++) { int ybase = 450 + s * 100; paint.setcolor(colors[s]); for (int axis = 0; axis < 3; axis++) { Draw historical graph. int xbase = 20 + axis * 220; Path path = new Path(); path.moveto(xbase, ybase - sensordatahistory[start][s][axis]*8); for (int i = 1; i < 200; i++) { int h = (start + i) % 200; path.lineto(xbase + i, ybase - sensordatahistory[h][s][axis]*8); canvas.drawpath(path, paint); 33

The Gravity App Extension of the Bouncing Objects app Drawing using Surface View Use motion sensors to control the movement of the objects. Gravity Linear acceleration (demo) Gyroscope (demo) Most code is same as the Bouncing Objects app The Gravity App public class MyView extends SurfaceView implements SurfaceHolder.Callback { private SensorManager sensormanager; private Sensor sensor; private SensorEventListener listener; private float[] sensordata = new float[3]; private static final int MAX_V = 15; private static final float GRAVITY_F = 0.2f; Constructors and methods 34 35 Constructors public MyView(Context context) { super(context); init(); public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); private void init() { holder = getholder(); holder.addcallback(this); initshapes(5); sensormanager = (SensorManager) getcontext().getsystemservice(context.sensor_service); initsensor(sensor.type_gravity); 36 Initialization of the Sensor private void initsensor(int type) { sensor = sensormanager.getdefaultsensor(type); if (sensor = null) { listener = new SensorEventListener() { @Override public void onsensorchanged(sensorevent event) { for (int i = 0; i < 3; i++) sensordata[i] = event.values[i]; @Override public void onaccuracychanged(sensor sensor, int accuracy) { }; 37

Control the Movement of Objects class MyShape { void move() { float gx sensordata[1]; // landscape mode float gx = sensordata[1]; // landscape mode float gy = sensordata[0]; // landscape mode dx += gx * GRAVITY_F; dy += gy * GRAVITY_F; dx = Math.min(Math.max(dx, -MAX_V), MAX_V); dy = Math.min(Math.max(dy, -MAX_V), MAX_V); Rect bounds = drawable.getbounds(); if (bounds.right >= width && dx > 0 bounds.left < 0 && dx < 0) dx = -dx; if (bounds.bottom >= height && dy > 0 bounds.top < 0 && dy < 0) dy = -dy; bounds.left += dx; bounds.right += dx; bounds.top += dy; bounds.bottom += dy; Map the sensor coordinates to 2D landscape coordinates Use acceleration to adjust velocity 40 Register & Unregister the Listener public void startanimation() { done = false; if (listener = null) sensormanager.registerlistener(listener, sensor, SensorManager.SENSOR_DELAY_GANE); if (surfaceavailble) startrenderingthread(); public void stopanimation() { done = true; if (listener = null) sensormanager.unregisterlistener(listener); 41 The Sample Code The sample apps in this lecture are available in D2L MotionSensors.zip Gravity.zip Each zip archive contains the entire project folder Unzip the file and import to Android Studio Next Final project demo, next Tuesday " Android is a trademark of Google Inc. " Some contents and diagrams are from Google Inc. Licensed under Apache 2.0 and Creative Commons Attribution 2.5. 42 43