Websites Navigation: Airbit | Shop | m-shell.net
Languages: EN | DE

Module compass: Magnetometer Measurements

  
Compatibility of module compass
Selected Nokia S60 5th Edition phones devices with builtin magnetometer.ok

This module returns the measurements of the builtin magnetic field sensor. Please note that only a few devices have a such a sensor.

Each measurement corresponds to a vector in three dimensional space indicating the direction of the magnetic field, and a value indicating the device heading:

KeyMeaningType
xx-component of the magnetic field vectorNumber
yx-component of the magnetic field vectorNumber
zx-component of the magnetic field vectorNumber
northNumber of degrees between device top and magnetic north (0 to 359).Number

compass.get

• function get() → Array

Gets the current magnetometer measurement.

Throws ErrNotSupported if the device has no magnetic field sensor.

// Check whether the device heads north or south
function headsNorth()
  c=compass.get();
  return c["north"]<90 or c["north"]>270
end
print headsNorth()
→ true

compass.new

• function new(xyzdelta=20, northdelta=5, timeout=-1) → Array|null

Waits until any component of the magnetic field vector changes by at least xyzdelta, or the magnetic north direction changes by at least northdelta, then returns the current measurement. If timeout>=0 and timeout milliseconds have passed without any change, null is returned.

Throws ErrNotSupported if the device has no magnetic field sensor.

The following example draws a blue line in magnetic north direction:

// compute the center and radius
s=graph.size();
cx=s[0]/2; cy=s[1]/2; r=cx;
if cy<r then r=cy end;
// initially there is no line
dx=0; dy=0;
do
  graph.show();
  c=compass.new();
  // erase the old line
  graph.pen(graph.bg());
  graph.line(cx, cy, cx+dx, cy+dy);
  // compute the new line
  alpha=c["north"]*math.pi/180;
  dx=-math.sin(alpha)*r;
  dy=-math.cos(alpha)*r;
  // draw the new line
  graph.pen(graph.blue);
  graph.line(cx, cy, cx+dx, cy+dy)
  // repeat forever
until false


© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-LIB-888
mShell Home  > Documentation  > Manuals