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