public final class OrientationFilter

  1. Object
  2. OrientationFilter

Deterministic complementary sensor-fusion filter that turns raw gyroscope, accelerometer and magnetometer readings into a device orientation quaternion. Pure math with no platform dependencies, so the same inputs always produce the same orientation on every platform.

The gyroscope drives the orientation between samples; the accelerometer slowly corrects accumulated tilt drift toward gravity and the magnetometer (when available) corrects yaw drift toward magnetic north. The blend is controlled by #setGyroWeight(float).

Conventions: the device frame has X right, Y up and Z toward the user (the com.codename1.sensors convention); the world frame is Y up with -Z forward. The output quaternion rotates device-frame vectors into the world frame.

Constructors

public OrientationFilter()

Methods

public void setGyroWeight(float w)Sets the complementary blend coefficient in (0, 1]: the fraction of each update that trusts the integrated gyroscope over the accelerometer/magnetometer reference. Higher values are smoother but drift-correct more slowly. Default 0.98.
public float getGyroWeight()The complementary blend coefficient.
public void update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz, float dtSeconds)Advances the filter by one sensor sample.
public void getOrientation(float[] quatOut4)Copies the current orientation quaternion into quatOut4 as {x, y, z, w}.
public float[] getOrientation()The current orientation quaternion as a newly allocated array.
public void reset()Resets the filter to the identity orientation.
public void recenterYaw()Rotates the orientation around world up so the current forward direction becomes the new “straight ahead”, keeping pitch and roll.

Inherited methods

Constructor details

OrientationFilter

public OrientationFilter()

Method details

setGyroWeight

public void setGyroWeight(float w)
Sets the complementary blend coefficient in (0, 1]: the fraction of each update that trusts the integrated gyroscope over the accelerometer/magnetometer reference. Higher values are smoother but drift-correct more slowly. Default 0.98.

getGyroWeight

public float getGyroWeight()
The complementary blend coefficient.

update

public void update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz, float dtSeconds)
Advances the filter by one sensor sample.

Parameters

gx float
, gy, gz: gyroscope rotation rates around the device axes in radians per second; pass zeros when no gyroscope exists
gy float
Not documented.
gz float
Not documented.
ax float
, ay, az: accelerometer reading including gravity in meters per second squared; pass zeros (or NaN) to skip tilt correction
ay float
Not documented.
az float
Not documented.
mx float
, my, mz: magnetometer reading in microtesla; pass NaN to skip yaw correction
my float
Not documented.
mz float
Not documented.
dtSeconds float
the time since the previous update

getOrientation

public void getOrientation(float[] quatOut4)
Copies the current orientation quaternion into quatOut4 as {x, y, z, w}.

getOrientation

public float[] getOrientation()
The current orientation quaternion as a newly allocated array.

reset

public void reset()
Resets the filter to the identity orientation.

recenterYaw

public void recenterYaw()
Rotates the orientation around world up so the current forward direction becomes the new “straight ahead”, keeping pitch and roll. Use to let the user recenter the view.