001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenix6.configs;
008
009import com.ctre.phoenix6.StatusCode;
010import com.ctre.phoenix6.configs.jni.ConfigJNI;
011import com.ctre.phoenix6.spns.*;
012import com.ctre.phoenix6.signals.*;
013
014/**
015 * Gains for the specified slot.
016 * <p>
017 * If this slot is selected, these gains are used in closed loop
018 * control requests.
019 */
020public class Slot0Configs implements ParentConfiguration
021{
022    /**
023     * Proportional Gain
024     * <p>
025     * The units for this gain is dependent on the control mode. Since
026     * this gain is multiplied by error in the input, the units should be
027     * defined as units of output per unit of input error. For example,
028     * when controlling velocity using a duty cycle closed loop, the units
029     * for the proportional gain will be duty cycle per rps of error, or
030     * 1/rps.
031     * 
032     * <ul>
033     *   <li> <b>Minimum Value:</b> 0
034     *   <li> <b>Maximum Value:</b> 3.4e+38
035     *   <li> <b>Default Value:</b> 0
036     *   <li> <b>Units:</b> 
037     * </ul>
038     */
039    public double kP = 0;
040    /**
041     * Integral Gain
042     * <p>
043     * The units for this gain is dependent on the control mode. Since
044     * this gain is multiplied by error in the input integrated over time
045     * (in units of seconds), the units should be defined as units of
046     * output per unit of integrated input error. For example, when
047     * controlling velocity using a duty cycle closed loop, integrating
048     * velocity over time results in rps * s = rotations. Therefore, the
049     * units for the integral gain will be duty cycle per rotation of
050     * accumulated error, or 1/rot.
051     * 
052     * <ul>
053     *   <li> <b>Minimum Value:</b> 0
054     *   <li> <b>Maximum Value:</b> 3.4e+38
055     *   <li> <b>Default Value:</b> 0
056     *   <li> <b>Units:</b> 
057     * </ul>
058     */
059    public double kI = 0;
060    /**
061     * Derivative Gain
062     * <p>
063     * The units for this gain is dependent on the control mode. Since
064     * this gain is multiplied by the derivative of error in the input
065     * with respect to time (in units of seconds), the units should be
066     * defined as units of output per unit of the differentiated input
067     * error. For example, when controlling velocity using a duty cycle
068     * closed loop, the derivative of velocity with respect to time is
069     * rps/s, which is acceleration. Therefore, the units for the
070     * derivative gain will be duty cycle per unit of acceleration error,
071     * or 1/(rps/s).
072     * 
073     * <ul>
074     *   <li> <b>Minimum Value:</b> 0
075     *   <li> <b>Maximum Value:</b> 3.4e+38
076     *   <li> <b>Default Value:</b> 0
077     *   <li> <b>Units:</b> 
078     * </ul>
079     */
080    public double kD = 0;
081    /**
082     * Static Feedforward Gain
083     * <p>
084     * This is added to the closed loop output. The unit for this constant
085     * is dependent on the control mode, typically fractional duty cycle,
086     * voltage, or torque current.
087     * <p>
088     * The sign is typically determined by reference velocity when using
089     * position, velocity, and Motion Magic® closed loop modes. However,
090     * when using position closed loop with zero velocity reference (no
091     * motion profiling), the application can instead use the position
092     * closed loop error by setting the Static Feedforward Sign
093     * configuration parameter.  When doing so, we recommend the minimal
094     * amount of kS, otherwise the motor output may dither when closed
095     * loop error is near zero.
096     * 
097     * <ul>
098     *   <li> <b>Minimum Value:</b> -512
099     *   <li> <b>Maximum Value:</b> 511
100     *   <li> <b>Default Value:</b> 0
101     *   <li> <b>Units:</b> 
102     * </ul>
103     */
104    public double kS = 0;
105    /**
106     * Velocity Feedforward Gain
107     * <p>
108     * The units for this gain is dependent on the control mode. Since
109     * this gain is multiplied by the requested velocity, the units should
110     * be defined as units of output per unit of requested input velocity.
111     * For example, when controlling velocity using a duty cycle closed
112     * loop, the units for the velocity feedfoward gain will be duty cycle
113     * per requested rps, or 1/rps.
114     * 
115     * <ul>
116     *   <li> <b>Minimum Value:</b> 0
117     *   <li> <b>Maximum Value:</b> 3.4e+38
118     *   <li> <b>Default Value:</b> 0
119     *   <li> <b>Units:</b> 
120     * </ul>
121     */
122    public double kV = 0;
123    /**
124     * Acceleration Feedforward Gain
125     * <p>
126     * The units for this gain is dependent on the control mode. Since
127     * this gain is multiplied by the requested acceleration, the units
128     * should be defined as units of output per unit of requested input
129     * acceleration. For example, when controlling velocity using a duty
130     * cycle closed loop, the units for the acceleration feedfoward gain
131     * will be duty cycle per requested rps/s, or 1/(rps/s).
132     * 
133     * <ul>
134     *   <li> <b>Minimum Value:</b> 0
135     *   <li> <b>Maximum Value:</b> 3.4e+38
136     *   <li> <b>Default Value:</b> 0
137     *   <li> <b>Units:</b> 
138     * </ul>
139     */
140    public double kA = 0;
141    /**
142     * Gravity Feedforward/Feedback Gain
143     * <p>
144     * This is added to the closed loop output. The sign is determined by
145     * GravityType. The unit for this constant is dependent on the control
146     * mode, typically fractional duty cycle, voltage, or torque current.
147     * 
148     * <ul>
149     *   <li> <b>Minimum Value:</b> -512
150     *   <li> <b>Maximum Value:</b> 511
151     *   <li> <b>Default Value:</b> 0
152     *   <li> <b>Units:</b> 
153     * </ul>
154     */
155    public double kG = 0;
156    /**
157     * Gravity Feedforward/Feedback Type
158     * <p>
159     * This determines the type of the gravity feedforward/feedback.
160     * <p>
161     * Choose Elevator_Static for systems where the gravity feedforward is
162     * constant, such as an elevator. The gravity feedforward output will
163     * always have the same sign.
164     * <p>
165     * Choose Arm_Cosine for systems where the gravity feedback is
166     * dependent on the angular position of the mechanism, such as an arm.
167     * The gravity feedback output will vary depending on the mechanism
168     * angular position. Note that the sensor offset and ratios must be
169     * configured so that the sensor reports a position of 0 when the
170     * mechanism is horizonal (parallel to the ground), and the reported
171     * sensor position is 1:1 with the mechanism.
172     * 
173     */
174    public GravityTypeValue GravityType = GravityTypeValue.Elevator_Static;
175    /**
176     * Static Feedforward Sign during position closed loop
177     * <p>
178     * This determines the sign of the applied kS during position
179     * closed-loop modes. The default behavior uses the velocity reference
180     * sign. This works well with velocity closed loop, Motion Magic®
181     * controls, and position closed loop when velocity reference is
182     * specified (motion profiling).
183     * <p>
184     * However, when using position closed loop with zero velocity
185     * reference (no motion profiling), the application may want to apply
186     * static feedforward based on the closed loop error sign instead.
187     * When doing so, we recommend the minimal amount of kS, otherwise the
188     * motor output may dither when closed loop error is near zero.
189     * 
190     */
191    public StaticFeedforwardSignValue StaticFeedforwardSign = StaticFeedforwardSignValue.UseVelocitySign;
192    
193    /**
194     * Modifies this configuration's kP parameter and returns itself for
195     * method-chaining and easier to use config API.
196     * <p>
197     * Proportional Gain
198     * <p>
199     * The units for this gain is dependent on the control mode. Since
200     * this gain is multiplied by error in the input, the units should be
201     * defined as units of output per unit of input error. For example,
202     * when controlling velocity using a duty cycle closed loop, the units
203     * for the proportional gain will be duty cycle per rps of error, or
204     * 1/rps.
205     * 
206     * <ul>
207     *   <li> <b>Minimum Value:</b> 0
208     *   <li> <b>Maximum Value:</b> 3.4e+38
209     *   <li> <b>Default Value:</b> 0
210     *   <li> <b>Units:</b> 
211     * </ul>
212     *
213     * @param newKP Parameter to modify
214     * @return Itself
215     */
216    public Slot0Configs withKP(double newKP)
217    {
218        kP = newKP;
219        return this;
220    }
221    /**
222     * Modifies this configuration's kI parameter and returns itself for
223     * method-chaining and easier to use config API.
224     * <p>
225     * Integral Gain
226     * <p>
227     * The units for this gain is dependent on the control mode. Since
228     * this gain is multiplied by error in the input integrated over time
229     * (in units of seconds), the units should be defined as units of
230     * output per unit of integrated input error. For example, when
231     * controlling velocity using a duty cycle closed loop, integrating
232     * velocity over time results in rps * s = rotations. Therefore, the
233     * units for the integral gain will be duty cycle per rotation of
234     * accumulated error, or 1/rot.
235     * 
236     * <ul>
237     *   <li> <b>Minimum Value:</b> 0
238     *   <li> <b>Maximum Value:</b> 3.4e+38
239     *   <li> <b>Default Value:</b> 0
240     *   <li> <b>Units:</b> 
241     * </ul>
242     *
243     * @param newKI Parameter to modify
244     * @return Itself
245     */
246    public Slot0Configs withKI(double newKI)
247    {
248        kI = newKI;
249        return this;
250    }
251    /**
252     * Modifies this configuration's kD parameter and returns itself for
253     * method-chaining and easier to use config API.
254     * <p>
255     * Derivative Gain
256     * <p>
257     * The units for this gain is dependent on the control mode. Since
258     * this gain is multiplied by the derivative of error in the input
259     * with respect to time (in units of seconds), the units should be
260     * defined as units of output per unit of the differentiated input
261     * error. For example, when controlling velocity using a duty cycle
262     * closed loop, the derivative of velocity with respect to time is
263     * rps/s, which is acceleration. Therefore, the units for the
264     * derivative gain will be duty cycle per unit of acceleration error,
265     * or 1/(rps/s).
266     * 
267     * <ul>
268     *   <li> <b>Minimum Value:</b> 0
269     *   <li> <b>Maximum Value:</b> 3.4e+38
270     *   <li> <b>Default Value:</b> 0
271     *   <li> <b>Units:</b> 
272     * </ul>
273     *
274     * @param newKD Parameter to modify
275     * @return Itself
276     */
277    public Slot0Configs withKD(double newKD)
278    {
279        kD = newKD;
280        return this;
281    }
282    /**
283     * Modifies this configuration's kS parameter and returns itself for
284     * method-chaining and easier to use config API.
285     * <p>
286     * Static Feedforward Gain
287     * <p>
288     * This is added to the closed loop output. The unit for this constant
289     * is dependent on the control mode, typically fractional duty cycle,
290     * voltage, or torque current.
291     * <p>
292     * The sign is typically determined by reference velocity when using
293     * position, velocity, and Motion Magic® closed loop modes. However,
294     * when using position closed loop with zero velocity reference (no
295     * motion profiling), the application can instead use the position
296     * closed loop error by setting the Static Feedforward Sign
297     * configuration parameter.  When doing so, we recommend the minimal
298     * amount of kS, otherwise the motor output may dither when closed
299     * loop error is near zero.
300     * 
301     * <ul>
302     *   <li> <b>Minimum Value:</b> -512
303     *   <li> <b>Maximum Value:</b> 511
304     *   <li> <b>Default Value:</b> 0
305     *   <li> <b>Units:</b> 
306     * </ul>
307     *
308     * @param newKS Parameter to modify
309     * @return Itself
310     */
311    public Slot0Configs withKS(double newKS)
312    {
313        kS = newKS;
314        return this;
315    }
316    /**
317     * Modifies this configuration's kV parameter and returns itself for
318     * method-chaining and easier to use config API.
319     * <p>
320     * Velocity Feedforward Gain
321     * <p>
322     * The units for this gain is dependent on the control mode. Since
323     * this gain is multiplied by the requested velocity, the units should
324     * be defined as units of output per unit of requested input velocity.
325     * For example, when controlling velocity using a duty cycle closed
326     * loop, the units for the velocity feedfoward gain will be duty cycle
327     * per requested rps, or 1/rps.
328     * 
329     * <ul>
330     *   <li> <b>Minimum Value:</b> 0
331     *   <li> <b>Maximum Value:</b> 3.4e+38
332     *   <li> <b>Default Value:</b> 0
333     *   <li> <b>Units:</b> 
334     * </ul>
335     *
336     * @param newKV Parameter to modify
337     * @return Itself
338     */
339    public Slot0Configs withKV(double newKV)
340    {
341        kV = newKV;
342        return this;
343    }
344    /**
345     * Modifies this configuration's kA parameter and returns itself for
346     * method-chaining and easier to use config API.
347     * <p>
348     * Acceleration Feedforward Gain
349     * <p>
350     * The units for this gain is dependent on the control mode. Since
351     * this gain is multiplied by the requested acceleration, the units
352     * should be defined as units of output per unit of requested input
353     * acceleration. For example, when controlling velocity using a duty
354     * cycle closed loop, the units for the acceleration feedfoward gain
355     * will be duty cycle per requested rps/s, or 1/(rps/s).
356     * 
357     * <ul>
358     *   <li> <b>Minimum Value:</b> 0
359     *   <li> <b>Maximum Value:</b> 3.4e+38
360     *   <li> <b>Default Value:</b> 0
361     *   <li> <b>Units:</b> 
362     * </ul>
363     *
364     * @param newKA Parameter to modify
365     * @return Itself
366     */
367    public Slot0Configs withKA(double newKA)
368    {
369        kA = newKA;
370        return this;
371    }
372    /**
373     * Modifies this configuration's kG parameter and returns itself for
374     * method-chaining and easier to use config API.
375     * <p>
376     * Gravity Feedforward/Feedback Gain
377     * <p>
378     * This is added to the closed loop output. The sign is determined by
379     * GravityType. The unit for this constant is dependent on the control
380     * mode, typically fractional duty cycle, voltage, or torque current.
381     * 
382     * <ul>
383     *   <li> <b>Minimum Value:</b> -512
384     *   <li> <b>Maximum Value:</b> 511
385     *   <li> <b>Default Value:</b> 0
386     *   <li> <b>Units:</b> 
387     * </ul>
388     *
389     * @param newKG Parameter to modify
390     * @return Itself
391     */
392    public Slot0Configs withKG(double newKG)
393    {
394        kG = newKG;
395        return this;
396    }
397    /**
398     * Modifies this configuration's GravityType parameter and returns itself for
399     * method-chaining and easier to use config API.
400     * <p>
401     * Gravity Feedforward/Feedback Type
402     * <p>
403     * This determines the type of the gravity feedforward/feedback.
404     * <p>
405     * Choose Elevator_Static for systems where the gravity feedforward is
406     * constant, such as an elevator. The gravity feedforward output will
407     * always have the same sign.
408     * <p>
409     * Choose Arm_Cosine for systems where the gravity feedback is
410     * dependent on the angular position of the mechanism, such as an arm.
411     * The gravity feedback output will vary depending on the mechanism
412     * angular position. Note that the sensor offset and ratios must be
413     * configured so that the sensor reports a position of 0 when the
414     * mechanism is horizonal (parallel to the ground), and the reported
415     * sensor position is 1:1 with the mechanism.
416     * 
417     *
418     * @param newGravityType Parameter to modify
419     * @return Itself
420     */
421    public Slot0Configs withGravityType(GravityTypeValue newGravityType)
422    {
423        GravityType = newGravityType;
424        return this;
425    }
426    /**
427     * Modifies this configuration's StaticFeedforwardSign parameter and returns itself for
428     * method-chaining and easier to use config API.
429     * <p>
430     * Static Feedforward Sign during position closed loop
431     * <p>
432     * This determines the sign of the applied kS during position
433     * closed-loop modes. The default behavior uses the velocity reference
434     * sign. This works well with velocity closed loop, Motion Magic®
435     * controls, and position closed loop when velocity reference is
436     * specified (motion profiling).
437     * <p>
438     * However, when using position closed loop with zero velocity
439     * reference (no motion profiling), the application may want to apply
440     * static feedforward based on the closed loop error sign instead.
441     * When doing so, we recommend the minimal amount of kS, otherwise the
442     * motor output may dither when closed loop error is near zero.
443     * 
444     *
445     * @param newStaticFeedforwardSign Parameter to modify
446     * @return Itself
447     */
448    public Slot0Configs withStaticFeedforwardSign(StaticFeedforwardSignValue newStaticFeedforwardSign)
449    {
450        StaticFeedforwardSign = newStaticFeedforwardSign;
451        return this;
452    }
453
454    public static Slot0Configs from(SlotConfigs value)
455    {
456        return new Slot0Configs() {{
457            kP = value.kP;
458            kI = value.kI;
459            kD = value.kD;
460            kS = value.kS;
461            kV = value.kV;
462            kA = value.kA;
463            kG = value.kG;
464            GravityType = value.GravityType;
465            StaticFeedforwardSign = value.StaticFeedforwardSign;
466        }};
467    }
468
469    @Override
470    public String toString()
471    {
472        String ss = "Config Group: Slot0\n";
473        ss += "Name: \"kP\" Value: \"" + kP + "\"" + "\n";
474        ss += "Name: \"kI\" Value: \"" + kI + "\"" + "\n";
475        ss += "Name: \"kD\" Value: \"" + kD + "\"" + "\n";
476        ss += "Name: \"kS\" Value: \"" + kS + "\"" + "\n";
477        ss += "Name: \"kV\" Value: \"" + kV + "\"" + "\n";
478        ss += "Name: \"kA\" Value: \"" + kA + "\"" + "\n";
479        ss += "Name: \"kG\" Value: \"" + kG + "\"" + "\n";
480        ss += "Name: \"GravityType\" Value: \"" + GravityType + "\"" + "\n";
481        ss += "Name: \"StaticFeedforwardSign\" Value: \"" + StaticFeedforwardSign + "\"" + "\n";
482        return ss;
483    }
484
485    /**
486     *
487     */
488    public StatusCode deserialize(String to_deserialize)
489    {
490        kP = ConfigJNI.Deserializedouble(SpnValue.Slot0_kP.value, to_deserialize);
491        kI = ConfigJNI.Deserializedouble(SpnValue.Slot0_kI.value, to_deserialize);
492        kD = ConfigJNI.Deserializedouble(SpnValue.Slot0_kD.value, to_deserialize);
493        kS = ConfigJNI.Deserializedouble(SpnValue.Slot0_kS.value, to_deserialize);
494        kV = ConfigJNI.Deserializedouble(SpnValue.Slot0_kV.value, to_deserialize);
495        kA = ConfigJNI.Deserializedouble(SpnValue.Slot0_kA.value, to_deserialize);
496        kG = ConfigJNI.Deserializedouble(SpnValue.Slot0_kG.value, to_deserialize);
497        GravityType = GravityTypeValue.valueOf(ConfigJNI.Deserializeint(SpnValue.Slot0_kG_Type.value, to_deserialize));
498        StaticFeedforwardSign = StaticFeedforwardSignValue.valueOf(ConfigJNI.Deserializeint(SpnValue.Slot0_kS_Sign.value, to_deserialize));
499        return  StatusCode.OK;
500    }
501
502    /**
503     *
504     */
505    public String serialize()
506    {
507        String ss = "";
508        ss += ConfigJNI.Serializedouble(SpnValue.Slot0_kP.value, kP);
509        ss += ConfigJNI.Serializedouble(SpnValue.Slot0_kI.value, kI);
510        ss += ConfigJNI.Serializedouble(SpnValue.Slot0_kD.value, kD);
511        ss += ConfigJNI.Serializedouble(SpnValue.Slot0_kS.value, kS);
512        ss += ConfigJNI.Serializedouble(SpnValue.Slot0_kV.value, kV);
513        ss += ConfigJNI.Serializedouble(SpnValue.Slot0_kA.value, kA);
514        ss += ConfigJNI.Serializedouble(SpnValue.Slot0_kG.value, kG);
515        ss += ConfigJNI.Serializeint(SpnValue.Slot0_kG_Type.value, GravityType.value);
516        ss += ConfigJNI.Serializeint(SpnValue.Slot0_kS_Sign.value, StaticFeedforwardSign.value);
517        return ss;
518    }
519}
520