From 3da5dc4e0f7b4f41dc22297be9ea926b67025338 Mon Sep 17 00:00:00 2001 From: Karl Zeilhofer Date: Wed, 9 Dec 2015 11:23:13 +0100 Subject: [PATCH 1/2] added functionality for 24/7-runtime in FilterOnePole.cpp and fixed a numeric underflow bug --- FilterOnePole.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/FilterOnePole.cpp b/FilterOnePole.cpp index 8c42eb7..a9db372 100644 --- a/FilterOnePole.cpp +++ b/FilterOnePole.cpp @@ -17,10 +17,17 @@ void FilterOnePole::setFilter( FILTER_TYPE ft, float fc, float initialValue ) { } float FilterOnePole::input( float inVal ) { - long time = micros(); - ElapsedUS = float(time - LastUS); // cast to float here, for math + long int time = micros(); + long int ElapsedUS = time - LastUS; // stick to long int here, because of underflow with float on subtractions. + // (31+1 bits of long int compared to 23+1 bit of the mantissa of float) LastUS = time; // update this now + // minimize the effect of an overflow of micros(), which happens every 70 Minutes. + if(ElapsedUS < 0) + { + ElapsedUS = 0; + } + // shift the data values Ylast = Y; X = inVal; // this is now the most recent input value From bbccc4d2b4e6eeabad8767612c8384b174b88265 Mon Sep 17 00:00:00 2001 From: Karl Zeilhofer Date: Fri, 11 Dec 2015 09:40:03 +0100 Subject: [PATCH 2/2] bugfix: division by zero on micros() overflow --- FilterOnePole.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FilterOnePole.cpp b/FilterOnePole.cpp index a9db372..f4a12ba 100644 --- a/FilterOnePole.cpp +++ b/FilterOnePole.cpp @@ -25,7 +25,7 @@ float FilterOnePole::input( float inVal ) { // minimize the effect of an overflow of micros(), which happens every 70 Minutes. if(ElapsedUS < 0) { - ElapsedUS = 0; + ElapsedUS = 10; // do not set to zero!!! (division by zero) } // shift the data values