From 43af12a408e45bc4d4bceb66319e76a745d6d97a Mon Sep 17 00:00:00 2001 From: aftersomemath Date: Sat, 2 Apr 2016 13:46:07 -0400 Subject: [PATCH] Add automatically sized buffer for filter --- EncoderMod.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/EncoderMod.h b/EncoderMod.h index d94b299..ee3afcf 100644 --- a/EncoderMod.h +++ b/EncoderMod.h @@ -54,7 +54,7 @@ #define FILTER_TIME_LIMIT 10000 //uS at 100/255 pwm we get about 1/0.004 = 250uS/tick so this averages about 4 ticks #define US_INTERVAL 50 //This must be lower than time difference between ticks will ever be. #define FILTER_INTERVALS (FILTER_TIME_LIMIT/US_INTERVAL) -#define MAX_BUFFER_SIZE 5000 //This needs to be larger than the max number of ticks that can happen in our filter time limit it needs to be larger so that memory access violations don't occur +#define MAX_BUFFER_SIZE ((int)(FILTER_TIME_LIMIT / 166.66)) //For 10:1 micro metal gear motors. 1sec/(3000rpm at shaft * 10:1 * 12enc/rev) = 166uS/tick // All the data needed by interrupts is consolidated into this ugly struct @@ -166,6 +166,7 @@ class Encoder return 0.0; } + int ticksInFilter = 0; while(1){ //Get how many discrete intervals if(encoder.uSBuffer[index] == 0){ @@ -174,6 +175,7 @@ class Encoder } int intervals = abs(encoder.uSBuffer[index])/US_INTERVAL; sumIntervals += intervals; + ticksInFilter++; if(sumIntervals <= FILTER_INTERVALS){ velocitySum += intervals * (2.0/(float)encoder.uSBuffer[index]); } else {