IMU

1 minute read

I was looking for an easy way to access an IMU for some quick tests. I settled on a smartphone sensor because all the sensor fusion / calibration software is built in. Furthermore, the device motion and device orientation events available in the Web API make it easy to grab the data, send it to a computer through websockets and then pipe it into any application.

Device Orientation:

window.addEventListener("deviceorientation", function(event) {
    console.log(
        event.absolute, // Boolean whether or not the data is "absolute"
                        // Absolute data means that if your phone is
                        // flat on a surface, pointing north, all three
                        // readings should be 0.
        event.alpha,    // Yaw
        event.beta,     // Pitch
        event.gamma     // Roll
    );
});

These numbers are all in degrees. A sample is available on this site or on codepen. It may take a bit of time for the orientation to calibrate itself.

Additionally, there is also the device motion event, which can get the accelerometer and gyroscope data.

Device Motion:

window.addEventListener("devicemotion", function(event) {
    console.log(
        event.acceleration, // object containing {x, y, z} fields
        event.accelerationIncludingGravity,
        event.rotationRate, // object containing {alpha, beta, gamma} fields
        event.interval      // dt
    );
});

Rotation rate might not be useful for orientation given the orientation data already available, but acceleration can filtered and then used for positioning.