View Single Post
  #1   Spotlight this post!  
Unread 12-02-2013, 19:53
geel9 geel9 is offline
Registered User
FRC #1317
 
Join Date: Feb 2011
Location: Westerville
Posts: 5
geel9 is an unknown quantity at this point
PID loops using rate

Hey,

We're trying to get our PID loops to control the motors' rate, as opposed to their distance.

Here is our code:

Code:
//Initializes the motors.
    private final SpeedController left = new Jaguar(1);
    private final SpeedController right = new Jaguar(2);
    
    //Initializes the Encoders.
    private final Encoder leftEncoder = new Encoder(1, 2);
    private final Encoder rightEncoder = new Encoder(3, 4);
    
    //Proportional, Integral, and Dervative constants.
    //These values will need to be tuned for your robot.
    private final double Kp = 0.1;
    private final double Ki = 0.001;
    private final double Kd = 0;
    
    //This must be fully initialized in the constructor, after the settings
    //for the encoders have been done.
    private final PIDController leftPID;
    private final PIDController rightPID;

    public PIDDrive() {
        //Sets the distance per pulse in inches.
        leftEncoder.setDistancePerPulse(-1);
        rightEncoder.setDistancePerPulse(-1);

        
        //Starts the encoders.
        leftEncoder.start();
        rightEncoder.start();
        
        //Sets the encoders to use distance for PID.
        //If this is not done, the robot may not go anywhere.
        //It is also possible to use rate, by changing kDistance to kRate.
        leftEncoder.setPIDSourceParameter(Encoder.PIDSourceParameter.kRate);
        rightEncoder.setPIDSourceParameter(Encoder.PIDSourceParameter.kRate);
        
        //Initializes the PID Controllers
        leftPID = new PIDController(Kp, Ki, Kd, leftEncoder, left);
        rightPID = new PIDController(Kp, Ki, Kd, rightEncoder, right);
        
        leftPID.setPercentTolerance(25);
        rightPID.setPercentTolerance(25);
        
        leftPID.setContinuous();
        rightPID.setContinuous();
        
        //Enables the PID Controllers.
        leftPID.enable();
        rightPID.enable();
        

    }

    public static void WriteDriverStationLine(DriverStationLCD.Line line, String text){
        DriverStationLCD.getInstance().println(line, 1, text);
        DriverStationLCD.getInstance().updateLCD();
    }
    /**
     * This function is called once each time the robot enters operator control.
     * Teleop commands are put in here
     */
    public void operatorControl() {
        leftPID.setSetpoint(1000);
        rightPID.setSetpoint(1000);
        
        while(isOperatorControl()){
            
            WriteDriverStationLine(DriverStationLCD.Line.kUser1, "E: " + leftPID.getError());
            WriteDriverStationLine(DriverStationLCD.Line.kUser2, "E2: " + rightPID.getError());
            WriteDriverStationLine(DriverStationLCD.Line.kUser3, "D: " + leftEncoder.getRate());
            WriteDriverStationLine(DriverStationLCD.Line.kUser4, "D2: " + rightEncoder.getRate());
        }
The wheels spin forward, then rapidly switch directions.
Reply With Quote