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.*;
012
013import edu.wpi.first.units.*;
014
015import edu.wpi.first.units.measure.*;
016import static edu.wpi.first.units.Units.*;
017
018/**
019 * Configs that affect how software-limit switches behave.
020 * <p>
021 * Includes enabling software-limit switches and the threshold at
022 * which they are tripped.
023 */
024public class SoftwareLimitSwitchConfigs implements ParentConfiguration
025{
026    /**
027     * If enabled, the motor output is set to neutral if position exceeds
028     * ForwardSoftLimitThreshold and forward output is requested.
029     * 
030     * <ul>
031     *   <li> <b>Default Value:</b> False
032     * </ul>
033     */
034    public boolean ForwardSoftLimitEnable = false;
035    /**
036     * If enabled, the motor output is set to neutral if position exceeds
037     * ReverseSoftLimitThreshold and reverse output is requested.
038     * 
039     * <ul>
040     *   <li> <b>Default Value:</b> False
041     * </ul>
042     */
043    public boolean ReverseSoftLimitEnable = false;
044    /**
045     * Position threshold for forward soft limit features.
046     * ForwardSoftLimitEnable must be enabled for this to take effect.
047     * 
048     * <ul>
049     *   <li> <b>Minimum Value:</b> -3.4e+38
050     *   <li> <b>Maximum Value:</b> 3.4e+38
051     *   <li> <b>Default Value:</b> 0
052     *   <li> <b>Units:</b> rotations
053     * </ul>
054     */
055    public double ForwardSoftLimitThreshold = 0;
056    /**
057     * Position threshold for reverse soft limit features.
058     * ReverseSoftLimitEnable must be enabled for this to take effect.
059     * 
060     * <ul>
061     *   <li> <b>Minimum Value:</b> -3.4e+38
062     *   <li> <b>Maximum Value:</b> 3.4e+38
063     *   <li> <b>Default Value:</b> 0
064     *   <li> <b>Units:</b> rotations
065     * </ul>
066     */
067    public double ReverseSoftLimitThreshold = 0;
068    
069    /**
070     * Modifies this configuration's ForwardSoftLimitEnable parameter and returns itself for
071     * method-chaining and easier to use config API.
072     * <p>
073     * If enabled, the motor output is set to neutral if position exceeds
074     * ForwardSoftLimitThreshold and forward output is requested.
075     * 
076     * <ul>
077     *   <li> <b>Default Value:</b> False
078     * </ul>
079     *
080     * @param newForwardSoftLimitEnable Parameter to modify
081     * @return Itself
082     */
083    public SoftwareLimitSwitchConfigs withForwardSoftLimitEnable(boolean newForwardSoftLimitEnable)
084    {
085        ForwardSoftLimitEnable = newForwardSoftLimitEnable;
086        return this;
087    }
088    
089    /**
090     * Modifies this configuration's ReverseSoftLimitEnable parameter and returns itself for
091     * method-chaining and easier to use config API.
092     * <p>
093     * If enabled, the motor output is set to neutral if position exceeds
094     * ReverseSoftLimitThreshold and reverse output is requested.
095     * 
096     * <ul>
097     *   <li> <b>Default Value:</b> False
098     * </ul>
099     *
100     * @param newReverseSoftLimitEnable Parameter to modify
101     * @return Itself
102     */
103    public SoftwareLimitSwitchConfigs withReverseSoftLimitEnable(boolean newReverseSoftLimitEnable)
104    {
105        ReverseSoftLimitEnable = newReverseSoftLimitEnable;
106        return this;
107    }
108    
109    /**
110     * Modifies this configuration's ForwardSoftLimitThreshold parameter and returns itself for
111     * method-chaining and easier to use config API.
112     * <p>
113     * Position threshold for forward soft limit features.
114     * ForwardSoftLimitEnable must be enabled for this to take effect.
115     * 
116     * <ul>
117     *   <li> <b>Minimum Value:</b> -3.4e+38
118     *   <li> <b>Maximum Value:</b> 3.4e+38
119     *   <li> <b>Default Value:</b> 0
120     *   <li> <b>Units:</b> rotations
121     * </ul>
122     *
123     * @param newForwardSoftLimitThreshold Parameter to modify
124     * @return Itself
125     */
126    public SoftwareLimitSwitchConfigs withForwardSoftLimitThreshold(double newForwardSoftLimitThreshold)
127    {
128        ForwardSoftLimitThreshold = newForwardSoftLimitThreshold;
129        return this;
130    }
131    
132    /**
133     * Modifies this configuration's ForwardSoftLimitThreshold parameter and returns itself for
134     * method-chaining and easier to use config API.
135     * <p>
136     * Position threshold for forward soft limit features.
137     * ForwardSoftLimitEnable must be enabled for this to take effect.
138     * 
139     * <ul>
140     *   <li> <b>Minimum Value:</b> -3.4e+38
141     *   <li> <b>Maximum Value:</b> 3.4e+38
142     *   <li> <b>Default Value:</b> 0
143     *   <li> <b>Units:</b> rotations
144     * </ul>
145     *
146     * @param newForwardSoftLimitThreshold Parameter to modify
147     * @return Itself
148     */
149    public SoftwareLimitSwitchConfigs withForwardSoftLimitThreshold(Angle newForwardSoftLimitThreshold)
150    {
151        ForwardSoftLimitThreshold = newForwardSoftLimitThreshold.in(Rotations);
152        return this;
153    }
154    
155    /**
156     * Helper method to get this configuration's ForwardSoftLimitThreshold parameter converted
157     * to a unit type. If not using the Java units library, {@link #ForwardSoftLimitThreshold}
158     * can be accessed directly instead.
159     * <p>
160     * Position threshold for forward soft limit features.
161     * ForwardSoftLimitEnable must be enabled for this to take effect.
162     * 
163     * <ul>
164     *   <li> <b>Minimum Value:</b> -3.4e+38
165     *   <li> <b>Maximum Value:</b> 3.4e+38
166     *   <li> <b>Default Value:</b> 0
167     *   <li> <b>Units:</b> rotations
168     * </ul>
169     *
170     * @return ForwardSoftLimitThreshold
171     */
172    public Angle getForwardSoftLimitThresholdMeasure()
173    {
174        return Rotations.of(ForwardSoftLimitThreshold);
175    }
176    
177    /**
178     * Modifies this configuration's ReverseSoftLimitThreshold parameter and returns itself for
179     * method-chaining and easier to use config API.
180     * <p>
181     * Position threshold for reverse soft limit features.
182     * ReverseSoftLimitEnable must be enabled for this to take effect.
183     * 
184     * <ul>
185     *   <li> <b>Minimum Value:</b> -3.4e+38
186     *   <li> <b>Maximum Value:</b> 3.4e+38
187     *   <li> <b>Default Value:</b> 0
188     *   <li> <b>Units:</b> rotations
189     * </ul>
190     *
191     * @param newReverseSoftLimitThreshold Parameter to modify
192     * @return Itself
193     */
194    public SoftwareLimitSwitchConfigs withReverseSoftLimitThreshold(double newReverseSoftLimitThreshold)
195    {
196        ReverseSoftLimitThreshold = newReverseSoftLimitThreshold;
197        return this;
198    }
199    
200    /**
201     * Modifies this configuration's ReverseSoftLimitThreshold parameter and returns itself for
202     * method-chaining and easier to use config API.
203     * <p>
204     * Position threshold for reverse soft limit features.
205     * ReverseSoftLimitEnable must be enabled for this to take effect.
206     * 
207     * <ul>
208     *   <li> <b>Minimum Value:</b> -3.4e+38
209     *   <li> <b>Maximum Value:</b> 3.4e+38
210     *   <li> <b>Default Value:</b> 0
211     *   <li> <b>Units:</b> rotations
212     * </ul>
213     *
214     * @param newReverseSoftLimitThreshold Parameter to modify
215     * @return Itself
216     */
217    public SoftwareLimitSwitchConfigs withReverseSoftLimitThreshold(Angle newReverseSoftLimitThreshold)
218    {
219        ReverseSoftLimitThreshold = newReverseSoftLimitThreshold.in(Rotations);
220        return this;
221    }
222    
223    /**
224     * Helper method to get this configuration's ReverseSoftLimitThreshold parameter converted
225     * to a unit type. If not using the Java units library, {@link #ReverseSoftLimitThreshold}
226     * can be accessed directly instead.
227     * <p>
228     * Position threshold for reverse soft limit features.
229     * ReverseSoftLimitEnable must be enabled for this to take effect.
230     * 
231     * <ul>
232     *   <li> <b>Minimum Value:</b> -3.4e+38
233     *   <li> <b>Maximum Value:</b> 3.4e+38
234     *   <li> <b>Default Value:</b> 0
235     *   <li> <b>Units:</b> rotations
236     * </ul>
237     *
238     * @return ReverseSoftLimitThreshold
239     */
240    public Angle getReverseSoftLimitThresholdMeasure()
241    {
242        return Rotations.of(ReverseSoftLimitThreshold);
243    }
244
245    
246
247    @Override
248    public String toString()
249    {
250        String ss = "Config Group: SoftwareLimitSwitch\n";
251        ss += "    ForwardSoftLimitEnable: " + ForwardSoftLimitEnable + "\n";
252        ss += "    ReverseSoftLimitEnable: " + ReverseSoftLimitEnable + "\n";
253        ss += "    ForwardSoftLimitThreshold: " + ForwardSoftLimitThreshold + " rotations" + "\n";
254        ss += "    ReverseSoftLimitThreshold: " + ReverseSoftLimitThreshold + " rotations" + "\n";
255        return ss;
256    }
257
258    /**
259     *
260     */
261    public StatusCode deserialize(String to_deserialize)
262    {
263        ForwardSoftLimitEnable = ConfigJNI.Deserializeboolean(SpnValue.Config_ForwardSoftLimitEnable.value, to_deserialize);
264        ReverseSoftLimitEnable = ConfigJNI.Deserializeboolean(SpnValue.Config_ReverseSoftLimitEnable.value, to_deserialize);
265        ForwardSoftLimitThreshold = ConfigJNI.Deserializedouble(SpnValue.Config_ForwardSoftLimitThreshold.value, to_deserialize);
266        ReverseSoftLimitThreshold = ConfigJNI.Deserializedouble(SpnValue.Config_ReverseSoftLimitThreshold.value, to_deserialize);
267        return  StatusCode.OK;
268    }
269
270    /**
271     *
272     */
273    public String serialize()
274    {
275        String ss = "";
276        ss += ConfigJNI.Serializeboolean(SpnValue.Config_ForwardSoftLimitEnable.value, ForwardSoftLimitEnable);
277        ss += ConfigJNI.Serializeboolean(SpnValue.Config_ReverseSoftLimitEnable.value, ReverseSoftLimitEnable);
278        ss += ConfigJNI.Serializedouble(SpnValue.Config_ForwardSoftLimitThreshold.value, ForwardSoftLimitThreshold);
279        ss += ConfigJNI.Serializedouble(SpnValue.Config_ReverseSoftLimitThreshold.value, ReverseSoftLimitThreshold);
280        return ss;
281    }
282}
283