Quote:
Originally Posted by John_1102
I just got a VERY constant rate from using this function I wrote.
Code:
public void getSpeed(){
samples[counter] = Shooter_En.getRaw();
counter++;
Shooter_En.reset();
if(samples[9] > 0){
counter = 0;
for(int i = 0; i <= 9; i++){
sum = sum + samples[i];
}
AVG = sum / 10;
Rate = AVG / 0.48;
sum = 0;
}
}
|
When does
samples[9] get reset to zero?
If it's getting reset, you're only getting a new speed every 10 cycles.
If it's not, then once it becomes non-zero it stays that way, and you re-zero the counter every cycle so the filter does nothing (except skew the calculation).
What you want is a circular buffer (ring buffer) that you populate each cycle and use each cycle to get a new speed, like this:
Code:
public void getSpeed(){
samples[counter++] = Shooter_En.getRaw();
Shooter_En.reset();
if(counter>9) counter=0;
sum=0;
for(int i = 0; i <= 9; i++) sum += samples[i];
Rate = sum / (10* 0.48);
}