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
014import edu.wpi.first.units.*;
015
016import edu.wpi.first.units.measure.*;
017import static edu.wpi.first.units.Units.*;
018
019/**
020 * Configs that directly affect motor output.
021 * <p>
022 * Includes motor invert, neutral mode, and other features related to
023 * motor output.
024 */
025public class MotorOutputConfigs implements ParentConfiguration, Cloneable {
026    /**
027     * Invert state of the device as seen from the front of the motor.
028     * 
029     */
030    public InvertedValue Inverted = InvertedValue.CounterClockwise_Positive;
031    /**
032     * The state of the motor controller bridge when output is neutral or
033     * disabled.
034     * 
035     */
036    public NeutralModeValue NeutralMode = NeutralModeValue.Coast;
037    /**
038     * Configures the output deadband duty cycle during duty cycle and
039     * voltage based control modes.
040     * 
041     * <ul>
042     *   <li> <b>Minimum Value:</b> 0.0
043     *   <li> <b>Maximum Value:</b> 0.25
044     *   <li> <b>Default Value:</b> 0
045     *   <li> <b>Units:</b> fractional
046     * </ul>
047     */
048    public double DutyCycleNeutralDeadband = 0;
049    /**
050     * Maximum (forward) output during duty cycle based control modes.
051     * 
052     * <ul>
053     *   <li> <b>Minimum Value:</b> -1.0
054     *   <li> <b>Maximum Value:</b> 1.0
055     *   <li> <b>Default Value:</b> 1
056     *   <li> <b>Units:</b> fractional
057     * </ul>
058     */
059    public double PeakForwardDutyCycle = 1;
060    /**
061     * Minimum (reverse) output during duty cycle based control modes.
062     * 
063     * <ul>
064     *   <li> <b>Minimum Value:</b> -1.0
065     *   <li> <b>Maximum Value:</b> 1.0
066     *   <li> <b>Default Value:</b> -1
067     *   <li> <b>Units:</b> fractional
068     * </ul>
069     */
070    public double PeakReverseDutyCycle = -1;
071    /**
072     * When a control request UseTimesync is enabled, this determines the
073     * time-sychronized frequency at which control requests are applied.
074     * <p>
075     * The application of the control request will be delayed until the
076     * next timesync boundary at the frequency defined by this config.
077     * When set to 0 Hz, timesync will never be used for control requests,
078     * regardless of the value of UseTimesync.
079     * 
080     * <ul>
081     *   <li> <b>Minimum Value:</b> 50
082     *   <li> <b>Maximum Value:</b> 500
083     *   <li> <b>Default Value:</b> 0
084     *   <li> <b>Units:</b> Hz
085     * </ul>
086     */
087    public double ControlTimesyncFreqHz = 0;
088    
089    /**
090     * Modifies this configuration's Inverted parameter and returns itself for
091     * method-chaining and easier to use config API.
092     * <p>
093     * Invert state of the device as seen from the front of the motor.
094     * 
095     *
096     * @param newInverted Parameter to modify
097     * @return Itself
098     */
099    public final MotorOutputConfigs withInverted(InvertedValue newInverted)
100    {
101        Inverted = newInverted;
102        return this;
103    }
104    
105    /**
106     * Modifies this configuration's NeutralMode parameter and returns itself for
107     * method-chaining and easier to use config API.
108     * <p>
109     * The state of the motor controller bridge when output is neutral or
110     * disabled.
111     * 
112     *
113     * @param newNeutralMode Parameter to modify
114     * @return Itself
115     */
116    public final MotorOutputConfigs withNeutralMode(NeutralModeValue newNeutralMode)
117    {
118        NeutralMode = newNeutralMode;
119        return this;
120    }
121    
122    /**
123     * Modifies this configuration's DutyCycleNeutralDeadband parameter and returns itself for
124     * method-chaining and easier to use config API.
125     * <p>
126     * Configures the output deadband duty cycle during duty cycle and
127     * voltage based control modes.
128     * 
129     * <ul>
130     *   <li> <b>Minimum Value:</b> 0.0
131     *   <li> <b>Maximum Value:</b> 0.25
132     *   <li> <b>Default Value:</b> 0
133     *   <li> <b>Units:</b> fractional
134     * </ul>
135     *
136     * @param newDutyCycleNeutralDeadband Parameter to modify
137     * @return Itself
138     */
139    public final MotorOutputConfigs withDutyCycleNeutralDeadband(double newDutyCycleNeutralDeadband)
140    {
141        DutyCycleNeutralDeadband = newDutyCycleNeutralDeadband;
142        return this;
143    }
144    
145    /**
146     * Modifies this configuration's PeakForwardDutyCycle parameter and returns itself for
147     * method-chaining and easier to use config API.
148     * <p>
149     * Maximum (forward) output during duty cycle based control modes.
150     * 
151     * <ul>
152     *   <li> <b>Minimum Value:</b> -1.0
153     *   <li> <b>Maximum Value:</b> 1.0
154     *   <li> <b>Default Value:</b> 1
155     *   <li> <b>Units:</b> fractional
156     * </ul>
157     *
158     * @param newPeakForwardDutyCycle Parameter to modify
159     * @return Itself
160     */
161    public final MotorOutputConfigs withPeakForwardDutyCycle(double newPeakForwardDutyCycle)
162    {
163        PeakForwardDutyCycle = newPeakForwardDutyCycle;
164        return this;
165    }
166    
167    /**
168     * Modifies this configuration's PeakReverseDutyCycle parameter and returns itself for
169     * method-chaining and easier to use config API.
170     * <p>
171     * Minimum (reverse) output during duty cycle based control modes.
172     * 
173     * <ul>
174     *   <li> <b>Minimum Value:</b> -1.0
175     *   <li> <b>Maximum Value:</b> 1.0
176     *   <li> <b>Default Value:</b> -1
177     *   <li> <b>Units:</b> fractional
178     * </ul>
179     *
180     * @param newPeakReverseDutyCycle Parameter to modify
181     * @return Itself
182     */
183    public final MotorOutputConfigs withPeakReverseDutyCycle(double newPeakReverseDutyCycle)
184    {
185        PeakReverseDutyCycle = newPeakReverseDutyCycle;
186        return this;
187    }
188    
189    /**
190     * Modifies this configuration's ControlTimesyncFreqHz parameter and returns itself for
191     * method-chaining and easier to use config API.
192     * <p>
193     * When a control request UseTimesync is enabled, this determines the
194     * time-sychronized frequency at which control requests are applied.
195     * <p>
196     * The application of the control request will be delayed until the
197     * next timesync boundary at the frequency defined by this config.
198     * When set to 0 Hz, timesync will never be used for control requests,
199     * regardless of the value of UseTimesync.
200     * 
201     * <ul>
202     *   <li> <b>Minimum Value:</b> 50
203     *   <li> <b>Maximum Value:</b> 500
204     *   <li> <b>Default Value:</b> 0
205     *   <li> <b>Units:</b> Hz
206     * </ul>
207     *
208     * @param newControlTimesyncFreqHz Parameter to modify
209     * @return Itself
210     */
211    public final MotorOutputConfigs withControlTimesyncFreqHz(double newControlTimesyncFreqHz)
212    {
213        ControlTimesyncFreqHz = newControlTimesyncFreqHz;
214        return this;
215    }
216    
217    /**
218     * Modifies this configuration's ControlTimesyncFreqHz parameter and returns itself for
219     * method-chaining and easier to use config API.
220     * <p>
221     * When a control request UseTimesync is enabled, this determines the
222     * time-sychronized frequency at which control requests are applied.
223     * <p>
224     * The application of the control request will be delayed until the
225     * next timesync boundary at the frequency defined by this config.
226     * When set to 0 Hz, timesync will never be used for control requests,
227     * regardless of the value of UseTimesync.
228     * 
229     * <ul>
230     *   <li> <b>Minimum Value:</b> 50
231     *   <li> <b>Maximum Value:</b> 500
232     *   <li> <b>Default Value:</b> 0
233     *   <li> <b>Units:</b> Hz
234     * </ul>
235     *
236     * @param newControlTimesyncFreqHz Parameter to modify
237     * @return Itself
238     */
239    public final MotorOutputConfigs withControlTimesyncFreqHz(Frequency newControlTimesyncFreqHz)
240    {
241        ControlTimesyncFreqHz = newControlTimesyncFreqHz.in(Hertz);
242        return this;
243    }
244    
245    /**
246     * Helper method to get this configuration's ControlTimesyncFreqHz parameter converted
247     * to a unit type. If not using the Java units library, {@link #ControlTimesyncFreqHz}
248     * can be accessed directly instead.
249     * <p>
250     * When a control request UseTimesync is enabled, this determines the
251     * time-sychronized frequency at which control requests are applied.
252     * <p>
253     * The application of the control request will be delayed until the
254     * next timesync boundary at the frequency defined by this config.
255     * When set to 0 Hz, timesync will never be used for control requests,
256     * regardless of the value of UseTimesync.
257     * 
258     * <ul>
259     *   <li> <b>Minimum Value:</b> 50
260     *   <li> <b>Maximum Value:</b> 500
261     *   <li> <b>Default Value:</b> 0
262     *   <li> <b>Units:</b> Hz
263     * </ul>
264     *
265     * @return ControlTimesyncFreqHz
266     */
267    public final Frequency getControlTimesyncFreqHzMeasure()
268    {
269        return Hertz.of(ControlTimesyncFreqHz);
270    }
271
272    
273
274    @Override
275    public String toString() {
276        String ss = "Config Group: MotorOutput\n";
277        ss += "    Inverted: " + Inverted + "\n";
278        ss += "    NeutralMode: " + NeutralMode + "\n";
279        ss += "    DutyCycleNeutralDeadband: " + DutyCycleNeutralDeadband + " fractional" + "\n";
280        ss += "    PeakForwardDutyCycle: " + PeakForwardDutyCycle + " fractional" + "\n";
281        ss += "    PeakReverseDutyCycle: " + PeakReverseDutyCycle + " fractional" + "\n";
282        ss += "    ControlTimesyncFreqHz: " + ControlTimesyncFreqHz + " Hz" + "\n";
283        return ss;
284    }
285
286    /**
287     * Get the serialized form of this configuration group.
288     *
289     * @return Serialized form of this config group
290     */
291    @Override
292    public final String serialize() {
293        String ss = "";
294        ss += ConfigJNI.Serializeint(SpnValue.Config_Inverted.value, Inverted.value);
295        ss += ConfigJNI.Serializeint(SpnValue.Config_NeutralMode.value, NeutralMode.value);
296        ss += ConfigJNI.Serializedouble(SpnValue.Config_DutyCycleNeutralDB.value, DutyCycleNeutralDeadband);
297        ss += ConfigJNI.Serializedouble(SpnValue.Config_PeakForwardDC.value, PeakForwardDutyCycle);
298        ss += ConfigJNI.Serializedouble(SpnValue.Config_PeakReverseDC.value, PeakReverseDutyCycle);
299        ss += ConfigJNI.Serializedouble(SpnValue.Config_ControlTimesyncFreq.value, ControlTimesyncFreqHz);
300        return ss;
301    }
302
303    /**
304     * Take a string and deserialize it to this configuration group.
305     *
306     * @return Return code of the deserialize method
307     */
308    @Override
309    public final StatusCode deserialize(String to_deserialize) {
310        Inverted = InvertedValue.valueOf(ConfigJNI.Deserializeint(SpnValue.Config_Inverted.value, to_deserialize));
311        NeutralMode = NeutralModeValue.valueOf(ConfigJNI.Deserializeint(SpnValue.Config_NeutralMode.value, to_deserialize));
312        DutyCycleNeutralDeadband = ConfigJNI.Deserializedouble(SpnValue.Config_DutyCycleNeutralDB.value, to_deserialize);
313        PeakForwardDutyCycle = ConfigJNI.Deserializedouble(SpnValue.Config_PeakForwardDC.value, to_deserialize);
314        PeakReverseDutyCycle = ConfigJNI.Deserializedouble(SpnValue.Config_PeakReverseDC.value, to_deserialize);
315        ControlTimesyncFreqHz = ConfigJNI.Deserializedouble(SpnValue.Config_ControlTimesyncFreq.value, to_deserialize);
316        return  StatusCode.OK;
317    }
318
319    @Override
320    public MotorOutputConfigs clone() {
321        try {
322            return (MotorOutputConfigs)super.clone();
323        } catch (CloneNotSupportedException ex) {
324            /* this should never happen */
325            throw new RuntimeException(ex);
326        }
327    }
328}
329