+ mpuevent = new EventSource("http://localhost:314");
+ var x = [], y = [], z = [], cnt = -1, inertia = 6;
+
+ mpuevent.addEventListener("bearing", function(e) {
+ bearing = e.data.split(" ");
+ yaw = -parseFloat(bearing[0]);
+ }, false);
+ mpuevent.addEventListener("motion", function(e) {
+ motion = e.data.split(" ");
+
+ cnt = (cnt + 1) % inertia;
+ x[cnt] = parseFloat(motion[0]);
+ y[cnt] = parseFloat(motion[1]);
+ z[cnt] = parseFloat(motion[2]);
+
+ // tx = 0; x.forEach( function(n, i){ tx += n; } ); tx /= inertia;
+ ty = 0; y.forEach( function(n, i){ ty += n; } ); ty /= inertia;
+ tz = 0; z.forEach( function(n, i){ tz += n; } ); tz /= inertia;
+
+ pitch = Math.asin((tz / 9.81 > 1)?1:(tz/9.81)) / Math.PI * 180 + 22.5;
+ roll = - Math.asin((ty / 9.81 > 1)?1:(ty/9.81)) / Math.PI * 180;
+ // yaw = (yaw + ty) % 360;
+ }, false );
+