-
Notifications
You must be signed in to change notification settings - Fork 0
/
adr.go
80 lines (69 loc) · 2.1 KB
/
adr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package band
import "errors"
var demodulationFloor = map[string]float32{
"SF7BW125": -7.5,
"SF8BW125": -10,
"SF9BW125": -12.5,
"SF10BW125": -15,
"SF11BW125": -17.5,
"SF12BW125": -20,
"SF7BW250": -4.5,
"SF8BW500": -4,
}
func linkMargin(dataRate string, snr float32) float32 {
if floor, ok := demodulationFloor[dataRate]; ok {
return snr - floor
}
return 0
}
// ADRConfig contains configuration for Adaptive Data Rate
type ADRConfig struct {
MinDataRate int
MaxDataRate int
MinTXPower int
MaxTXPower int
StepTXPower int
}
// ErrADRUnavailable is returned when ADR is not available
var ErrADRUnavailable = errors.New("ADR Unavailable")
// ADRSettings gets the ADR settings given a dataRate, txPower, SNR and device margin
func (f *FrequencyPlan) ADRSettings(dataRate string, txPower int, snr float32, deviceMargin float32) (desiredDataRate string, desiredTxPower int, err error) {
if f.ADR == nil {
return dataRate, txPower, ErrADRUnavailable
}
margin := linkMargin(dataRate, snr) - deviceMargin
drIdx, err := f.GetDataRateIndexFor(dataRate)
if err != nil {
return dataRate, txPower, err
}
nStep := int(margin / 3)
// Increase the data rate with each step
for nStep > 0 && drIdx < f.ADR.MaxDataRate {
drIdx++
nStep--
}
nStep = int(float32(nStep*3) / float32(f.ADR.StepTXPower))
// Decrease the Tx power by f.ADR.StepTXPower for each step, until min reached
for nStep > 0 && txPower > f.ADR.MinTXPower {
txPower -= f.ADR.StepTXPower
nStep--
}
if txPower < f.ADR.MinTXPower {
txPower = f.ADR.MinTXPower
}
// Increase the Tx power by 3 for each step, until max reached
for nStep < 0 && txPower < f.ADR.MaxTXPower {
txPower += f.ADR.StepTXPower
nStep++
}
if txPower > f.ADR.MaxTXPower {
txPower = f.ADR.MaxTXPower
}
desiredDataRate, err = f.GetDataRateStringForIndex(drIdx)
if err != nil {
return dataRate, txPower, err // This should maybe panic; it means that f.ADR is incosistent with f.DataRates
}
return desiredDataRate, txPower, nil
}