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 related to CANdi's PWM interface on the Signal 2 input
020 * (S2IN)
021 * <p>
022 * All the configs related to the PWM interface for CANdi on S1,
023 * including absolute sensor offset, absolute sensor discontinuity
024 * point and sensor direction.
025 */
026public class PWM2Configs implements ParentConfiguration
027{
028    /**
029     * The offset applied to the PWM sensor.
030     * <p>
031     * This can be used to zero the sensor position in applications where
032     * the sensor is 1:1 with the mechanism.
033     * 
034     * <ul>
035     *   <li> <b>Minimum Value:</b> -1
036     *   <li> <b>Maximum Value:</b> 1
037     *   <li> <b>Default Value:</b> 0.0
038     *   <li> <b>Units:</b> rotations
039     * </ul>
040     */
041    public double AbsoluteSensorOffset = 0.0;
042    /**
043     * The positive discontinuity point of the absolute sensor in
044     * rotations. This determines the point at which the absolute sensor
045     * wraps around, keeping the absolute position in the range [x-1, x).
046     * 
047     * <ul>
048     *   <li> Setting this to 1 makes the absolute position unsigned [0,
049     * 1)
050     *   <li> Setting this to 0.5 makes the absolute position signed
051     * [-0.5, 0.5)
052     *   <li> Setting this to 0 makes the absolute position always
053     * negative [-1, 0)
054     * </ul>
055     * 
056     * Many rotational mechanisms such as arms have a region of motion
057     * that is unreachable. This should be set to the center of that
058     * region of motion, in non-negative rotations. This affects the
059     * position of the device at bootup.
060     * <p>
061     * For example, consider an arm which can travel from -0.2 to 0.6
062     * rotations with a little leeway, where 0 is horizontally forward.
063     * Since -0.2 rotations has the same absolute position as 0.8
064     * rotations, we can say that the arm typically does not travel in the
065     * range (0.6, 0.8) rotations. As a result, the discontinuity point
066     * would be the center of that range, which is 0.7 rotations. This
067     * results in an absolute sensor range of [-0.3, 0.7) rotations.
068     * <p>
069     * On a Talon motor controller, this is only supported when using the
070     * PulseWidth sensor source.
071     * 
072     * <ul>
073     *   <li> <b>Minimum Value:</b> 0.0
074     *   <li> <b>Maximum Value:</b> 1.0
075     *   <li> <b>Default Value:</b> 0.5
076     *   <li> <b>Units:</b> rotations
077     * </ul>
078     */
079    public double AbsoluteSensorDiscontinuityPoint = 0.5;
080    /**
081     * Direction of the PWM sensor to determine positive rotation. Invert
082     * this so that forward motion on the mechanism results in an increase
083     * in PWM position.
084     * 
085     * <ul>
086     *   <li> <b>Default Value:</b> False
087     * </ul>
088     */
089    public boolean SensorDirection = false;
090    
091    /**
092     * Modifies this configuration's AbsoluteSensorOffset parameter and returns itself for
093     * method-chaining and easier to use config API.
094     * <p>
095     * The offset applied to the PWM sensor.
096     * <p>
097     * This can be used to zero the sensor position in applications where
098     * the sensor is 1:1 with the mechanism.
099     * 
100     * <ul>
101     *   <li> <b>Minimum Value:</b> -1
102     *   <li> <b>Maximum Value:</b> 1
103     *   <li> <b>Default Value:</b> 0.0
104     *   <li> <b>Units:</b> rotations
105     * </ul>
106     *
107     * @param newAbsoluteSensorOffset Parameter to modify
108     * @return Itself
109     */
110    public PWM2Configs withAbsoluteSensorOffset(double newAbsoluteSensorOffset)
111    {
112        AbsoluteSensorOffset = newAbsoluteSensorOffset;
113        return this;
114    }
115    
116    /**
117     * Modifies this configuration's AbsoluteSensorOffset parameter and returns itself for
118     * method-chaining and easier to use config API.
119     * <p>
120     * The offset applied to the PWM sensor.
121     * <p>
122     * This can be used to zero the sensor position in applications where
123     * the sensor is 1:1 with the mechanism.
124     * 
125     * <ul>
126     *   <li> <b>Minimum Value:</b> -1
127     *   <li> <b>Maximum Value:</b> 1
128     *   <li> <b>Default Value:</b> 0.0
129     *   <li> <b>Units:</b> rotations
130     * </ul>
131     *
132     * @param newAbsoluteSensorOffset Parameter to modify
133     * @return Itself
134     */
135    public PWM2Configs withAbsoluteSensorOffset(Angle newAbsoluteSensorOffset)
136    {
137        AbsoluteSensorOffset = newAbsoluteSensorOffset.in(Rotations);
138        return this;
139    }
140    
141    /**
142     * Helper method to get this configuration's AbsoluteSensorOffset parameter converted
143     * to a unit type. If not using the Java units library, {@link #AbsoluteSensorOffset}
144     * can be accessed directly instead.
145     * <p>
146     * The offset applied to the PWM sensor.
147     * <p>
148     * This can be used to zero the sensor position in applications where
149     * the sensor is 1:1 with the mechanism.
150     * 
151     * <ul>
152     *   <li> <b>Minimum Value:</b> -1
153     *   <li> <b>Maximum Value:</b> 1
154     *   <li> <b>Default Value:</b> 0.0
155     *   <li> <b>Units:</b> rotations
156     * </ul>
157     *
158     * @return AbsoluteSensorOffset
159     */
160    public Angle getAbsoluteSensorOffsetMeasure()
161    {
162        return Rotations.of(AbsoluteSensorOffset);
163    }
164    
165    /**
166     * Modifies this configuration's AbsoluteSensorDiscontinuityPoint parameter and returns itself for
167     * method-chaining and easier to use config API.
168     * <p>
169     * The positive discontinuity point of the absolute sensor in
170     * rotations. This determines the point at which the absolute sensor
171     * wraps around, keeping the absolute position in the range [x-1, x).
172     * 
173     * <ul>
174     *   <li> Setting this to 1 makes the absolute position unsigned [0,
175     * 1)
176     *   <li> Setting this to 0.5 makes the absolute position signed
177     * [-0.5, 0.5)
178     *   <li> Setting this to 0 makes the absolute position always
179     * negative [-1, 0)
180     * </ul>
181     * 
182     * Many rotational mechanisms such as arms have a region of motion
183     * that is unreachable. This should be set to the center of that
184     * region of motion, in non-negative rotations. This affects the
185     * position of the device at bootup.
186     * <p>
187     * For example, consider an arm which can travel from -0.2 to 0.6
188     * rotations with a little leeway, where 0 is horizontally forward.
189     * Since -0.2 rotations has the same absolute position as 0.8
190     * rotations, we can say that the arm typically does not travel in the
191     * range (0.6, 0.8) rotations. As a result, the discontinuity point
192     * would be the center of that range, which is 0.7 rotations. This
193     * results in an absolute sensor range of [-0.3, 0.7) rotations.
194     * <p>
195     * On a Talon motor controller, this is only supported when using the
196     * PulseWidth sensor source.
197     * 
198     * <ul>
199     *   <li> <b>Minimum Value:</b> 0.0
200     *   <li> <b>Maximum Value:</b> 1.0
201     *   <li> <b>Default Value:</b> 0.5
202     *   <li> <b>Units:</b> rotations
203     * </ul>
204     *
205     * @param newAbsoluteSensorDiscontinuityPoint Parameter to modify
206     * @return Itself
207     */
208    public PWM2Configs withAbsoluteSensorDiscontinuityPoint(double newAbsoluteSensorDiscontinuityPoint)
209    {
210        AbsoluteSensorDiscontinuityPoint = newAbsoluteSensorDiscontinuityPoint;
211        return this;
212    }
213    
214    /**
215     * Modifies this configuration's AbsoluteSensorDiscontinuityPoint parameter and returns itself for
216     * method-chaining and easier to use config API.
217     * <p>
218     * The positive discontinuity point of the absolute sensor in
219     * rotations. This determines the point at which the absolute sensor
220     * wraps around, keeping the absolute position in the range [x-1, x).
221     * 
222     * <ul>
223     *   <li> Setting this to 1 makes the absolute position unsigned [0,
224     * 1)
225     *   <li> Setting this to 0.5 makes the absolute position signed
226     * [-0.5, 0.5)
227     *   <li> Setting this to 0 makes the absolute position always
228     * negative [-1, 0)
229     * </ul>
230     * 
231     * Many rotational mechanisms such as arms have a region of motion
232     * that is unreachable. This should be set to the center of that
233     * region of motion, in non-negative rotations. This affects the
234     * position of the device at bootup.
235     * <p>
236     * For example, consider an arm which can travel from -0.2 to 0.6
237     * rotations with a little leeway, where 0 is horizontally forward.
238     * Since -0.2 rotations has the same absolute position as 0.8
239     * rotations, we can say that the arm typically does not travel in the
240     * range (0.6, 0.8) rotations. As a result, the discontinuity point
241     * would be the center of that range, which is 0.7 rotations. This
242     * results in an absolute sensor range of [-0.3, 0.7) rotations.
243     * <p>
244     * On a Talon motor controller, this is only supported when using the
245     * PulseWidth sensor source.
246     * 
247     * <ul>
248     *   <li> <b>Minimum Value:</b> 0.0
249     *   <li> <b>Maximum Value:</b> 1.0
250     *   <li> <b>Default Value:</b> 0.5
251     *   <li> <b>Units:</b> rotations
252     * </ul>
253     *
254     * @param newAbsoluteSensorDiscontinuityPoint Parameter to modify
255     * @return Itself
256     */
257    public PWM2Configs withAbsoluteSensorDiscontinuityPoint(Angle newAbsoluteSensorDiscontinuityPoint)
258    {
259        AbsoluteSensorDiscontinuityPoint = newAbsoluteSensorDiscontinuityPoint.in(Rotations);
260        return this;
261    }
262    
263    /**
264     * Helper method to get this configuration's AbsoluteSensorDiscontinuityPoint parameter converted
265     * to a unit type. If not using the Java units library, {@link #AbsoluteSensorDiscontinuityPoint}
266     * can be accessed directly instead.
267     * <p>
268     * The positive discontinuity point of the absolute sensor in
269     * rotations. This determines the point at which the absolute sensor
270     * wraps around, keeping the absolute position in the range [x-1, x).
271     * 
272     * <ul>
273     *   <li> Setting this to 1 makes the absolute position unsigned [0,
274     * 1)
275     *   <li> Setting this to 0.5 makes the absolute position signed
276     * [-0.5, 0.5)
277     *   <li> Setting this to 0 makes the absolute position always
278     * negative [-1, 0)
279     * </ul>
280     * 
281     * Many rotational mechanisms such as arms have a region of motion
282     * that is unreachable. This should be set to the center of that
283     * region of motion, in non-negative rotations. This affects the
284     * position of the device at bootup.
285     * <p>
286     * For example, consider an arm which can travel from -0.2 to 0.6
287     * rotations with a little leeway, where 0 is horizontally forward.
288     * Since -0.2 rotations has the same absolute position as 0.8
289     * rotations, we can say that the arm typically does not travel in the
290     * range (0.6, 0.8) rotations. As a result, the discontinuity point
291     * would be the center of that range, which is 0.7 rotations. This
292     * results in an absolute sensor range of [-0.3, 0.7) rotations.
293     * <p>
294     * On a Talon motor controller, this is only supported when using the
295     * PulseWidth sensor source.
296     * 
297     * <ul>
298     *   <li> <b>Minimum Value:</b> 0.0
299     *   <li> <b>Maximum Value:</b> 1.0
300     *   <li> <b>Default Value:</b> 0.5
301     *   <li> <b>Units:</b> rotations
302     * </ul>
303     *
304     * @return AbsoluteSensorDiscontinuityPoint
305     */
306    public Angle getAbsoluteSensorDiscontinuityPointMeasure()
307    {
308        return Rotations.of(AbsoluteSensorDiscontinuityPoint);
309    }
310    
311    /**
312     * Modifies this configuration's SensorDirection parameter and returns itself for
313     * method-chaining and easier to use config API.
314     * <p>
315     * Direction of the PWM sensor to determine positive rotation. Invert
316     * this so that forward motion on the mechanism results in an increase
317     * in PWM position.
318     * 
319     * <ul>
320     *   <li> <b>Default Value:</b> False
321     * </ul>
322     *
323     * @param newSensorDirection Parameter to modify
324     * @return Itself
325     */
326    public PWM2Configs withSensorDirection(boolean newSensorDirection)
327    {
328        SensorDirection = newSensorDirection;
329        return this;
330    }
331
332    
333
334    @Override
335    public String toString()
336    {
337        String ss = "Config Group: PWM2\n";
338        ss += "    AbsoluteSensorOffset: " + AbsoluteSensorOffset + " rotations" + "\n";
339        ss += "    AbsoluteSensorDiscontinuityPoint: " + AbsoluteSensorDiscontinuityPoint + " rotations" + "\n";
340        ss += "    SensorDirection: " + SensorDirection + "\n";
341        return ss;
342    }
343
344    /**
345     *
346     */
347    public StatusCode deserialize(String to_deserialize)
348    {
349        AbsoluteSensorOffset = ConfigJNI.Deserializedouble(SpnValue.Config_PWM2_AbsoluteSensorOffset.value, to_deserialize);
350        AbsoluteSensorDiscontinuityPoint = ConfigJNI.Deserializedouble(SpnValue.Config_PWM2_AbsoluteSensorDiscontinuityPoint.value, to_deserialize);
351        SensorDirection = ConfigJNI.Deserializeboolean(SpnValue.Config_PWM2_SensorDirection.value, to_deserialize);
352        return  StatusCode.OK;
353    }
354
355    /**
356     *
357     */
358    public String serialize()
359    {
360        String ss = "";
361        ss += ConfigJNI.Serializedouble(SpnValue.Config_PWM2_AbsoluteSensorOffset.value, AbsoluteSensorOffset);
362        ss += ConfigJNI.Serializedouble(SpnValue.Config_PWM2_AbsoluteSensorDiscontinuityPoint.value, AbsoluteSensorDiscontinuityPoint);
363        ss += ConfigJNI.Serializeboolean(SpnValue.Config_PWM2_SensorDirection.value, SensorDirection);
364        return ss;
365    }
366}
367