@@ -33,6 +33,7 @@ const motors = {
3333 sensorGyroScale : 2000 ,
3434 sensorAccelRate : 20 ,
3535 sensorAccelScale : 2 ,
36+ amperageHistory : [ ] , // Store amperage readings with timestamps for average calculation
3637 sensorSelectValues : {
3738 gyroScale : {
3839 1 : 1 ,
@@ -77,6 +78,9 @@ motors.initialize = async function (callback) {
7778 self . configHasChanged = false ;
7879 self . configChanges = { } ;
7980
81+ // Reset amperage history when initializing
82+ self . amperageHistory = [ ] ;
83+
8084 // Update filtering defaults based on API version
8185 const FILTER_DEFAULT = FC . getFilterDefaults ( ) ;
8286
@@ -463,6 +467,8 @@ motors.initialize = async function (callback) {
463467 const motorVoltage = $ ( ".motors-bat-voltage" ) ;
464468 const motorMahDrawingElement = $ ( ".motors-bat-mah-drawing" ) ;
465469 const motorMahDrawnElement = $ ( ".motors-bat-mah-drawn" ) ;
470+ const motorAmperageAverage5sElement = $ ( ".motors-bat-amperage-average-5s" ) ;
471+ const motorAmperageAverage10sElement = $ ( ".motors-bat-amperage-average-10s" ) ;
466472
467473 const rawDataTextElements = {
468474 x : [ ] ,
@@ -639,6 +645,55 @@ motors.initialize = async function (callback) {
639645 motorVoltage . text ( i18n . getMessage ( "motorsVoltageValue" , [ FC . ANALOG . voltage ] ) ) ;
640646 motorMahDrawingElement . text ( i18n . getMessage ( "motorsADrawingValue" , [ FC . ANALOG . amperage . toFixed ( 2 ) ] ) ) ;
641647 motorMahDrawnElement . text ( i18n . getMessage ( "motorsmAhDrawnValue" , [ FC . ANALOG . mAhdrawn ] ) ) ;
648+
649+ // Calculate amperage averages using timestamp-based approach
650+ const currentAmperage = FC . ANALOG . amperage ;
651+ const currentTimestamp = performance . now ( ) ;
652+
653+ // Add current reading with timestamp
654+ TABS . motors . amperageHistory . push ( {
655+ value : currentAmperage ,
656+ timestamp : currentTimestamp ,
657+ } ) ;
658+
659+ // Calculate time thresholds
660+ const tenSecondsAgo = currentTimestamp - 10000 ;
661+ const fiveSecondsAgo = currentTimestamp - 5000 ;
662+
663+ // Remove readings older than 10 seconds and calculate averages in one pass
664+ const validReadings = [ ] ;
665+ let amperageSum5s = 0 ;
666+ let amperageSum10s = 0 ;
667+ let count5s = 0 ;
668+ let count10s = 0 ;
669+
670+ for ( const reading of TABS . motors . amperageHistory ) {
671+ if ( reading . timestamp >= tenSecondsAgo ) {
672+ validReadings . push ( reading ) ;
673+ amperageSum10s += reading . value ;
674+ count10s ++ ;
675+
676+ if ( reading . timestamp >= fiveSecondsAgo ) {
677+ amperageSum5s += reading . value ;
678+ count5s ++ ;
679+ }
680+ }
681+ }
682+
683+ // Update history with only valid readings
684+ TABS . motors . amperageHistory = validReadings ;
685+
686+ // Calculate averages
687+ const amperageAverage5s = count5s > 0 ? amperageSum5s / count5s : 0 ;
688+ const amperageAverage10s = count10s > 0 ? amperageSum10s / count10s : 0 ;
689+
690+ // Display average amperages
691+ motorAmperageAverage5sElement . text (
692+ i18n . getMessage ( "motorsAmperageAverage5sValue" , [ amperageAverage5s . toFixed ( 2 ) ] ) ,
693+ ) ;
694+ motorAmperageAverage10sElement . text (
695+ i18n . getMessage ( "motorsAmperageAverage10sValue" , [ amperageAverage10s . toFixed ( 2 ) ] ) ,
696+ ) ;
642697 }
643698 }
644699
0 commit comments