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