title: Measuring speed and distance from Accelerometer datas
body: <p>I am using a LSM9DS0 sensor with 9DOF (Accelerometer, Gyro, Magnetometer)</p>
  
  <p>After receiving the acceleration from X, Y, Z axes (in G) , i do the following steps</p>
  
  <p>Let us say the Accelerations are AccelX_Avg, AccelY_Avg, AccelZ_Avg in the X, Y, Z directions</p>
  
  <pre><code>Accel_Avg = (sqrt(AccelX_Avg*AccelX_Avg + AccelY_Avg*AccelY_Avg + AccelZ_Avg*AccelZ_Avg)) * 9.88888;
  </code></pre>
  
  <p>Then i calculate the speed and distance</p>
  
  <pre><code>if (i_offset_distance == 0) { // calculate the speed(V1) and distance(D1) for first instant 
        i_offset_distance = i_offset_distance + 1 ;
        Serial.println("\nFirst is done \n");
        Measured_Velocity_1 = (Accel_Avg* dt_ODR_A);
        Distance_covered_1 = (Measured_Velocity*dt_ODR_A) + (0.5*Accel_Avg*dt_ODR_A*dt_ODR_A);
        }
  
        else{
  
        Measured_Velocity = Measured_Velocity_1 + (Accel_Avg* dt_ODR_A); // V2 = V1 + Accelration * Dt 
        Measured_Velocity_1 = Measured_Velocity;
        Distance_covered = (Measured_Velocity*dt_ODR_A) + (0.5*Accel_Avg*dt_ODR_A*dt_ODR_A) + Distance_covered_1;
        Distance_covered_1 = Distance_covered;
        i_offset_distance = i_offset_distance + 1;
  
        } 
  </code></pre>
  
  <p>It gives me everytime incremntal values of velocity and distance travelled, how ever i am expecting, when the sensor would be at rest Acceleration (0,0,g), the velocity should be zero. Also if it is not moving, it should give distance_covered to be also 0. </p>
  
  <p>How can i get speed and total_distance covered correctly? </p>
  
  <p>Any form of advise through code implementation, concept, algorithm would be much appreciated. Thank you.  </p>