From 72caa24450e2c7ae9e0416dac0567adc230c1983 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Tue, 5 Jan 2016 00:02:56 -0500 Subject: [PATCH] auto-sync --- firmware/controllers/algo/accel_enrichment.cpp | 3 +++ firmware/util/cyclic_buffer.h | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/firmware/controllers/algo/accel_enrichment.cpp b/firmware/controllers/algo/accel_enrichment.cpp index caf8921ff2..479a7d6fb9 100644 --- a/firmware/controllers/algo/accel_enrichment.cpp +++ b/firmware/controllers/algo/accel_enrichment.cpp @@ -63,6 +63,8 @@ floatms_t WallFuel::getWallFuel(int injectorIndex) { } float AccelEnrichmemnt::getDelta() { + if (cb.getCount() == 0) + return 0; // no recent data return cb.maxValue(cb.getSize()); } @@ -90,6 +92,7 @@ float AccelEnrichmemnt::getEngineLoadEnrichment(DECLARE_ENGINE_PARAMETER_F) { } void AccelEnrichmemnt::reset() { + cb.clear(); delta = 0; currentValue = NAN; } diff --git a/firmware/util/cyclic_buffer.h b/firmware/util/cyclic_buffer.h index 2d13f09a8c..83bff6a43f 100644 --- a/firmware/util/cyclic_buffer.h +++ b/firmware/util/cyclic_buffer.h @@ -17,6 +17,8 @@ static const short CB_MAX_SIZE = 64; +#define BUFFER_MAX_VALUE 200123 + template class cyclic_buffer { @@ -40,12 +42,16 @@ class cyclic_buffer T minValue(int length); void setSize(int size); int getSize(); + int getCount(); void clear(); private: void baseC(int size); volatile T elements[CB_MAX_SIZE]; volatile int currentIndex; + /** + * number of elements added into this buffer, would be eventually bigger then size + */ volatile int count; int size; }; @@ -115,6 +121,11 @@ int cyclic_buffer::getSize() { return size; } +template +int cyclic_buffer::getCount() { + return count; +} + template T cyclic_buffer::get(int index) { while (index < 0) { @@ -129,10 +140,11 @@ T cyclic_buffer::get(int index) { template T cyclic_buffer::maxValue(int length) { if (length > count) { + // not enough data in buffer length = count; } int ci = currentIndex; // local copy to increase thread-safety - T result = -2000000; // todo: better min value? + T result = -BUFFER_MAX_VALUE; // todo: better min value? for (int i = 0; i < length; ++i) { int index = ci - i; while (index < 0) { @@ -152,7 +164,7 @@ T cyclic_buffer::minValue(int length) { length = count; } int ci = currentIndex; // local copy to increase thread-safety - T result = +2000000; // todo: better max value? + T result = +BUFFER_MAX_VALUE; // todo: better max value? for (int i = 0; i < length; ++i) { int index = ci - i; while (index < 0) { @@ -191,7 +203,7 @@ template void cyclic_buffer::clear() { memset((void*) elements, 0, sizeof(elements)); // I would usually use static_cast, but due to the elements being volatile we cannot. count = 0; - count = 0; + currentIndex = 0; } #endif //CYCLIC_BUFFER_H