beetlebug▾
/

Module ztcl::motion

ztcl::motion gives the readings of the motion sensors of a phone: the accelerometer, the gyroscope, the rotation, and the heading of the compass.

set h [ztcl::motion watch accelerometer -command {apply {{d} {
    puts "x [dict get $d x]  y [dict get $d y]  z [dict get $d z] m/s²"
    ztcl::motion cancel $::h
    set ::done 1
}}}]
vwait ::done
x 0.0  y 0.0  z 9.81 m/s²

This example runs with ztcl --allow sensors,event --fake-device: the fake host of the ztcl program gives a phone that lies face up on a table. On a phone, the same script gets the sensor of the phone.

How a reading arrives

ztcl::motion watch KIND -command SCRIPT starts the sensor and gives a handle such as motion1 at once, with no reading. The sensor of the phone then sends a reading about each -interval milliseconds, from a thread of the OS, to the event loop of the interpreter. For each reading the event loop runs SCRIPT with one more argument, the reading as a dict such as x 0.0 y 0.0 z 9.81 time 1000, so SCRIPT is a command prefix that takes one argument, such as apply {{d} {...}} or the name of a proc.

So no reading arrives while the event loop does not run: a script needs vwait, update, or the event loop of Tk. A watch runs until ztcl::motion cancel HANDLE, or until the interpreter ends. ztcl::motion has no one-time read: to read the sensor one time, cancel the watch in its first script, as the example does.

The readings

The axes of a phone in portrait: x to the right, y up, z out of the screen

The axes are those of the phone in portrait, seen from its screen: x points to the right, y to the top, and z out of the screen, toward the person. They stay with the phone when it turns: a turn of the screen to landscape does not change them. iOS and Android give the same units and signs.

Reading Unit Meaning
accelerometer x, y, z m/s² The acceleration of the phone with gravity. A phone that lies face up and still reads x 0 y 0 z 9.81; a phone that stands upright in portrait reads +9.81 on y.
gyroscope x, y, z rad/s The speed of the turn about each axis. A turn counterclockwise, seen from the end of the axis, is positive.
rotation x, y, z, w none The attitude of the phone as a unit quaternion in the frame east, north, up of the Earth, as the rotation vector of Android gives it.
heading magnetic degrees The direction of the top of the phone (y) from magnetic north, clockwise, 0 to 360.
heading accuracy degrees How far magnetic can be off, or -1 when the device gives no figure.
heading true degrees The direction from true north. The key exists only while the device knows its position.
time (each sensor) ms The time of the reading, in milliseconds since the start of the phone. Use it for the time between two readings; it is no time of the clock.

When there is no reading

Case What the script gets
The capability sensors is off The command does not exist: invalid command name "ztcl::motion", with the errorCode TCL LOOKUP COMMAND ztcl::motion.
The device has no such sensor, or the host has no service (the ztcl program on a desktop) has gives 0, and watch gives the error the gyroscope is not available on this platform (with the name of the sensor), with the errorCode ZTCL MOTION UNAVAILABLE.
The permission of the user The motion sensors need none, on iOS and on Android.

Test with has first: a script that works with no sensor stays usable on each device.

The rate and the battery

-interval MS asks for a reading about each MS milliseconds, from 10 to 60,000, and 100 by default. It is a wish: the OS can give the readings later, and a fast rate is a cost. Android 12 and later give an app at most 200 readings a second, and iOS about 100. A sensor uses the battery while it runs, so cancel a watch when the app does not need it, for example when its window is hidden.

Platforms

Platform accelerometer, gyroscope, rotation heading
iOS (ztk) CoreMotion CoreLocation
Android (ztk) the sensors of the NDK the rotation vector, with no true
macOS (ztk) none: has gives 0 CoreLocation, on a Mac with a compass
The ztcl program, Linux, Windows none none
The web none none

The ztcl program has no sensor; --fake-device gives it the fake host of the examples (src/device_fake.zig).

Commands

Command Description
ztcl::motion has KIND 1 when the device has the sensor KIND: accelerometer, gyroscope, rotation, or heading.
ztcl::motion watch KIND ?-interval MS? -command SCRIPT Gives a handle such as motion1, and runs SCRIPT with a reading about each MS milliseconds.
ztcl::motion cancel HANDLE Stops a watch. A handle that ended is no error.

The command is not a package: it exists when the embedder turns on the capability sensors, and it is absent otherwise. A child interp has it only when its parent has it. While a watch is active, the event loop wakes at least every 20 milliseconds, since the host gives the readings from its own thread.

Try it

Ask which sensors the device has, then read the heading three times:

foreach k {accelerometer gyroscope rotation heading} {
    puts "$k [ztcl::motion has $k]"
}
set n 0
set h [ztcl::motion watch heading -interval 100 -command {apply {{d} {
    puts "heading [dict get $d magnetic]° at [dict get $d time] ms"
    if {[incr ::n] == 3} { set ::done 1 }
}}}]
vwait ::done
ztcl::motion cancel $h
accelerometer 1
gyroscope 1
rotation 1
heading 1
heading 0.0° at 1000 ms
heading 30.0° at 1100 ms
heading 60.0° at 1200 ms

A WebAssembly module gets the same readings through the import family motion (the device APIs for WebAssembly).

The host

An embedder fills motion_has and motion of Interp.device_host, the table of ztcl::device. motion gets a Sink, a handle, the sensor, and the interval, and the host posts each reading with Sink.post, from any thread, converted to the units above. stop ends the watch.