View Single Post
  #1   Spotlight this post!  
Unread 10-09-2008, 14:04
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Servo 'smoothing'

Quote:
Originally Posted by Andy A. View Post
I'm assuming the pot's being used on the OI side of things float around a bit and that variation shows up in the servos as jitter (bounces around a position rapidly and seemingly randomly). I know that the servos them selves are fine because if I generate the pwm value in code they hold position with no jitter.
I'll take your word for it that the problem is with the input, but if I were working on it myself I'd be printing out the OI values just to make extra certain that I was addressing the correct issue.

Quote:
So I am I right assuming I need some sort of algorithm that takes the analog inputs from my crummy pots and smooths them out...My gut tells me there is a really simple way to do this,...
There is indeed a really simple way to do this: just read a bunch of input values and average them.

You can do that in several ways. The easiest is something like this:

Code:
// DISCLAIMER: THIS CODE IS UNTESTED

// declare some variables at the beginning of the code
char incount = 0; // this will count the input values being accumulated
int accumulator = 0; // this will accumulate input values
char smooth = 128; // this will hold the smoothed input value

// ....

// do this each time a new OI value is available
  accumulator += (int)potval; // replace 'potval' as appropriate, e.g. p1_aux
  if( incount++ == 8 ) // is this the eighth sample?
  {
    smooth = accumulator << 3; // a quick and sneaky way to divide by 8
    incount = 0; // set to count another eight samples
    accumulator = 0; // reset accumulator
  }
Now you can use smooth as your input value. It will only change once every eight samples, or about five times per second. If it's still too noisy, you can increase to 16 samples at the cost of even slower response. If you need it to be quicker, there are other ways to do it which take more code and more temporary storage, but which begin to respond more immediately. Filtering is always a tradeoff between stability and response time.