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 rot
068     * per sec², which is acceleration. Therefore, the units for the
069     * derivative gain will be duty cycle per unit of acceleration error,
070     * or 1/(rot per sec²).
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 rot per sec², or 1/(rot per sec²).
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 using the minimal amount of kS,
187     * otherwise the motor output may dither when closed loop error is
188     * 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, or 1/rps.
204     * 
205     * <ul>
206     *   <li> <b>Minimum Value:</b> 0
207     *   <li> <b>Maximum Value:</b> 3.4e+38
208     *   <li> <b>Default Value:</b> 0
209     *   <li> <b>Units:</b> 
210     * </ul>
211     *
212     * @param newKP Parameter to modify
213     * @return Itself
214     */
215    public Slot1Configs withKP(double newKP)
216    {
217        kP = newKP;
218        return this;
219    }
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 Slot1Configs withKI(double newKI)
247    {
248        kI = newKI;
249        return this;
250    }
251    
252    /**
253     * Modifies this configuration's kD parameter and returns itself for
254     * method-chaining and easier to use config API.
255     * <p>
256     * Derivative Gain.
257     * <p>
258     * The units for this gain is dependent on the control mode. Since
259     * this gain is multiplied by the derivative of error in the input
260     * with respect to time (in units of seconds), the units should be
261     * defined as units of output per unit of the differentiated input
262     * error. For example, when controlling velocity using a duty cycle
263     * closed loop, the derivative of velocity with respect to time is rot
264     * per sec², which is acceleration. Therefore, the units for the
265     * derivative gain will be duty cycle per unit of acceleration error,
266     * or 1/(rot per sec²).
267     * 
268     * <ul>
269     *   <li> <b>Minimum Value:</b> 0
270     *   <li> <b>Maximum Value:</b> 3.4e+38
271     *   <li> <b>Default Value:</b> 0
272     *   <li> <b>Units:</b> 
273     * </ul>
274     *
275     * @param newKD Parameter to modify
276     * @return Itself
277     */
278    public Slot1Configs withKD(double newKD)
279    {
280        kD = newKD;
281        return this;
282    }
283    
284    /**
285     * Modifies this configuration's kS parameter and returns itself for
286     * method-chaining and easier to use config API.
287     * <p>
288     * Static Feedforward Gain.
289     * <p>
290     * This is added to the closed loop output. The unit for this constant
291     * is dependent on the control mode, typically fractional duty cycle,
292     * voltage, or torque current.
293     * <p>
294     * The sign is typically determined by reference velocity when using
295     * position, velocity, and Motion Magic® closed loop modes. However,
296     * when using position closed loop with zero velocity reference (no
297     * motion profiling), the application can instead use the position
298     * closed loop error by setting the Static Feedforward Sign
299     * configuration parameter.  When doing so, we recommend the minimal
300     * amount of kS, otherwise the motor output may dither when closed
301     * loop error is near zero.
302     * 
303     * <ul>
304     *   <li> <b>Minimum Value:</b> -512
305     *   <li> <b>Maximum Value:</b> 511
306     *   <li> <b>Default Value:</b> 0
307     *   <li> <b>Units:</b> 
308     * </ul>
309     *
310     * @param newKS Parameter to modify
311     * @return Itself
312     */
313    public Slot1Configs withKS(double newKS)
314    {
315        kS = newKS;
316        return this;
317    }
318    
319    /**
320     * Modifies this configuration's kV parameter and returns itself for
321     * method-chaining and easier to use config API.
322     * <p>
323     * Velocity Feedforward Gain.
324     * <p>
325     * The units for this gain is dependent on the control mode. Since
326     * this gain is multiplied by the requested velocity, the units should
327     * be defined as units of output per unit of requested input velocity.
328     * For example, when controlling velocity using a duty cycle closed
329     * loop, the units for the velocity feedfoward gain will be duty cycle
330     * per requested rps, or 1/rps.
331     * 
332     * <ul>
333     *   <li> <b>Minimum Value:</b> 0
334     *   <li> <b>Maximum Value:</b> 3.4e+38
335     *   <li> <b>Default Value:</b> 0
336     *   <li> <b>Units:</b> 
337     * </ul>
338     *
339     * @param newKV Parameter to modify
340     * @return Itself
341     */
342    public Slot1Configs withKV(double newKV)
343    {
344        kV = newKV;
345        return this;
346    }
347    
348    /**
349     * Modifies this configuration's kA parameter and returns itself for
350     * method-chaining and easier to use config API.
351     * <p>
352     * Acceleration Feedforward Gain.
353     * <p>
354     * The units for this gain is dependent on the control mode. Since
355     * this gain is multiplied by the requested acceleration, the units
356     * should be defined as units of output per unit of requested input
357     * acceleration. For example, when controlling velocity using a duty
358     * cycle closed loop, the units for the acceleration feedfoward gain
359     * will be duty cycle per requested rot per sec², or 1/(rot per sec²).
360     * 
361     * <ul>
362     *   <li> <b>Minimum Value:</b> 0
363     *   <li> <b>Maximum Value:</b> 3.4e+38
364     *   <li> <b>Default Value:</b> 0
365     *   <li> <b>Units:</b> 
366     * </ul>
367     *
368     * @param newKA Parameter to modify
369     * @return Itself
370     */
371    public Slot1Configs withKA(double newKA)
372    {
373        kA = newKA;
374        return this;
375    }
376    
377    /**
378     * Modifies this configuration's kG parameter and returns itself for
379     * method-chaining and easier to use config API.
380     * <p>
381     * Gravity Feedforward/Feedback Gain.
382     * <p>
383     * This is added to the closed loop output. The sign is determined by
384     * GravityType. The unit for this constant is dependent on the control
385     * mode, typically fractional duty cycle, voltage, or torque current.
386     * 
387     * <ul>
388     *   <li> <b>Minimum Value:</b> -512
389     *   <li> <b>Maximum Value:</b> 511
390     *   <li> <b>Default Value:</b> 0
391     *   <li> <b>Units:</b> 
392     * </ul>
393     *
394     * @param newKG Parameter to modify
395     * @return Itself
396     */
397    public Slot1Configs withKG(double newKG)
398    {
399        kG = newKG;
400        return this;
401    }
402    
403    /**
404     * Modifies this configuration's GravityType parameter and returns itself for
405     * method-chaining and easier to use config API.
406     * <p>
407     * Gravity Feedforward/Feedback Type.
408     * <p>
409     * This determines the type of the gravity feedforward/feedback.
410     * <p>
411     * Choose Elevator_Static for systems where the gravity feedforward is
412     * constant, such as an elevator. The gravity feedforward output will
413     * always be positive.
414     * <p>
415     * Choose Arm_Cosine for systems where the gravity feedback is
416     * dependent on the angular position of the mechanism, such as an arm.
417     * The gravity feedback output will vary depending on the mechanism
418     * angular position. Note that the sensor offset and ratios must be
419     * configured so that the sensor position is 0 when the mechanism is
420     * horizonal, and one rotation of the mechanism corresponds to one
421     * rotation of the sensor position.
422     * 
423     *
424     * @param newGravityType Parameter to modify
425     * @return Itself
426     */
427    public Slot1Configs withGravityType(GravityTypeValue newGravityType)
428    {
429        GravityType = newGravityType;
430        return this;
431    }
432    
433    /**
434     * Modifies this configuration's StaticFeedforwardSign parameter and returns itself for
435     * method-chaining and easier to use config API.
436     * <p>
437     * Static Feedforward Sign during position closed loop.
438     * <p>
439     * This determines the sign of the applied kS during position
440     * closed-loop modes. The default behavior uses the velocity reference
441     * sign. This works well with velocity closed loop, Motion Magic®
442     * controls, and position closed loop when velocity reference is
443     * specified (motion profiling).
444     * <p>
445     * However, when using position closed loop with zero velocity
446     * reference (no motion profiling), the application may want to apply
447     * static feedforward based on the closed loop error sign instead.
448     * When doing so, we recommend using the minimal amount of kS,
449     * otherwise the motor output may dither when closed loop error is
450     * near zero.
451     * 
452     *
453     * @param newStaticFeedforwardSign Parameter to modify
454     * @return Itself
455     */
456    public Slot1Configs withStaticFeedforwardSign(StaticFeedforwardSignValue newStaticFeedforwardSign)
457    {
458        StaticFeedforwardSign = newStaticFeedforwardSign;
459        return this;
460    }
461
462    public static Slot1Configs from(SlotConfigs value)
463    {
464        return new Slot1Configs() {{
465            kP = value.kP;
466            kI = value.kI;
467            kD = value.kD;
468            kS = value.kS;
469            kV = value.kV;
470            kA = value.kA;
471            kG = value.kG;
472            GravityType = value.GravityType;
473            StaticFeedforwardSign = value.StaticFeedforwardSign;
474        }};
475    }
476
477    @Override
478    public String toString()
479    {
480        String ss = "Config Group: Slot1\n";
481        ss += "    kP: " + kP + "\n";
482        ss += "    kI: " + kI + "\n";
483        ss += "    kD: " + kD + "\n";
484        ss += "    kS: " + kS + "\n";
485        ss += "    kV: " + kV + "\n";
486        ss += "    kA: " + kA + "\n";
487        ss += "    kG: " + kG + "\n";
488        ss += "    GravityType: " + GravityType + "\n";
489        ss += "    StaticFeedforwardSign: " + StaticFeedforwardSign + "\n";
490        return ss;
491    }
492
493    /**
494     *
495     */
496    public StatusCode deserialize(String to_deserialize)
497    {
498        kP = ConfigJNI.Deserializedouble(SpnValue.Slot1_kP.value, to_deserialize);
499        kI = ConfigJNI.Deserializedouble(SpnValue.Slot1_kI.value, to_deserialize);
500        kD = ConfigJNI.Deserializedouble(SpnValue.Slot1_kD.value, to_deserialize);
501        kS = ConfigJNI.Deserializedouble(SpnValue.Slot1_kS.value, to_deserialize);
502        kV = ConfigJNI.Deserializedouble(SpnValue.Slot1_kV.value, to_deserialize);
503        kA = ConfigJNI.Deserializedouble(SpnValue.Slot1_kA.value, to_deserialize);
504        kG = ConfigJNI.Deserializedouble(SpnValue.Slot1_kG.value, to_deserialize);
505        GravityType = GravityTypeValue.valueOf(ConfigJNI.Deserializeint(SpnValue.Slot1_kG_Type.value, to_deserialize));
506        StaticFeedforwardSign = StaticFeedforwardSignValue.valueOf(ConfigJNI.Deserializeint(SpnValue.Slot1_kS_Sign.value, to_deserialize));
507        return  StatusCode.OK;
508    }
509
510    /**
511     *
512     */
513    public String serialize()
514    {
515        String ss = "";
516        ss += ConfigJNI.Serializedouble(SpnValue.Slot1_kP.value, kP);
517        ss += ConfigJNI.Serializedouble(SpnValue.Slot1_kI.value, kI);
518        ss += ConfigJNI.Serializedouble(SpnValue.Slot1_kD.value, kD);
519        ss += ConfigJNI.Serializedouble(SpnValue.Slot1_kS.value, kS);
520        ss += ConfigJNI.Serializedouble(SpnValue.Slot1_kV.value, kV);
521        ss += ConfigJNI.Serializedouble(SpnValue.Slot1_kA.value, kA);
522        ss += ConfigJNI.Serializedouble(SpnValue.Slot1_kG.value, kG);
523        ss += ConfigJNI.Serializeint(SpnValue.Slot1_kG_Type.value, GravityType.value);
524        ss += ConfigJNI.Serializeint(SpnValue.Slot1_kS_Sign.value, StaticFeedforwardSign.value);
525        return ss;
526    }
527}
528