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.phoenixpro.controls;
008
009import com.ctre.phoenix6.StatusCode;
010import com.ctre.phoenix6.controls.jni.ControlJNI;
011import com.ctre.phoenix6.controls.jni.ControlConfigJNI;
012
013/**
014 * Requests Motion Magic® to target a final position using a motion profile. 
015 * Users can optionally provide a duty cycle feedforward.
016 * <p>
017 * Motion Magic® produces a motion profile in real-time while attempting to honor the Cruise Velocity,
018 * Acceleration, and Jerk value specified via the Motion Magic® configuration values.  Target position can be
019 * changed on-the-fly and Motion Magic® will do its best to adjust the profile.  This control mode is duty
020 * cycled based, so relevant closed-loop gains will use fractional duty cycle for the numerator:  +1.0
021 * represents full forward output.
022 *
023 * @deprecated Classes in the phoenixpro package will be removed in 2024.
024 *             Users should instead use classes from the phoenix6 package.
025 */
026@Deprecated(forRemoval = true)
027public class MotionMagicDutyCycle extends ControlRequest
028{
029    private boolean applyConfigsOnRequest;
030    /**
031     * Position to drive toward in rotations.
032     */
033    public double Position;
034    /**
035     * Set to true to use FOC commutation, which increases peak power by ~15%. Set
036     * to false to use trapezoidal commutation.  FOC improves motor performance by
037     * leveraging torque (current) control.  However, this may be inconvenient for
038     * applications that require specifying duty cycle or voltage.  CTR-Electronics
039     * has developed a hybrid method that combines the performances gains of FOC
040     * while still allowing applications to provide duty cycle or voltage demand. 
041     * This not to be confused with simple sinusoidal control or phase voltage
042     * control which lacks the performance gains.
043     */
044    public boolean EnableFOC;
045    /**
046     * Feedforward to apply in fractional units between -1 and +1.
047     */
048    public double FeedForward;
049    /**
050     * Select which gains are applied by selecting the slot.  Use the configuration
051     * api to set the gain values for the selected slot before enabling this
052     * feature. Slot must be within [0,2].
053     */
054    public int Slot;
055    /**
056     * Set to true to static-brake the rotor when output is zero (or within
057     * deadband).  Set to false to use the NeutralMode configuration setting
058     * (default). This flag exists to provide the fundamental behavior of this
059     * control when output is zero, which is to provide 0V to the motor.
060     */
061    public boolean OverrideBrakeDurNeutral;
062
063    
064    /**
065     * The period at which this control will update at.
066     * This is designated in Hertz, with a minimum of 20 Hz
067     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
068     * <p>
069     * If this field is set to 0 Hz, the control request will
070     * be sent immediately as a one-shot frame. This may be useful
071     * for advanced applications that require outputs to be
072     * synchronized with data acquisition. In this case, we
073     * recommend not exceeding 50 ms between control calls.
074     */
075    public double UpdateFreqHz = 100; // Default to 100Hz
076
077    /**
078     * The timeout when sending configs associated with this control
079     */
080    public double configTimeout = 0.1;
081
082    /**
083     * Requests Motion Magic® to target a final position using a motion profile.
084     *  Users can optionally provide a duty cycle feedforward.
085     * <p>
086     * Motion Magic® produces a motion profile in real-time while attempting to
087     * honor the Cruise Velocity, Acceleration, and Jerk value specified via the
088     * Motion Magic® configuration values.  Target position can be changed
089     * on-the-fly and Motion Magic® will do its best to adjust the profile. 
090     * This control mode is duty cycled based, so relevant closed-loop gains
091     * will use fractional duty cycle for the numerator:  +1.0 represents full
092     * forward output.
093     *
094     * @param Position     Position to drive toward in rotations.
095     * @param EnableFOC     Set to true to use FOC commutation, which increases
096     *                      peak power by ~15%. Set to false to use trapezoidal
097     *                      commutation.  FOC improves motor performance by
098     *                      leveraging torque (current) control.  However, this
099     *                      may be inconvenient for applications that require
100     *                      specifying duty cycle or voltage.  CTR-Electronics
101     *                      has developed a hybrid method that combines the
102     *                      performances gains of FOC while still allowing
103     *                      applications to provide duty cycle or voltage
104     *                      demand.  This not to be confused with simple
105     *                      sinusoidal control or phase voltage control which
106     *                      lacks the performance gains.
107     * @param FeedForward     Feedforward to apply in fractional units between
108     *                        -1 and +1.
109     * @param Slot     Select which gains are applied by selecting the slot. 
110     *                 Use the configuration api to set the gain values for the
111     *                 selected slot before enabling this feature. Slot must be
112     *                 within [0,2].
113     * @param OverrideBrakeDurNeutral     Set to true to static-brake the rotor
114     *                                    when output is zero (or within
115     *                                    deadband).  Set to false to use the
116     *                                    NeutralMode configuration setting
117     *                                    (default). This flag exists to provide
118     *                                    the fundamental behavior of this
119     *                                    control when output is zero, which is
120     *                                    to provide 0V to the motor.
121     *
122     * @deprecated Classes in the phoenixpro package will be removed in 2024.
123     *             Users should instead use classes from the phoenix6 package.
124     */
125    @Deprecated(forRemoval = true)
126    public MotionMagicDutyCycle(double Position, boolean EnableFOC, double FeedForward, int Slot, boolean OverrideBrakeDurNeutral)
127    {
128        super("MotionMagicDutyCycle");
129        this.Position = Position;
130        this.EnableFOC = EnableFOC;
131        this.FeedForward = FeedForward;
132        this.Slot = Slot;
133        this.OverrideBrakeDurNeutral = OverrideBrakeDurNeutral;
134    }
135
136    /**
137     * Requests Motion Magic® to target a final position using a motion profile.
138     *  Users can optionally provide a duty cycle feedforward.
139     * <p>
140     * Motion Magic® produces a motion profile in real-time while attempting to
141     * honor the Cruise Velocity, Acceleration, and Jerk value specified via the
142     * Motion Magic® configuration values.  Target position can be changed
143     * on-the-fly and Motion Magic® will do its best to adjust the profile. 
144     * This control mode is duty cycled based, so relevant closed-loop gains
145     * will use fractional duty cycle for the numerator:  +1.0 represents full
146     * forward output.
147     *
148     * @param Position     Position to drive toward in rotations.
149     *
150     * @deprecated Classes in the phoenixpro package will be removed in 2024.
151     *             Users should instead use classes from the phoenix6 package.
152     */
153    @Deprecated(forRemoval = true)
154    public MotionMagicDutyCycle(double Position)
155    {
156        this(Position, true, 0.0, 0, false);
157    }
158
159    @Override
160    public String toString()
161    {
162        String ss = "class: MotionMagicDutyCycle\n";
163        ss += "Position: " + Position + "\n";
164        ss += "EnableFOC: " + EnableFOC + "\n";
165        ss += "FeedForward: " + FeedForward + "\n";
166        ss += "Slot: " + Slot + "\n";
167        ss += "OverrideBrakeDurNeutral: " + OverrideBrakeDurNeutral + "\n";
168        return ss;
169    }
170
171    @Override
172    public StatusCode sendRequest(String network, int deviceHash, boolean cancelOtherRequests)
173    {
174        var ref = requestReference.getNameValues();
175        ref.put("Position", String.valueOf(this.Position));
176        ref.put("EnableFOC", String.valueOf(this.EnableFOC));
177        ref.put("FeedForward", String.valueOf(this.FeedForward));
178        ref.put("Slot", String.valueOf(this.Slot));
179        ref.put("OverrideBrakeDurNeutral", String.valueOf(this.OverrideBrakeDurNeutral));
180        String ss = "";
181        
182        ControlConfigJNI.JNI_RequestConfigApply(network, deviceHash, configTimeout, ss, applyConfigsOnRequest);
183        applyConfigsOnRequest = false;
184        return StatusCode.valueOf(ControlJNI.JNI_RequestControlMotionMagicDutyCycle(
185                network, deviceHash, UpdateFreqHz, cancelOtherRequests, Position, EnableFOC, FeedForward, Slot, OverrideBrakeDurNeutral));
186    }
187    
188    /**
189     * Modifies this Control Request's Position parameter and returns itself for
190     * method-chaining and easier to use request API.
191     *
192     * @param newPosition Parameter to modify
193     * @return Itself
194     */
195    public MotionMagicDutyCycle withPosition(double newPosition)
196    {
197        Position = newPosition;
198        return this;
199    }
200    
201    /**
202     * Modifies this Control Request's EnableFOC parameter and returns itself for
203     * method-chaining and easier to use request API.
204     *
205     * @param newEnableFOC Parameter to modify
206     * @return Itself
207     */
208    public MotionMagicDutyCycle withEnableFOC(boolean newEnableFOC)
209    {
210        EnableFOC = newEnableFOC;
211        return this;
212    }
213    
214    /**
215     * Modifies this Control Request's FeedForward parameter and returns itself for
216     * method-chaining and easier to use request API.
217     *
218     * @param newFeedForward Parameter to modify
219     * @return Itself
220     */
221    public MotionMagicDutyCycle withFeedForward(double newFeedForward)
222    {
223        FeedForward = newFeedForward;
224        return this;
225    }
226    
227    /**
228     * Modifies this Control Request's Slot parameter and returns itself for
229     * method-chaining and easier to use request API.
230     *
231     * @param newSlot Parameter to modify
232     * @return Itself
233     */
234    public MotionMagicDutyCycle withSlot(int newSlot)
235    {
236        Slot = newSlot;
237        return this;
238    }
239    
240    /**
241     * Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
242     * method-chaining and easier to use request API.
243     *
244     * @param newOverrideBrakeDurNeutral Parameter to modify
245     * @return Itself
246     */
247    public MotionMagicDutyCycle withOverrideBrakeDurNeutral(boolean newOverrideBrakeDurNeutral)
248    {
249        OverrideBrakeDurNeutral = newOverrideBrakeDurNeutral;
250        return this;
251    }
252    /**
253     * Sets the period at which this control will update at.
254     * This is designated in Hertz, with a minimum of 20 Hz
255     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
256     * <p>
257     * If this field is set to 0 Hz, the control request will
258     * be sent immediately as a one-shot frame. This may be useful
259     * for advanced applications that require outputs to be
260     * synchronized with data acquisition. In this case, we
261     * recommend not exceeding 50 ms between control calls.
262     *
263     * @param newUpdateFreqHz Parameter to modify
264     * @return Itself
265     */
266    public MotionMagicDutyCycle withUpdateFreqHz(double newUpdateFreqHz)
267    {
268        UpdateFreqHz = newUpdateFreqHz;
269        return this;
270    }
271    /**
272     * Forces configs to be applied the next time this is used in a setControl.
273     * <p>
274     * This is not necessary in the majority of cases, because Phoenix will make sure configs are
275     * properly set when they are not already set
276     */
277    public void forceApplyConfigs() { applyConfigsOnRequest = true; }
278}
279