View Single Post
  #2   Spotlight this post!  
Unread 26-12-2012, 20:10
apples000's Avatar
apples000 apples000 is offline
Registered User
no team
 
Join Date: Mar 2012
Rookie Year: 2012
Location: United States
Posts: 222
apples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant future
Re: Questions about Encoders and PIDControllers

The simple answer is that if you do what you've described, the pid loop will try, but fail to reach you speed setpoint. I'm not sure if you're trying to do speed or position, but if you're doing speed it isn't as easy. The problem is that the pid loop won't work if you're trying to control speed. With a positional PID loop, when the setpoint is reached, the pid loop will output a zero command to the motor. This makes sense because the motor has reached where it wants to be, and should stop. If you're trying to control speed, you don't want the pid loop to set the motor speed to zero once you've reached the right speed, but instead, you should keep the motor output it is at now. What you have to do is add the error calculated from the pid loop to the current motor output. To do this, you need a class which implements pidoutput. Here's an example in java.
Code:
public class VelocityControl extends Subsystem implements PIDOutput{
    private PIDController pid;
    private Encoder encoder;
    private Jaguar jaguar;
    public VelocityControl(){
        encoder = new Encoder(1, 2);
        jaguar = new Jaguar(1);
        encoder.setDistancePerPulse(0.01);//set this normally
        encoder.setPIDSourceParameter(Encoder.PIDSourceParameter.kRate);
        pid = new PIDController(1, 0.1, 0.02, encoder, this);//set/tune your pid gains as normal
        pid.setOutputRange(0.03, -0.03); //For our shooter, these values worked well with the default 50 ms loop time for the pid loop
        //They work even though the jaguar range is 1 to -1 because 0.03 gets added to itself many times
    }

    public void initDefaultCommand() {
    }

    public void pidWrite(double output) {
        jaguar.set(jaguar.get()+output);
    }
}

Last edited by apples000 : 26-12-2012 at 20:25. Reason: mistake
Reply With Quote