You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
inav-configurator/js/serial_queue.js

272 lines
8.0 KiB
JavaScript

'use strict';
var helper = helper || {};
helper.mspQueue = (function (serial, MSP) {
var publicScope = {},
privateScope = {};
privateScope.handlerFrequency = 100;
privateScope.balancerFrequency = 20;
privateScope.loadFilter = new classes.SimpleSmoothFilter(1, 0.9);
privateScope.roundtripFilter = new classes.SimpleSmoothFilter(20, 0.99);
privateScope.hardwareRoundtripFilter = new classes.SimpleSmoothFilter(10, 0.99);
/**
* Target load for MSP queue. When load is above target, throttling might start to appear
* @type {number}
*/
privateScope.targetLoad = 1.5;
privateScope.statusDropFactor = 0.75;
privateScope.currentLoad = 0;
/**
* PID controller used to perform throttling
* @type {classes.PidController}
*/
privateScope.loadPidController = new classes.PidController();
privateScope.loadPidController.setTarget(privateScope.targetLoad);
privateScope.loadPidController.setOutput(0, 99, 0);
privateScope.loadPidController.setGains(10, 4, 1);
privateScope.loadPidController.setItermLimit(0, 90);
8 years ago
privateScope.dropRatio = 0;
publicScope.computeDropRatio = function () {
privateScope.dropRatio = privateScope.loadPidController.run(publicScope.getLoad());
};
publicScope.getDropRatio = function () {
return privateScope.dropRatio;
};
8 years ago
privateScope.queue = [];
privateScope.softLock = false;
privateScope.hardLock = false;
privateScope.lockMethod = 'soft';
publicScope.setLockMethod = function (method) {
privateScope.lockMethod = method;
};
publicScope.setSoftLock = function () {
privateScope.softLock = new Date().getTime();
};
publicScope.setHardLock = function () {
privateScope.hardLock = new Date().getTime();
};
publicScope.freeSoftLock = function () {
privateScope.softLock = false;
};
publicScope.freeHardLock = function () {
privateScope.hardLock = false;
};
publicScope.isLocked = function () {
if (privateScope.lockMethod === 'soft') {
return privateScope.softLock !== false;
} else {
return privateScope.hardLock !== false;
}
};
/**
* This method is periodically executed and moves MSP request
* from a queue to serial port. This allows to throttle requests,
* adjust rate of new frames being sent and prohibit situation in which
* serial port is saturated, virtually overloaded, with outgoing data
*
* This also implements serial port sharing problem: only 1 frame can be transmitted
* at once
*
* MSP class no longer implements blocking, it is queue responsibility
*/
publicScope.executor = function () {
/*
* Debug
*/
helper.eventFrequencyAnalyzer.put("execute");
8 years ago
privateScope.loadFilter.apply(privateScope.queue.length);
8 years ago
/*
* if port is blocked or there is no connection, do not process the queue
*/
if (publicScope.isLocked() || serial.connectionId === false) {
helper.eventFrequencyAnalyzer.put("port in use");
return false;
}
var request = privateScope.get();
if (request !== undefined) {
/*
* Lock serial port as being in use right now
*/
publicScope.setSoftLock();
publicScope.setHardLock();
request.timer = setTimeout(function () {
console.log('MSP data request timed-out: ' + request.code);
/*
* Remove current callback
*/
MSP.removeCallback(request.code);
/*
* To prevent infinite retry situation, allow retry only while counter is positive
*/
if (request.retryCounter > 0) {
request.retryCounter--;
/*
* Create new entry in the queue
*/
publicScope.put(request);
}
}, serial.getTimeout());
if (request.sentOn === null) {
request.sentOn = new Date().getTime();
}
/*
* Set receive callback here
*/
MSP.putCallback(request);
helper.eventFrequencyAnalyzer.put('message sent');
/*
* Send data to serial port
*/
serial.send(request.messageBody, function (sendInfo) {
if (sendInfo.bytesSent == request.messageBody.byteLength) {
/*
* message has been sent, check callbacks and free resource
*/
if (request.onSend) {
request.onSend();
}
publicScope.freeSoftLock();
}
});
}
};
privateScope.get = function () {
return privateScope.queue.shift();
};
publicScope.flush = function () {
privateScope.queue = [];
};
publicScope.put = function (mspRequest) {
privateScope.queue.push(mspRequest);
};
publicScope.getLength = function () {
return privateScope.queue.length;
};
8 years ago
/**
* 1s MSP load computed as number of messages in a queue in given period
* @returns {number}
8 years ago
*/
publicScope.getLoad = function () {
8 years ago
return privateScope.loadFilter.get();
8 years ago
};
publicScope.getRoundtrip = function () {
8 years ago
return privateScope.roundtripFilter.get();
};
/**
*
* @param {number} number
*/
publicScope.putRoundtrip = function (number) {
8 years ago
privateScope.roundtripFilter.apply(number);
};
publicScope.getHardwareRoundtrip = function () {
8 years ago
return privateScope.hardwareRoundtripFilter.get();
};
/**
*
* @param {number} number
*/
publicScope.putHardwareRoundtrip = function (number) {
8 years ago
privateScope.hardwareRoundtripFilter.apply(number);
};
publicScope.balancer = function () {
8 years ago
privateScope.currentLoad = privateScope.loadFilter.get();
helper.mspQueue.computeDropRatio();
/*
* Also, check if port lock if hanging. Free is so
*/
var currentTimestamp = new Date().getTime(),
threshold = publicScope.getHardwareRoundtrip() * 3;
if (threshold > 1000) {
threshold = 1000;
}
if (privateScope.softLock !== false && currentTimestamp - privateScope.softLock > threshold) {
privateScope.softLock = false;
helper.eventFrequencyAnalyzer.put('force free soft lock');
}
if (privateScope.hardLock !== false && currentTimestamp - privateScope.hardLock > threshold) {
privateScope.hardLock = false;
helper.eventFrequencyAnalyzer.put('force free hard lock');
}
};
publicScope.shouldDrop = function () {
return (Math.round(Math.random()*100) < privateScope.dropRatio);
};
publicScope.shouldDropStatus = function () {
return (Math.round(Math.random()*100) < (privateScope.dropRatio * privateScope.statusDropFactor));
};
/**
* This method return periodic for polling interval that should populate queue in 75% or less
* @param {number} requestedInterval
* @param {number} messagesInInterval
* @returns {number}
*/
publicScope.getIntervalPrediction = function (requestedInterval, messagesInInterval) {
var requestedRate = (1000 / requestedInterval) * messagesInInterval,
availableRate = (1000 / publicScope.getRoundtrip()) * 0.75;
if (requestedRate < availableRate) {
return requestedInterval;
} else {
return (1000 / availableRate) * messagesInInterval;
}
};
8 years ago
setInterval(publicScope.executor, Math.round(1000 / privateScope.handlerFrequency));
setInterval(publicScope.balancer, Math.round(1000 / privateScope.balancerFrequency));
return publicScope;
})(serial, MSP);