AP_Periph : BATT_MONITOR forced to HAL_BATT_MONITOR_DEFAULT after reboot

I am using AP_Periph on Matek M9N-F405
In libraries/AP_BattMonitor/AP_BattMonitor.cpp , we have in master :

#ifdef HAL_BATT_MONITOR_DEFAULT
if (_params[0]._type == 0) {
    // we can't use set_default() as the type is used as a flag for parameter conversion
    _params[0]._type.set(int8_t(HAL_BATT_MONITOR_DEFAULT));
}
#endif

Thus, if I disable instance 0 of BatteryMonitor by setting BATT_MONITOR from 4 (Default) to 0 using SLCAN gui, it seems to be taken into account.
But on next reboot the value is forced to HAL_BATT_MONITOR_DEFAULT.

Using BatteryMonitor on AP_Periph is useless in my setup, so I should be able to disabled it. But may be it is mandatory in AP_Periph for other reasons…

My proposal:

#ifndef HAL_BUILD_AP_PERIPH
#ifdef HAL_BATT_MONITOR_DEFAULT
if (_params[0]._type == 0) {
    // we can't use set_default() as the type is used as a flag for parameter conversion
    _params[0]._type.set(int8_t(HAL_BATT_MONITOR_DEFAULT));
}
#endif
#endif

Would it be ok ?

This is probably a question for @tridge but I wonder why HAL_BATT_MONITOR_DEFAULT is set in the first place for AP_Periph on this board (or any board).

If you don’t hear from any other devs, it might be a good idea to raise a PR with your suggested change and then we can mark it for review at the dev call which will mean it will be discussed. I’ve also added this item to the 4.1 issues list.

I fixed this a bit ago and this code is gone now.

I do plan on changing the hwdefs for the matek dlvr and gps ap_periphs to have the battery off by default off, along with the other possible sensors off by default.

So we stop spamming the CAN network with messages for sensors that aren’t there by default.