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