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;
012import com.ctre.phoenixpro.configs.*;
013
014/**
015 * Request a specified voltage.
016 * <p>
017 * This control mode will attempt to apply the specified voltage to the motor. If the supply voltage is below
018 * the requested voltage, the motor controller will output the supply voltage.
019 */
020public class VoltageOut extends ControlRequest
021{
022    private boolean applyConfigsOnRequest;
023    /**
024     * Voltage to attempt to drive at
025     */
026    public double Output;
027    /**
028     * Set to true to use FOC commutation, which increases peak power by ~15%. Set
029     * to false to use trapezoidal commutation.  FOC improves motor performance by
030     * leveraging torque (current) control.  However, this may be inconvenient for
031     * applications that require specifying duty cycle or voltage.  CTR-Electronics
032     * has developed a hybrid method that combines the performances gains of FOC
033     * while still allowing applications to provide duty cycle or voltage demand. 
034     * This not to be confused with simple sinusoidal control or phase voltage
035     * control which lacks the performance gains.
036     */
037    public boolean EnableFOC;
038    /**
039     * Set to true to static-brake the rotor when output is zero (or within
040     * deadband).  Set to false to use the NeutralMode configuration setting
041     * (default). This flag exists to provide the fundamental behavior of this
042     * control when output is zero, which is to provide 0V to the motor.
043     */
044    public boolean OverrideBrakeDurNeutral;
045
046    /**
047     *  Voltage-specific configs
048     * <p>
049     *  Voltage-specific configs
050     */
051    public VoltageConfigs Voltage = new VoltageConfigs();
052    /**
053     * The period at which this control will update at.
054     * This is designated in Hertz, with a minimum of 20 Hz
055     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
056     * <p>
057     * If this field is set to 0 Hz, the control request will
058     * be sent immediately as a one-shot frame. This may be useful
059     * for advanced applications that require outputs to be
060     * synchronized with data acquisition. In this case, we
061     * recommend not exceeding 50 ms between control calls.
062     */
063    public double UpdateFreqHz = 100; // Default to 100Hz
064
065    /**
066     * The timeout when sending configs associated with this control
067     */
068    public double configTimeout = 0.1;
069
070    /**
071     * Request a specified voltage.
072     * <p>
073     * This control mode will attempt to apply the specified voltage to the
074     * motor. If the supply voltage is below the requested voltage, the motor
075     * controller will output the supply voltage.
076     *
077     * @param Output     Voltage to attempt to drive at
078     * @param EnableFOC     Set to true to use FOC commutation, which increases
079     *                      peak power by ~15%. Set to false to use trapezoidal
080     *                      commutation.  FOC improves motor performance by
081     *                      leveraging torque (current) control.  However, this
082     *                      may be inconvenient for applications that require
083     *                      specifying duty cycle or voltage.  CTR-Electronics
084     *                      has developed a hybrid method that combines the
085     *                      performances gains of FOC while still allowing
086     *                      applications to provide duty cycle or voltage
087     *                      demand.  This not to be confused with simple
088     *                      sinusoidal control or phase voltage control which
089     *                      lacks the performance gains.
090     * @param OverrideBrakeDurNeutral     Set to true to static-brake the rotor
091     *                                    when output is zero (or within
092     *                                    deadband).  Set to false to use the
093     *                                    NeutralMode configuration setting
094     *                                    (default). This flag exists to provide
095     *                                    the fundamental behavior of this
096     *                                    control when output is zero, which is
097     *                                    to provide 0V to the motor.
098     */
099    public VoltageOut(double Output, boolean EnableFOC, boolean OverrideBrakeDurNeutral)
100    {
101        super("VoltageOut");
102        this.Output = Output;
103        this.EnableFOC = EnableFOC;
104        this.OverrideBrakeDurNeutral = OverrideBrakeDurNeutral;
105    }
106
107    /**
108     * Request a specified voltage.
109     * <p>
110     * This control mode will attempt to apply the specified voltage to the
111     * motor. If the supply voltage is below the requested voltage, the motor
112     * controller will output the supply voltage.
113     *
114     * @param Output     Voltage to attempt to drive at
115     */
116    public VoltageOut(double Output)
117    {
118        this(Output, true, false);
119    }
120
121    @Override
122    public String toString()
123    {
124        String ss = "class: VoltageOut\n";
125        ss += "Output: " + Output + "\n";
126        ss += "EnableFOC: " + EnableFOC + "\n";
127        ss += "OverrideBrakeDurNeutral: " + OverrideBrakeDurNeutral + "\n";
128        return ss;
129    }
130
131    @Override
132    public StatusCode sendRequest(String network, int deviceHash, boolean cancelOtherRequests)
133    {
134        var ref = requestReference.getNameValues();
135        ref.put("Output", String.valueOf(this.Output));
136        ref.put("EnableFOC", String.valueOf(this.EnableFOC));
137        ref.put("OverrideBrakeDurNeutral", String.valueOf(this.OverrideBrakeDurNeutral));
138        String ss = "";
139        ss += Voltage.serialize();
140        ControlConfigJNI.JNI_RequestConfigApply(network, deviceHash, configTimeout, ss, applyConfigsOnRequest);
141        applyConfigsOnRequest = false;
142        return StatusCode.valueOf(ControlJNI.JNI_RequestControlVoltageOut(
143                network, deviceHash, UpdateFreqHz, cancelOtherRequests, Output, EnableFOC, OverrideBrakeDurNeutral));
144    }
145    
146    /**
147     * Modifies this Control Request's Output parameter and returns itself for
148     * method-chaining and easier to use request API.
149     *
150     * @param newOutput Parameter to modify
151     * @return Itself
152     */
153    public VoltageOut withOutput(double newOutput)
154    {
155        Output = newOutput;
156        return this;
157    }
158    
159    /**
160     * Modifies this Control Request's EnableFOC parameter and returns itself for
161     * method-chaining and easier to use request API.
162     *
163     * @param newEnableFOC Parameter to modify
164     * @return Itself
165     */
166    public VoltageOut withEnableFOC(boolean newEnableFOC)
167    {
168        EnableFOC = newEnableFOC;
169        return this;
170    }
171    
172    /**
173     * Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
174     * method-chaining and easier to use request API.
175     *
176     * @param newOverrideBrakeDurNeutral Parameter to modify
177     * @return Itself
178     */
179    public VoltageOut withOverrideBrakeDurNeutral(boolean newOverrideBrakeDurNeutral)
180    {
181        OverrideBrakeDurNeutral = newOverrideBrakeDurNeutral;
182        return this;
183    }
184    /**
185     * Sets the period at which this control will update at.
186     * This is designated in Hertz, with a minimum of 20 Hz
187     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
188     * <p>
189     * If this field is set to 0 Hz, the control request will
190     * be sent immediately as a one-shot frame. This may be useful
191     * for advanced applications that require outputs to be
192     * synchronized with data acquisition. In this case, we
193     * recommend not exceeding 50 ms between control calls.
194     *
195     * @param newUpdateFreqHz Parameter to modify
196     * @return Itself
197     */
198    public VoltageOut withUpdateFreqHz(double newUpdateFreqHz)
199    {
200        UpdateFreqHz = newUpdateFreqHz;
201        return this;
202    }
203    /**
204     * Forces configs to be applied the next time this is used in a setControl.
205     * <p>
206     * This is not necessary in the majority of cases, because Phoenix will make sure configs are
207     * properly set when they are not already set
208     */
209    public void forceApplyConfigs() { applyConfigsOnRequest = true; }
210}
211