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;
010
011/**
012 * Class description for the Talon FX integrated motor controller.
013 *
014 * This handles the configurations for the {@link com.ctre.phoenix6.hardware.TalonFX}
015 */
016public class TalonFXConfiguration implements ParentConfiguration
017{
018    /**
019     * True if we should factory default newer unsupported configs,
020     * false to leave newer unsupported configs alone.
021     * <p>
022     * This flag addresses a corner case where the device may have
023     * firmware with newer configs that didn't exist when this
024     * version of the API was built. If this occurs and this
025     * flag is true, unsupported new configs will be factory
026     * defaulted to avoid unexpected behavior.
027     * <p>
028     * This is also the behavior in Phoenix 5, so this flag
029     * is defaulted to true to match.
030     */
031    public boolean FutureProofConfigs = true;
032
033    
034    /**
035     * Configs that directly affect motor output.
036     * <p>
037     * Includes motor invert, neutral mode, and other features related to
038     * motor output.
039     */
040    public MotorOutputConfigs MotorOutput = new MotorOutputConfigs();
041    
042    /**
043     * Configs that directly affect current limiting features.
044     * <p>
045     * Contains the supply/stator current limit thresholds and whether to
046     * enable them.
047     */
048    public CurrentLimitsConfigs CurrentLimits = new CurrentLimitsConfigs();
049    
050    /**
051     * Configs that affect Voltage control types.
052     * <p>
053     * Includes peak output voltages and other configs affecting voltage
054     * measurements.
055     */
056    public VoltageConfigs Voltage = new VoltageConfigs();
057    
058    /**
059     * Configs that affect Torque Current control types.
060     * <p>
061     * Includes the maximum and minimum applied torque output and the
062     * neutral deadband used during TorqueCurrentFOC requests.
063     */
064    public TorqueCurrentConfigs TorqueCurrent = new TorqueCurrentConfigs();
065    
066    /**
067     * Configs that affect the feedback of this motor controller.
068     * <p>
069     * Includes feedback sensor source, any offsets for the feedback
070     * sensor, and various ratios to describe the relationship between the
071     * sensor and the mechanism for closed looping.
072     */
073    public FeedbackConfigs Feedback = new FeedbackConfigs();
074    
075    /**
076     * Configs related to sensors used for differential control of a
077     * mechanism.
078     * <p>
079     * Includes the differential sensor sources and IDs.
080     */
081    public DifferentialSensorsConfigs DifferentialSensors = new DifferentialSensorsConfigs();
082    
083    /**
084     * Configs related to constants used for differential control of a
085     * mechanism.
086     * <p>
087     * Includes the differential peak outputs.
088     */
089    public DifferentialConstantsConfigs DifferentialConstants = new DifferentialConstantsConfigs();
090    
091    /**
092     * Configs that affect the open-loop control of this motor controller.
093     * <p>
094     * Open-loop ramp rates for the various control types.
095     */
096    public OpenLoopRampsConfigs OpenLoopRamps = new OpenLoopRampsConfigs();
097    
098    /**
099     * Configs that affect the closed-loop control of this motor
100     * controller.
101     * <p>
102     * Closed-loop ramp rates for the various control types.
103     */
104    public ClosedLoopRampsConfigs ClosedLoopRamps = new ClosedLoopRampsConfigs();
105    
106    /**
107     * Configs that change how the motor controller behaves under
108     * different limit switch states.
109     * <p>
110     * Includes configs such as enabling limit switches, configuring the
111     * remote sensor ID, the source, and the position to set on limit.
112     */
113    public HardwareLimitSwitchConfigs HardwareLimitSwitch = new HardwareLimitSwitchConfigs();
114    
115    /**
116     * Configs that affect audible components of the device.
117     * <p>
118     * Includes configuration for the beep on boot.
119     */
120    public AudioConfigs Audio = new AudioConfigs();
121    
122    /**
123     * Configs that affect how software-limit switches behave.
124     * <p>
125     * Includes enabling software-limit switches and the threshold at
126     * which they are tripped.
127     */
128    public SoftwareLimitSwitchConfigs SoftwareLimitSwitch = new SoftwareLimitSwitchConfigs();
129    
130    /**
131     * Configs for Motion Magic®.
132     * <p>
133     * Includes Velocity, Acceleration, Jerk, and Expo parameters.
134     */
135    public MotionMagicConfigs MotionMagic = new MotionMagicConfigs();
136    
137    /**
138     * Custom Params.
139     * <p>
140     * Custom paramaters that have no real impact on controller.
141     */
142    public CustomParamsConfigs CustomParams = new CustomParamsConfigs();
143    
144    /**
145     * Configs that affect general behavior during closed-looping.
146     * <p>
147     * Includes Continuous Wrap features.
148     */
149    public ClosedLoopGeneralConfigs ClosedLoopGeneral = new ClosedLoopGeneralConfigs();
150    
151    /**
152     * Gains for the specified slot.
153     * <p>
154     * If this slot is selected, these gains are used in closed loop
155     * control requests.
156     */
157    public Slot0Configs Slot0 = new Slot0Configs();
158    
159    /**
160     * Gains for the specified slot.
161     * <p>
162     * If this slot is selected, these gains are used in closed loop
163     * control requests.
164     */
165    public Slot1Configs Slot1 = new Slot1Configs();
166    
167    /**
168     * Gains for the specified slot.
169     * <p>
170     * If this slot is selected, these gains are used in closed loop
171     * control requests.
172     */
173    public Slot2Configs Slot2 = new Slot2Configs();
174    
175    /**
176     * Modifies this configuration's MotorOutput parameter and returns itself for
177     * method-chaining and easier to use config API.
178     * <p>
179     * Configs that directly affect motor output.
180     * <p>
181     * Includes motor invert, neutral mode, and other features related to
182     * motor output.
183     *
184     * @param newMotorOutput Parameter to modify
185     * @return Itself
186     */
187    public TalonFXConfiguration withMotorOutput(MotorOutputConfigs newMotorOutput)
188    {
189        MotorOutput = newMotorOutput;
190        return this;
191    }
192    
193    /**
194     * Modifies this configuration's CurrentLimits parameter and returns itself for
195     * method-chaining and easier to use config API.
196     * <p>
197     * Configs that directly affect current limiting features.
198     * <p>
199     * Contains the supply/stator current limit thresholds and whether to
200     * enable them.
201     *
202     * @param newCurrentLimits Parameter to modify
203     * @return Itself
204     */
205    public TalonFXConfiguration withCurrentLimits(CurrentLimitsConfigs newCurrentLimits)
206    {
207        CurrentLimits = newCurrentLimits;
208        return this;
209    }
210    
211    /**
212     * Modifies this configuration's Voltage parameter and returns itself for
213     * method-chaining and easier to use config API.
214     * <p>
215     * Configs that affect Voltage control types.
216     * <p>
217     * Includes peak output voltages and other configs affecting voltage
218     * measurements.
219     *
220     * @param newVoltage Parameter to modify
221     * @return Itself
222     */
223    public TalonFXConfiguration withVoltage(VoltageConfigs newVoltage)
224    {
225        Voltage = newVoltage;
226        return this;
227    }
228    
229    /**
230     * Modifies this configuration's TorqueCurrent parameter and returns itself for
231     * method-chaining and easier to use config API.
232     * <p>
233     * Configs that affect Torque Current control types.
234     * <p>
235     * Includes the maximum and minimum applied torque output and the
236     * neutral deadband used during TorqueCurrentFOC requests.
237     *
238     * @param newTorqueCurrent Parameter to modify
239     * @return Itself
240     */
241    public TalonFXConfiguration withTorqueCurrent(TorqueCurrentConfigs newTorqueCurrent)
242    {
243        TorqueCurrent = newTorqueCurrent;
244        return this;
245    }
246    
247    /**
248     * Modifies this configuration's Feedback parameter and returns itself for
249     * method-chaining and easier to use config API.
250     * <p>
251     * Configs that affect the feedback of this motor controller.
252     * <p>
253     * Includes feedback sensor source, any offsets for the feedback
254     * sensor, and various ratios to describe the relationship between the
255     * sensor and the mechanism for closed looping.
256     *
257     * @param newFeedback Parameter to modify
258     * @return Itself
259     */
260    public TalonFXConfiguration withFeedback(FeedbackConfigs newFeedback)
261    {
262        Feedback = newFeedback;
263        return this;
264    }
265    
266    /**
267     * Modifies this configuration's DifferentialSensors parameter and returns itself for
268     * method-chaining and easier to use config API.
269     * <p>
270     * Configs related to sensors used for differential control of a
271     * mechanism.
272     * <p>
273     * Includes the differential sensor sources and IDs.
274     *
275     * @param newDifferentialSensors Parameter to modify
276     * @return Itself
277     */
278    public TalonFXConfiguration withDifferentialSensors(DifferentialSensorsConfigs newDifferentialSensors)
279    {
280        DifferentialSensors = newDifferentialSensors;
281        return this;
282    }
283    
284    /**
285     * Modifies this configuration's DifferentialConstants parameter and returns itself for
286     * method-chaining and easier to use config API.
287     * <p>
288     * Configs related to constants used for differential control of a
289     * mechanism.
290     * <p>
291     * Includes the differential peak outputs.
292     *
293     * @param newDifferentialConstants Parameter to modify
294     * @return Itself
295     */
296    public TalonFXConfiguration withDifferentialConstants(DifferentialConstantsConfigs newDifferentialConstants)
297    {
298        DifferentialConstants = newDifferentialConstants;
299        return this;
300    }
301    
302    /**
303     * Modifies this configuration's OpenLoopRamps parameter and returns itself for
304     * method-chaining and easier to use config API.
305     * <p>
306     * Configs that affect the open-loop control of this motor controller.
307     * <p>
308     * Open-loop ramp rates for the various control types.
309     *
310     * @param newOpenLoopRamps Parameter to modify
311     * @return Itself
312     */
313    public TalonFXConfiguration withOpenLoopRamps(OpenLoopRampsConfigs newOpenLoopRamps)
314    {
315        OpenLoopRamps = newOpenLoopRamps;
316        return this;
317    }
318    
319    /**
320     * Modifies this configuration's ClosedLoopRamps parameter and returns itself for
321     * method-chaining and easier to use config API.
322     * <p>
323     * Configs that affect the closed-loop control of this motor
324     * controller.
325     * <p>
326     * Closed-loop ramp rates for the various control types.
327     *
328     * @param newClosedLoopRamps Parameter to modify
329     * @return Itself
330     */
331    public TalonFXConfiguration withClosedLoopRamps(ClosedLoopRampsConfigs newClosedLoopRamps)
332    {
333        ClosedLoopRamps = newClosedLoopRamps;
334        return this;
335    }
336    
337    /**
338     * Modifies this configuration's HardwareLimitSwitch parameter and returns itself for
339     * method-chaining and easier to use config API.
340     * <p>
341     * Configs that change how the motor controller behaves under
342     * different limit switch states.
343     * <p>
344     * Includes configs such as enabling limit switches, configuring the
345     * remote sensor ID, the source, and the position to set on limit.
346     *
347     * @param newHardwareLimitSwitch Parameter to modify
348     * @return Itself
349     */
350    public TalonFXConfiguration withHardwareLimitSwitch(HardwareLimitSwitchConfigs newHardwareLimitSwitch)
351    {
352        HardwareLimitSwitch = newHardwareLimitSwitch;
353        return this;
354    }
355    
356    /**
357     * Modifies this configuration's Audio parameter and returns itself for
358     * method-chaining and easier to use config API.
359     * <p>
360     * Configs that affect audible components of the device.
361     * <p>
362     * Includes configuration for the beep on boot.
363     *
364     * @param newAudio Parameter to modify
365     * @return Itself
366     */
367    public TalonFXConfiguration withAudio(AudioConfigs newAudio)
368    {
369        Audio = newAudio;
370        return this;
371    }
372    
373    /**
374     * Modifies this configuration's SoftwareLimitSwitch parameter and returns itself for
375     * method-chaining and easier to use config API.
376     * <p>
377     * Configs that affect how software-limit switches behave.
378     * <p>
379     * Includes enabling software-limit switches and the threshold at
380     * which they are tripped.
381     *
382     * @param newSoftwareLimitSwitch Parameter to modify
383     * @return Itself
384     */
385    public TalonFXConfiguration withSoftwareLimitSwitch(SoftwareLimitSwitchConfigs newSoftwareLimitSwitch)
386    {
387        SoftwareLimitSwitch = newSoftwareLimitSwitch;
388        return this;
389    }
390    
391    /**
392     * Modifies this configuration's MotionMagic parameter and returns itself for
393     * method-chaining and easier to use config API.
394     * <p>
395     * Configs for Motion Magic®.
396     * <p>
397     * Includes Velocity, Acceleration, Jerk, and Expo parameters.
398     *
399     * @param newMotionMagic Parameter to modify
400     * @return Itself
401     */
402    public TalonFXConfiguration withMotionMagic(MotionMagicConfigs newMotionMagic)
403    {
404        MotionMagic = newMotionMagic;
405        return this;
406    }
407    
408    /**
409     * Modifies this configuration's CustomParams parameter and returns itself for
410     * method-chaining and easier to use config API.
411     * <p>
412     * Custom Params.
413     * <p>
414     * Custom paramaters that have no real impact on controller.
415     *
416     * @param newCustomParams Parameter to modify
417     * @return Itself
418     */
419    public TalonFXConfiguration withCustomParams(CustomParamsConfigs newCustomParams)
420    {
421        CustomParams = newCustomParams;
422        return this;
423    }
424    
425    /**
426     * Modifies this configuration's ClosedLoopGeneral parameter and returns itself for
427     * method-chaining and easier to use config API.
428     * <p>
429     * Configs that affect general behavior during closed-looping.
430     * <p>
431     * Includes Continuous Wrap features.
432     *
433     * @param newClosedLoopGeneral Parameter to modify
434     * @return Itself
435     */
436    public TalonFXConfiguration withClosedLoopGeneral(ClosedLoopGeneralConfigs newClosedLoopGeneral)
437    {
438        ClosedLoopGeneral = newClosedLoopGeneral;
439        return this;
440    }
441    
442    /**
443     * Modifies this configuration's Slot0 parameter and returns itself for
444     * method-chaining and easier to use config API.
445     * <p>
446     * Gains for the specified slot.
447     * <p>
448     * If this slot is selected, these gains are used in closed loop
449     * control requests.
450     *
451     * @param newSlot0 Parameter to modify
452     * @return Itself
453     */
454    public TalonFXConfiguration withSlot0(Slot0Configs newSlot0)
455    {
456        Slot0 = newSlot0;
457        return this;
458    }
459    
460    /**
461     * Modifies this configuration's Slot1 parameter and returns itself for
462     * method-chaining and easier to use config API.
463     * <p>
464     * Gains for the specified slot.
465     * <p>
466     * If this slot is selected, these gains are used in closed loop
467     * control requests.
468     *
469     * @param newSlot1 Parameter to modify
470     * @return Itself
471     */
472    public TalonFXConfiguration withSlot1(Slot1Configs newSlot1)
473    {
474        Slot1 = newSlot1;
475        return this;
476    }
477    
478    /**
479     * Modifies this configuration's Slot2 parameter and returns itself for
480     * method-chaining and easier to use config API.
481     * <p>
482     * Gains for the specified slot.
483     * <p>
484     * If this slot is selected, these gains are used in closed loop
485     * control requests.
486     *
487     * @param newSlot2 Parameter to modify
488     * @return Itself
489     */
490    public TalonFXConfiguration withSlot2(Slot2Configs newSlot2)
491    {
492        Slot2 = newSlot2;
493        return this;
494    }
495
496    @Override
497    public String toString()
498    {
499        String ss = "";
500        ss += MotorOutput.toString();
501        ss += CurrentLimits.toString();
502        ss += Voltage.toString();
503        ss += TorqueCurrent.toString();
504        ss += Feedback.toString();
505        ss += DifferentialSensors.toString();
506        ss += DifferentialConstants.toString();
507        ss += OpenLoopRamps.toString();
508        ss += ClosedLoopRamps.toString();
509        ss += HardwareLimitSwitch.toString();
510        ss += Audio.toString();
511        ss += SoftwareLimitSwitch.toString();
512        ss += MotionMagic.toString();
513        ss += CustomParams.toString();
514        ss += ClosedLoopGeneral.toString();
515        ss += Slot0.toString();
516        ss += Slot1.toString();
517        ss += Slot2.toString();
518        return ss;
519    }
520
521    /**
522     * Get the serialized form of this configuration
523     *
524     * @return Serialized form of this config group
525     */
526    public String serialize()
527    {
528        String ss = "";
529        ss += MotorOutput.serialize();
530        ss += CurrentLimits.serialize();
531        ss += Voltage.serialize();
532        ss += TorqueCurrent.serialize();
533        ss += Feedback.serialize();
534        ss += DifferentialSensors.serialize();
535        ss += DifferentialConstants.serialize();
536        ss += OpenLoopRamps.serialize();
537        ss += ClosedLoopRamps.serialize();
538        ss += HardwareLimitSwitch.serialize();
539        ss += Audio.serialize();
540        ss += SoftwareLimitSwitch.serialize();
541        ss += MotionMagic.serialize();
542        ss += CustomParams.serialize();
543        ss += ClosedLoopGeneral.serialize();
544        ss += Slot0.serialize();
545        ss += Slot1.serialize();
546        ss += Slot2.serialize();
547        return ss;
548    }
549
550    /**
551     * Take a string and deserialize it to this configuration
552     *
553     * @return Return code of the deserialize method
554     */
555    public StatusCode deserialize(String to_deserialize)
556    {
557        StatusCode err = StatusCode.OK;
558        err = MotorOutput.deserialize(to_deserialize);
559        err = CurrentLimits.deserialize(to_deserialize);
560        err = Voltage.deserialize(to_deserialize);
561        err = TorqueCurrent.deserialize(to_deserialize);
562        err = Feedback.deserialize(to_deserialize);
563        err = DifferentialSensors.deserialize(to_deserialize);
564        err = DifferentialConstants.deserialize(to_deserialize);
565        err = OpenLoopRamps.deserialize(to_deserialize);
566        err = ClosedLoopRamps.deserialize(to_deserialize);
567        err = HardwareLimitSwitch.deserialize(to_deserialize);
568        err = Audio.deserialize(to_deserialize);
569        err = SoftwareLimitSwitch.deserialize(to_deserialize);
570        err = MotionMagic.deserialize(to_deserialize);
571        err = CustomParams.deserialize(to_deserialize);
572        err = ClosedLoopGeneral.deserialize(to_deserialize);
573        err = Slot0.deserialize(to_deserialize);
574        err = Slot1.deserialize(to_deserialize);
575        err = Slot2.deserialize(to_deserialize);
576        return err;
577    }
578};