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
013/**
014 * Configs that directly affect current limiting features.
015 * <p>
016 * Contains the supply/stator current limit thresholds and whether to
017 * enable them.
018 */
019public class CurrentLimitsConfigs implements ParentConfiguration
020{
021    /**
022     * The amount of current allowed in the motor (motoring and regen
023     * current).  Note this requires StatorCurrentLimitEnable to be true.
024     * <p>
025     * This is only applicable for non-torque current control modes.  For
026     * torque current control, set the PeakForwardTorqueCurrent and
027     * PeakReverseTorqueCurrent in TorqueCurrentConfigs instead.
028     * <p>
029     * Stator current is directly proportional to torque, so this limit
030     * can be used to restrict the torque output of the motor, such as
031     * preventing wheel slip for a drivetrain.  Additionally, stator
032     * current limits can prevent brownouts during acceleration; supply
033     * current will never exceed the stator current limit and is often
034     * significantly lower than stator current.
035     * <p>
036     * A reasonable starting point for a stator current limit is 120 A,
037     * with values commonly ranging from 80-160 A. Mechanisms with a hard
038     * stop may need a smaller limit to reduce the torque applied when
039     * running into the hard stop.
040     * 
041     * <ul>
042     *   <li> <b>Minimum Value:</b> 0.0
043     *   <li> <b>Maximum Value:</b> 800.0
044     *   <li> <b>Default Value:</b> 0
045     *   <li> <b>Units:</b> A
046     * </ul>
047     */
048    public double StatorCurrentLimit = 0;
049    /**
050     * Enable motor stator current limiting.
051     * 
052     * <ul>
053     *   <li> <b>Default Value:</b> False
054     * </ul>
055     */
056    public boolean StatorCurrentLimitEnable = false;
057    /**
058     * The amount of supply current allowed.  Note this requires
059     * SupplyCurrentLimitEnable to be true.  Use SupplyCurrentThreshold
060     * and SupplyTimeThreshold to allow brief periods of high-current
061     * before limiting occurs.
062     * <p>
063     * This is only applicable for non-torque current control modes.  For
064     * torque current control, set the PeakForwardTorqueCurrent and
065     * PeakReverseTorqueCurrent in TorqueCurrentConfigs instead.
066     * <p>
067     * Supply current is the current drawn from the battery, so this limit
068     * can be used to prevent breaker trips and improve battery longevity.
069     *  Additionally, in the rare case where the robot experiences
070     * brownouts despite configuring stator current limits, a supply
071     * current limit can further help avoid brownouts. However, such
072     * brownouts are most commonly caused by a bad battery or poor power
073     * wiring.
074     * <p>
075     * A reasonable starting point for a supply current limit is 60 A with
076     * a threshold of 80 A for 0.1 seconds. Supply current limits commonly
077     * range from 20-80 A depending on the breaker used.
078     * 
079     * <ul>
080     *   <li> <b>Minimum Value:</b> 0.0
081     *   <li> <b>Maximum Value:</b> 800.0
082     *   <li> <b>Default Value:</b> 0
083     *   <li> <b>Units:</b> A
084     * </ul>
085     */
086    public double SupplyCurrentLimit = 0;
087    /**
088     * Enable motor supply current limiting.
089     * 
090     * <ul>
091     *   <li> <b>Default Value:</b> False
092     * </ul>
093     */
094    public boolean SupplyCurrentLimitEnable = false;
095    /**
096     * Delay supply current limiting until current exceeds this threshold
097     * for longer than SupplyTimeThreshold.  This allows current draws
098     * above SupplyCurrentLimit for a fixed period of time.  This has no
099     * effect if SupplyCurrentLimit is greater than this value.
100     * 
101     * <ul>
102     *   <li> <b>Minimum Value:</b> 0.0
103     *   <li> <b>Maximum Value:</b> 511
104     *   <li> <b>Default Value:</b> 0
105     *   <li> <b>Units:</b> A
106     * </ul>
107     */
108    public double SupplyCurrentThreshold = 0;
109    /**
110     * Allows unlimited current for a period of time before current
111     * limiting occurs.  Current threshold is the maximum of
112     * SupplyCurrentThreshold and SupplyCurrentLimit.
113     * 
114     * <ul>
115     *   <li> <b>Minimum Value:</b> 0.0
116     *   <li> <b>Maximum Value:</b> 1.275
117     *   <li> <b>Default Value:</b> 0
118     *   <li> <b>Units:</b> sec
119     * </ul>
120     */
121    public double SupplyTimeThreshold = 0;
122    
123    /**
124     * Modifies this configuration's StatorCurrentLimit parameter and returns itself for
125     * method-chaining and easier to use config API.
126     * <p>
127     * The amount of current allowed in the motor (motoring and regen
128     * current).  Note this requires StatorCurrentLimitEnable to be true.
129     * <p>
130     * This is only applicable for non-torque current control modes.  For
131     * torque current control, set the PeakForwardTorqueCurrent and
132     * PeakReverseTorqueCurrent in TorqueCurrentConfigs instead.
133     * <p>
134     * Stator current is directly proportional to torque, so this limit
135     * can be used to restrict the torque output of the motor, such as
136     * preventing wheel slip for a drivetrain.  Additionally, stator
137     * current limits can prevent brownouts during acceleration; supply
138     * current will never exceed the stator current limit and is often
139     * significantly lower than stator current.
140     * <p>
141     * A reasonable starting point for a stator current limit is 120 A,
142     * with values commonly ranging from 80-160 A. Mechanisms with a hard
143     * stop may need a smaller limit to reduce the torque applied when
144     * running into the hard stop.
145     * 
146     * <ul>
147     *   <li> <b>Minimum Value:</b> 0.0
148     *   <li> <b>Maximum Value:</b> 800.0
149     *   <li> <b>Default Value:</b> 0
150     *   <li> <b>Units:</b> A
151     * </ul>
152     *
153     * @param newStatorCurrentLimit Parameter to modify
154     * @return Itself
155     */
156    public CurrentLimitsConfigs withStatorCurrentLimit(double newStatorCurrentLimit)
157    {
158        StatorCurrentLimit = newStatorCurrentLimit;
159        return this;
160    }
161    /**
162     * Modifies this configuration's StatorCurrentLimitEnable parameter and returns itself for
163     * method-chaining and easier to use config API.
164     * <p>
165     * Enable motor stator current limiting.
166     * 
167     * <ul>
168     *   <li> <b>Default Value:</b> False
169     * </ul>
170     *
171     * @param newStatorCurrentLimitEnable Parameter to modify
172     * @return Itself
173     */
174    public CurrentLimitsConfigs withStatorCurrentLimitEnable(boolean newStatorCurrentLimitEnable)
175    {
176        StatorCurrentLimitEnable = newStatorCurrentLimitEnable;
177        return this;
178    }
179    /**
180     * Modifies this configuration's SupplyCurrentLimit parameter and returns itself for
181     * method-chaining and easier to use config API.
182     * <p>
183     * The amount of supply current allowed.  Note this requires
184     * SupplyCurrentLimitEnable to be true.  Use SupplyCurrentThreshold
185     * and SupplyTimeThreshold to allow brief periods of high-current
186     * before limiting occurs.
187     * <p>
188     * This is only applicable for non-torque current control modes.  For
189     * torque current control, set the PeakForwardTorqueCurrent and
190     * PeakReverseTorqueCurrent in TorqueCurrentConfigs instead.
191     * <p>
192     * Supply current is the current drawn from the battery, so this limit
193     * can be used to prevent breaker trips and improve battery longevity.
194     *  Additionally, in the rare case where the robot experiences
195     * brownouts despite configuring stator current limits, a supply
196     * current limit can further help avoid brownouts. However, such
197     * brownouts are most commonly caused by a bad battery or poor power
198     * wiring.
199     * <p>
200     * A reasonable starting point for a supply current limit is 60 A with
201     * a threshold of 80 A for 0.1 seconds. Supply current limits commonly
202     * range from 20-80 A depending on the breaker used.
203     * 
204     * <ul>
205     *   <li> <b>Minimum Value:</b> 0.0
206     *   <li> <b>Maximum Value:</b> 800.0
207     *   <li> <b>Default Value:</b> 0
208     *   <li> <b>Units:</b> A
209     * </ul>
210     *
211     * @param newSupplyCurrentLimit Parameter to modify
212     * @return Itself
213     */
214    public CurrentLimitsConfigs withSupplyCurrentLimit(double newSupplyCurrentLimit)
215    {
216        SupplyCurrentLimit = newSupplyCurrentLimit;
217        return this;
218    }
219    /**
220     * Modifies this configuration's SupplyCurrentLimitEnable parameter and returns itself for
221     * method-chaining and easier to use config API.
222     * <p>
223     * Enable motor supply current limiting.
224     * 
225     * <ul>
226     *   <li> <b>Default Value:</b> False
227     * </ul>
228     *
229     * @param newSupplyCurrentLimitEnable Parameter to modify
230     * @return Itself
231     */
232    public CurrentLimitsConfigs withSupplyCurrentLimitEnable(boolean newSupplyCurrentLimitEnable)
233    {
234        SupplyCurrentLimitEnable = newSupplyCurrentLimitEnable;
235        return this;
236    }
237    /**
238     * Modifies this configuration's SupplyCurrentThreshold parameter and returns itself for
239     * method-chaining and easier to use config API.
240     * <p>
241     * Delay supply current limiting until current exceeds this threshold
242     * for longer than SupplyTimeThreshold.  This allows current draws
243     * above SupplyCurrentLimit for a fixed period of time.  This has no
244     * effect if SupplyCurrentLimit is greater than this value.
245     * 
246     * <ul>
247     *   <li> <b>Minimum Value:</b> 0.0
248     *   <li> <b>Maximum Value:</b> 511
249     *   <li> <b>Default Value:</b> 0
250     *   <li> <b>Units:</b> A
251     * </ul>
252     *
253     * @param newSupplyCurrentThreshold Parameter to modify
254     * @return Itself
255     */
256    public CurrentLimitsConfigs withSupplyCurrentThreshold(double newSupplyCurrentThreshold)
257    {
258        SupplyCurrentThreshold = newSupplyCurrentThreshold;
259        return this;
260    }
261    /**
262     * Modifies this configuration's SupplyTimeThreshold parameter and returns itself for
263     * method-chaining and easier to use config API.
264     * <p>
265     * Allows unlimited current for a period of time before current
266     * limiting occurs.  Current threshold is the maximum of
267     * SupplyCurrentThreshold and SupplyCurrentLimit.
268     * 
269     * <ul>
270     *   <li> <b>Minimum Value:</b> 0.0
271     *   <li> <b>Maximum Value:</b> 1.275
272     *   <li> <b>Default Value:</b> 0
273     *   <li> <b>Units:</b> sec
274     * </ul>
275     *
276     * @param newSupplyTimeThreshold Parameter to modify
277     * @return Itself
278     */
279    public CurrentLimitsConfigs withSupplyTimeThreshold(double newSupplyTimeThreshold)
280    {
281        SupplyTimeThreshold = newSupplyTimeThreshold;
282        return this;
283    }
284
285    
286
287    @Override
288    public String toString()
289    {
290        String ss = "Config Group: CurrentLimits\n";
291        ss += "Name: \"StatorCurrentLimit\" Value: \"" + StatorCurrentLimit + "A\"" + "\n";
292        ss += "Name: \"StatorCurrentLimitEnable\" Value: \"" + StatorCurrentLimitEnable + "\"" + "\n";
293        ss += "Name: \"SupplyCurrentLimit\" Value: \"" + SupplyCurrentLimit + "A\"" + "\n";
294        ss += "Name: \"SupplyCurrentLimitEnable\" Value: \"" + SupplyCurrentLimitEnable + "\"" + "\n";
295        ss += "Name: \"SupplyCurrentThreshold\" Value: \"" + SupplyCurrentThreshold + "A\"" + "\n";
296        ss += "Name: \"SupplyTimeThreshold\" Value: \"" + SupplyTimeThreshold + "sec\"" + "\n";
297        return ss;
298    }
299
300    /**
301     *
302     */
303    public StatusCode deserialize(String to_deserialize)
304    {
305        StatorCurrentLimit = ConfigJNI.Deserializedouble(SpnValue.Config_StatorCurrentLimit.value, to_deserialize);
306        StatorCurrentLimitEnable = ConfigJNI.Deserializeboolean(SpnValue.Config_StatorCurrLimitEn.value, to_deserialize);
307        SupplyCurrentLimit = ConfigJNI.Deserializedouble(SpnValue.Config_SupplyCurrentLimit.value, to_deserialize);
308        SupplyCurrentLimitEnable = ConfigJNI.Deserializeboolean(SpnValue.Config_SupplyCurrLimitEn.value, to_deserialize);
309        SupplyCurrentThreshold = ConfigJNI.Deserializedouble(SpnValue.Config_SupplyCurrThres.value, to_deserialize);
310        SupplyTimeThreshold = ConfigJNI.Deserializedouble(SpnValue.Config_SupplyTimeThres.value, to_deserialize);
311        return  StatusCode.OK;
312    }
313
314    /**
315     *
316     */
317    public String serialize()
318    {
319        String ss = "";
320        ss += ConfigJNI.Serializedouble(SpnValue.Config_StatorCurrentLimit.value, StatorCurrentLimit);
321        ss += ConfigJNI.Serializeboolean(SpnValue.Config_StatorCurrLimitEn.value, StatorCurrentLimitEnable);
322        ss += ConfigJNI.Serializedouble(SpnValue.Config_SupplyCurrentLimit.value, SupplyCurrentLimit);
323        ss += ConfigJNI.Serializeboolean(SpnValue.Config_SupplyCurrLimitEn.value, SupplyCurrentLimitEnable);
324        ss += ConfigJNI.Serializedouble(SpnValue.Config_SupplyCurrThres.value, SupplyCurrentThreshold);
325        ss += ConfigJNI.Serializedouble(SpnValue.Config_SupplyTimeThres.value, SupplyTimeThreshold);
326        return ss;
327    }
328}
329