001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.motorcontrol.can;
003
004/**
005 * Configurables available to a slot
006 */
007public class SlotConfiguration{
008
009    /**
010     * P Gain
011         *
012         * This is multiplied by closed loop error in sensor units.  
013         * Note the closed loop output interprets a final value of 1023 as full output.  
014         * So use a gain of '0.25' to get full output if err is 4096u (Mag Encoder 1 rotation)
015     */
016    public double kP;
017    /**
018     * I Gain
019     *
020         * This is multiplied by accumulated closed loop error in sensor units every PID Loop.  
021         * Note the closed loop output interprets a final value of 1023 as full output.  
022         * So use a gain of '0.00025' to get full output if err is 4096u for 1000 loops (accumulater holds 4,096,000),
023         * [which is equivalent to one CTRE mag encoder rotation for 1000 milliseconds].
024     */
025    public double kI;
026    /**
027     * D Gain
028         *
029         * This is multiplied by derivative error (sensor units per PID loop, typically 1ms).  
030         * Note the closed loop output interprets a final value of 1023 as full output.  
031         * So use a gain of '250' to get full output if derr is 4096u (Mag Encoder 1 rotation) per 1000 loops (typ 1 sec)
032     */
033    public double kD;
034    /**
035     * F Gain
036         *
037         * See documentation for calculation details.  
038         * If using velocity, motion magic, or motion profile, 
039         * use (1023 * duty-cycle / sensor-velocity-sensor-units-per-100ms).
040         *
041     */
042    public double kF;
043    /**
044     * Integral zone (in native units)
045         * 
046         * If the (absolute) closed-loop error is outside of this zone, integral
047         * accumulator is automatically cleared. This ensures than integral wind up
048         * events will stop after the sensor gets far enough from its target.
049         *
050     */
051    public double integralZone;
052    /**
053     * Allowable closed loop error to neutral (in native units)
054     */
055    public double allowableClosedloopError;
056    /**
057     * Max integral accumulator (in native units)
058     */
059    public double maxIntegralAccumulator;
060    /**
061     * Peak output from closed loop [0,1]
062     */
063    public double closedLoopPeakOutput;
064    /**
065     * Desired period of closed loop [1,64]ms
066     */
067    public int closedLoopPeriod;
068
069    public SlotConfiguration() {
070
071        kP = 0.0;
072        kI = 0.0;
073        kD = 0.0;
074        kF = 0.0;
075        integralZone = 0;
076        allowableClosedloopError = 0;
077        maxIntegralAccumulator = 0.0;
078        closedLoopPeakOutput = 1.0;
079        closedLoopPeriod = 1;
080    }
081
082    /**
083     * @return String representation of configs
084     */
085        public String toString() {
086                return toString("");
087        }
088
089    /**
090     * @param prependString
091     *              String to prepend to configs
092     * @return String representation of configs
093     */
094    public String toString(String prependString) {
095
096        String retstr = prependString + ".kP = " + String.valueOf(kP) + ";\n";
097        retstr += prependString + ".kI = " + String.valueOf(kI) + ";\n";
098        retstr += prependString + ".kD = " + String.valueOf(kD) + ";\n";
099        retstr += prependString + ".kF = " + String.valueOf(kF) + ";\n";
100        retstr += prependString + ".integralZone = " + String.valueOf(integralZone) + ";\n";
101        retstr += prependString + ".allowableClosedloopError = " + String.valueOf(allowableClosedloopError) + ";\n";
102        retstr += prependString + ".maxIntegralAccumulator = " + String.valueOf(maxIntegralAccumulator) + ";\n";
103        retstr += prependString + ".closedLoopPeakOutput = " + String.valueOf(closedLoopPeakOutput) + ";\n";
104        retstr += prependString + ".closedLoopPeriod = " + String.valueOf(closedLoopPeriod) + ";\n";
105
106        return retstr;
107
108    }
109
110} // class SlotConfiguration
111