Class 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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfloatThe complementary blend coefficient.float[]The current orientation quaternion as a newly allocated array.voidgetOrientation(float[] quatOut4) Copies the current orientation quaternion intoquatOut4as{x, y, z, w}.voidRotates the orientation around world up so the current forward direction becomes the new "straight ahead", keeping pitch and roll.voidreset()Resets the filter to the identity orientation.voidsetGyroWeight(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.voidupdate(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.
-
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. Default0.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,gy,gz: gyroscope rotation rates around the device axes in radians per second; pass zeros when no gyroscope exists -
ax,ay,az: accelerometer reading including gravity in meters per second squared; pass zeros (or NaN) to skip tilt correction -
mx,my,mz: magnetometer reading in microtesla; pass NaN to skip yaw correction -
dtSeconds: the time since the previous update
-
-
getOrientation
public void getOrientation(float[] quatOut4) Copies the current orientation quaternion intoquatOut4as{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.
-