diff --git a/firmware/controllers/core/interpolation.cpp b/firmware/controllers/core/interpolation.cpp index b159c862b8..b9cf0425d8 100644 --- a/firmware/controllers/core/interpolation.cpp +++ b/firmware/controllers/core/interpolation.cpp @@ -156,6 +156,17 @@ int findIndex2(const float array[], unsigned size, float value) { return i || *array <= value ? i : -1; } +/** + * in order to use binary search we need to know that axis elements are sorted + */ +void ensureArrayIsAscending(const char *msg, const float array[], int size) { + for (int i = 0; i < size - 1; i ++) { + if (array[i] >= array[i+ 1]) { + firmwareError(CUSTOM_ERR_6538, "invalid axis %s at %f", msg, array[i]); + } + } +} + /** @brief Binary search * @returns the highest index within sorted array such that array[i] is greater than or equal to the parameter * @note If the parameter is smaller than the first element of the array, -1 is returned. diff --git a/firmware/controllers/core/interpolation.h b/firmware/controllers/core/interpolation.h index 26f83d9ad1..d7e3758fdb 100644 --- a/firmware/controllers/core/interpolation.h +++ b/firmware/controllers/core/interpolation.h @@ -22,6 +22,7 @@ int findIndex(const float array[], int size, float value); int findIndexMsg(const char *msg, const float array[], int size, float value); +void ensureArrayIsAscending(const char *msg, const float array[], int size); int findIndex2(const float array[], unsigned size, float value); float interpolate(float x1, float y1, float x2, float y2, float x); float interpolateMsg(const char *msg, float x1, float y1, float x2, float y2, float x);