@@ -610,20 +610,13 @@ export function FlightLog(logData) {
610610 * sourceChunks and destChunks can be the same array.
611611 */
612612 function injectComputedFields ( sourceChunks , destChunks ) {
613- let gyroADC = [
614- fieldNameToIndex [ "gyroADC[0]" ] ,
615- fieldNameToIndex [ "gyroADC[1]" ] ,
616- fieldNameToIndex [ "gyroADC[2]" ] ,
617- ] ;
618- let accSmooth = [
619- fieldNameToIndex [ "accSmooth[0]" ] ,
620- fieldNameToIndex [ "accSmooth[1]" ] ,
621- fieldNameToIndex [ "accSmooth[2]" ] ,
622- ] ;
623- let magADC = [
624- fieldNameToIndex [ "magADC[0]" ] ,
625- fieldNameToIndex [ "magADC[1]" ] ,
626- fieldNameToIndex [ "magADC[2]" ] ,
613+ let gyroADC = [ fieldNameToIndex [ "gyroADC[0]" ] , fieldNameToIndex [ "gyroADC[1]" ] , fieldNameToIndex [ "gyroADC[2]" ] ] ;
614+ let accSmooth = [ fieldNameToIndex [ "accSmooth[0]" ] , fieldNameToIndex [ "accSmooth[1]" ] , fieldNameToIndex [ "accSmooth[2]" ] ] ;
615+ let magADC = [ fieldNameToIndex [ "magADC[0]" ] , fieldNameToIndex [ "magADC[1]" ] , fieldNameToIndex [ "magADC[2]" ] ] ;
616+ let imuQuaternion = [
617+ fieldNameToIndex [ "imuQuaternion[0]" ] ,
618+ fieldNameToIndex [ "imuQuaternion[1]" ] ,
619+ fieldNameToIndex [ "imuQuaternion[2]" ] ,
627620 ] ;
628621 let rcCommand = [
629622 fieldNameToIndex [ "rcCommand[0]" ] ,
@@ -668,15 +661,14 @@ export function FlightLog(logData) {
668661
669662 let sourceChunkIndex ;
670663 let destChunkIndex ;
671- let attitude ;
672664
673665 const sysConfig = that . getSysConfig ( ) ;
674666
675667 if ( destChunks . length === 0 ) {
676668 return ;
677669 }
678670
679- // Do we have mag fields? If not mark that data as absent
671+ // Do we have mag fields? If not mark that data as absent
680672 if ( ! magADC [ 0 ] ) {
681673 magADC = false ;
682674 }
@@ -689,6 +681,10 @@ export function FlightLog(logData) {
689681 accSmooth = false ;
690682 }
691683
684+ if ( ! imuQuaternion [ 0 ] ) {
685+ imuQuaternion = false ;
686+ }
687+
692688 if ( ! rcCommand [ 0 ] ) {
693689 rcCommand = false ;
694690 }
@@ -726,33 +722,49 @@ export function FlightLog(logData) {
726722
727723 if ( ! destChunk . hasAdditionalFields ) {
728724 destChunk . hasAdditionalFields = true ;
729-
730- let chunkIMU = new IMU ( sourceChunks [ sourceChunkIndex ] . initialIMU ) ;
725+ const chunkIMU = new IMU ( sourceChunk . initialIMU ) ;
731726
732727 for ( let i = 0 ; i < sourceChunk . frames . length ; i ++ ) {
733728 let srcFrame = sourceChunk . frames [ i ] ,
734729 destFrame = destChunk . frames [ i ] ,
735730 fieldIndex = destFrame . length - ADDITIONAL_COMPUTED_FIELD_COUNT ;
736731
737- if ( ! that . isFieldDisabled ( ) . GYRO ) {
738- //don't calculate attitude if no gyro data
739- attitude = chunkIMU . updateEstimatedAttitude (
740- [
741- srcFrame [ gyroADC [ 0 ] ] ,
742- srcFrame [ gyroADC [ 1 ] ] ,
743- srcFrame [ gyroADC [ 2 ] ] ,
744- ] ,
745- [
746- srcFrame [ accSmooth [ 0 ] ] ,
747- srcFrame [ accSmooth [ 1 ] ] ,
748- srcFrame [ accSmooth [ 2 ] ] ,
749- ] ,
750- srcFrame [ FlightLogParser . prototype . FLIGHT_LOG_FIELD_INDEX_TIME ] ,
751- sysConfig . acc_1G ,
752- sysConfig . gyroScale ,
753- magADC
754- ) ;
732+ if ( imuQuaternion ) {
733+ const scaleFromFixedInt16 = 0x7FFF ; // 0x7FFF = 2^15 - 1
734+ const q = {
735+ x : srcFrame [ imuQuaternion [ 0 ] ] / scaleFromFixedInt16 ,
736+ y : srcFrame [ imuQuaternion [ 1 ] ] / scaleFromFixedInt16 ,
737+ z : srcFrame [ imuQuaternion [ 2 ] ] / scaleFromFixedInt16 ,
738+ w : 1.0 ,
739+ } ;
740+ q . w = Math . sqrt ( 1.0 - ( q . x ** 2 + q . y ** 2 + q . z ** 2 ) ) ;
741+ const xx = q . x ** 2 ,
742+ xy = q . x * q . y ,
743+ xz = q . x * q . z ,
744+ wx = q . w * q . x ,
745+ yy = q . y ** 2 ,
746+ yz = q . y * q . z ,
747+ wy = q . w * q . y ,
748+ zz = q . z ** 2 ,
749+ wz = q . w * q . z ;
750+ let roll = Math . atan2 ( ( + 2.0 * ( wx + yz ) ) , ( + 1.0 - 2.0 * ( xx + yy ) ) ) ;
751+ let pitch = ( ( 0.5 * Math . PI ) - Math . acos ( + 2.0 * ( wy - xz ) ) ) ;
752+ let heading = - Math . atan2 ( ( + 2.0 * ( wz + xy ) ) , ( + 1.0 - 2.0 * ( yy + zz ) ) ) ;
753+ if ( heading < 0 ) {
754+ heading += 2.0 * Math . PI ;
755+ }
755756
757+ destFrame [ fieldIndex ++ ] = roll ;
758+ destFrame [ fieldIndex ++ ] = pitch ;
759+ destFrame [ fieldIndex ++ ] = heading ;
760+ } else {
761+ const attitude = chunkIMU . updateEstimatedAttitude (
762+ [ srcFrame [ gyroADC [ 0 ] ] , srcFrame [ gyroADC [ 1 ] ] , srcFrame [ gyroADC [ 2 ] ] ] ,
763+ [ srcFrame [ accSmooth [ 0 ] ] , srcFrame [ accSmooth [ 1 ] ] , srcFrame [ accSmooth [ 2 ] ] ] ,
764+ srcFrame [ FlightLogParser . prototype . FLIGHT_LOG_FIELD_INDEX_TIME ] ,
765+ sysConfig . acc_1G ,
766+ sysConfig . gyroScale ,
767+ magADC ) ;
756768 destFrame [ fieldIndex ++ ] = attitude . roll ;
757769 destFrame [ fieldIndex ++ ] = attitude . pitch ;
758770 destFrame [ fieldIndex ++ ] = attitude . heading ;
0 commit comments