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