7 - 2 - Sensors - Part 2 (11-40)

5
[BLANK_AUDIO] Hi, I'm Adam Porter, and this is Programming Mobile Applications for Android Handheld Systems. In the example application that we just looked at, I tried to hold the device perfectly straight up. And if I'd been able to do that, the accelerometer would ideally have reported values around x equals 0 meters per second squared, y equals 9.81 meters per second squared and Z equals 0 meters per second squared. But as you saw in the example application, the accelerometers values fluctuated. All applications will experience this kind of thing due to natural user movement, non flat surfaces, electrical noise and so forth. When creating sensor enabled applications, developers will often apply transforms to the raw data to smooth it out. 2 common kinds of transforms are called low-pass filters and high-pass filters. Let's talk about each one of those one at a time. Low-pass filters are used to deemphasize small transient force changes while emphasizing the long-term constant forces. You might use a low-pass filter when your application needs to pay attention to the constant force of gravity for example, and you don't want to be affected just because your hands shakes a little. A real life example of this would be something like a carpenter's level. The bubble needs to move based on gravity, not based on small hand twitches. In contrast, you use a high-pass filter when you want to emphasize the transient force changes, and you want to deemphasize the constant force components. You might use a high-pass filter when your application should ignore the constant force of gravity for example, but should respond to the specific moves that the user makes. A real life example of this might be a percussion instrument like a set or maracas. You don't really care about gravity here, you care about how the user is shaking the

Transcript of 7 - 2 - Sensors - Part 2 (11-40)

Page 1: 7 - 2 - Sensors - Part 2 (11-40)

[BLANK_AUDIO]Hi, I'm Adam Porter, and this isProgramming Mobile Applications forAndroid Handheld Systems.In the example application that we justlooked at,I tried to hold the device perfectlystraight up.And if I'd been able to do that, theaccelerometer would ideally have reportedvalues around x equals 0meters per second squared, y equals 9.81meters persecond squared and Z equals 0 meters persecond squared.But as you saw in the example application,the accelerometers values fluctuated.All applications will experience this kindof thingdue to natural user movement, non flatsurfaces,electrical noise and so forth.When creating sensor enabled applications,developers will often applytransforms to the raw data to smooth itout.2 common kinds of transforms are calledlow-pass filters and high-pass filters.Let's talk about each one of those one ata time.Low-pass filters are used to deemphasizesmall transient force changes whileemphasizing the long-term constant forces.You might use a low-pass filter when yourapplication needs to pay attention to theconstant forceof gravity for example, and you don'twant tobe affected just because your hands shakesa little.A real life example of this would besomething like a carpenter's level.The bubble needs to move based on gravity,not based on small hand twitches.In contrast, you use a high-pass filterwhen you want to emphasizethe transient force changes, and youwant to deemphasize the constant forcecomponents.You might use a high-pass filter whenyour application should ignore theconstant forceof gravity for example, but should respondto the specific moves that the user makes.A real life example of this might bea percussion instrument like a set ormaracas.You don't really care about gravity here,youcare about how the user is shaking the

Page 2: 7 - 2 - Sensors - Part 2 (11-40)

instrument.The next application is called sensorfiltered accelerometer.This application applies both a low pass,and a high pass filter to the rawaccelerometer values and then, it displaysthe filtered values.Let's run it.Sohere's my device.Now I'll start up the Sensor FilteredAccelerometer application.As you can see, this application displays9 text views with numbers in them.These numbers correspond to the x, y, andz values being readfrom the device's accelerometer.The raw values, after applying alow pass filter and those raw values afterapplying a high pass filter.If we let the application run for a while,we'll see that the low pass values beginto approximate our ideal accelerometerreadings roughly 0for the x and z axes, and roughly 9.81 forthe y axis.At the same time, you can see that thehigh-pass values all tend toward 0.If I rotate the device counterclockwise,you see the high pass x value go positive.And if I rotate the device clockwise,you'llsee the high pass x value go negative.Let's look at the source code for thisapplication.Here's the sensor filter accelerometerapplication open in the IDE.Now I'll open the main activity.And notice again that this classimplements the sensor event listenerinterface.So it can receive callbacks from thesensor manager.Now and on create the application gets areference to the sensor manager.Next, it gets a reference to the device'saccelerometer by calling sensormanager.get default sensor passing in thetype constant that corresponds to theaccelerometer.In the on resume method, the applicationregisters this class as a listenerfor accelerometer events by calling theregister listener method.And next, the on pause method unregistersthis class asa listener for any sensors to which it maybe listening.Scrolling down, we now come to the onsensor changed method.

Page 3: 7 - 2 - Sensors - Part 2 (11-40)

And as before, this method first checks tomake sure that thisevent is an accelerometer reading.And then it checks that a certain amountoftime has passed since the last reading wasdisplayed.If it has, the code records theaccelerometer's x,y and z values, and then applies the lowpass filter to each of the raw valuesafterwhich the code applies at the high-passfilter to eachof the raw values.Let's look at the code for the 2 filters.Here's the low-pass method, which computesthe low-pass filter.This method takes 2 parameters: thecurrent reading and the long term average.It then computes the filter value, as, asa kind of weighted average.In this case,the filtered value equals 80% of the longterm average plus 20% of the currentreading.Over time, this calculation moves towardstheideal values that we talked about earlier.Scrolling down, here's the high passmethodwhich computes the high pass filteredvalues.And this method also takes 2 parameters -the current reading, and the long termaverage which is actuallycomputed by the low pass method, that wejust talked about.This code then subtracts the long-termaverage from the current reading andtherefore represents the part of thereading that is not due to gravity.This example application is calledSensorCompassand this application uses the device'saccelerometer and its magnetometer toorienta compass arrow towards magnetic north.So here's my device.Now I'll start up the sensor compassapplication.As you can see, this application displaysa greencircle with a white arrow.Right now, this arrowpoints towards magnetic north.However if I begin torotate the device, you see that the arrowcontinues to point towardsthe north which of course is exactly what

Page 4: 7 - 2 - Sensors - Part 2 (11-40)

a compass should do.Let's look at the sourcecode for this application.Here's the sensor compass application openin the IDE.Now I'll open the main activity.Lets scroll down to the on create method.As with the other applications, this onebegins by setting up the user interface,and in particular, it creates a customview that holdsthe compass arrow and then it adds thatview to the activities main view.It thengets a reference to the sensor manager.After that, it gets a reference to thedevice's accelerometer andit gets a reference to the device'smagnetometer by calling sensormanager.getdefaultsensor and by passing inthe appropriate type constants.In the onResume method, the code registersthis class a listener for accelerometerevents and for magnetometer events, bycalling the registerListener method.The onPause method unregisters this classas a listener for all sensors.The onSensor change method processes theincoming sensor events.This method first checks whether the eventis an accelerometer ora magnatometer event and then copies theappropriate event data.Next, if there are readings from each ofthe 2 sensors, the code calledthe sensormanager.getrotationmatrix methodpassing in the sensor readings and anarray in which to store the rotationmatrix.If that method was successful, then thecode called the get sensor manager.get orientation method passing in therotation matrix that we just acquiredfrom the call to get rotation matrix.It also passes inanother array called orientation matrix.When this method returns, orientationmatrixwill hold the information the applicationneedsto determine how the device is orientedwith respect to the earth's magneticnorth.The code then grabs this value from theorientation matrix and since this value ismeasuredin radians, the code then converts theradian valueto degrees.After that, the code invalidates the

Page 5: 7 - 2 - Sensors - Part 2 (11-40)

compass arrow view and then it clears thearrays that holds the sensor readings.Let's look at the compass arrow view tosee how it uses the new orientationinformation.Scrolling down to the on draw method, thecode first saves the current canvasand then it rotates this view on thecanvas by an amountequal to minus 1 times m rotationin degrees.So basically, the idea here is that if thedevice is pointing say 90 degrees awayfrom north, then the compass arrowmust rotate back 90 degrees in order forthe compass arrow to keep pointing north.That's all for this lesson on sensors.See you next time when we'll talk aboutlocation and maps.Thank you.