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