feat(radio): calibrate dual-profile preambles from live switch timing

This commit is contained in:
mikecarper committed 2026-09-22 00:09:37 -07:00
1 parent 8c7f4df1cd
commit 04697db82e
16 files changed
+508 -93

No files matched your search

+48 -8
View File
@@ -11,7 +11,6 @@ int main() {
assert(Profiles::MinListenSymbols==4.6);
assert(Profiles::SlowListenSymbols==4.6);
assert(Profiles::MinFastListenSymbols==4.6);
assert(Profiles::SwitchBudgetUs==600);
assert(Profiles::LoopBudgetUs==300);
auto a=params(10,125),b=params(10,125);
auto t=Profiles::calculateChirpTiming(a,b,1,0,1000,4000);
@@ -41,7 +40,11 @@ int main() {
for (unsigned sf0=5;sf0<=12;++sf0) for(unsigned sf1=5;sf1<=12;++sf1) {
Profiles p;p.primary=params(sf0,bw0);p.secondary.params=params(sf1,bw1);
p.secondary.mode=mesh::RadioProfileMode::Rx;
auto minimum=Profiles::calculateChirpTiming(p.primary,p.secondary.params);
for (unsigned n=0;n<Profiles::SwitchTestSamplesPerDirection;++n) {
p.sampleSwitch(0,1,545);p.sampleSwitch(1,0,545);
}
assert(p.switchBudgetUs()==600);
auto minimum=Profiles::calculateChirpTiming(p.primary,p.secondary.params,0,0,p.switchBudgetUs());
assert(minimum.valid);
for(unsigned i=0;i<2;++i) {
const double symbols=4.6;
@@ -50,12 +53,17 @@ int main() {
assert(p.automaticPreamble(i)==minimum.preamble[i]);
}
if (!p.automaticPreambleFits()) continue;
if (p.automaticPreamble(0)>Profiles::MaxAutomaticPreamble
|| p.automaticPreamble(1)>Profiles::MaxAutomaticPreamble) {
assert(p.preamble(0,32)<=128 && p.preamble(1,32)<=128);
continue;
}
auto actual=p.chirpTiming();
assert(actual.valid);
assert(p.listenUs(actual.slow)==Profiles::minimumListenUs(p.params(actual.slow),true));
assert(p.listenUs(actual.slow^1)>=Profiles::minimumListenUs(p.params(actual.slow^1)));
const double available=std::floor(p.preamble(actual.slow,32)*actual.symbol_us[actual.slow]/2
-p.listenUs(actual.slow)-2*Profiles::SwitchBudgetUs-Profiles::LoopBudgetUs);
-p.listenUs(actual.slow)-2*p.switchBudgetUs()-Profiles::LoopBudgetUs);
const uint32_t fast_floor=Profiles::minimumListenUs(p.params(actual.slow^1));
assert(p.listenUs(actual.slow^1)==(available>fast_floor ? (uint32_t)available : fast_floor));
assert(2*actual.cycle_us<=p.preamble(actual.slow,32)*actual.symbol_us[actual.slow]+1e-6);
@@ -64,13 +72,21 @@ int main() {
}
Profiles p;p.primary=params(7,62.5);p.secondary.params=params(7,500);
p.secondary.mode=mesh::RadioProfileMode::Rx;
assert(p.preamble(0,32)==128 && p.preamble(1,32)==128 && !p.switchTestReady());
for (unsigned n=0;n<Profiles::SwitchTestSamplesPerDirection;++n) {
p.sampleSwitch(0,1,545);p.sampleSwitch(1,0,545);
}
assert(p.listenUs(0)==9421 && p.listenUs(1)==21847);
assert(p.chirpTiming().switch_us==600 && p.chirpTiming().loop_us==300 && p.chirpTiming().preamble[1]==64);
auto before=p.chirpTiming();const auto wire0=p.preamble(0,32),wire1=p.preamble(1,32);
p.longest_switch_us=8428;auto overrun=p.chirpTiming();
assert(overrun.switch_us==8428 && overrun.preamble[0]>before.preamble[0]);
assert(p.preamble(0,32)==wire0 && p.preamble(1,32)==wire1);
p.longest_switch_us=0;p.primary.preamble=64;
p.sampleSwitch(0,1,8428);auto overrun=p.chirpTiming();
assert(overrun.switch_us==9271 && overrun.preamble[1]>before.preamble[1]);
assert(p.preamble(0,32)>=wire0 && p.preamble(1,32)>=wire1);
p.resetSwitchTest();
for (unsigned n=0;n<Profiles::SwitchTestSamplesPerDirection;++n) {
p.sampleSwitch(0,1,545);p.sampleSwitch(1,0,545);
}
p.primary.preamble=64;
assert(p.chirpTiming().preamble[0]==64 && p.listenUs(1)>21847);
p.secondary.params.preamble=32;
assert(p.preamble(1,32)==32 && p.chirpTiming().preamble[1]>32);
@@ -97,5 +113,29 @@ int main() {
assert(p.automaticPreambleFits() && p.listenUs(1)==1418);
// Long requested visits remain visible, not replaced by policy minima.
t=Profiles::calculateChirpTiming(params(7,62.5),params(8,500),20000,30000);
assert(t.listen_us[0]==20000 && t.listen_us[1]==30000 && t.cycle_us==51500);
assert(t.listen_us[0]==20000 && t.listen_us[1]==30000 && t.cycle_us==50300);
Profiles measured;
measured.primary=params(7,500);
measured.secondary.params=params(7,500);
measured.secondary.mode=mesh::RadioProfileMode::Rx;
assert(!measured.switchTestReady() && measured.switchBudgetUs()==0);
assert(measured.preamble(0,32)==128 && measured.preamble(1,32)==128);
measured.sampleSwitch(0,0,10000); measured.sampleSwitch(0,1,0);
assert(measured.switch_test_samples[0]==0);
for (unsigned n=0;n<Profiles::SwitchTestSamplesPerDirection;++n) {
measured.sampleSwitch(0,1,531); measured.sampleSwitch(1,0,480);
}
assert(measured.switchTestReady() && measured.switchBudgetUs()==585); // ceil(531 * 1.1)
assert(measured.preamble(0,32)>=32 && measured.preamble(0,32)%8==0);
assert(measured.preamble(1,32)>=32 && measured.preamble(1,32)%8==0);
measured.sampleSwitch(0,1,8428);
assert(measured.switchBudgetUs()==9271);
assert(measured.automaticPreamble(0)>128);
assert(measured.preamble(0,32)==128 && measured.chirpTiming().preamble[0]>128);
measured.secondary.params.preamble=88; // explicit overrides remain unchanged
assert(measured.preamble(1,32)==88);
auto replacement=measured.secondary;replacement.params.freq=910.5;
measured.setSecondary(replacement,true);
assert(!measured.switchTestReady() && measured.switchBudgetUs()==0);
assert(measured.preamble(0,32)==128 && measured.preamble(1,32)==88);
}
+53 -14
View File
@@ -42,6 +42,13 @@ struct Fixture {
return reply;
}
void advance(uint32_t ms, bool epoch=true) { g_mock_millis+=ms; if(epoch)clock.epoch+=ms/1000; cli.loop(); }
void finishTimingTest(uint32_t switch_us=545) {
for (unsigned i=0;i<mesh::RadioProfiles::SwitchTestSamplesPerDirection;++i) {
radio.p.sampleSwitch(0,1,switch_us);
radio.p.sampleSwitch(1,0,switch_us);
}
assert(radio.p.switchTestReady());
}
};
static void permanentScheduleOrder() {
for (bool fail_first_save : {false, true}) {
@@ -324,9 +331,14 @@ int main(int argc, char** argv) {
Fixture f;
assert(!strcmp(f.cmd("get radio2"),"> off"));
assert(!strcmp(f.cmd("get radio2.cross"),"> auto"));
assert(strstr(f.cmd("set radio2 910.5,500,7,5,rxtx"),"WARN recommended preamble: radio2=64"));
assert(strstr(f.cmd("set radio2 910.5,500,7,5,rxtx"),"timing self-test pending"));
assert(!f.radio.p.enabled()); f.advance(2000); assert(f.radio.p.enabled());
assert(strstr(f.cmd("get radio2.timing"),"self-test pending"));
f.finishTimingTest();
assert(strstr(f.cmd("get radio2"),"rxtx,64 (auto)"));
assert(strstr(f.reply,"switch=600us"));
assert(strstr(f.cmd("set radio2 910.5,500,7,5,rxtx"),"preamble=64;"));
assert(strstr(f.reply,"switch=600us"));
assert(strstr(f.cmd("get radio2.scan"),"slow=radio; listen_us=9421,21847"));
assert(strstr(f.cmd("get radio2.timing"),"chirps=4.60,85.34; need=32,64"));
assert(strstr(f.reply,"switch=600us"));
@@ -347,6 +359,22 @@ int main(int argc, char** argv) {
f.cmd("set radio2 off"); f.advance(2000); assert(!f.radio.p.enabled());
f.cmd("get radio2.status");
}
{
Fixture f;
assert(strstr(f.cmd("set radio2 910.5,500,7,5,rx"),"timing self-test pending"));
assert(!strstr(f.reply,"switch="));
f.advance(2000);
f.finishTimingTest(497); // 497 us observed with a 10% guard -> 547 us.
assert(f.radio.p.switchBudgetUs()==547);
assert(strstr(f.cmd("get radio2"),"switch=547us"));
assert(strstr(f.cmd("set radio2 910.5,500,7,5,rx"),"switch=547us"));
assert(strstr(f.cmd("get radio2.timing"),"switch=547us"));
assert(!strstr(strstr(f.reply,"switch=")+1,"switch="));
assert(strstr(f.cmd("get radio2.status"),"budget=547us"));
assert(!strstr(f.reply,"switch=547us"));
assert(strstr(f.cmd("set radio2 911.5,500,7,5,rx"),"timing self-test pending"));
assert(!strstr(f.reply,"switch=547us"));
}
{
Fixture f;
const char* invalid[] = {"910.5,500,7,5", "NaN,500,7,5,rx", "910.5,0,7,5,rx",
@@ -367,20 +395,20 @@ int main(int argc, char** argv) {
Fixture f;
assert(!strcmp(f.cmd("get radio2.timing"),"> off"));
// A short explicit preamble is not rewritten, and cannot hide a warning.
assert(strstr(f.cmd("set radio2 910.5,500,7,5,rx,32"),"WARN recommended preamble: radio2=64"));
assert(strstr(f.reply,"short override: radio2"));
assert(strstr(f.cmd("set radio2 910.5,500,7,5,rx,32"),"timing self-test pending"));
f.advance(2000);
f.finishTimingTest();
assert(f.radio.p.secondary.params.preamble==32);
assert(strstr(f.cmd("get radio2"),"short override: radio2"));
f.radio.p.longest_switch_us=8428;
assert(strstr(f.cmd("get radio2.timing"),"switch=8428us"));
assert(strstr(f.reply,"WARN recommended preamble: radio="));
f.radio.p.sampleSwitch(0,1,8428);
assert(strstr(f.cmd("get radio2.timing"),"switch=9271us"));
assert(strstr(f.reply,"WARN recommended preamble: radio2="));
assert(f.radio.p.secondary.params.preamble==32);
// Primary getters use the saved primary's SF/BW instead of a temporary one.
f.radio.p.primary.sf=10; f.radio.p.primary.bw=125;
strcpy(f.reply,"> radio");
f.cli.appendSavedPreamble(f.reply,sizeof(f.reply),7,62.5);
assert(strstr(f.reply,"WARN recommended preamble: radio="));
assert(strstr(f.reply,"timing self-test pending"));
// Bounded appenders cannot corrupt neighboring bytes, even with tiny replies.
for (size_t n=1;n<=160;++n) {
char bytes[162]; memset(bytes,0x55,sizeof(bytes)); bytes[0]=0;
@@ -389,23 +417,22 @@ int main(int argc, char** argv) {
}
f.radio.p.primary.sf=10;f.radio.p.primary.bw=125;
f.radio.p.secondary.params.sf=10;f.radio.p.secondary.params.bw=125;
f.radio.p.longest_switch_us=0;
f.radio.p.resetSwitchTest(); f.finishTimingTest();
assert(!strstr(f.cmd("get radio2.timing"),"WARN"));
// A selected value below 32 still gets the actual recommendation, not
// merely a warning label with no preamble to use.
assert(strstr(f.cmd("set radio2 910.5,125,10,5,rx,16"),"WARN recommended preamble: radio2=32"));
assert(strstr(f.reply,"short override: radio2"));
assert(strstr(f.cmd("set radio2 910.5,125,10,5,rx,16"),"timing self-test pending"));
}
{
Fixture f;
f.radio.p.primary.sf=7;f.radio.p.primary.bw=500;
assert(strstr(f.cmd("set radio2 910.5,62.5,7,5,rx"),"WARN recommended preamble: radio=64"));
assert(strstr(f.cmd("set radio2 910.5,62.5,7,5,rx"),"timing self-test pending"));
f.advance(2000);
f.finishTimingTest();
assert(strstr(f.cmd("get radio2.timing"),"need=64,32"));
strcpy(f.reply,"OK - reboot to apply");
f.cli.appendPrimaryChirpWarning(f.reply,sizeof(f.reply),7,500,32);
assert(strstr(f.reply,"WARN recommended preamble: radio=64"));
assert(strstr(f.reply,"short override: radio"));
assert(strstr(f.reply,"timing self-test pending"));
strcpy(f.reply,"OK - reboot to apply");
f.cli.appendPrimaryChirpWarning(f.reply,sizeof(f.reply),7,62.5,0);
assert(!strstr(f.reply,"WARN")); // Preview requested tuple, not the live one.
@@ -417,13 +444,25 @@ int main(int argc, char** argv) {
{
Fixture f;
// The tested pair is allocated automatically without changing explicit 32.
assert(strstr(f.cmd("set radio2 910.5,500,8,5,rx,32"),"WARN recommended preamble: radio2=88"));
assert(strstr(f.cmd("set radio2 910.5,500,8,5,rx,32"),"timing self-test pending"));
f.advance(2000);
f.finishTimingTest();
assert(f.radio.p.secondary.params.preamble==32);
assert(strstr(f.cmd("get radio2.scan"),"slow=radio; listen_us=9421,21847"));
assert(strstr(f.cmd("get radio2.timing"),"chirps=4.60,42.67; need=32,88"));
assert(strstr(f.reply,"loop=300us"));
}
{
Fixture f;
f.radio.p.primary.bw=500;
assert(strstr(f.cmd("set radio2 910.5,500,7,5,rx"),"timing self-test pending"));
f.advance(2000);
f.finishTimingTest(8428);
assert(f.radio.p.switchBudgetUs()==9271);
assert(f.radio.p.preamble(0,32)==128);
assert(strstr(f.cmd("get radio2.timing"),"WARN need >128: radio"));
assert(strstr(f.cmd("get radio2.status"),"WARN need >128: radio"));
}
{
Fixture f;
f.cmd("set radio2 910.5,500,9,5,rxtx,40"); f.advance(2000);
+10 -4
View File
@@ -692,10 +692,13 @@ TEST(RadioProfiles, CrossPolicyAndReceiveOnlyMatrix) {
}
}
TEST(RadioProfiles, AutomaticPreamblesIncludeNominalSwitchingMargin) {
TEST(RadioProfiles, AutomaticPreamblesUseSelfTestedSwitchingMargin) {
DualProfileTestRadio radio;
auto& p = radio.config;
EXPECT_EQ(600, p.SwitchBudgetUs);
for (unsigned i = 0; i < p.SwitchTestSamplesPerDirection; ++i) {
p.sampleSwitch(0, 1, 545); p.sampleSwitch(1, 0, 545);
}
EXPECT_EQ(600, p.switchBudgetUs());
EXPECT_EQ(300, p.LoopBudgetUs);
const uint16_t expected[] = {64, 88, 32};
for (int sf = 7; sf <= 9; ++sf) {
@@ -705,9 +708,9 @@ TEST(RadioProfiles, AutomaticPreamblesIncludeNominalSwitchingMargin) {
EXPECT_EQ(0, p.preamble(1, 32) % 8);
EXPECT_EQ(9421, p.listenUs(0));
EXPECT_EQ(21847, p.listenUs(1));
EXPECT_LE(2 * (p.listenUs(0) + p.listenUs(1) + 2 * p.SwitchBudgetUs + p.LoopBudgetUs),
EXPECT_LE(2 * (p.listenUs(0) + p.listenUs(1) + 2 * p.switchBudgetUs() + p.LoopBudgetUs),
p.preamble(0, 32) * p.symbolUs(p.primary));
EXPECT_LE(p.listenUs(1) + 2 * p.SwitchBudgetUs + p.LoopBudgetUs
EXPECT_LE(p.listenUs(1) + 2 * p.switchBudgetUs() + p.LoopBudgetUs
+ p.AcquisitionSymbols * p.symbolUs(p.primary), p.preamble(0, 32) * p.symbolUs(p.primary));
}
p.secondary.params.preamble = 40;
@@ -719,6 +722,9 @@ TEST(RadioProfiles, AutomaticPreamblesIncludeNominalSwitchingMargin) {
TEST(RadioProfiles, SlowerProfileDeterminesOrderAndFasterReceiveWindow) {
DualProfileTestRadio radio;
auto& p = radio.config;
for (unsigned i = 0; i < p.SwitchTestSamplesPerDirection; ++i) {
p.sampleSwitch(0, 1, 545); p.sampleSwitch(1, 0, 545);
}
EXPECT_EQ(0, p.slowerProfile());
std::swap(p.primary, p.secondary.params);
EXPECT_EQ(1, p.slowerProfile());
+31 -2
View File
@@ -49,7 +49,7 @@ struct RadioLibWrapper {
uint32_t _profile_scan_generation[2]={};
uint16_t _physical_preamble=32;
float _cur_freq=909.5, _cur_bw=62.5;
unsigned applies=0, failRxStarts=0;
unsigned applies=0, failRxStarts=0, switch_apply_us=1200;
unsigned coding_writes=0;
bool coding_success=true;
bool setCodingRate(uint8_t) { ++coding_writes; return coding_success; }
@@ -72,7 +72,7 @@ struct RadioLibWrapper {
state=STATE_RX; _profile_visit_us=micros(); _rx_ps_armed=_rx_ps_enabled;
}
void stopReceiveDutyCycle() { _rx_ps_armed=false; }
bool applyParams(float f,float,uint8_t,uint8_t) { ++applies; elapsed_us+=1200; return !fail || f==909.5f; }
bool applyParams(float f,float,uint8_t,uint8_t) { ++applies; elapsed_us+=switch_apply_us; return !fail || f==909.5f; }
void cacheParams(float f,float b,uint8_t s,uint8_t c) { _cur_freq=f;_cur_bw=b;_cur_sf=s;_cur_cr=c; }
bool restoreAfterDeepInit() { return true; }
void recalibrateNoiseFloor() { _noise_floor_valid=false; }
@@ -98,6 +98,35 @@ struct RadioLibWrapper {
@METHODS@
int main() {
using Result=mesh::RadioParamApplyResult;
{
RadioLibWrapper w; w.enable();
assert(w._profiles.switchTestReady());
assert(w._profiles.switch_test_samples[0]==4 && w._profiles.switch_test_samples[1]==4);
assert(w._profiles.switchBudgetUs()==1320); // measured 1200 us plus 10%, no nominal floor
assert(w._profiles.preamble(0,32)>=32 && w._profiles.preamble(0,32)<=128);
assert(w._profiles.preamble(1,32)>=32 && w._profiles.preamble(1,32)<=128);
assert(w._profiles.preamble(0,32)%8==0 && w._profiles.preamble(1,32)%8==0);
}
{
RadioLibWrapper w; w._profiles.primary.bw=w._cur_bw=500; w.switch_apply_us=8428;
w.enable();
assert(w._profiles.switchTestReady() && w._profiles.switchBudgetUs()==9271);
assert(w._profiles.preamble(0,32)==128 && w._profiles.preamble(1,32)<=128);
assert(w._profiles.chirpTiming().preamble[0]>128);
}
{
RadioLibWrapper w; w.packet=true; w.enable();
assert(!w._profiles.switchTestReady() && w._profiles.switch_test_samples[0]==0);
w.packet=false; w.serviceProfileScan(); assert(w._profiles.switchTestReady());
w._profiles.setSecondary({},false);
assert(!w._profiles.switchTestReady());
}
{
RadioLibWrapper w; w.fail=true; w.enable();
assert(w._profiles.switch_failures==1 && w._profiles.switch_test_samples[0]==0);
w.fail=false; elapsed_us+=1000000; w.serviceProfileScan();
assert(w._profiles.switchTestReady());
}
for (unsigned blocked=0; blocked<5; ++blocked) {
RadioLibWrapper w;
w._cw_active = blocked==0; w.busy = blocked==1; w.packet = blocked==2;