View Single Post
  #29   Spotlight this post!  
Unread 31-01-2012, 22:23
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,015
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Speed PID Function

Quote:
Originally Posted by John_1102 View Post
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);
}


Last edited by Ether : 31-01-2012 at 22:57.