diff --git a/libraries/AC_Fence/AC_Fence.h b/libraries/AC_Fence/AC_Fence.h index 1e8bd1d37c..e8eb88943e 100644 --- a/libraries/AC_Fence/AC_Fence.h +++ b/libraries/AC_Fence/AC_Fence.h @@ -159,6 +159,12 @@ public: static const struct AP_Param::GroupInfo var_info[]; +#if AP_SDCARD_STORAGE_ENABLED + bool failed_sdcard_storage(void) const { + return _poly_loader.failed_sdcard_storage(); + } +#endif + private: static AC_Fence *_singleton; diff --git a/libraries/AC_Fence/AC_PolyFence_loader.cpp b/libraries/AC_Fence/AC_PolyFence_loader.cpp index fbae61b2e8..ba38dea63a 100644 --- a/libraries/AC_Fence/AC_PolyFence_loader.cpp +++ b/libraries/AC_Fence/AC_PolyFence_loader.cpp @@ -3,6 +3,7 @@ #if AP_FENCE_ENABLED #include +#include #ifndef AC_FENCE_DUMMY_METHODS_ENABLED #define AC_FENCE_DUMMY_METHODS_ENABLED (!(APM_BUILD_TYPE(APM_BUILD_Rover) | APM_BUILD_COPTER_OR_HELI | APM_BUILD_TYPE(APM_BUILD_ArduPlane) | APM_BUILD_TYPE(APM_BUILD_ArduSub) | (AP_FENCE_ENABLED == 1))) @@ -27,10 +28,32 @@ extern const AP_HAL::HAL& hal; -static const StorageAccess fence_storage(StorageManager::StorageFence); +static StorageAccess fence_storage(StorageManager::StorageFence); + +#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS +#define AC_FENCE_SDCARD_FILENAME "APM/fence.stg" +#else +#define AC_FENCE_SDCARD_FILENAME "fence.stg" +#endif void AC_PolyFence_loader::init() { +#if AP_SDCARD_STORAGE_ENABLED + // check for extra storage on microsd + const auto *bc = AP::boardConfig(); + if (bc != nullptr) { + const auto size_kb = bc->get_sdcard_fence_kb(); + if (size_kb > 0) { + _failed_sdcard_storage = !fence_storage.attach_file(AC_FENCE_SDCARD_FILENAME, size_kb); + if (_failed_sdcard_storage) { + // wipe fence if storage not available, but don't + // save. This allows sdcard error to be fixed and + // reboot + _total.set(0); + } + } + } +#endif if (!check_indexed()) { // tell the user, perhaps? } diff --git a/libraries/AC_Fence/AC_PolyFence_loader.h b/libraries/AC_Fence/AC_PolyFence_loader.h index 5c87b5b4a7..4470cfa4fb 100644 --- a/libraries/AC_Fence/AC_PolyFence_loader.h +++ b/libraries/AC_Fence/AC_PolyFence_loader.h @@ -186,6 +186,11 @@ public: } +#if AP_SDCARD_STORAGE_ENABLED + bool failed_sdcard_storage(void) const { + return _failed_sdcard_storage; + } +#endif private: // multi-thread access support @@ -423,6 +428,11 @@ private: bool index_eeprom() WARN_IF_UNUSED; uint16_t fence_storage_space_required(const AC_PolyFenceItem *new_items, uint16_t count); + +#if AP_SDCARD_STORAGE_ENABLED + // true if we failed to load SDCard storage on init + bool _failed_sdcard_storage; +#endif }; #endif // AP_FENCE_ENABLED