Motion
Sensors, steps and activity
Everything CoreMotion reports. Each sensor is read once per call: the reading starts, the first sample comes back, the sensor stops. A script cannot subscribe to a stream, because a script runs to its last line and is then serialised, with no event loop left to receive a second sample. Ask for a series by sampling in a loop. Units are CoreMotion's own: g for acceleration, radians per second for rotation, microtesla for the magnetic field, radians for attitude, kilopascals for pressure. Nothing here answers on the simulator, and history reaches back seven days. Check Motion.isAvailable before reading a sensor that the device may not have.
Properties
isAvailableobjectWhich sensors this device has: { accelerometer, gyroscope, magnetometer, attitude, barometer, steps, activity }.
Methods
Motion.accelerometer()AnyAcceleration on the three axes, in g, gravity included.
returns { x, y, z } in g.
Motion.gyroscope()AnyRotation rate on the three axes, in radians per second.
returns { x, y, z } in rad/s.
Motion.magnetometer()AnyThe magnetic field on the three axes, in microtesla. Raw, so it includes whatever the device itself is generating.
returns { x, y, z } in µT.
Motion.attitude()AnyThe fused reading: where the device is pointing, with gravity told apart from the acceleration you caused.
returns { pitch, roll, yaw } in radians, plus gravity and userAcceleration as { x, y, z } in g, rotationRate in rad/s, and heading in degrees from magnetic north when the compass has settled. heading is absent while it has not.
Motion.pressure()AnyThe barometer.
returns { kPa, relativeAltitude }. relativeAltitude is metres climbed since the reading began, so on a single sample it is near zero.
Motion.steps(from?, to?)AnySteps over a window. With no arguments, since midnight; Motion.steps(DateTime.today()) is the same call written out. Anything older than seven days is clamped to seven days ago, which is all CoreMotion keeps.
- from Any
- to Any
returns { steps, distance, floorsAscended, floorsDescended, averageActivePace, from, to }. distance is a Distance, averageActivePace is seconds per metre and is absent when the window holds no movement, from and to are epoch seconds.
Motion.activity(from?, to?)AnyWhat the device was doing over a window, as a list of spans. With no arguments, since midnight.
- from Any
- to Any
returns [{ kind, confidence, start, end }]. kind is stationary, walking, running, cycling or automotive; confidence is low, medium or high; start and end are epoch seconds. CoreMotion records a change rather than a span, so each entry runs until the next one begins and the last runs to the end of the window.
Motion.activityTotals(from?, to?)AnyHow long was spent doing what over a window. With no arguments, since midnight.
- from Any
- to Any
returns { stationary, walking, running, cycling, automotive }, each a Duration. A kind the window holds none of is absent.
Examples
How level the device is
var a = Motion.attitude();
var degrees = a.roll * 180 / Math.PI;