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.hardware.DeviceIdentifier;
011import com.ctre.phoenix6.configs.jni.ConfigJNI;
012import com.ctre.phoenix6.spns.*;
013import edu.wpi.first.units.*;
014import edu.wpi.first.units.measure.*;
015import static edu.wpi.first.units.Units.*;
016
017/**
018 * Class for CANcoder, a CAN based magnetic encoder that provides absolute and
019 * relative position along with filtered velocity.
020 *
021 * This handles the configurations for the {@link com.ctre.phoenix6.hardware.CANcoder}
022 */
023public class CANcoderConfigurator extends ParentConfigurator
024{
025    public CANcoderConfigurator (DeviceIdentifier id)
026    {
027        super(id);
028    }
029
030    /**
031     * Refreshes the values of the specified config group.
032     * <p>
033     * This will wait up to {@link #DefaultTimeoutSeconds}.
034     * <p>
035     * Call to refresh the selected configs from the device.
036     *
037     * @param configs The configs to refresh
038     * @return StatusCode of refreshing the configs
039     */
040    public StatusCode refresh(CANcoderConfiguration configs)
041    {
042        return refresh(configs, DefaultTimeoutSeconds);
043    }
044
045    /**
046     * Refreshes the values of the specified config group.
047     * <p>
048     * Call to refresh the selected configs from the device.
049     *
050     * @param configs The configs to refresh
051     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
052     * @return StatusCode of refreshing the configs
053     */
054    public StatusCode refresh(CANcoderConfiguration configs, double timeoutSeconds)
055    {
056        StringBuilder serializedString = new StringBuilder();
057        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
058        if (err == StatusCode.OK) {
059            /* Only deserialize if we successfully got configs */
060            configs.deserialize(serializedString.toString());
061        }
062        return err;
063    }
064
065    /**
066     * Applies the contents of the specified config to the device.
067     * <p>
068     * This will wait up to {@link #DefaultTimeoutSeconds}.
069     * <p>
070     * Call to apply the selected configs.
071     *
072     * @param configs Configs to apply against.
073     * @return StatusCode of the set command
074     */
075    public StatusCode apply(CANcoderConfiguration configs)
076    {
077        return apply(configs, DefaultTimeoutSeconds);
078    }
079
080    /**
081     * Applies the contents of the specified config to the device.
082     * <p>
083     * Call to apply the selected configs.
084     *
085     * @param configs Configs to apply against.
086     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
087     * @return StatusCode of the set command
088     */
089    public StatusCode apply(CANcoderConfiguration configs, double timeoutSeconds)
090    {
091        return setConfigsPrivate(configs.serialize(), timeoutSeconds, configs.FutureProofConfigs, false);
092    }
093
094
095    /**
096     * Refreshes the values of the specified config group.
097     * <p>
098     * This will wait up to {@link #DefaultTimeoutSeconds}.
099     * <p>
100     * Call to refresh the selected configs from the device.
101     *
102     * @param configs The configs to refresh
103     * @return StatusCode of refreshing the configs
104     */
105    public StatusCode refresh(MagnetSensorConfigs configs)
106    {
107        return refresh(configs, DefaultTimeoutSeconds);
108    }
109
110    /**
111     * Refreshes the values of the specified config group.
112     * <p>
113     * Call to refresh the selected configs from the device.
114     *
115     * @param configs The configs to refresh
116     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
117     * @return StatusCode of refreshing the configs
118     */
119    public StatusCode refresh(MagnetSensorConfigs configs, double timeoutSeconds)
120    {
121        StringBuilder serializedString = new StringBuilder();
122        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
123        if (err == StatusCode.OK) {
124            /* Only deserialize if we successfully got configs */
125            configs.deserialize(serializedString.toString());
126        }
127        return err;
128    }
129
130    /**
131     * Applies the contents of the specified config to the device.
132     * <p>
133     * This will wait up to {@link #DefaultTimeoutSeconds}.
134     * <p>
135     * Call to apply the selected configs.
136     *
137     * @param configs Configs to apply against.
138     * @return StatusCode of the set command
139     */
140    public StatusCode apply(MagnetSensorConfigs configs)
141    {
142        return apply(configs, DefaultTimeoutSeconds);
143    }
144
145    /**
146     * Applies the contents of the specified config to the device.
147     * <p>
148     * Call to apply the selected configs.
149     *
150     * @param configs Configs to apply against.
151     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
152     * @return StatusCode of the set command
153     */
154    public StatusCode apply(MagnetSensorConfigs configs, double timeoutSeconds)
155    {
156        return setConfigsPrivate(configs.serialize(), timeoutSeconds, false, false);
157    }
158
159
160    /**
161     * Refreshes the values of the specified config group.
162     * <p>
163     * This will wait up to {@link #DefaultTimeoutSeconds}.
164     * <p>
165     * Call to refresh the selected configs from the device.
166     *
167     * @param configs The configs to refresh
168     * @return StatusCode of refreshing the configs
169     */
170    public StatusCode refresh(CustomParamsConfigs configs)
171    {
172        return refresh(configs, DefaultTimeoutSeconds);
173    }
174
175    /**
176     * Refreshes the values of the specified config group.
177     * <p>
178     * Call to refresh the selected configs from the device.
179     *
180     * @param configs The configs to refresh
181     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
182     * @return StatusCode of refreshing the configs
183     */
184    public StatusCode refresh(CustomParamsConfigs configs, double timeoutSeconds)
185    {
186        StringBuilder serializedString = new StringBuilder();
187        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
188        if (err == StatusCode.OK) {
189            /* Only deserialize if we successfully got configs */
190            configs.deserialize(serializedString.toString());
191        }
192        return err;
193    }
194
195    /**
196     * Applies the contents of the specified config to the device.
197     * <p>
198     * This will wait up to {@link #DefaultTimeoutSeconds}.
199     * <p>
200     * Call to apply the selected configs.
201     *
202     * @param configs Configs to apply against.
203     * @return StatusCode of the set command
204     */
205    public StatusCode apply(CustomParamsConfigs configs)
206    {
207        return apply(configs, DefaultTimeoutSeconds);
208    }
209
210    /**
211     * Applies the contents of the specified config to the device.
212     * <p>
213     * Call to apply the selected configs.
214     *
215     * @param configs Configs to apply against.
216     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
217     * @return StatusCode of the set command
218     */
219    public StatusCode apply(CustomParamsConfigs configs, double timeoutSeconds)
220    {
221        return setConfigsPrivate(configs.serialize(), timeoutSeconds, false, false);
222    }
223
224    
225    /**
226     * Sets the current position of the device.
227     * <p>
228     * This will wait up to {@link #DefaultTimeoutSeconds}.
229     * <p>
230     * This is available in the configurator in case the user wants
231     * to initialize their device entirely without passing a device
232     * reference down to the code that performs the initialization.
233     * In this case, the user passes down the configurator object
234     * and performs all the initialization code on the object.
235     * 
236     * @param newValue Value to set to. Units are in rotations.
237     * @return StatusCode of the set command
238     */
239    public StatusCode setPosition(double newValue) {
240        return setPosition(newValue, DefaultTimeoutSeconds);
241    }
242    /**
243     * Sets the current position of the device.
244     * <p>
245     * This is available in the configurator in case the user wants
246     * to initialize their device entirely without passing a device
247     * reference down to the code that performs the initialization.
248     * In this case, the user passes down the configurator object
249     * and performs all the initialization code on the object.
250     * 
251     * @param newValue Value to set to. Units are in rotations.
252     * @param timeoutSeconds Maximum time to wait up to in seconds.
253     * @return StatusCode of the set command
254     */
255    public StatusCode setPosition(double newValue, double timeoutSeconds) {
256        String serialized = ConfigJNI.Serializedouble(SpnValue.CANcoder_SetSensorPosition.value, newValue);
257        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
258    }
259    
260    /**
261     * Sets the current position of the device.
262     * <p>
263     * This will wait up to {@link #DefaultTimeoutSeconds}.
264     * <p>
265     * This is available in the configurator in case the user wants
266     * to initialize their device entirely without passing a device
267     * reference down to the code that performs the initialization.
268     * In this case, the user passes down the configurator object
269     * and performs all the initialization code on the object.
270     * 
271     * @param newValue Value to set to. Units are in rotations.
272     * @return StatusCode of the set command
273     */
274    public StatusCode setPosition(Angle newValue) {
275        return setPosition(newValue.in(Rotations));
276    }
277    /**
278     * Sets the current position of the device.
279     * <p>
280     * This is available in the configurator in case the user wants
281     * to initialize their device entirely without passing a device
282     * reference down to the code that performs the initialization.
283     * In this case, the user passes down the configurator object
284     * and performs all the initialization code on the object.
285     * 
286     * @param newValue Value to set to. Units are in rotations.
287     * @param timeoutSeconds Maximum time to wait up to in seconds.
288     * @return StatusCode of the set command
289     */
290    public StatusCode setPosition(Angle newValue, double timeoutSeconds) {
291        return setPosition(newValue.in(Rotations), timeoutSeconds);
292    }
293    
294    /**
295     * Clear the sticky faults in the device.
296     * <p>
297     * This typically has no impact on the device functionality.  Instead,
298     * it just clears telemetry faults that are accessible via API and
299     * Tuner Self-Test.
300     * <p>
301     * This will wait up to {@link #DefaultTimeoutSeconds}.
302     * <p>
303     * This is available in the configurator in case the user wants
304     * to initialize their device entirely without passing a device
305     * reference down to the code that performs the initialization.
306     * In this case, the user passes down the configurator object
307     * and performs all the initialization code on the object.
308     * 
309     * @return StatusCode of the set command
310     */
311    public StatusCode clearStickyFaults() {
312        return clearStickyFaults(DefaultTimeoutSeconds);
313    }
314    /**
315     * Clear the sticky faults in the device.
316     * <p>
317     * This typically has no impact on the device functionality.  Instead,
318     * it just clears telemetry faults that are accessible via API and
319     * Tuner Self-Test.
320     * <p>
321     * This is available in the configurator in case the user wants
322     * to initialize their device entirely without passing a device
323     * reference down to the code that performs the initialization.
324     * In this case, the user passes down the configurator object
325     * and performs all the initialization code on the object.
326     * 
327     * @param timeoutSeconds Maximum time to wait up to in seconds.
328     * @return StatusCode of the set command
329     */
330    public StatusCode clearStickyFaults(double timeoutSeconds) {
331        String serialized = ConfigJNI.Serializedouble(SpnValue.SPN_ClearStickyFaults.value, 0);
332        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
333    }
334    
335    /**
336     * Clear sticky fault: Hardware fault occurred
337     * <p>
338     * This will wait up to {@link #DefaultTimeoutSeconds}.
339     * <p>
340     * This is available in the configurator in case the user wants
341     * to initialize their device entirely without passing a device
342     * reference down to the code that performs the initialization.
343     * In this case, the user passes down the configurator object
344     * and performs all the initialization code on the object.
345     * 
346     * @return StatusCode of the set command
347     */
348    public StatusCode clearStickyFault_Hardware() {
349        return clearStickyFault_Hardware(DefaultTimeoutSeconds);
350    }
351    /**
352     * Clear sticky fault: Hardware fault occurred
353     * <p>
354     * This is available in the configurator in case the user wants
355     * to initialize their device entirely without passing a device
356     * reference down to the code that performs the initialization.
357     * In this case, the user passes down the configurator object
358     * and performs all the initialization code on the object.
359     * 
360     * @param timeoutSeconds Maximum time to wait up to in seconds.
361     * @return StatusCode of the set command
362     */
363    public StatusCode clearStickyFault_Hardware(double timeoutSeconds) {
364        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_Hardware.value, 0);
365        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
366    }
367    
368    /**
369     * Clear sticky fault: Device supply voltage dropped to near brownout
370     * levels
371     * <p>
372     * This will wait up to {@link #DefaultTimeoutSeconds}.
373     * <p>
374     * This is available in the configurator in case the user wants
375     * to initialize their device entirely without passing a device
376     * reference down to the code that performs the initialization.
377     * In this case, the user passes down the configurator object
378     * and performs all the initialization code on the object.
379     * 
380     * @return StatusCode of the set command
381     */
382    public StatusCode clearStickyFault_Undervoltage() {
383        return clearStickyFault_Undervoltage(DefaultTimeoutSeconds);
384    }
385    /**
386     * Clear sticky fault: Device supply voltage dropped to near brownout
387     * levels
388     * <p>
389     * This is available in the configurator in case the user wants
390     * to initialize their device entirely without passing a device
391     * reference down to the code that performs the initialization.
392     * In this case, the user passes down the configurator object
393     * and performs all the initialization code on the object.
394     * 
395     * @param timeoutSeconds Maximum time to wait up to in seconds.
396     * @return StatusCode of the set command
397     */
398    public StatusCode clearStickyFault_Undervoltage(double timeoutSeconds) {
399        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_Undervoltage.value, 0);
400        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
401    }
402    
403    /**
404     * Clear sticky fault: Device boot while detecting the enable signal
405     * <p>
406     * This will wait up to {@link #DefaultTimeoutSeconds}.
407     * <p>
408     * This is available in the configurator in case the user wants
409     * to initialize their device entirely without passing a device
410     * reference down to the code that performs the initialization.
411     * In this case, the user passes down the configurator object
412     * and performs all the initialization code on the object.
413     * 
414     * @return StatusCode of the set command
415     */
416    public StatusCode clearStickyFault_BootDuringEnable() {
417        return clearStickyFault_BootDuringEnable(DefaultTimeoutSeconds);
418    }
419    /**
420     * Clear sticky fault: Device boot while detecting the enable signal
421     * <p>
422     * This is available in the configurator in case the user wants
423     * to initialize their device entirely without passing a device
424     * reference down to the code that performs the initialization.
425     * In this case, the user passes down the configurator object
426     * and performs all the initialization code on the object.
427     * 
428     * @param timeoutSeconds Maximum time to wait up to in seconds.
429     * @return StatusCode of the set command
430     */
431    public StatusCode clearStickyFault_BootDuringEnable(double timeoutSeconds) {
432        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_BootDuringEnable.value, 0);
433        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
434    }
435    
436    /**
437     * Clear sticky fault: An unlicensed feature is in use, device may not
438     * behave as expected.
439     * <p>
440     * This will wait up to {@link #DefaultTimeoutSeconds}.
441     * <p>
442     * This is available in the configurator in case the user wants
443     * to initialize their device entirely without passing a device
444     * reference down to the code that performs the initialization.
445     * In this case, the user passes down the configurator object
446     * and performs all the initialization code on the object.
447     * 
448     * @return StatusCode of the set command
449     */
450    public StatusCode clearStickyFault_UnlicensedFeatureInUse() {
451        return clearStickyFault_UnlicensedFeatureInUse(DefaultTimeoutSeconds);
452    }
453    /**
454     * Clear sticky fault: An unlicensed feature is in use, device may not
455     * behave as expected.
456     * <p>
457     * This is available in the configurator in case the user wants
458     * to initialize their device entirely without passing a device
459     * reference down to the code that performs the initialization.
460     * In this case, the user passes down the configurator object
461     * and performs all the initialization code on the object.
462     * 
463     * @param timeoutSeconds Maximum time to wait up to in seconds.
464     * @return StatusCode of the set command
465     */
466    public StatusCode clearStickyFault_UnlicensedFeatureInUse(double timeoutSeconds) {
467        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_UnlicensedFeatureInUse.value, 0);
468        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
469    }
470    
471    /**
472     * Clear sticky fault: The magnet distance is not correct or magnet is
473     * missing
474     * <p>
475     * This will wait up to {@link #DefaultTimeoutSeconds}.
476     * <p>
477     * This is available in the configurator in case the user wants
478     * to initialize their device entirely without passing a device
479     * reference down to the code that performs the initialization.
480     * In this case, the user passes down the configurator object
481     * and performs all the initialization code on the object.
482     * 
483     * @return StatusCode of the set command
484     */
485    public StatusCode clearStickyFault_BadMagnet() {
486        return clearStickyFault_BadMagnet(DefaultTimeoutSeconds);
487    }
488    /**
489     * Clear sticky fault: The magnet distance is not correct or magnet is
490     * missing
491     * <p>
492     * This is available in the configurator in case the user wants
493     * to initialize their device entirely without passing a device
494     * reference down to the code that performs the initialization.
495     * In this case, the user passes down the configurator object
496     * and performs all the initialization code on the object.
497     * 
498     * @param timeoutSeconds Maximum time to wait up to in seconds.
499     * @return StatusCode of the set command
500     */
501    public StatusCode clearStickyFault_BadMagnet(double timeoutSeconds) {
502        String serialized = ConfigJNI.Serializedouble(SpnValue.ClearStickyFault_CANCODER_BadMagnet.value, 0);
503        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
504    }
505}