Skip to content

Commit 1adf972

Browse files
committed
refactor: enhance throttling factor calculation and introduce pow_of_2 utility function
1 parent 8265cd0 commit 1adf972

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/mesh/Default.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
2+
#include <MeshRadio.h>
23
#include <NodeDB.h>
4+
#include <RadioInterface.h>
5+
#include <cmath>
36
#include <cstdint>
47
#include <meshUtils.h>
58
#define ONE_DAY 24 * 60 * 60
@@ -63,21 +66,26 @@ class Default
6366
if (numOnlineNodes <= 40) {
6467
return 1.0;
6568
} else {
66-
float throttlingFactor = pow(2, config.lora.spread_factor) / (config.lora.bandwidth * 100000);
69+
// Get bandwidth in kHz - convert from code if not using preset
70+
float bwKHz =
71+
config.lora.use_preset ? modemPresetToBwKHz(config.lora.modem_preset, false) : bwCodeToKHz(config.lora.bandwidth);
6772

73+
// throttlingFactor = 2^SF / (BW_in_kHz * scaling_divisor)
74+
// With scaling_divisor=100:
6875
// In SF11 and BW=250khz (longfast), this gives 0.08192 rather than the original 0.075
6976
// In SF10 and BW=250khz (mediumslow), this gives 0.04096 rather than the original 0.04
7077
// In SF9 and BW=250khz (mediumfast), this gives 0.02048 rather than the original 0.02
7178
// In SF7 and BW=250khz (shortfast), this gives 0.00512 rather than the original 0.01
79+
float throttlingFactor = static_cast<float>(pow_of_2(config.lora.spread_factor)) / (bwKHz * 100.0f);
7280

7381
#if USERPREFS_EVENT_MODE
7482
// If we are in event mode, scale down the throttling factor by 4
75-
throttlingFactor = pow(2, config.lora.spread_factor) / (config.lora.bandwidth * 25000);
83+
throttlingFactor = static_cast<float>(pow_of_2(config.lora.spread_factor)) / (bwKHz * 25.0f);
7684
#endif
7785

7886
// Scaling up traffic based on number of nodes over 40
7987
int nodesOverForty = (numOnlineNodes - 40);
80-
return 1.0 + (nodesOverForty * throttlingFactor); // Each number of online node scales by 0.075 (default)
88+
return 1.0 + (nodesOverForty * throttlingFactor); // Each number of online node scales by throttle factor
8189
}
8290
}
83-
};
91+
};

src/mesh/RadioInterface.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "configuration.h"
1717
#include "detect/LoRaRadioType.h"
1818
#include "main.h"
19+
#include "meshUtils.h" // for pow_of_2
1920
#include "sleep.h"
2021
#include <assert.h>
2122
#include <pb_decode.h>
@@ -31,12 +32,6 @@
3132
#include "STM32WLE5JCInterface.h"
3233
#endif
3334

34-
// Calculate 2^n without calling pow()
35-
uint32_t pow_of_2(uint32_t n)
36-
{
37-
return 1 << n;
38-
}
39-
4035
#define RDEF(name, freq_start, freq_end, duty_cycle, spacing, power_limit, audio_permitted, frequency_switching, wide_lora) \
4136
{ \
4237
meshtastic_Config_LoRaConfig_RegionCode_##name, freq_start, freq_end, duty_cycle, spacing, power_limit, audio_permitted, \

src/meshUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ const std::string vformat(const char *const zcFormat, ...);
3838
// Get actual string length for nanopb char array fields.
3939
size_t pb_string_length(const char *str, size_t max_len);
4040

41+
/// Calculate 2^n without calling pow() - used for spreading factor and other calculations
42+
inline uint32_t pow_of_2(uint32_t n)
43+
{
44+
return 1 << n;
45+
}
46+
4147
#define IS_ONE_OF(item, ...) isOneOf(item, sizeof((int[]){__VA_ARGS__}) / sizeof(int), __VA_ARGS__)

0 commit comments

Comments
 (0)