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 affect the magnet sensor and how to interpret it.
021 * <p>
022 * Includes sensor direction, the sensor discontinuity point, and the
023 * magnet offset.
024 */
025public class MagnetSensorConfigs implements ParentConfiguration, Cloneable {
026    /**
027     * Direction of the sensor to determine positive rotation, as seen
028     * facing the LED side of the CANcoder.
029     * 
030     */
031    public SensorDirectionValue SensorDirection = SensorDirectionValue.CounterClockwise_Positive;
032    /**
033     * This offset is added to the reported position, allowing the
034     * application to trim the zero position.  When set to the default
035     * value of zero, position reports zero when magnet north pole aligns
036     * with the LED.
037     * 
038     * <ul>
039     *   <li> <b>Minimum Value:</b> -1
040     *   <li> <b>Maximum Value:</b> 1
041     *   <li> <b>Default Value:</b> 0
042     *   <li> <b>Units:</b> rotations
043     * </ul>
044     */
045    public double MagnetOffset = 0;
046    /**
047     * The positive discontinuity point of the absolute sensor in
048     * rotations. This determines the point at which the absolute sensor
049     * wraps around, keeping the absolute position (after offset) in the
050     * range [x-1, x).
051     * 
052     * <ul>
053     *   <li> Setting this to 1 makes the absolute position unsigned [0,
054     * 1)
055     *   <li> Setting this to 0.5 makes the absolute position signed
056     * [-0.5, 0.5)
057     *   <li> Setting this to 0 makes the absolute position always
058     * negative [-1, 0)
059     * </ul>
060     * 
061     * Many rotational mechanisms such as arms have a region of motion
062     * that is unreachable. This should be set to the center of that
063     * region of motion, in non-negative rotations. This affects the
064     * position of the device at bootup.
065     * <p>
066     * For example, consider an arm which can travel from -0.2 to 0.6
067     * rotations with a little leeway, where 0 is horizontally forward.
068     * Since -0.2 rotations has the same absolute position as 0.8
069     * rotations, we can say that the arm typically does not travel in the
070     * range (0.6, 0.8) rotations. As a result, the discontinuity point
071     * would be the center of that range, which is 0.7 rotations. This
072     * results in an absolute sensor range of [-0.3, 0.7) rotations.
073     * <p>
074     * Given a total range of motion less than 1 rotation, users can
075     * calculate the discontinuity point using mean(lowerLimit,
076     * upperLimit) + 0.5. If that results in a value outside the range [0,
077     * 1], either cap the value to [0, 1], or add/subtract 1.0 rotation
078     * from your lower and upper limits of motion.
079     * <p>
080     * On a Talon motor controller, this is only supported when using the
081     * PulseWidth sensor source.
082     * 
083     * <ul>
084     *   <li> <b>Minimum Value:</b> 0.0
085     *   <li> <b>Maximum Value:</b> 1.0
086     *   <li> <b>Default Value:</b> 0.5
087     *   <li> <b>Units:</b> rotations
088     * </ul>
089     */
090    public double AbsoluteSensorDiscontinuityPoint = 0.5;
091    
092    /**
093     * Modifies this configuration's SensorDirection parameter and returns itself for
094     * method-chaining and easier to use config API.
095     * <p>
096     * Direction of the sensor to determine positive rotation, as seen
097     * facing the LED side of the CANcoder.
098     * 
099     *
100     * @param newSensorDirection Parameter to modify
101     * @return Itself
102     */
103    public final MagnetSensorConfigs withSensorDirection(SensorDirectionValue newSensorDirection)
104    {
105        SensorDirection = newSensorDirection;
106        return this;
107    }
108    
109    /**
110     * Modifies this configuration's MagnetOffset parameter and returns itself for
111     * method-chaining and easier to use config API.
112     * <p>
113     * This offset is added to the reported position, allowing the
114     * application to trim the zero position.  When set to the default
115     * value of zero, position reports zero when magnet north pole aligns
116     * with the LED.
117     * 
118     * <ul>
119     *   <li> <b>Minimum Value:</b> -1
120     *   <li> <b>Maximum Value:</b> 1
121     *   <li> <b>Default Value:</b> 0
122     *   <li> <b>Units:</b> rotations
123     * </ul>
124     *
125     * @param newMagnetOffset Parameter to modify
126     * @return Itself
127     */
128    public final MagnetSensorConfigs withMagnetOffset(double newMagnetOffset)
129    {
130        MagnetOffset = newMagnetOffset;
131        return this;
132    }
133    
134    /**
135     * Modifies this configuration's MagnetOffset parameter and returns itself for
136     * method-chaining and easier to use config API.
137     * <p>
138     * This offset is added to the reported position, allowing the
139     * application to trim the zero position.  When set to the default
140     * value of zero, position reports zero when magnet north pole aligns
141     * with the LED.
142     * 
143     * <ul>
144     *   <li> <b>Minimum Value:</b> -1
145     *   <li> <b>Maximum Value:</b> 1
146     *   <li> <b>Default Value:</b> 0
147     *   <li> <b>Units:</b> rotations
148     * </ul>
149     *
150     * @param newMagnetOffset Parameter to modify
151     * @return Itself
152     */
153    public final MagnetSensorConfigs withMagnetOffset(Angle newMagnetOffset)
154    {
155        MagnetOffset = newMagnetOffset.in(Rotations);
156        return this;
157    }
158    
159    /**
160     * Helper method to get this configuration's MagnetOffset parameter converted
161     * to a unit type. If not using the Java units library, {@link #MagnetOffset}
162     * can be accessed directly instead.
163     * <p>
164     * This offset is added to the reported position, allowing the
165     * application to trim the zero position.  When set to the default
166     * value of zero, position reports zero when magnet north pole aligns
167     * with the LED.
168     * 
169     * <ul>
170     *   <li> <b>Minimum Value:</b> -1
171     *   <li> <b>Maximum Value:</b> 1
172     *   <li> <b>Default Value:</b> 0
173     *   <li> <b>Units:</b> rotations
174     * </ul>
175     *
176     * @return MagnetOffset
177     */
178    public final Angle getMagnetOffsetMeasure()
179    {
180        return Rotations.of(MagnetOffset);
181    }
182    
183    /**
184     * Modifies this configuration's AbsoluteSensorDiscontinuityPoint parameter and returns itself for
185     * method-chaining and easier to use config API.
186     * <p>
187     * The positive discontinuity point of the absolute sensor in
188     * rotations. This determines the point at which the absolute sensor
189     * wraps around, keeping the absolute position (after offset) in the
190     * range [x-1, x).
191     * 
192     * <ul>
193     *   <li> Setting this to 1 makes the absolute position unsigned [0,
194     * 1)
195     *   <li> Setting this to 0.5 makes the absolute position signed
196     * [-0.5, 0.5)
197     *   <li> Setting this to 0 makes the absolute position always
198     * negative [-1, 0)
199     * </ul>
200     * 
201     * Many rotational mechanisms such as arms have a region of motion
202     * that is unreachable. This should be set to the center of that
203     * region of motion, in non-negative rotations. This affects the
204     * position of the device at bootup.
205     * <p>
206     * For example, consider an arm which can travel from -0.2 to 0.6
207     * rotations with a little leeway, where 0 is horizontally forward.
208     * Since -0.2 rotations has the same absolute position as 0.8
209     * rotations, we can say that the arm typically does not travel in the
210     * range (0.6, 0.8) rotations. As a result, the discontinuity point
211     * would be the center of that range, which is 0.7 rotations. This
212     * results in an absolute sensor range of [-0.3, 0.7) rotations.
213     * <p>
214     * Given a total range of motion less than 1 rotation, users can
215     * calculate the discontinuity point using mean(lowerLimit,
216     * upperLimit) + 0.5. If that results in a value outside the range [0,
217     * 1], either cap the value to [0, 1], or add/subtract 1.0 rotation
218     * from your lower and upper limits of motion.
219     * <p>
220     * On a Talon motor controller, this is only supported when using the
221     * PulseWidth sensor source.
222     * 
223     * <ul>
224     *   <li> <b>Minimum Value:</b> 0.0
225     *   <li> <b>Maximum Value:</b> 1.0
226     *   <li> <b>Default Value:</b> 0.5
227     *   <li> <b>Units:</b> rotations
228     * </ul>
229     *
230     * @param newAbsoluteSensorDiscontinuityPoint Parameter to modify
231     * @return Itself
232     */
233    public final MagnetSensorConfigs withAbsoluteSensorDiscontinuityPoint(double newAbsoluteSensorDiscontinuityPoint)
234    {
235        AbsoluteSensorDiscontinuityPoint = newAbsoluteSensorDiscontinuityPoint;
236        return this;
237    }
238    
239    /**
240     * Modifies this configuration's AbsoluteSensorDiscontinuityPoint parameter and returns itself for
241     * method-chaining and easier to use config API.
242     * <p>
243     * The positive discontinuity point of the absolute sensor in
244     * rotations. This determines the point at which the absolute sensor
245     * wraps around, keeping the absolute position (after offset) in the
246     * range [x-1, x).
247     * 
248     * <ul>
249     *   <li> Setting this to 1 makes the absolute position unsigned [0,
250     * 1)
251     *   <li> Setting this to 0.5 makes the absolute position signed
252     * [-0.5, 0.5)
253     *   <li> Setting this to 0 makes the absolute position always
254     * negative [-1, 0)
255     * </ul>
256     * 
257     * Many rotational mechanisms such as arms have a region of motion
258     * that is unreachable. This should be set to the center of that
259     * region of motion, in non-negative rotations. This affects the
260     * position of the device at bootup.
261     * <p>
262     * For example, consider an arm which can travel from -0.2 to 0.6
263     * rotations with a little leeway, where 0 is horizontally forward.
264     * Since -0.2 rotations has the same absolute position as 0.8
265     * rotations, we can say that the arm typically does not travel in the
266     * range (0.6, 0.8) rotations. As a result, the discontinuity point
267     * would be the center of that range, which is 0.7 rotations. This
268     * results in an absolute sensor range of [-0.3, 0.7) rotations.
269     * <p>
270     * Given a total range of motion less than 1 rotation, users can
271     * calculate the discontinuity point using mean(lowerLimit,
272     * upperLimit) + 0.5. If that results in a value outside the range [0,
273     * 1], either cap the value to [0, 1], or add/subtract 1.0 rotation
274     * from your lower and upper limits of motion.
275     * <p>
276     * On a Talon motor controller, this is only supported when using the
277     * PulseWidth sensor source.
278     * 
279     * <ul>
280     *   <li> <b>Minimum Value:</b> 0.0
281     *   <li> <b>Maximum Value:</b> 1.0
282     *   <li> <b>Default Value:</b> 0.5
283     *   <li> <b>Units:</b> rotations
284     * </ul>
285     *
286     * @param newAbsoluteSensorDiscontinuityPoint Parameter to modify
287     * @return Itself
288     */
289    public final MagnetSensorConfigs withAbsoluteSensorDiscontinuityPoint(Angle newAbsoluteSensorDiscontinuityPoint)
290    {
291        AbsoluteSensorDiscontinuityPoint = newAbsoluteSensorDiscontinuityPoint.in(Rotations);
292        return this;
293    }
294    
295    /**
296     * Helper method to get this configuration's AbsoluteSensorDiscontinuityPoint parameter converted
297     * to a unit type. If not using the Java units library, {@link #AbsoluteSensorDiscontinuityPoint}
298     * can be accessed directly instead.
299     * <p>
300     * The positive discontinuity point of the absolute sensor in
301     * rotations. This determines the point at which the absolute sensor
302     * wraps around, keeping the absolute position (after offset) in the
303     * range [x-1, x).
304     * 
305     * <ul>
306     *   <li> Setting this to 1 makes the absolute position unsigned [0,
307     * 1)
308     *   <li> Setting this to 0.5 makes the absolute position signed
309     * [-0.5, 0.5)
310     *   <li> Setting this to 0 makes the absolute position always
311     * negative [-1, 0)
312     * </ul>
313     * 
314     * Many rotational mechanisms such as arms have a region of motion
315     * that is unreachable. This should be set to the center of that
316     * region of motion, in non-negative rotations. This affects the
317     * position of the device at bootup.
318     * <p>
319     * For example, consider an arm which can travel from -0.2 to 0.6
320     * rotations with a little leeway, where 0 is horizontally forward.
321     * Since -0.2 rotations has the same absolute position as 0.8
322     * rotations, we can say that the arm typically does not travel in the
323     * range (0.6, 0.8) rotations. As a result, the discontinuity point
324     * would be the center of that range, which is 0.7 rotations. This
325     * results in an absolute sensor range of [-0.3, 0.7) rotations.
326     * <p>
327     * Given a total range of motion less than 1 rotation, users can
328     * calculate the discontinuity point using mean(lowerLimit,
329     * upperLimit) + 0.5. If that results in a value outside the range [0,
330     * 1], either cap the value to [0, 1], or add/subtract 1.0 rotation
331     * from your lower and upper limits of motion.
332     * <p>
333     * On a Talon motor controller, this is only supported when using the
334     * PulseWidth sensor source.
335     * 
336     * <ul>
337     *   <li> <b>Minimum Value:</b> 0.0
338     *   <li> <b>Maximum Value:</b> 1.0
339     *   <li> <b>Default Value:</b> 0.5
340     *   <li> <b>Units:</b> rotations
341     * </ul>
342     *
343     * @return AbsoluteSensorDiscontinuityPoint
344     */
345    public final Angle getAbsoluteSensorDiscontinuityPointMeasure()
346    {
347        return Rotations.of(AbsoluteSensorDiscontinuityPoint);
348    }
349
350    
351
352    @Override
353    public String toString() {
354        String ss = "Config Group: MagnetSensor\n";
355        ss += "    SensorDirection: " + SensorDirection + "\n";
356        ss += "    MagnetOffset: " + MagnetOffset + " rotations" + "\n";
357        ss += "    AbsoluteSensorDiscontinuityPoint: " + AbsoluteSensorDiscontinuityPoint + " rotations" + "\n";
358        return ss;
359    }
360
361    /**
362     * Get the serialized form of this configuration group.
363     *
364     * @return Serialized form of this config group
365     */
366    @Override
367    public final String serialize() {
368        String ss = "";
369        ss += ConfigJNI.Serializeint(SpnValue.CANcoder_SensorDirection.value, SensorDirection.value);
370        ss += ConfigJNI.Serializedouble(SpnValue.CANCoder_MagnetOffset.value, MagnetOffset);
371        ss += ConfigJNI.Serializedouble(SpnValue.Config_AbsoluteSensorDiscontinuityPoint.value, AbsoluteSensorDiscontinuityPoint);
372        return ss;
373    }
374
375    /**
376     * Take a string and deserialize it to this configuration group.
377     *
378     * @return Return code of the deserialize method
379     */
380    @Override
381    public final StatusCode deserialize(String to_deserialize) {
382        SensorDirection = SensorDirectionValue.valueOf(ConfigJNI.Deserializeint(SpnValue.CANcoder_SensorDirection.value, to_deserialize));
383        MagnetOffset = ConfigJNI.Deserializedouble(SpnValue.CANCoder_MagnetOffset.value, to_deserialize);
384        AbsoluteSensorDiscontinuityPoint = ConfigJNI.Deserializedouble(SpnValue.Config_AbsoluteSensorDiscontinuityPoint.value, to_deserialize);
385        return  StatusCode.OK;
386    }
387
388    @Override
389    public MagnetSensorConfigs clone() {
390        try {
391            return (MagnetSensorConfigs)super.clone();
392        } catch (CloneNotSupportedException ex) {
393            /* this should never happen */
394            throw new RuntimeException(ex);
395        }
396    }
397}
398