nl_tokens: --------------------------------------------------------------------------------------------------- IMU algorithm update
pl_tokens: <mask> mahonyAHRSupdateIMU ( gx , gy , gz , ax , ay , az ) { let recipNorm ; let halfvx , halfvy , halfvz ; let halfex , halfey , halfez ; // Compute feedback only if accelerometer measurement valid (afunctions NaN in accelerometer normalisation) if ( ax !== 0 && ay !== 0 && az !== 0 ) { // Normalise accelerometer measurement recipNorm = ( ax * ax + ay * ay + az * az ) ** - 0.5 ; ax *= recipNorm ; ay *= recipNorm ; az *= recipNorm ; // Estimated direction of gravity and vector perpendicular to magnetic flux halfvx = q1 * q3 - q0 * q2 ; halfvy = q0 * q1 + q2 * q3 ; halfvz = q0 * q0 - 0.5 + q3 * q3 ; // Error is sum of cross product between estimated and measured direction of gravity halfex = ay * halfvz - az * halfvy ; halfey = az * halfvx - ax * halfvz ; halfez = ax * halfvy - ay * halfvx ; // Compute and apply integral feedback if enabled if ( twoKi > 0.0 ) { integralFBx += twoKi * halfex * recipSampleFreq ; // integral error scaled by Ki integralFBy += twoKi * halfey * recipSampleFreq ; integralFBz += twoKi * halfez * recipSampleFreq ; gx += integralFBx ; // apply integral feedback gy += integralFBy ; gz += integralFBz ; } else { integralFBx = 0.0 ; // prevent integral windup integralFBy = 0.0 ; integralFBz = 0.0 ; } // Apply proportional feedback gx += twoKp * halfex ; gy += twoKp * halfey ; gz += twoKp * halfez ; } // Integrate rate of change of quaternion gx *= 0.5 * recipSampleFreq ; // pre-multiply common factors gy *= 0.5 * recipSampleFreq ; gz *= 0.5 * recipSampleFreq ; const qa = q0 ; const qb = q1 ; const qc = q2 ; q0 += - qb * gx - qc * gy - q3 * gz ; q1 += qa * gx + qc * gz - q3 * gy ; q2 += qa * gy - qb * gz + q3 * gx ; q3 += qa * gz + qb * gy - qc * gx ; // Normalise quaternion recipNorm = ( q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3 ) ** - 0.5 ; q0 *= recipNorm ; q1 *= recipNorm ; q2 *= recipNorm ; q3 *= recipNorm ; }