ELET4133: Embedded Systems Topic 15 Sensors
Agenda What is a sensor? Different types of sensors Detecting sensors Example application of the accelerometer 2
What is a sensor? Piece of hardware that collects data from the physical world, transforms it into useable data and transfers it to the computer or microcontroller Apps read the data (as voltage or current), Makes a decision based on its value Does whatever it needs to do Sensors operate as input devices only To receive data we have to setup a listener to react to new incoming data 3
Sensor Types Light Sensor As of Android 2.3 Proximity Sensor Gravity Sensor Temperature Sensor Linear Accerlation Sensor Pressure Sensor Rotation Vector Sensor Gyroscope Sensor Near Field Accelerometer Commincation (NFC) Magnetic Field Sensor Sensor Orientation Sensor Works differently than the others 4
Detecting a Sensor Not all devices have all sensors The emulator has only an accelerometer Basically for orientation purposes To discover which sensors are on your device you can use a direct or indorect method Direct: get a list of available sensors from the SensorManager, set up listeners for them, then retrieve data from them Assumes user has already installed the App Indirect: in the manifest you can specify the resoruces a device must have to use this App 5
Develop an App to List Sensors The App should look like this: It will use a simple Linear Layout, but have a ScrollView Then a TextView within the ScrollView In case there is a long list of sensors 6
Start with the Layout Setup a TextView within a Scroll View within a Linear Layout Try it graphically: First: changed the default layout (Relative) to a Vertical Linear Layout 7
Start with the Layout Setup a TextView within a Scroll View within a Linear Layout Try it graphically: First: changed the default layout (Relative) to a Vertical Linear Layout Then: Select a Scroll View from the Composite Pallete 8
Start with the Layout Setup a TextView within a Scroll View within a Linear Layout Try it graphically: First: changed the default layout (Relative) to a Vertical Linear Layout Then: Select a Scroll View from the Composite Pallete and drag it over to the graphical layout screen 9
Start with the Layout As you drag it into the Linear Layout, it will turn orange 10
Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. 11
Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. Then if you pull it down and let go, the scrollview will expand to fill the screen. 12
Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. Then if you pull it down and let go, the scrollview will expand to fill the screen. Then drop in the textview 13
Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. Then if you pull it down and let go, the scrollview will expand to fill the screen. Then drop in the textview The xml file appears as shown here 14
Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. Then if you pull it down and let go, the scrollview will expand to fill the screen. Then drop in the textview The warning message states that either the LinearLayout or the ScrollView are unecessary The xml file appears as shown here 15
Start with the Layout But, it was left the way it was because it didn t hurt anything and it was just a warning 16
Add Functionality 17
Start the Java File This is the Java file that has (so far) been generated by Eclipse 18
Add the TextView This is the Java file that has (so far) been generated by Eclipse TextView mytextview = (TextView)findViewById(R.id.textView1); This statement was added to create an identifier and a reference for the TextView. When this statement was first typed in, an error was generated, but there was nothing wrong that I could see. 19
Add the TextView This is the Java file that has (so far) been generated by Eclipse TextView mytextview = (TextView)findViewById(R.id.textView1); This statement was added to create an identifier and a reference for the TextView. When this statement was first typed So, [CNTRL][SHIFT]+O was typed, which added the correct import statement in, an error was generated, but there was nothing wrong that I so that the error was resolved could see. 20
Add the SensorManager Next, we need a reference to the Sensor Manager The command is: SensorManager sensormanager = (SensorManager) this.getsystemservice(sensor_service); There can be only one Sensor Manager, and so we need it for the system s sensor service. The command is inserted here. 21
Add the getsensorlist Command Now that we have a sensor manager, we can use the getsensorlist ( ) method It will return a list of all the sensors The command is: List<Sensor> sensors = sensormanager.getsensorlist(sensor.type_all); 22
Add the getsensorlist Command Now that we have a sensor manager, we can use the getsensorlist ( ) method It will return a list of all the sensors The command is: List<Sensor> sensors = sensormanager.getsensorlist(sensor.type_all); And, again, we have an error. [CNTRL][SHIFT]+O imports the correct package 23
Help Windows As the command is being typed, several Help screens pop up: 24
Help Windows The.getSensorList help 25
Help Windows The.getSensorList help and the Type Help (Type of List) 26
The Information Next, we need to build a String to hold the information that we want to display in the TextView: The list of sensors 27
The Information To do this, use a StringBuilder Allows you to append information to an existing String First, create a new StringBuilder Then append your starting message to it 28
The Information This command returns a list of all the sensors Then, we have to use some kind of loop to build the list of sensors 29
The Information Create a new Sensor called sensor 30
The Information Create a new Sensor called sensor As the sensor.getname statement is being typed, the help screen pops up so we can see what methods are available to sensor 31
The Information Create a new Sensor called sensor As the sensor.getname statement is being typed, the help screen pops up so we can see what methods are available to sensor 32
The Information An identifier name (sensortypes) was used, but it has no corresponding type So an error occurs This type has not been entered yet If you hover the cursor over the error, 9 quick fixes are suggested: 33
The Information An identifier name (sensortypes) was used, but it has no corresponding type So an error occurs This type has not been entered yet If you hover the cursor over the error, 9 quick fixes are suggested: 34
The Information But, to fix it, we are going to create a HashMap A HashMap is a subclass of Map that allows us to create a list of key and value pairs This will allow us to get a string for the Type of sensor in the list 35
The HashMap private HashMap<Integer, String> sensortypes = new HashMap<Integer, String>(); {// Initialize the map of sensor types and values sensortypes.put(sensor.type_accelerometer, "TYPE_ACCELEROMETER"); sensortypes.put(sensor.type_gyroscope, "TYPE_GYROSCOPE"); sensortypes.put(sensor.type_light, "TYPE_LIGHT"); sensortypes.put(sensor.type_magnetic_field, "TYPE_MAGNETIC_FIELD"); sensortypes.put(sensor.type_orientation, "TYPE_ORIENTATION"); sensortypes.put(sensor.type_pressure, "TYPE_PRESSURE"); sensortypes.put(sensor.type_proximity, "TYPE_PROXIMITY"); sensortypes.put(sensor.type_temperature, "TYPE_TEMPERATURE"); sensortypes.put(sensor.type_gravity, "TYPE_GRAVITY"); sensortypes.put(sensor.type_linear_acceleration, "TYPE_LINEAR_ACCELERATION"); sensortypes.put(sensor.type_rotation_vector, "TYPE_ROTATION_VECTOR"); } //End of Hash Map The HashMap matches an Integer constant (TYPE_LIGHT) to a String TYPE_LIGHT 36
The HashMap private HashMap<Integer, String> sensortypes = new HashMap<Integer, String>(); {// Initialize the map of sensor types and values sensortypes.put(sensor.type_accelerometer, "TYPE_ACCELEROMETER"); sensortypes.put(sensor.type_gyroscope, "TYPE_GYROSCOPE"); sensortypes.put(sensor.type_light, "TYPE_LIGHT"); sensortypes.put(sensor.type_magnetic_field, "TYPE_MAGNETIC_FIELD"); // sensortypes.put(sensor.type_orientation, "TYPE_ORIENTATION"); sensortypes.put(sensor.type_pressure, "TYPE_PRESSURE"); sensortypes.put(sensor.type_proximity, "TYPE_PROXIMITY"); // sensortypes.put(sensor.type_temperature, "TYPE_TEMPERATURE"); sensortypes.put(sensor.type_gravity, "TYPE_GRAVITY"); sensortypes.put(sensor.type_linear_acceleration, "TYPE_LINEAR_ACCELERATION"); sensortypes.put(sensor.type_rotation_vector, "TYPE_ROTATION_VECTOR"); } //End of Hash Map Two of the TYPES gave warning messages saying that they had been deprecated for this version of Android, so for now they were commented 37
One more thing We have to set the text into the TextView so that it will print the available sensors to the screen of the android 38
Execution The project was then tested on the emulator There are no sensors on the emulator, so all that was shown was the first string that was built 39
Execution The project was then tested on the emulator There are no sensors on the emulator, so all that was shown was the first string that was built Then, it was tested on the phone All of its sensors (that were found in that list) were printed on the screen 40
Accelerometer Test App 41
Accelerometer Test App This App will read the values on the Acceleromoter and print them on the screen If nothing else, it is a Test App to make usre that: There is an Accelerometer on the phone The Accelerometer is working The program will read the Acceleromoeter 42
Start with the Layout Again, we ll start with the Layout Keep it very simple The default RelativeLayout 43
Start with the Layout Again, we ll start with the Layout Keep it very simple The default RelativeLayout Add 4 TextViews to print the values for X, Y, and Z (and the title) 44
The Layout The default Relative Layout is: Has the single TextView for Hello World <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> 45
The TextViews The first part: The 1 st TextView is for the Title <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relative"> <TextView android:textsize="30dp" android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content /> <TextView android:textsize="20dp" android:layout_below="@+id/name" android:id="@+id/xval" android:layout_width="fill_parent" android:layout_height="wrap_content"/>..... 46
The TextViews The rest: </RelativeLayout> <TextView android:textsize="20dp" android:id="@+id/yval" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/xval"/> <TextView android:textsize="20dp" android:id="@+id/zval" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/yval"/> 47
The MainActivity.java We are going to use the Accelerometer, so we have several things we must do: 1. Extend Activity 2. Implement a SensorEventListener (for changes) 3. oncreate( ) method 4. Implement an event handler for when the accelerometer changes values 48
The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; Create the necessary identifiers @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 49
The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; Setup the oncreate( ) method @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 50
The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; Set the content view @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 51
The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); Create a new SensorManager (always before the sensor) } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 52
The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); Create a new accelerometer } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 53
The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); Setup the needed textviews 54
The Event Handler @Override public final void onsensorchanged(sensorevent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; The Event Handler declaration } //display values using TextView title.settext(r.string.app_name); tv.settext("x axis" +"\t\t"+x); tv1.settext("y axis" + "\t\t" +y); tv2.settext("z axis" +"\t\t" +z); 55
The Event Handler @Override public final void onsensorchanged(sensorevent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; } float y = event.values[1]; float z = event.values[2]; //display values using TextView title.settext(r.string.app_name); tv.settext("x axis" +"\t\t"+x); tv1.settext("y axis" + "\t\t" +y); tv2.settext("z axis" +"\t\t" +z); The values for x, y, and z as read off the accelerometer 56
The Event Handler @Override public final void onsensorchanged(sensorevent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; } //display values using TextView title.settext(r.string.app_name); tv.settext("x axis" +"\t\t"+x); tv1.settext("y axis" + "\t\t" +y); tv2.settext("z axis" +"\t\t" +z); Print out the title (AccelerometerDemo) 57
The Event Handler @Override public final void onsensorchanged(sensorevent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; } //display values using TextView title.settext(r.string.app_name); tv.settext("x axis" +"\t\t"+x); tv1.settext("y axis" + "\t\t" +y); tv2.settext("z axis" +"\t\t" +z); Print out the three values) 58
Other Methods @Override protected void onresume() { super.onresume(); msensormanager.registerlistener(this, maccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onpause() { super.onpause(); msensormanager.unregisterlistener(this); } Two of the necessary methods In case the App is paused and resumed 59
Execution When executed on the emulator Again, nothing happens Must run it on the phone to test it 60
Summary We defined a sensor And looked at the different types of sensors on an android phone We wrote an App to detect the sensors on the phone and list them on the screen Developed an example application of the accelerometer 61