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;
011
012import java.util.HashMap;
013import java.util.Map;
014
015/**
016 * Requires Phoenix Pro;
017 * Request PID to target velocity with torque current feedforward.
018 * <p>
019 * This control mode will set the motor's velocity setpoint to the velocity specified by the user. In
020 * addition, it will apply an additional torque current as an arbitrary feedforward value.
021 */
022public class VelocityTorqueCurrentFOC extends ControlRequest implements Cloneable
023{
024    /**
025     * Velocity to drive toward in rotations per second.
026     */
027    public double Velocity;
028    /**
029     * Acceleration to drive toward in rotations per second squared. This is
030     * typically used for motion profiles generated by the robot program.
031     */
032    public double Acceleration;
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     * Set to true to force forward limiting.  This allows users to use other limit
053     * switch sensors connected to robot controller.  This also allows use of active
054     * sensors that require external power.
055     */
056    public boolean LimitForwardMotion;
057    /**
058     * Set to true to force reverse limiting.  This allows users to use other limit
059     * switch sensors connected to robot controller.  This also allows use of active
060     * sensors that require external power.
061     */
062    public boolean LimitReverseMotion;
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     * Requires Phoenix Pro;
079     * Request PID to target velocity with torque current feedforward.
080     * <p>
081     * This control mode will set the motor's velocity setpoint to the velocity
082     * specified by the user. In addition, it will apply an additional torque
083     * current as an arbitrary feedforward value.
084     * 
085     * @param Velocity    Velocity to drive toward in rotations per second.
086     * @param Acceleration    Acceleration to drive toward in rotations per second
087     *                        squared. This is typically used for motion profiles
088     *                        generated by the robot program.
089     * @param FeedForward    Feedforward to apply in torque current in Amperes. 
090     *                       User can use motor's kT to scale Newton-meter to
091     *                       Amperes.
092     * @param Slot    Select which gains are applied by selecting the slot.  Use the
093     *                configuration api to set the gain values for the selected slot
094     *                before enabling this feature. Slot must be within [0,2].
095     * @param OverrideCoastDurNeutral    Set to true to coast the rotor when output
096     *                                   is zero (or within deadband).  Set to false
097     *                                   to use the NeutralMode configuration
098     *                                   setting (default). This flag exists to
099     *                                   provide the fundamental behavior of this
100     *                                   control when output is zero, which is to
101     *                                   provide 0A (zero torque).
102     * @param LimitForwardMotion    Set to true to force forward limiting.  This
103     *                              allows users to use other limit switch sensors
104     *                              connected to robot controller.  This also allows
105     *                              use of active sensors that require external
106     *                              power.
107     * @param LimitReverseMotion    Set to true to force reverse limiting.  This
108     *                              allows users to use other limit switch sensors
109     *                              connected to robot controller.  This also allows
110     *                              use of active sensors that require external
111     *                              power.
112     */
113    public VelocityTorqueCurrentFOC(double Velocity, double Acceleration, double FeedForward, int Slot, boolean OverrideCoastDurNeutral, boolean LimitForwardMotion, boolean LimitReverseMotion)
114    {
115        super("VelocityTorqueCurrentFOC");
116        this.Velocity = Velocity;
117        this.Acceleration = Acceleration;
118        this.FeedForward = FeedForward;
119        this.Slot = Slot;
120        this.OverrideCoastDurNeutral = OverrideCoastDurNeutral;
121        this.LimitForwardMotion = LimitForwardMotion;
122        this.LimitReverseMotion = LimitReverseMotion;
123    }
124
125        /**
126     * Requires Phoenix Pro;
127     * Request PID to target velocity with torque current feedforward.
128     * <p>
129     * This control mode will set the motor's velocity setpoint to the velocity
130     * specified by the user. In addition, it will apply an additional torque
131     * current as an arbitrary feedforward value.
132     * 
133     * @param Velocity    Velocity to drive toward in rotations per second.
134     */
135    public VelocityTorqueCurrentFOC(double Velocity)
136    {
137        this(Velocity, 0.0, 0.0, 0, false, false, false);
138    }
139
140    @Override
141    public String toString()
142    {
143        String ss = "class: VelocityTorqueCurrentFOC\n";
144        ss += "Velocity: " + Velocity + "\n";
145        ss += "Acceleration: " + Acceleration + "\n";
146        ss += "FeedForward: " + FeedForward + "\n";
147        ss += "Slot: " + Slot + "\n";
148        ss += "OverrideCoastDurNeutral: " + OverrideCoastDurNeutral + "\n";
149        ss += "LimitForwardMotion: " + LimitForwardMotion + "\n";
150        ss += "LimitReverseMotion: " + LimitReverseMotion + "\n";
151        return ss;
152    }
153
154    @Override
155    public StatusCode sendRequest(String network, int deviceHash, boolean cancelOtherRequests)
156    {
157        return StatusCode.valueOf(ControlJNI.JNI_RequestControlVelocityTorqueCurrentFOC(
158                network, deviceHash, UpdateFreqHz, cancelOtherRequests, Velocity, Acceleration, FeedForward, Slot, OverrideCoastDurNeutral, LimitForwardMotion, LimitReverseMotion));
159    }
160
161    /**
162     * Gets information about this control request.
163     *
164     * @return Map of control parameter names and corresponding applied values
165     */
166    @Override
167    public Map<String, String> getControlInfo()
168    {
169        var controlInfo = new HashMap<String, String>();
170        controlInfo.put("Name", getName());
171        controlInfo.put("Velocity", String.valueOf(this.Velocity));
172        controlInfo.put("Acceleration", String.valueOf(this.Acceleration));
173        controlInfo.put("FeedForward", String.valueOf(this.FeedForward));
174        controlInfo.put("Slot", String.valueOf(this.Slot));
175        controlInfo.put("OverrideCoastDurNeutral", String.valueOf(this.OverrideCoastDurNeutral));
176        controlInfo.put("LimitForwardMotion", String.valueOf(this.LimitForwardMotion));
177        controlInfo.put("LimitReverseMotion", String.valueOf(this.LimitReverseMotion));
178        return controlInfo;
179    }
180    
181    /**
182     * Modifies this Control Request's Velocity parameter and returns itself for
183     * method-chaining and easier to use request API.
184     * <p>
185     * Velocity to drive toward in rotations per second.
186     *
187     * @param newVelocity Parameter to modify
188     * @return Itself
189     */
190    public VelocityTorqueCurrentFOC withVelocity(double newVelocity)
191    {
192        Velocity = newVelocity;
193        return this;
194    }
195    
196    /**
197     * Modifies this Control Request's Acceleration parameter and returns itself for
198     * method-chaining and easier to use request API.
199     * <p>
200     * Acceleration to drive toward in rotations per second squared. This is
201     * typically used for motion profiles generated by the robot program.
202     *
203     * @param newAcceleration Parameter to modify
204     * @return Itself
205     */
206    public VelocityTorqueCurrentFOC withAcceleration(double newAcceleration)
207    {
208        Acceleration = newAcceleration;
209        return this;
210    }
211    
212    /**
213     * Modifies this Control Request's FeedForward parameter and returns itself for
214     * method-chaining and easier to use request API.
215     * <p>
216     * Feedforward to apply in torque current in Amperes.  User can use motor's kT
217     * to scale Newton-meter to Amperes.
218     *
219     * @param newFeedForward Parameter to modify
220     * @return Itself
221     */
222    public VelocityTorqueCurrentFOC withFeedForward(double newFeedForward)
223    {
224        FeedForward = newFeedForward;
225        return this;
226    }
227    
228    /**
229     * Modifies this Control Request's Slot parameter and returns itself for
230     * method-chaining and easier to use request API.
231     * <p>
232     * Select which gains are applied by selecting the slot.  Use the configuration
233     * api to set the gain values for the selected slot before enabling this
234     * feature. Slot must be within [0,2].
235     *
236     * @param newSlot Parameter to modify
237     * @return Itself
238     */
239    public VelocityTorqueCurrentFOC withSlot(int newSlot)
240    {
241        Slot = newSlot;
242        return this;
243    }
244    
245    /**
246     * Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
247     * method-chaining and easier to use request API.
248     * <p>
249     * Set to true to coast the rotor when output is zero (or within deadband).  Set
250     * to false to use the NeutralMode configuration setting (default). This flag
251     * exists to provide the fundamental behavior of this control when output is
252     * zero, which is to provide 0A (zero torque).
253     *
254     * @param newOverrideCoastDurNeutral Parameter to modify
255     * @return Itself
256     */
257    public VelocityTorqueCurrentFOC withOverrideCoastDurNeutral(boolean newOverrideCoastDurNeutral)
258    {
259        OverrideCoastDurNeutral = newOverrideCoastDurNeutral;
260        return this;
261    }
262    
263    /**
264     * Modifies this Control Request's LimitForwardMotion parameter and returns itself for
265     * method-chaining and easier to use request API.
266     * <p>
267     * Set to true to force forward limiting.  This allows users to use other limit
268     * switch sensors connected to robot controller.  This also allows use of active
269     * sensors that require external power.
270     *
271     * @param newLimitForwardMotion Parameter to modify
272     * @return Itself
273     */
274    public VelocityTorqueCurrentFOC withLimitForwardMotion(boolean newLimitForwardMotion)
275    {
276        LimitForwardMotion = newLimitForwardMotion;
277        return this;
278    }
279    
280    /**
281     * Modifies this Control Request's LimitReverseMotion parameter and returns itself for
282     * method-chaining and easier to use request API.
283     * <p>
284     * Set to true to force reverse limiting.  This allows users to use other limit
285     * switch sensors connected to robot controller.  This also allows use of active
286     * sensors that require external power.
287     *
288     * @param newLimitReverseMotion Parameter to modify
289     * @return Itself
290     */
291    public VelocityTorqueCurrentFOC withLimitReverseMotion(boolean newLimitReverseMotion)
292    {
293        LimitReverseMotion = newLimitReverseMotion;
294        return this;
295    }
296    /**
297     * Sets the period at which this control will update at.
298     * This is designated in Hertz, with a minimum of 20 Hz
299     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
300     * <p>
301     * If this field is set to 0 Hz, the control request will
302     * be sent immediately as a one-shot frame. This may be useful
303     * for advanced applications that require outputs to be
304     * synchronized with data acquisition. In this case, we
305     * recommend not exceeding 50 ms between control calls.
306     *
307     * @param newUpdateFreqHz Parameter to modify
308     * @return Itself
309     */
310    public VelocityTorqueCurrentFOC withUpdateFreqHz(double newUpdateFreqHz)
311    {
312        UpdateFreqHz = newUpdateFreqHz;
313        return this;
314    }
315
316    @Override
317    public VelocityTorqueCurrentFOC clone()
318    {
319        try {
320            return (VelocityTorqueCurrentFOC)super.clone();
321        } catch (CloneNotSupportedException ex) {
322            /* this should never happen */
323            throw new RuntimeException(ex);
324        }
325    }
326}
327