mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-19 14:25:13 +03:00
refactoring
This commit is contained in:
parent
a6ad0887b3
commit
38a1d5a40c
4 changed files with 80 additions and 78 deletions
|
@ -2,65 +2,6 @@
|
||||||
|
|
||||||
var helper = helper || {};
|
var helper = helper || {};
|
||||||
|
|
||||||
var SimpleSmoothFilterClass = function (initialValue, smoothingFactor) {
|
|
||||||
|
|
||||||
var publicScope = {};
|
|
||||||
|
|
||||||
publicScope.value = initialValue;
|
|
||||||
publicScope.smoothFactor = smoothingFactor;
|
|
||||||
|
|
||||||
if (publicScope.smoothFactor >= 1) {
|
|
||||||
publicScope.smoothFactor = 0.99;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (publicScope.smoothFactor <= 0) {
|
|
||||||
publicScope.smoothFactor = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
publicScope.apply = function (newValue) {
|
|
||||||
publicScope.value = (newValue * (1 - publicScope.smoothFactor)) + (publicScope.value * publicScope.smoothFactor);
|
|
||||||
|
|
||||||
return publicScope;
|
|
||||||
};
|
|
||||||
|
|
||||||
publicScope.get = function () {
|
|
||||||
return publicScope.value;
|
|
||||||
};
|
|
||||||
|
|
||||||
return publicScope;
|
|
||||||
};
|
|
||||||
|
|
||||||
//FIXME extract it to separate file
|
|
||||||
var WalkingAverageClass = function (maxLength) {
|
|
||||||
|
|
||||||
var table = [],
|
|
||||||
self = {};
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {number} data
|
|
||||||
*/
|
|
||||||
self.put = function (data) {
|
|
||||||
table.push(data);
|
|
||||||
if (table.length > maxLength) {
|
|
||||||
table.shift();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
self.getAverage = function () {
|
|
||||||
if (table.length > 0) {
|
|
||||||
var sum = table.reduce(function (a, b) {
|
|
||||||
return a + b;
|
|
||||||
});
|
|
||||||
return sum / table.length;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return self;
|
|
||||||
};
|
|
||||||
|
|
||||||
helper.mspQueue = (function (serial, MSP) {
|
helper.mspQueue = (function (serial, MSP) {
|
||||||
|
|
||||||
var publicScope = {},
|
var publicScope = {},
|
||||||
|
@ -69,12 +10,9 @@ helper.mspQueue = (function (serial, MSP) {
|
||||||
privateScope.handlerFrequency = 100;
|
privateScope.handlerFrequency = 100;
|
||||||
privateScope.balancerFrequency = 10;
|
privateScope.balancerFrequency = 10;
|
||||||
|
|
||||||
privateScope.loadAverage = new WalkingAverageClass(privateScope.handlerFrequency);
|
privateScope.loadFilter = new classes.SimpleSmoothFilter(0.5, 0.996);
|
||||||
privateScope.roundtripAverage = new WalkingAverageClass(50);
|
privateScope.roundtripFilter = new classes.SimpleSmoothFilter(20, 0.996);
|
||||||
privateScope.hardwareRoundtripAverage = new WalkingAverageClass(50);
|
privateScope.hardwareRoundtripFilter = new classes.SimpleSmoothFilter(5, 0.996);
|
||||||
|
|
||||||
privateScope.pastLoadFilter = new SimpleSmoothFilterClass(1, 0.99);
|
|
||||||
privateScope.currentLoadFilter = new SimpleSmoothFilterClass(1, 0.7);
|
|
||||||
|
|
||||||
privateScope.targetLoad = 1.5;
|
privateScope.targetLoad = 1.5;
|
||||||
privateScope.statusDropFactor = 0.75;
|
privateScope.statusDropFactor = 0.75;
|
||||||
|
@ -88,12 +26,12 @@ helper.mspQueue = (function (serial, MSP) {
|
||||||
D: 2
|
D: 2
|
||||||
},
|
},
|
||||||
Iterm: 0,
|
Iterm: 0,
|
||||||
ItermLimit: 80,
|
ItermLimit: 85,
|
||||||
previousError: 0,
|
previousError: 0,
|
||||||
output: {
|
output: {
|
||||||
min: 0,
|
min: 0,
|
||||||
max: 95,
|
max: 97,
|
||||||
minThreshold: 2
|
minThreshold: 0
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -103,7 +41,7 @@ helper.mspQueue = (function (serial, MSP) {
|
||||||
var error = privateScope.currentLoad - privateScope.targetLoad;
|
var error = privateScope.currentLoad - privateScope.targetLoad;
|
||||||
|
|
||||||
var Pterm = error * privateScope.loadPid.gains.P,
|
var Pterm = error * privateScope.loadPid.gains.P,
|
||||||
Dterm = (error - privateScope.loadPid.previousError) * privateScope.loadPid.gains.P;
|
Dterm = (error - privateScope.loadPid.previousError) * privateScope.loadPid.gains.D;
|
||||||
|
|
||||||
privateScope.loadPid.previousError = error;
|
privateScope.loadPid.previousError = error;
|
||||||
|
|
||||||
|
@ -144,9 +82,7 @@ helper.mspQueue = (function (serial, MSP) {
|
||||||
*/
|
*/
|
||||||
publicScope.executor = function () {
|
publicScope.executor = function () {
|
||||||
|
|
||||||
privateScope.loadAverage.put(privateScope.queue.length);
|
privateScope.loadFilter.apply(privateScope.queue.length);
|
||||||
privateScope.pastLoadFilter.apply(privateScope.currentLoad);
|
|
||||||
privateScope.currentLoadFilter.apply(privateScope.currentLoad);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* if port is blocked or there is no connection, do not process the queue
|
* if port is blocked or there is no connection, do not process the queue
|
||||||
|
@ -228,11 +164,11 @@ helper.mspQueue = (function (serial, MSP) {
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
publicScope.getLoad = function () {
|
publicScope.getLoad = function () {
|
||||||
return privateScope.currentLoad;
|
return privateScope.loadFilter.get();
|
||||||
};
|
};
|
||||||
|
|
||||||
publicScope.getRoundtrip = function () {
|
publicScope.getRoundtrip = function () {
|
||||||
return privateScope.roundtripAverage.getAverage();
|
return privateScope.roundtripFilter.get();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -240,11 +176,11 @@ helper.mspQueue = (function (serial, MSP) {
|
||||||
* @param {number} number
|
* @param {number} number
|
||||||
*/
|
*/
|
||||||
publicScope.putRoundtrip = function (number) {
|
publicScope.putRoundtrip = function (number) {
|
||||||
privateScope.roundtripAverage.put(number);
|
privateScope.roundtripFilter.apply(number);
|
||||||
};
|
};
|
||||||
|
|
||||||
publicScope.getHardwareRoundtrip = function () {
|
publicScope.getHardwareRoundtrip = function () {
|
||||||
return privateScope.hardwareRoundtripAverage.getAverage();
|
return privateScope.hardwareRoundtripFilter.get();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -252,11 +188,11 @@ helper.mspQueue = (function (serial, MSP) {
|
||||||
* @param {number} number
|
* @param {number} number
|
||||||
*/
|
*/
|
||||||
publicScope.putHardwareRoundtrip = function (number) {
|
publicScope.putHardwareRoundtrip = function (number) {
|
||||||
privateScope.hardwareRoundtripAverage.put(number);
|
privateScope.hardwareRoundtripFilter.apply(number);
|
||||||
};
|
};
|
||||||
|
|
||||||
publicScope.balancer = function () {
|
publicScope.balancer = function () {
|
||||||
privateScope.currentLoad = privateScope.loadAverage.getAverage();
|
privateScope.currentLoad = privateScope.loadFilter.get();
|
||||||
helper.mspQueue.computeDropRatio();
|
helper.mspQueue.computeDropRatio();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
31
js/simple_smooth_filter.js
Normal file
31
js/simple_smooth_filter.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var classes = classes || {};
|
||||||
|
|
||||||
|
classes.SimpleSmoothFilter = function (initialValue, smoothingFactor) {
|
||||||
|
|
||||||
|
var self = {};
|
||||||
|
|
||||||
|
self.value = initialValue;
|
||||||
|
self.smoothFactor = smoothingFactor;
|
||||||
|
|
||||||
|
if (self.smoothFactor >= 1) {
|
||||||
|
self.smoothFactor = 0.99;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self.smoothFactor <= 0) {
|
||||||
|
self.smoothFactor = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.apply = function (newValue) {
|
||||||
|
self.value = (newValue * (1 - self.smoothFactor)) + (self.value * self.smoothFactor);
|
||||||
|
|
||||||
|
return self;
|
||||||
|
};
|
||||||
|
|
||||||
|
self.get = function () {
|
||||||
|
return self.value;
|
||||||
|
};
|
||||||
|
|
||||||
|
return self;
|
||||||
|
};
|
33
js/walking_average_filter.js
Normal file
33
js/walking_average_filter.js
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var classes = classes || {};
|
||||||
|
|
||||||
|
classes.WalkingAverageFilter = function (maxLength) {
|
||||||
|
|
||||||
|
var table = [],
|
||||||
|
self = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {number} data
|
||||||
|
*/
|
||||||
|
self.put = function (data) {
|
||||||
|
table.push(data);
|
||||||
|
if (table.length > maxLength) {
|
||||||
|
table.shift();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.getAverage = function () {
|
||||||
|
if (table.length > 0) {
|
||||||
|
var sum = table.reduce(function (a, b) {
|
||||||
|
return a + b;
|
||||||
|
});
|
||||||
|
return sum / table.length;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return self;
|
||||||
|
};
|
|
@ -52,6 +52,8 @@
|
||||||
<script type="text/javascript" src="./js/injected_methods.js"></script>
|
<script type="text/javascript" src="./js/injected_methods.js"></script>
|
||||||
<script type="text/javascript" src="./js/intervals.js"></script>
|
<script type="text/javascript" src="./js/intervals.js"></script>
|
||||||
<script type="text/javascript" src="./js/timeouts.js"></script>
|
<script type="text/javascript" src="./js/timeouts.js"></script>
|
||||||
|
<script type="text/javascript" src="./js/simple_smooth_filter.js"></script>
|
||||||
|
<script type="text/javascript" src="./js/walking_average_filter.js"></script>
|
||||||
<script type="text/javascript" src="./js/gui.js"></script>
|
<script type="text/javascript" src="./js/gui.js"></script>
|
||||||
<script type="text/javascript" src="./js/msp/MSPCodes.js"></script>
|
<script type="text/javascript" src="./js/msp/MSPCodes.js"></script>
|
||||||
<script type="text/javascript" src="./js/msp/MSPHelper.js"></script>
|
<script type="text/javascript" src="./js/msp/MSPHelper.js"></script>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue