Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Sensor-based Apps Windows Phone Windows Store Windows Desktop

Similar presentations


Presentation on theme: "Building Sensor-based Apps Windows Phone Windows Store Windows Desktop"— Presentation transcript:

1 Building Sensor-based Apps Windows Phone Windows Store Windows Desktop
Tim Binkley-Jones manning.com/binkley

2 Sky Map for Windows Phone
SkyMap is an amazing planetarium for your windows phone. It enables you to point your phone at the sky and see what stars, constellations, planets or deep space objects are out there in real time.

3 Where is my server? Where is the conference room? HERE City Lens Simply hold up your phone as though taking a photo, and HERE City Lens overlays the best shops, restaurants and points of interest right on your display.

4 Windows 8 Windows RT Windows Runtime Windows Store Windows Desktop
Windows What??? Windows RT Windows 8 and Windows RT are two different operating systems that both use the Windows 8 kernel. Windows Runtime (a.k.a WinRT) is the set of common APIs used to interact with the Windows 8 kernel. The Windows 8 kernel and the Windows Runtime APIs have been built to run on both Intel x86 based PCs and ARM based devices. Windows Store applications use the Windows Runtime and only the Windows Runtime. Windows Store applications run on both Windows 8 and Windows RT. Windows Desktop applications are the same Windows applications that you are familiar with – just a new name. Only run on Windows 8. Desktop applications can also use the Windows Runtime APIs.

5 Windows Phone 8 an upgrade of Windows Phone 7
both and a slimmed down version of Windows 8 is an upgrade of Windows Phone 7 What about Windows Phone 8? In the Windows Phone 8 SDK there are two separate Sensor APIs. The APIs found in Microsoft.Devices.Sensors were originally part of the Windows Phone 7 SDK and have been brought forward to Windows Phone 8. The second Sensor API, found in the Windows.Devices.Sensors comes from the Windows 8 Runtime that the Windows Phone shares with the Windows 8 operating system.

6 Image taken from the MSDN article “Windows Phone API reference”
The API surface area of Windows Runtime is very large, with over 11,000 members. This is represented by area 2 in the above diagram and consists of approximately 2,800 members. These are represented by area 3 in the diagram and total about 600 members. Image taken from the MSDN article “Windows Phone API reference”

7 Taken from the MSDN documentation for the Windows Runtime Accelerometer class

8 Taken from the MSDN documentation for the Windows Runtime Geolocator class

9 Taken from the MSDN documentation for the Windows Runtime LightSensor class

10 Available Sensors Camera, Microphone Geolocator LightSensor
Accelerometer, Gyrometer Compass, Inclinometer, OrientationSensor Camera and microphone do not have shared APIs. Geolocator is service over the GPS and location database. It will use cell towers, ip addresses, and GPS. Compass, Inclinometer, and OrientationSensor are also services over sensors.

11 Each of the sensors reports data relative to the x, y, z coordinate system defined by the Windows Phone device. The device’s coordinate system is fixed to the device, and moves as the phone moves. The x axis extends out the sides of the device, with positive x pointing to the right side of the device, and negative x pointing to the left side of the device. The y axis runs through the top and bottom of the device, with positive y pointing toward the top. The z axis runs from back to front, with positive z pointing out the front of the device. The coordinate system used by the sensors doesn’t necessarily match the coordinate system used by other APIs. One example is the coordinate system used by XAML. In portrait mode XAML, the y axis points in the opposite direction, with positive y pointing out the bottom of the device.

12

13

14 <PropertyGroup>
<TargetPlatformVersion>8.0</TargetPlatformVersion> </PropertyGroup> What about Windows Desktop? Using the Windows Runtime API’s in a Windows Desktop application Manually edit project file, add reference to core, add reference to System.Runtime façade

15 For more details, see the MSDN article titled
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\ .NETFramework\v4.5\Facades\System.Runtime.dll For more details, see the MSDN article titled “Managed desktop apps and Windows Runtime”

16 A static factory method that returns a sensor instance.
Member Type Description GetDefault Method A static factory method that returns a sensor instance. GetCurrentReading Returns a read-only object containing the currently available sensor data. ReadingChanged Event An event raised when ever the current reading changes. ReportInterval Property Specifies how often the sensor reads new data. The data returned by the GetCurrentReading will only change once every time interval. MinimumReportInterval A read-only value specifying the smallest value that can be passed set in the ReportInterval property. The Accelerometer, Compass, Gyrometer, Inclinometer and OrientationSensor do not share a common base class, the all have a number of identical properties, methods, and events

17 The acceleration along the each axis in g’s.
Accelerometer The acceleration along the each axis in g’s. public sealed class AccelerometerReading { public double AccelerationX { get; } public double AccelerationY { get; } public double AccelerationZ { get; } public DateTimeOffset Timestamp { get; } }

18 The accelerometer measures the acceleration component of the forces being applied to a device. Note that acceleration due to gravity isn’t reported by the sensor. Unless the device in is free fall, forces are always being applied to a device. The Accelerometer reports numbers in terms of the constant g, which is defined as the acceleration due to Earth’s gravity at sea level. The value of g is -9.8 m/s2. When a device is at rest lying on a table, the table is exerting a force on the device that offsets the pull of gravity. The Accelerometer measures the acceleration of the force the table applies. When the device is falling, the accelerometer reports zero acceleration

19 When you move a device, you apply a force along the direction you want the device to move. The force causes the device to accelerate, acceleration changes the velocity of the device, and it starts to move. Let’s say your phone is resting flat face up on the surface of a table at point A. Now you give your device a modest push so that it slides along the surface to point B. The initial push is a moderate force in the positive X axis. After you release the device, allowing it to slide, your initial force stops, and the force due to friction begins to slow down the device until it stops moving. Figure 10.6 shows the values reported by the accelerometer in this scenario. The numbers are somewhat contrived, as real numbers will vary based on how hard the initial push is, and the amount of friction between the phone and the surface it’s resting upon.

20 The angular velocity, in degrees per second, about each axis.
Gyrometer The angular velocity, in degrees per second, about each axis. public sealed class GyrometerReading { public double AngularVelocityX { get; } public double AngularVelocityY { get; } public double AngularVelocityZ { get; } public DateTimeOffset Timestamp { get; } } The Gyrometer sensor reports how quickly the device is turning on one or more of its axes. The rotational velocity is reported in degrees per second, and when a device is rotated in a complete circle it’ll rotate 360 degrees. The values are reported with counterclockwise being the positive direction. The Gyrometer only reports turning motion around an axis and if the device is held still, the sensor will report values of zero. If the device is moved from point A to point B without any twisting motion, the Gyrometer will also report zero. The Gyrometer reports values with the GyrometerReading struct. Rotational velocities are read from the GyrometerReading through the AngularVeclocityX, AngularVelocityY, and AngularVelocityZ properties that break absolute movement into rotation about the x, y, and z axes.

21 Compass Heading - the angle between the magnetic field, and the direction the device is pointed public sealed class CompassReading { public double HeadingMagneticNorth { get; } public double? HeadingTrueNorth { get; } public DateTimeOffset Timestamp { get; } } The Compass is useful when an application needs to know which direction a device is pointed relative to the real world. The Compass reports direction relative to the Magnetic North Pole. This information is useful for applications such as the astronomy application we discussed earlier, where the application updates the display based on the direction the user is facing. The compass is also useful in motion-sensitive applications that need to know when a device is rotated.

22 The Compass senses the strength and direction of the Earth’s magnetic field. If the compass detects that a device is aligned with the Earth’s magnetic field, then it knows that the device pointed, or headed, north. If a device is not aligned with the magnetic field, then the compass measures the device’s heading - the angle between the magnetic field, and the direction the device is pointed,

23 However the Earth’s Magnetic North Pole is not in the same location as the Earth’s actual North Pole. Not only are the two poles not in the same location, the Magnetic North Pole is constantly moving about. Each year the Magnetic North Pole shifts approximately 25 miles. As you might have guessed, the HeadingMagneticNorth property measures the device’s heading relative to the Magnetic North Pole. The HeadingTrueNorth property measures the device’s direction relative to the actual North Pole. The difference between the two heading measurements is called magnetic declination.

24 Inclinometer and Orientation Sensor
Rotation around each access public sealed class InclinometerReading { public float PitchDegrees { get; } public float RollDegrees { get; } public float YawDegrees { get; } public DateTimeOffset Timestamp { get; } } Rotation matrix and quaternion Unlike the other sensors we’ve covered so far in this chapter, the Inclinometer and OrientationSensor are not hardware-based sensors. These two classes are wrappers around the Accelerometer, Compass, and Gyrometer. Instead of sensing data from hardware, the Inclinometer and OrientationSensor consume data from the other sensors and perform some convenient number crunching. The Inclinometer class reports the results of its data analysis in the InclinometerReading class. The InclinometerReading class reports Yaw, Pitch, and Roll values. The OrientationSensor class delivers data via the OrientationSensorReading class, which provides both a rotation matrix and a quaternion that can be used for coordinate mapping public sealed class OrientationSensorReading { public SensorQuaternion Quaternion { get; } public SensorRotationMatrix RotationMatrix { get; } public DateTimeOffset Timestamp { get; } }

25 Real world point: (0, 10, 0) Relative point: (0, 10, 0)
To understand the uses of the Inclinometer and the OrientationSensor you need to understand the frame of reference, or coordinate system for both the real world and the device. These two classes assume a real-world coordinate system where y points due north, z points straight up, and x points due east. When the device is lying flat, face up, with the top of the device pointing north, the device’s frame of reference matches the real world frame of reference. An object at point (0, 10, 0) in the world frame will have the same coordinates in the device frame Real world point: (0, 10, 0) Relative point: (0, 10, 0)

26 Real world point: (0, 10, 0) Relative point: (-10, 0, 0)
When the device is rotated, its frame of reference no longer matches the real world frame of reference. If the top of the device lying flat is rotated to point east, the device is considered to be rotated 270 degrees. The inclinometer reading will have a Yaw reading of 270 degrees. The Yaw, rotation about the z axis, is read as the counterclockwise angle between the two y axes. Now the device’s y axis is pointing east and the x axis is pointing south. Again, consider an object at the coordinate (0, 10, 0) in the world frame. This same object will have the coordinates (-10, 0, 0) in the device frame. Real world point: (0, 10, 0) Relative point: (-10, 0, 0)

27 Real world point: (0, 10, 0) Relative point: (-10, 0, 0)
With the top of the device still pointed east, raise the top of the device until it’s in the standing portrait orientation with the back of the device facing east. This is shown in figure In this case you’ve rotated the device frame about the x axis and changed the Pitch of the device. The inclinometer reading will still have a Yaw reading of 270 degrees, but will now also have a Pitch reading of 90 degrees. The Pitch, or rotation about the x axis, is read as a counterclockwise angle. Now the device’s y axis is pointing up toward the sky, aligned with the world frame’s z axis. The device’s z axis is pointing to the west. The device’s x axis is still pointing south. Again, consider an object at the coordinate (0, 10, 0) in the world frame. This same object will still have the coordinates (-10, 0, 0) in the device frame because changing the pitch didn’t change the direction of the device’s x axis. When working with the InclinometerReading, you must remember that the Yaw, Pitch, and Roll values are order-dependent. To translate a point in one frame of reference to a point in another frame of reference, you must apply Yaw first, followed by Pitch, then by Roll. Real world point: (0, 10, 0) Relative point: (-10, 0, 0)

28 Building Sensor-based Apps Windows Phone Windows Store Windows Desktop
Tim Binkley-Jones manning.com/binkley


Download ppt "Building Sensor-based Apps Windows Phone Windows Store Windows Desktop"

Similar presentations


Ads by Google