diff --git a/libraries/AP_Logger/AP_Logger.cpp b/libraries/AP_Logger/AP_Logger.cpp index 77d05e358d..25638c1e6f 100644 --- a/libraries/AP_Logger/AP_Logger.cpp +++ b/libraries/AP_Logger/AP_Logger.cpp @@ -92,7 +92,15 @@ const AP_Param::GroupInfo AP_Logger::var_info[] = { // @User: Standard // @Units: s AP_GROUPINFO("_FILE_TIMEOUT", 6, AP_Logger, _params.file_timeout, HAL_LOGGING_FILE_TIMEOUT), - + + // @Param: _FILE_MB_FREE + // @DisplayName: Old logs on the SD card will be deleted to maintain this amount of free space + // @Description: Set this such that the free space is larger than your largest typical flight log + // @Units: MB + // @Range: 10 1000 + // @User: Standard + AP_GROUPINFO("_FILE_MB_FREE", 7, AP_Logger, _params.min_MB_free, 500), + AP_GROUPEND }; diff --git a/libraries/AP_Logger/AP_Logger.h b/libraries/AP_Logger/AP_Logger.h index 1c7eadb4dc..f5ba692e51 100644 --- a/libraries/AP_Logger/AP_Logger.h +++ b/libraries/AP_Logger/AP_Logger.h @@ -343,6 +343,7 @@ public: AP_Int8 log_replay; AP_Int8 mav_bufsize; // in kilobytes AP_Int16 file_timeout; // in seconds + AP_Int16 min_MB_free; } _params; const struct LogStructure *structure(uint16_t num) const; diff --git a/libraries/AP_Logger/AP_Logger_File.cpp b/libraries/AP_Logger/AP_Logger_File.cpp index f3c32f89dd..5d18c389e6 100644 --- a/libraries/AP_Logger/AP_Logger_File.cpp +++ b/libraries/AP_Logger/AP_Logger_File.cpp @@ -34,6 +34,9 @@ extern const AP_HAL::HAL& hal; #define HAL_LOGGER_WRITE_CHUNK_SIZE 4096 #endif +#define MB_to_B 1000000 +#define B_to_MB 0.000001 + /* constructor */ @@ -190,22 +193,6 @@ int64_t AP_Logger_File::disk_space() return AP::FS().disk_space(_log_directory); } -// returns the available space in _log_directory as a percentage -// returns -1.0f on error -float AP_Logger_File::avail_space_percent() -{ - int64_t avail = disk_space_avail(); - if (avail == -1) { - return -1.0f; - } - int64_t space = disk_space(); - if (space == -1) { - return -1.0f; - } - - return (avail/(float)space) * 100; -} - // find_oldest_log - find oldest log in _log_directory // returns 0 if no log was found uint16_t AP_Logger_File::find_oldest_log() @@ -286,17 +273,19 @@ void AP_Logger_File::Prep_MinSpace() return; } + const int64_t target_free = (int64_t)_front._params.min_MB_free * MB_to_B; + _cached_oldest_log = 0; uint16_t log_to_remove = first_log_to_remove; uint16_t count = 0; do { - float avail = avail_space_percent(); - if (is_equal(avail, -1.0f)) { + int64_t avail = disk_space_avail(); + if (avail == -1) { break; } - if (avail >= min_avail_space_percent) { + if (avail >= target_free) { break; } if (count++ > MAX_LOG_FILES+10) { @@ -310,8 +299,8 @@ void AP_Logger_File::Prep_MinSpace() break; } if (file_exists(filename_to_remove)) { - hal.console->printf("Removing (%s) for minimum-space requirements (%.2f%% < %.0f%%)\n", - filename_to_remove, (double)avail, (double)min_avail_space_percent); + hal.console->printf("Removing (%s) for minimum-space requirements (%.0fMB < %.0fMB)\n", + filename_to_remove, (double)avail*B_to_MB, (double)target_free*B_to_MB); EXPECT_DELAY_MS(2000); if (AP::FS().unlink(filename_to_remove) == -1) { hal.console->printf("Failed to remove %s: %s\n", filename_to_remove, strerror(errno)); @@ -352,7 +341,12 @@ bool AP_Logger_File::NeedPrep() return false; } - if (avail_space_percent() < min_avail_space_percent) { + const int64_t actual = disk_space_avail(); + if (actual == -1) { + return false; + } + + if (actual < (int64_t)_front._params.min_MB_free * MB_to_B) { return true; } diff --git a/libraries/AP_Logger/AP_Logger_File.h b/libraries/AP_Logger/AP_Logger_File.h index d32d4e7a95..afcc53f6fd 100644 --- a/libraries/AP_Logger/AP_Logger_File.h +++ b/libraries/AP_Logger/AP_Logger_File.h @@ -97,17 +97,10 @@ private: uint16_t find_oldest_log(); int64_t disk_space_avail(); int64_t disk_space(); - float avail_space_percent(); bool file_exists(const char *filename) const; bool log_exists(const uint16_t lognum) const; -#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX - // I always seem to have less than 10% free space on my laptop: - const float min_avail_space_percent = 0.1f; -#else - const float min_avail_space_percent = 10.0f; -#endif // write buffer ByteBuffer _writebuf; const uint16_t _writebuf_chunk;