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 * Follow the motor output of another Talon while ignoring the master's invert
015 * setting.
016 * <p>
017 * If Talon is in torque control, the torque is copied - which will increase the total torque applied. If
018 * Talon is in percent supply output control, the duty cycle is matched.  Motor direction is strictly
019 * determined by the configured invert and not the master.  If you want motor direction to match or oppose the
020 * master, use FollowerRequest instead.
021 */
022public class StrictFollower extends ControlRequest
023{
024    private boolean applyConfigsOnRequest;
025    /**
026     * Device ID of the master to follow.
027     */
028    public int MasterID;
029
030    
031    /**
032     * The period at which this control will update at.
033     * This is designated in Hertz, with a minimum of 20 Hz
034     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
035     * <p>
036     * If this field is set to 0 Hz, the control request will
037     * be sent immediately as a one-shot frame. This may be useful
038     * for advanced applications that require outputs to be
039     * synchronized with data acquisition. In this case, we
040     * recommend not exceeding 50 ms between control calls.
041     */
042    public double UpdateFreqHz = 100; // Default to 100Hz
043
044    /**
045     * The timeout when sending configs associated with this control
046     */
047    public double configTimeout = 0.1;
048
049    /**
050     * Follow the motor output of another Talon while ignoring the master's
051     * invert setting.
052     * <p>
053     * If Talon is in torque control, the torque is copied - which will increase
054     * the total torque applied. If Talon is in percent supply output control,
055     * the duty cycle is matched.  Motor direction is strictly determined by the
056     * configured invert and not the master.  If you want motor direction to
057     * match or oppose the master, use FollowerRequest instead.
058     *
059     * @param MasterID     Device ID of the master to follow.
060     */
061    public StrictFollower(int MasterID)
062    {
063        super("StrictFollower");
064        this.MasterID = MasterID;
065    }
066
067    @Override
068    public String toString()
069    {
070        String ss = "class: StrictFollower\n";
071        ss += "MasterID: " + MasterID + "\n";
072        return ss;
073    }
074
075    @Override
076    public StatusCode sendRequest(String network, int deviceHash, boolean cancelOtherRequests)
077    {
078        var ref = requestReference.getNameValues();
079        ref.put("MasterID", String.valueOf(this.MasterID));
080        String ss = "";
081        
082        ControlConfigJNI.JNI_RequestConfigApply(network, deviceHash, configTimeout, ss, applyConfigsOnRequest);
083        applyConfigsOnRequest = false;
084        return StatusCode.valueOf(ControlJNI.JNI_RequestControlStrictFollower(
085                network, deviceHash, UpdateFreqHz, cancelOtherRequests, MasterID));
086    }
087    
088    /**
089     * Modifies this Control Request's MasterID parameter and returns itself for
090     * method-chaining and easier to use request API.
091     *
092     * @param newMasterID Parameter to modify
093     * @return Itself
094     */
095    public StrictFollower withMasterID(int newMasterID)
096    {
097        MasterID = newMasterID;
098        return this;
099    }
100    /**
101     * Sets the period at which this control will update at.
102     * This is designated in Hertz, with a minimum of 20 Hz
103     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
104     * <p>
105     * If this field is set to 0 Hz, the control request will
106     * be sent immediately as a one-shot frame. This may be useful
107     * for advanced applications that require outputs to be
108     * synchronized with data acquisition. In this case, we
109     * recommend not exceeding 50 ms between control calls.
110     *
111     * @param newUpdateFreqHz Parameter to modify
112     * @return Itself
113     */
114    public StrictFollower withUpdateFreqHz(double newUpdateFreqHz)
115    {
116        UpdateFreqHz = newUpdateFreqHz;
117        return this;
118    }
119    /**
120     * Forces configs to be applied the next time this is used in a setControl.
121     * <p>
122     * This is not necessary in the majority of cases, because Phoenix will make sure configs are
123     * properly set when they are not already set
124     */
125    public void forceApplyConfigs() { applyConfigsOnRequest = true; }
126}
127