Here's a rate limiter we had in our code from last year.
Code:
/**
* A simple rate limiter.
* http://www.chiefdelphi.com/forums/showpost.php?p=1212189&postcount=3
*
* @param input the input value (speed from command/joystick)
* @param speed the speed currently being traveled at
* @param posRateLimit the rate limit for accelerating
* @param negRateLimit the rate limit for decelerating
* @return the new output speed (rate limited)
*/
public static double rateLimit(double input, double speed,
double posRateLimit, double negRateLimit) {
if (input > 0) {
if (input > (speed + posRateLimit)) {
//Accelerating positively
speed = speed + posRateLimit;
} else if (input < (speed - negRateLimit)) {
//Decelerating positively
speed = speed - negRateLimit;
} else {
speed = input;
}
} else {
if (input < (speed - posRateLimit)) {
//Accelerating negatively
speed = speed - posRateLimit;
} else if (input > (speed + negRateLimit)) {
//Decelerating negatively
speed = speed + negRateLimit;
} else {
speed = input;
}
}
return speed;
}
You would/could call it like this:
Code:
newLeftSpeed = rateLimit(newLeftSpeed, currentLeftSpeed, 0.15, 0.15);
newRightSpeed = rateLimit(newRightSpeed, currentRightSpeed, 0.15, 0.15);
drivetrain.tankDrive(newLeftSpeed, newRightSpeed);
Where the
current and
new variables used above are class variables used to keep track of speed between loop iterations, and the tank drive just passes the values it receives on to the motor controllers for each side of the drivetrain. The
new speed variables could be coming right off your joysticks if using this code in teleop.
Also note, that the code uses the term speed to refer to the commanded value sent out to the motor controller (between 1.0 and -1.0). This is really just the voltage the motor controller will be commanded to output and doesn't have any direct correlation to the speed of travel of the robot. This code doesn't, and isn't intended to, make use of any sensor values in the rate limiter.
If you can post what type of java project you're working in (Simple Robot, Iterative Robot, or Command Based) we could help suggest how to integrate the code a little better.