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.controls;
008
009import com.ctre.phoenix6.StatusCode;
010import com.ctre.phoenix6.controls.jni.ControlJNI;
011import com.ctre.phoenix6.hardware.traits.*;
012
013import edu.wpi.first.units.*;
014import edu.wpi.first.units.measure.*;
015import static edu.wpi.first.units.Units.*;
016
017import java.util.HashMap;
018import java.util.Map;
019
020/**
021 * Requests Motion Magic® to target a final position using an exponential motion
022 * profile.  Users can optionally provide a duty cycle feedforward.
023 * <p>
024 * Motion Magic® Expo produces a motion profile in real-time while attempting to honor the Cruise Velocity
025 * (optional) and the mechanism kV and kA, specified via the Motion Magic® configuration values.  Note that
026 * unlike the slot gains, the Expo_kV and Expo_kA configs are always in output units of Volts.
027 * <p>
028 * Setting Cruise Velocity to 0 will allow the profile to run to the max possible velocity based on Expo_kV. 
029 * This control mode does not use the Acceleration or Jerk configs.
030 * <p>
031 * Target position can be changed on-the-fly and Motion Magic® will do its best to adjust the profile.  This
032 * control mode is duty cycle based, so relevant closed-loop gains will use fractional duty cycle for the
033 * numerator:  +1.0 represents full forward output.
034 */
035public class MotionMagicExpoDutyCycle extends ControlRequest implements Cloneable
036{
037    /**
038     * Position to drive toward in rotations.
039     * 
040     * <ul>
041     *   <li> Units: rotations
042     * </ul>
043     * 
044     */
045    public double Position;
046    /**
047     * Set to true to use FOC commutation (requires Phoenix Pro), which increases
048     * peak power by ~15% on supported devices (see {@link SupportsFOC}). Set to
049     * false to use trapezoidal commutation.
050     * <p>
051     * FOC improves motor performance by leveraging torque (current) control. 
052     * However, this may be inconvenient for applications that require specifying
053     * duty cycle or voltage.  CTR-Electronics has developed a hybrid method that
054     * combines the performances gains of FOC while still allowing applications to
055     * provide duty cycle or voltage demand.  This not to be confused with simple
056     * sinusoidal control or phase voltage control which lacks the performance
057     * gains.
058     */
059    public boolean EnableFOC = true;
060    /**
061     * Feedforward to apply in fractional units between -1 and +1.
062     * 
063     * <ul>
064     *   <li> Units: fractional
065     * </ul>
066     * 
067     */
068    public double FeedForward = 0.0;
069    /**
070     * Select which gains are applied by selecting the slot.  Use the configuration
071     * api to set the gain values for the selected slot before enabling this
072     * feature. Slot must be within [0,2].
073     */
074    public int Slot = 0;
075    /**
076     * Set to true to static-brake the rotor when output is zero (or within
077     * deadband).  Set to false to use the NeutralMode configuration setting
078     * (default). This flag exists to provide the fundamental behavior of this
079     * control when output is zero, which is to provide 0V to the motor.
080     */
081    public boolean OverrideBrakeDurNeutral = false;
082    /**
083     * Set to true to force forward limiting.  This allows users to use other limit
084     * switch sensors connected to robot controller.  This also allows use of active
085     * sensors that require external power.
086     */
087    public boolean LimitForwardMotion = false;
088    /**
089     * Set to true to force reverse limiting.  This allows users to use other limit
090     * switch sensors connected to robot controller.  This also allows use of active
091     * sensors that require external power.
092     */
093    public boolean LimitReverseMotion = false;
094    /**
095     * Set to true to ignore hardware limit switches and the LimitForwardMotion and
096     * LimitReverseMotion parameters, instead allowing motion.
097     * <p>
098     * This can be useful on mechanisms such as an intake/feeder, where a limit
099     * switch stops motion while intaking but should be ignored when feeding to a
100     * shooter.
101     * <p>
102     * The hardware limit faults and Forward/ReverseLimit signals will still report
103     * the values of the limit switches regardless of this parameter.
104     */
105    public boolean IgnoreHardwareLimits = false;
106    /**
107     * Set to true to delay applying this control request until a timesync boundary
108     * (requires Phoenix Pro and CANivore). This eliminates the impact of
109     * nondeterministic network delays in exchange for a larger but deterministic
110     * control latency.
111     * <p>
112     * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
113     * Additionally, when this is enabled, the UpdateFreqHz of this request should
114     * be set to 0 Hz.
115     */
116    public boolean UseTimesync = false;
117
118    /**
119     * The period at which this control will update at.
120     * This is designated in Hertz, with a minimum of 20 Hz
121     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
122     * <p>
123     * If this field is set to 0 Hz, the control request will
124     * be sent immediately as a one-shot frame. This may be useful
125     * for advanced applications that require outputs to be
126     * synchronized with data acquisition. In this case, we
127     * recommend not exceeding 50 ms between control calls.
128     */
129    public double UpdateFreqHz = 100;
130
131    /**
132     * Requests Motion Magic® to target a final position using an exponential motion
133     * profile.  Users can optionally provide a duty cycle feedforward.
134     * <p>
135     * Motion Magic® Expo produces a motion profile in real-time while attempting to
136     * honor the Cruise Velocity (optional) and the mechanism kV and kA, specified
137     * via the Motion Magic® configuration values.  Note that unlike the slot gains,
138     * the Expo_kV and Expo_kA configs are always in output units of Volts.
139     * <p>
140     * Setting Cruise Velocity to 0 will allow the profile to run to the max
141     * possible velocity based on Expo_kV.  This control mode does not use the
142     * Acceleration or Jerk configs.
143     * <p>
144     * Target position can be changed on-the-fly and Motion Magic® will do its best
145     * to adjust the profile.  This control mode is duty cycle based, so relevant
146     * closed-loop gains will use fractional duty cycle for the numerator:  +1.0
147     * represents full forward output.
148     * 
149     * @param Position    Position to drive toward in rotations.
150     */
151    public MotionMagicExpoDutyCycle(double Position)
152    {
153        super("MotionMagicExpoDutyCycle");
154        this.Position = Position;
155    }
156
157    /**
158     * Requests Motion Magic® to target a final position using an exponential motion
159     * profile.  Users can optionally provide a duty cycle feedforward.
160     * <p>
161     * Motion Magic® Expo produces a motion profile in real-time while attempting to
162     * honor the Cruise Velocity (optional) and the mechanism kV and kA, specified
163     * via the Motion Magic® configuration values.  Note that unlike the slot gains,
164     * the Expo_kV and Expo_kA configs are always in output units of Volts.
165     * <p>
166     * Setting Cruise Velocity to 0 will allow the profile to run to the max
167     * possible velocity based on Expo_kV.  This control mode does not use the
168     * Acceleration or Jerk configs.
169     * <p>
170     * Target position can be changed on-the-fly and Motion Magic® will do its best
171     * to adjust the profile.  This control mode is duty cycle based, so relevant
172     * closed-loop gains will use fractional duty cycle for the numerator:  +1.0
173     * represents full forward output.
174     * 
175     * @param Position    Position to drive toward in rotations.
176     */
177    public MotionMagicExpoDutyCycle(Angle Position)
178    {
179        this(Position.in(Rotations));
180    }
181
182    @Override
183    public String toString()
184    {
185        String ss = "Control: MotionMagicExpoDutyCycle\n";
186        ss += "    Position: " + Position + " rotations" + "\n";
187        ss += "    EnableFOC: " + EnableFOC + "\n";
188        ss += "    FeedForward: " + FeedForward + " fractional" + "\n";
189        ss += "    Slot: " + Slot + "\n";
190        ss += "    OverrideBrakeDurNeutral: " + OverrideBrakeDurNeutral + "\n";
191        ss += "    LimitForwardMotion: " + LimitForwardMotion + "\n";
192        ss += "    LimitReverseMotion: " + LimitReverseMotion + "\n";
193        ss += "    IgnoreHardwareLimits: " + IgnoreHardwareLimits + "\n";
194        ss += "    UseTimesync: " + UseTimesync + "\n";
195        return ss;
196    }
197
198    @Override
199    public StatusCode sendRequest(String network, int deviceHash)
200    {
201        return StatusCode.valueOf(ControlJNI.JNI_RequestControlMotionMagicExpoDutyCycle(
202                network, deviceHash, UpdateFreqHz, Position, EnableFOC, FeedForward, Slot, OverrideBrakeDurNeutral, LimitForwardMotion, LimitReverseMotion, IgnoreHardwareLimits, UseTimesync));
203    }
204
205    /**
206     * Gets information about this control request.
207     *
208     * @return Map of control parameter names and corresponding applied values
209     */
210    @Override
211    public Map<String, String> getControlInfo()
212    {
213        var controlInfo = new HashMap<String, String>();
214        controlInfo.put("Name", getName());
215        controlInfo.put("Position", String.valueOf(this.Position));
216        controlInfo.put("EnableFOC", String.valueOf(this.EnableFOC));
217        controlInfo.put("FeedForward", String.valueOf(this.FeedForward));
218        controlInfo.put("Slot", String.valueOf(this.Slot));
219        controlInfo.put("OverrideBrakeDurNeutral", String.valueOf(this.OverrideBrakeDurNeutral));
220        controlInfo.put("LimitForwardMotion", String.valueOf(this.LimitForwardMotion));
221        controlInfo.put("LimitReverseMotion", String.valueOf(this.LimitReverseMotion));
222        controlInfo.put("IgnoreHardwareLimits", String.valueOf(this.IgnoreHardwareLimits));
223        controlInfo.put("UseTimesync", String.valueOf(this.UseTimesync));
224        return controlInfo;
225    }
226    
227    /**
228     * Modifies this Control Request's Position parameter and returns itself for
229     * method-chaining and easier to use request API.
230     * <p>
231     * Position to drive toward in rotations.
232     * 
233     * <ul>
234     *   <li> Units: rotations
235     * </ul>
236     * 
237     *
238     * @param newPosition Parameter to modify
239     * @return Itself
240     */
241    public MotionMagicExpoDutyCycle withPosition(double newPosition)
242    {
243        Position = newPosition;
244        return this;
245    }
246    
247    /**
248     * Modifies this Control Request's Position parameter and returns itself for
249     * method-chaining and easier to use request API.
250     * <p>
251     * Position to drive toward in rotations.
252     * 
253     * <ul>
254     *   <li> Units: rotations
255     * </ul>
256     * 
257     *
258     * @param newPosition Parameter to modify
259     * @return Itself
260     */
261    public MotionMagicExpoDutyCycle withPosition(Angle newPosition)
262    {
263        Position = newPosition.in(Rotations);
264        return this;
265    }
266    
267    /**
268     * Helper method to get this Control Request's Position parameter converted
269     * to a unit type. If not using the Java units library, {@link #Position}
270     * can be accessed directly instead.
271     * <p>
272     * Position to drive toward in rotations.
273     * 
274     * <ul>
275     *   <li> Units: rotations
276     * </ul>
277     * 
278     *
279     * @return Position
280     */
281    public Angle getPositionMeasure()
282    {
283        return Rotations.of(Position);
284    }
285    
286    /**
287     * Modifies this Control Request's EnableFOC parameter and returns itself for
288     * method-chaining and easier to use request API.
289     * <p>
290     * Set to true to use FOC commutation (requires Phoenix Pro), which increases
291     * peak power by ~15% on supported devices (see {@link SupportsFOC}). Set to
292     * false to use trapezoidal commutation.
293     * <p>
294     * FOC improves motor performance by leveraging torque (current) control. 
295     * However, this may be inconvenient for applications that require specifying
296     * duty cycle or voltage.  CTR-Electronics has developed a hybrid method that
297     * combines the performances gains of FOC while still allowing applications to
298     * provide duty cycle or voltage demand.  This not to be confused with simple
299     * sinusoidal control or phase voltage control which lacks the performance
300     * gains.
301     *
302     * @param newEnableFOC Parameter to modify
303     * @return Itself
304     */
305    public MotionMagicExpoDutyCycle withEnableFOC(boolean newEnableFOC)
306    {
307        EnableFOC = newEnableFOC;
308        return this;
309    }
310    
311    /**
312     * Modifies this Control Request's FeedForward parameter and returns itself for
313     * method-chaining and easier to use request API.
314     * <p>
315     * Feedforward to apply in fractional units between -1 and +1.
316     * 
317     * <ul>
318     *   <li> Units: fractional
319     * </ul>
320     * 
321     *
322     * @param newFeedForward Parameter to modify
323     * @return Itself
324     */
325    public MotionMagicExpoDutyCycle withFeedForward(double newFeedForward)
326    {
327        FeedForward = newFeedForward;
328        return this;
329    }
330    
331    /**
332     * Modifies this Control Request's Slot parameter and returns itself for
333     * method-chaining and easier to use request API.
334     * <p>
335     * Select which gains are applied by selecting the slot.  Use the configuration
336     * api to set the gain values for the selected slot before enabling this
337     * feature. Slot must be within [0,2].
338     *
339     * @param newSlot Parameter to modify
340     * @return Itself
341     */
342    public MotionMagicExpoDutyCycle withSlot(int newSlot)
343    {
344        Slot = newSlot;
345        return this;
346    }
347    
348    /**
349     * Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
350     * method-chaining and easier to use request API.
351     * <p>
352     * Set to true to static-brake the rotor when output is zero (or within
353     * deadband).  Set to false to use the NeutralMode configuration setting
354     * (default). This flag exists to provide the fundamental behavior of this
355     * control when output is zero, which is to provide 0V to the motor.
356     *
357     * @param newOverrideBrakeDurNeutral Parameter to modify
358     * @return Itself
359     */
360    public MotionMagicExpoDutyCycle withOverrideBrakeDurNeutral(boolean newOverrideBrakeDurNeutral)
361    {
362        OverrideBrakeDurNeutral = newOverrideBrakeDurNeutral;
363        return this;
364    }
365    
366    /**
367     * Modifies this Control Request's LimitForwardMotion parameter and returns itself for
368     * method-chaining and easier to use request API.
369     * <p>
370     * Set to true to force forward limiting.  This allows users to use other limit
371     * switch sensors connected to robot controller.  This also allows use of active
372     * sensors that require external power.
373     *
374     * @param newLimitForwardMotion Parameter to modify
375     * @return Itself
376     */
377    public MotionMagicExpoDutyCycle withLimitForwardMotion(boolean newLimitForwardMotion)
378    {
379        LimitForwardMotion = newLimitForwardMotion;
380        return this;
381    }
382    
383    /**
384     * Modifies this Control Request's LimitReverseMotion parameter and returns itself for
385     * method-chaining and easier to use request API.
386     * <p>
387     * Set to true to force reverse limiting.  This allows users to use other limit
388     * switch sensors connected to robot controller.  This also allows use of active
389     * sensors that require external power.
390     *
391     * @param newLimitReverseMotion Parameter to modify
392     * @return Itself
393     */
394    public MotionMagicExpoDutyCycle withLimitReverseMotion(boolean newLimitReverseMotion)
395    {
396        LimitReverseMotion = newLimitReverseMotion;
397        return this;
398    }
399    
400    /**
401     * Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
402     * method-chaining and easier to use request API.
403     * <p>
404     * Set to true to ignore hardware limit switches and the LimitForwardMotion and
405     * LimitReverseMotion parameters, instead allowing motion.
406     * <p>
407     * This can be useful on mechanisms such as an intake/feeder, where a limit
408     * switch stops motion while intaking but should be ignored when feeding to a
409     * shooter.
410     * <p>
411     * The hardware limit faults and Forward/ReverseLimit signals will still report
412     * the values of the limit switches regardless of this parameter.
413     *
414     * @param newIgnoreHardwareLimits Parameter to modify
415     * @return Itself
416     */
417    public MotionMagicExpoDutyCycle withIgnoreHardwareLimits(boolean newIgnoreHardwareLimits)
418    {
419        IgnoreHardwareLimits = newIgnoreHardwareLimits;
420        return this;
421    }
422    
423    /**
424     * Modifies this Control Request's UseTimesync parameter and returns itself for
425     * method-chaining and easier to use request API.
426     * <p>
427     * Set to true to delay applying this control request until a timesync boundary
428     * (requires Phoenix Pro and CANivore). This eliminates the impact of
429     * nondeterministic network delays in exchange for a larger but deterministic
430     * control latency.
431     * <p>
432     * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
433     * Additionally, when this is enabled, the UpdateFreqHz of this request should
434     * be set to 0 Hz.
435     *
436     * @param newUseTimesync Parameter to modify
437     * @return Itself
438     */
439    public MotionMagicExpoDutyCycle withUseTimesync(boolean newUseTimesync)
440    {
441        UseTimesync = newUseTimesync;
442        return this;
443    }
444
445    /**
446     * Sets the period at which this control will update at.
447     * This is designated in Hertz, with a minimum of 20 Hz
448     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
449     * <p>
450     * If this field is set to 0 Hz, the control request will
451     * be sent immediately as a one-shot frame. This may be useful
452     * for advanced applications that require outputs to be
453     * synchronized with data acquisition. In this case, we
454     * recommend not exceeding 50 ms between control calls.
455     *
456     * @param newUpdateFreqHz Parameter to modify
457     * @return Itself
458     */
459    @Override
460    public MotionMagicExpoDutyCycle withUpdateFreqHz(double newUpdateFreqHz)
461    {
462        UpdateFreqHz = newUpdateFreqHz;
463        return this;
464    }
465
466    /**
467     * Sets the period at which this control will update at.
468     * This is designated in Hertz, with a minimum of 20 Hz
469     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
470     * <p>
471     * If this field is set to 0 Hz, the control request will
472     * be sent immediately as a one-shot frame. This may be useful
473     * for advanced applications that require outputs to be
474     * synchronized with data acquisition. In this case, we
475     * recommend not exceeding 50 ms between control calls.
476     *
477     * @param newUpdateFreqHz Parameter to modify
478     * @return Itself
479     */
480    @Override
481    public MotionMagicExpoDutyCycle withUpdateFreqHz(Frequency newUpdateFreqHz)
482    {
483        UpdateFreqHz = newUpdateFreqHz.in(Hertz);
484        return this;
485    }
486
487    @Override
488    public MotionMagicExpoDutyCycle clone()
489    {
490        try {
491            return (MotionMagicExpoDutyCycle)super.clone();
492        } catch (CloneNotSupportedException ex) {
493            /* this should never happen */
494            throw new RuntimeException(ex);
495        }
496    }
497}
498