c# - Getting lean angle from gyroscope and accelerometer wp8 -


i want calculate lean angle when ride motorcycle @ track , think using lumia 920 that. product got interest http://leanometer.com . found out using gyroscope bad drift on time using complementary filter accelerometer seemed way go. found code doing http://www.pieter-jan.com/node/11:

#define accelerometer_sensitivity 8192.0 #define gyroscope_sensitivity 65.536  #define m_pi 3.14159265359        #define dt 0.01                         // 10 ms sample rate!      void complementaryfilter(short accdata[3], short gyrdata[3], float *pitch, float *roll) {     float pitchacc, rollacc;                     // integrate gyroscope data -> int(angularspeed) = angle     *pitch += ((float)gyrdata[0] / gyroscope_sensitivity) * dt; // angle around x-axis     *roll -= ((float)gyrdata[1] / gyroscope_sensitivity) * dt;    // angle around y-axis      // compensate drift accelerometer data if !bullshit     // sensitivity = -2 2 g @ 16bit -> 2g = 32768 && 0.5g = 8192     int forcemagnitudeapprox = abs(accdata[0]) + abs(accdata[1]) + abs(accdata[2]);     if (forcemagnitudeapprox > 8192 && forcemagnitudeapprox < 32768)     {     // turning around x axis results in vector on y-axis         pitchacc = atan2f((float)accdata[1], (float)accdata[2]) * 180 / m_pi;         *pitch = *pitch * 0.98 + pitchacc * 0.02;      // turning around y axis results in vector on x-axis         rollacc = atan2f((float)accdata[0], (float)accdata[2]) * 180 / m_pi;         *roll = *roll * 0.98 + rollacc * 0.02;     } }  

i have converted code c# have problems accurate this. 1 thing not know gyro/accelerometer sensitivity or how it. after while started google more on acclerometer , angle , found this: http://www.hobbytronics.co.uk/accelerometer-info bit different angle above accelerometer seemed work. when using algorithm hobbytronics , putting code above got strange behavior tried near -1.4 degrees angle.

i got real code on computer how it:

var lastreading = new datetime(); var anglex = 0.0;  var gyro = new gyroscope(); gyro.timebetweenupdates = 20ms; gyro.currentvaluechanged += valuechanged; var acc = new accelerometer(); acc.timebetweenupdates = 20ms; gyro.start(); acc.start();  void valuechanged(sensorreading reading) {     if(lastreading < 1 sec old)     {         var dt = lastreading - reading.timestamp;         if(reading.x > 0.05 || reading.x < -0.05) // got errors if did not use this, maybe high?         {            var x = reading.x * dt.totalseconds;            var accdata = acc.currentvalue();             var accx = atan(accdata.x / math.sqrt(accdata.y*accdata.y+accdata.z*accdata.z));            anglex = 0.98*(anglex + x *180 / math.pi) + 0.02 * (accx *180 /math.pi);            txtanglex.text = "x:" + anglex.tostring("f");         }     }     lastreading = reading.timestamp;  } 

what missing? 1 way make better set angle accelerometer only, when lastreading older 1 sec know that not problem


Comments