<commit_before>package org.usfirst.frc.team339.Utils;

import com.ctre.CANTalon;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class CANPIDTuner
{
private int CANID = 0;

private CANTalon tunedMotorController = null;

private double P;

private double I;

private double D;

private double setpoint;

private boolean smartDashboard;

public CANPIDTuner (int canId, CANTalon talon,
        boolean smartDashboardAvailable)
{
    this.tunedMotorController = talon;
    this.CANID = canId;
    this.P = 0;
    this.I = 0;
    this.D = 0;
    this.setpoint = 0;
    this.smartDashboard = smartDashboardAvailable;
}

public void setupMotorController ()
{

}

public void setupDashboard ()
{
    SmartDashboard.putNumber("P", this.P);
    SmartDashboard.putNumber("I", this.I);
    SmartDashboard.putNumber("D", this.D);
    SmartDashboard.putNumber("Setpoint", this.setpoint);
    SmartDashboard.putNumber("Error",
            this.setpoint - this.tunedMotorController.getSpeed());
    SmartDashboard.putNumber("Speed", this.tunedMotorController.getSpeed())
}


}<commit_msg>Finished up and documented CANPIDTuner, rebuild javadoc if you want it.<commit_after>package org.usfirst.frc.team339.Utils;

import com.ctre.CANTalon;
import com.ctre.CANTalon.FeedbackDevice;
import com.ctre.CANTalon.TalonControlMode;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

/**
 * Class to make tuning PID loops easier, either by the smartdashboard, or in
 * the code via some other sensor (you'll have to write the code in teleop or
 * something for another sensor)
 * 
 * @author <NAME>
 * @written 1/21/17
 */
public class CANPIDTuner
{
private int CANID = 0;// TODO either use or remove

private CANTalon tunedMotorController = null;

private double P;

private double I;

private double D;

private double setpoint;

private boolean smartDashboard;

private double errorThresh = 20;

/**
 * Constructs a PID tuner object for a specific CANTalon motor.
 * 
 * @param canId
 *            Not currently used. There for future potential use.
 * @param talon
 *            The motor we're tuning on. It needs to have some form of rate
 *            sensor attached to it's breakout board.
 * @param smartDashboardAvailable
 *            If the smartdashboard is setup and used. We won't bother updating
 *            it if we have it.
 * @param errorThreshold
 *            The maximum error we find it acceptable to have (always positive,
 *            we use absolute value of the actual error).
 */
public CANPIDTuner (int canId, CANTalon talon,
        boolean smartDashboardAvailable, double errorThreshold)// TODO remove
                                                               // the
                                                               // smartDashboard
                                                               // thingy
{
    this.tunedMotorController = talon;
    this.CANID = canId;
    this.P = 0;
    this.I = 0;
    this.D = 0;
    this.setpoint = 0;
    this.smartDashboard = smartDashboardAvailable;
    this.errorThresh = errorThreshold;
}

private boolean wasIncorrect = false;

private Timer time = new Timer();

// TODO reference CTRE doc
/**
 * Initializes all the CAN stuff for the motor controller.
 * 
 * @param feedbackType
 *            The type of feedback device in the MotorController.
 * @param tunetype
 *            Speed or position. See the CTRE doc for more info
 * @param codesPerRev
 *            The number of signals the feedback devices gives per rotation, so
 *            the setpoint can function in rpm or revolutions.
 * @param reverseSensor
 *            Is the feedback device reversed.
 */
public void setupMotorController (FeedbackDevice feedbackType,
        TalonControlMode tunetype, int codesPerRev,
        boolean reverseSensor)
{
    this.tunedMotorController
            .setFeedbackDevice(feedbackType);
    this.tunedMotorController.changeControlMode(tunetype);
    this.tunedMotorController.configEncoderCodesPerRev(codesPerRev);
    this.tunedMotorController.reverseSensor(reverseSensor);
    this.tunedMotorController.setProfile(0);
    this.tunedMotorController.configPeakOutputVoltage(12f, 0f);
    this.tunedMotorController.configNominalOutputVoltage(0f, 0f);
    wasIncorrect = false;
    time.stop();
    time.reset();
}

/**
 * Initializes everything on the smartDashboard if we have one. If not, does
 * nothing.
 * Puts P, I, D, Setpoint, Error, and Speed.
 * P, I, D, and Setpoint are all editable and affect the function of the code.
 * Setpoint is in units of RPM or revolutions if you provided a number other
 * than 1 for the codesPerRev argument to setupMotorController.
 */
public void setupDashboard ()
{
    if (this.smartDashboard)
        {
        SmartDashboard.putNumber("P", this.P);
        SmartDashboard.putNumber("I", this.I);
        SmartDashboard.putNumber("D", this.D);
        SmartDashboard.putNumber("Setpoint", this.setpoint);
        SmartDashboard.putNumber("Error",
                this.setpoint - this.tunedMotorController.getSpeed());
        SmartDashboard.putNumber("Speed",
                this.tunedMotorController.getSpeed());
        }
}

/**
 * Gets information from the smartDashboard, puts more info back onto the
 * smartDashboard, and updates the values on the tuned motor controller.
 * Prints out when an error beyond the provided threshold is detected, and the
 * time for the PID loop to correct for it.
 */
public void update ()
{
    if (this.smartDashboard)
        {
        P = SmartDashboard.getNumber("P", 0);
        I = SmartDashboard.getNumber("I", 0);
        D = SmartDashboard.getNumber("D", 0);
        this.setpoint = SmartDashboard.getNumber("Setpoint", 0);
        SmartDashboard.putNumber("Error",
                this.setpoint - this.tunedMotorController.getSpeed());
        SmartDashboard.putNumber("Speed",
                this.tunedMotorController.getSpeed());
        }
    this.tunedMotorController.setPID(this.P, this.I, this.D);
    if (Math.abs(
            this.tunedMotorController
                    .getClosedLoopError()) >= this.errorThresh
            && wasIncorrect == false)
        {
        wasIncorrect = true;
        time.reset();
        time.start();
        System.out.println("Error detected, timing...");
        }
    if (Math.abs(
            this.tunedMotorController
                    .getClosedLoopError()) <= this.errorThresh
            && wasIncorrect == true)
        {
        wasIncorrect = false;
        time.stop();
        System.out.println("Time to correct error: " + time.get());
        }
}

/**
 * 
 * @param P
 *            The proportional constant for the PID loop.
 */
public void setP (double P)
{
    this.P = P;
}

/**
 * 
 * @param I
 *            The integral constant for the PID loop.
 */
public void setI (double I)
{
    this.I = I;
}

/**
 * 
 * @param D
 *            The derivative constant for the PID loop.
 */
public void setD (double D)
{
    this.D = D;
}

/**
 * 
 * @param P
 *            The proportional constant for the PID loop.
 * @param I
 *            The integral constant for the PID loop.
 * @param D
 *            The derivative constant for the PID loop.
 */
public void setPID (double p, double i, double d)
{
    this.P = p;
    this.I = i;
    this.D = d;
}

/**
 * 
 * @return
 *         The current proportional constant for the PID loop
 */
public double getP ()
{
    return this.P;
}

/**
 * 
 * @return
 *         The current integral constant for the PID loop
 */
public double getI ()
{
    return this.I;
}

/**
 * 
 * @return
 *         The current derivative constant for the PID loop
 */
public double getD ()
{
    return this.D;
}

/**
 * 
 * @param errThresh
 *            Sets the error threshold that we find acceptable. Always positive.
 */
public void setErrorThreshold (double errThresh)
{
    this.errorThresh = errThresh;
}

/**
 * 
 * @return
 *         The current error threshold.
 */
public double getErrorThreshold ()
{
    return this.errorThresh;
}

/**
 * 
 * @param setpoint
 *            The target velocity or position of the motor being tuned.
 *            in revolutions or rpm depending on PID type.
 */
public void setSetpoint (double setpoint)
{
    this.setpoint = setpoint;
}

/**
 * 
 * @return
 *         The current setpoint.
 */
public double getSetpoint ()
{
    return this.setpoint;
}

}
{'fraction_non_alphanumeric': 0.09523244781783681, 'fraction_numerical': 0.004032258064516129, 'mean_word_length': 2.4890359950351675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2645843', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2491, 'n_words': 744}