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 voltage 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 VelocityVoltage 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 volts
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 voltage 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 volts
099     * @param Slot     Select which gains are applied by selecting the slot. 
100     *                 Use the configuration api to set the gain values for the
101     *                 selected slot before enabling this feature. Slot must be
102     *                 within [0,2].
103     * @param OverrideBrakeDurNeutral     Set to true to static-brake the rotor
104     *                                    when output is zero (or within
105     *                                    deadband).  Set to false to use the
106     *                                    NeutralMode configuration setting
107     *                                    (default). This flag exists to provide
108     *                                    the fundamental behavior of this
109     *                                    control when output is zero, which is
110     *                                    to provide 0V to the motor.
111     *
112     * @deprecated Classes in the phoenixpro package will be removed in 2024.
113     *             Users should instead use classes from the phoenix6 package.
114     */
115    @Deprecated(forRemoval = true)
116    public VelocityVoltage(double Velocity, boolean EnableFOC, double FeedForward, int Slot, boolean OverrideBrakeDurNeutral)
117    {
118        super("VelocityVoltage");
119        this.Velocity = Velocity;
120        this.EnableFOC = EnableFOC;
121        this.FeedForward = FeedForward;
122        this.Slot = Slot;
123        this.OverrideBrakeDurNeutral = OverrideBrakeDurNeutral;
124    }
125
126    /**
127     * Request PID to target velocity with voltage 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 voltage
131     * as an arbitrary feedforward value.
132     *
133     * @param Velocity     Velocity to drive toward in rotations per second.
134     *
135     * @deprecated Classes in the phoenixpro package will be removed in 2024.
136     *             Users should instead use classes from the phoenix6 package.
137     */
138    @Deprecated(forRemoval = true)
139    public VelocityVoltage(double Velocity)
140    {
141        this(Velocity, true, 0.0, 0, false);
142    }
143
144    @Override
145    public String toString()
146    {
147        String ss = "class: VelocityVoltage\n";
148        ss += "Velocity: " + Velocity + "\n";
149        ss += "EnableFOC: " + EnableFOC + "\n";
150        ss += "FeedForward: " + FeedForward + "\n";
151        ss += "Slot: " + Slot + "\n";
152        ss += "OverrideBrakeDurNeutral: " + OverrideBrakeDurNeutral + "\n";
153        return ss;
154    }
155
156    @Override
157    public StatusCode sendRequest(String network, int deviceHash, boolean cancelOtherRequests)
158    {
159        var ref = requestReference.getNameValues();
160        ref.put("Velocity", String.valueOf(this.Velocity));
161        ref.put("EnableFOC", String.valueOf(this.EnableFOC));
162        ref.put("FeedForward", String.valueOf(this.FeedForward));
163        ref.put("Slot", String.valueOf(this.Slot));
164        ref.put("OverrideBrakeDurNeutral", String.valueOf(this.OverrideBrakeDurNeutral));
165        String ss = "";
166        
167        ControlConfigJNI.JNI_RequestConfigApply(network, deviceHash, configTimeout, ss, applyConfigsOnRequest);
168        applyConfigsOnRequest = false;
169        return StatusCode.valueOf(ControlJNI.JNI_RequestControlVelocityVoltage(
170                network, deviceHash, UpdateFreqHz, cancelOtherRequests, Velocity, EnableFOC, FeedForward, Slot, OverrideBrakeDurNeutral));
171    }
172    
173    /**
174     * Modifies this Control Request's Velocity parameter and returns itself for
175     * method-chaining and easier to use request API.
176     *
177     * @param newVelocity Parameter to modify
178     * @return Itself
179     */
180    public VelocityVoltage withVelocity(double newVelocity)
181    {
182        Velocity = newVelocity;
183        return this;
184    }
185    
186    /**
187     * Modifies this Control Request's EnableFOC parameter and returns itself for
188     * method-chaining and easier to use request API.
189     *
190     * @param newEnableFOC Parameter to modify
191     * @return Itself
192     */
193    public VelocityVoltage withEnableFOC(boolean newEnableFOC)
194    {
195        EnableFOC = newEnableFOC;
196        return this;
197    }
198    
199    /**
200     * Modifies this Control Request's FeedForward parameter and returns itself for
201     * method-chaining and easier to use request API.
202     *
203     * @param newFeedForward Parameter to modify
204     * @return Itself
205     */
206    public VelocityVoltage withFeedForward(double newFeedForward)
207    {
208        FeedForward = newFeedForward;
209        return this;
210    }
211    
212    /**
213     * Modifies this Control Request's Slot parameter and returns itself for
214     * method-chaining and easier to use request API.
215     *
216     * @param newSlot Parameter to modify
217     * @return Itself
218     */
219    public VelocityVoltage withSlot(int newSlot)
220    {
221        Slot = newSlot;
222        return this;
223    }
224    
225    /**
226     * Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
227     * method-chaining and easier to use request API.
228     *
229     * @param newOverrideBrakeDurNeutral Parameter to modify
230     * @return Itself
231     */
232    public VelocityVoltage withOverrideBrakeDurNeutral(boolean newOverrideBrakeDurNeutral)
233    {
234        OverrideBrakeDurNeutral = newOverrideBrakeDurNeutral;
235        return this;
236    }
237    /**
238     * Sets the period at which this control will update at.
239     * This is designated in Hertz, with a minimum of 20 Hz
240     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
241     * <p>
242     * If this field is set to 0 Hz, the control request will
243     * be sent immediately as a one-shot frame. This may be useful
244     * for advanced applications that require outputs to be
245     * synchronized with data acquisition. In this case, we
246     * recommend not exceeding 50 ms between control calls.
247     *
248     * @param newUpdateFreqHz Parameter to modify
249     * @return Itself
250     */
251    public VelocityVoltage withUpdateFreqHz(double newUpdateFreqHz)
252    {
253        UpdateFreqHz = newUpdateFreqHz;
254        return this;
255    }
256    /**
257     * Forces configs to be applied the next time this is used in a setControl.
258     * <p>
259     * This is not necessary in the majority of cases, because Phoenix will make sure configs are
260     * properly set when they are not already set
261     */
262    public void forceApplyConfigs() { applyConfigsOnRequest = true; }
263}
264