Skip to content

Commit 02e4e27

Browse files
Merge pull request #20 from MStenkaer/master
Corrected Sensade adr algorithm
2 parents 19fc85c + 70fb89e commit 02e4e27

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

configuration/chirpstack/adr-modules/sensade-adr-mod/sensade-adr.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,27 @@ export function id() {
4343
// txPowerIndex: 1,
4444
// nbTrans: 1
4545
// }
46+
4647
// Handle function for ADR request.
4748
export function handle(req) {
4849
// This defines the default response, which is equal to the current device state.
4950
let resp = {
50-
dr: req.DR,
51-
txPowerIndex: req.TxPowerIndex,
52-
nbTrans: req.NbTrans,
51+
dr: req.dr,
52+
txPowerIndex: 0,
53+
nbTrans: req.nbTrans,
5354
};
5455

55-
resp.txPowerIndex = 0;
56-
5756
// If ADR is disabled, return with current values.
58-
if (!req.ADR) {
59-
return [resp, null];
57+
if (!req.adr) {
58+
return resp;
6059
}
6160

6261
let lostPackageCnt = getLostPackageCount(req);
6362
// If 5 or more packages are lost during the last received packages, decrease the data-rate
6463
if (lostPackageCnt >= 5) {
6564
resp.dr -= 1;
6665
} else if (
67-
req.UplinkHistory.length === 20 &&
66+
req.uplinkHistory.length >= 20 &&
6867
getLostPackageCount(req, 20) <= 2
6968
) {
7069
// If 2 or fewer packages are lost during the last 20 received packages, the dr might be able to be increased
@@ -78,7 +77,7 @@ export function handle(req) {
7877
// A margin of around 10 seems okay
7978
// This value has been chosen based on the loRaSNR values from sensors installed in a parking lot,
8079
// where a fluctuation of +- 10 can be seen (should be caused by cars parking and leaving).
81-
let diffSNR = minSNR - req.requiredHistoryCount - installationMargin;
80+
let diffSNR = minSNR - req.requiredSnrForDr - installationMargin;
8281

8382
// Examples:
8483
// minSNR = -5
@@ -104,11 +103,11 @@ export function handle(req) {
104103
resp.dr = req.maxDr;
105104
}
106105

107-
if (req.MinDR > resp.dr) {
108-
resp.dr = req.MinDR;
106+
if (req.minDr > resp.dr) {
107+
resp.dr = req.minDr;
109108
}
110109

111-
return [resp, null];
110+
return resp;
112111
}
113112

114113
function getMinSNR(req) {
@@ -144,12 +143,12 @@ function getLostPackageCount(req, ...lastXElement) {
144143
const m = elements[i];
145144

146145
if (i === 0) {
147-
previousFCnt = m.FCnt;
146+
previousFCnt = m.fCnt;
148147
continue;
149148
}
150149

151-
lostPackets += m.FCnt - previousFCnt - 1; // there is always an expected difference of 1
152-
previousFCnt = m.FCnt;
150+
lostPackets += m.fCnt - previousFCnt - 1; // there is always an expected difference of 1
151+
previousFCnt = m.fCnt;
153152
}
154153

155154
return lostPackets;

0 commit comments

Comments
 (0)