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 * Follow the motor output of another Talon.
015 * <p>
016 * If Talon is in torque control, the torque is copied - which will increase the total torque applied. If
017 * Talon is in percent supply output control, the duty cycle is matched.  Motor direction either matches
018 * master's configured direction or opposes it based on OpposeMasterDirection.
019 *
020 * @deprecated Classes in the phoenixpro package will be removed in 2024.
021 *             Users should instead use classes from the phoenix6 package.
022 */
023@Deprecated(forRemoval = true)
024public class Follower extends ControlRequest
025{
026    private boolean applyConfigsOnRequest;
027    /**
028     * Device ID of the master to follow.
029     */
030    public int MasterID;
031    /**
032     * Set to false for motor invert to match the master's configured Invert - which
033     * is typical when master and follower are mechanically linked and spin in the
034     * same direction.  Set to true for motor invert to oppose the master's
035     * configured Invert - this is typical where the the master and follower
036     * mechanically spin in opposite directions.
037     */
038    public boolean OpposeMasterDirection;
039
040    
041    /**
042     * The period at which this control will update at.
043     * This is designated in Hertz, with a minimum of 20 Hz
044     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
045     * <p>
046     * If this field is set to 0 Hz, the control request will
047     * be sent immediately as a one-shot frame. This may be useful
048     * for advanced applications that require outputs to be
049     * synchronized with data acquisition. In this case, we
050     * recommend not exceeding 50 ms between control calls.
051     */
052    public double UpdateFreqHz = 20; // Default to 20Hz
053
054    /**
055     * The timeout when sending configs associated with this control
056     */
057    public double configTimeout = 0.1;
058
059    /**
060     * Follow the motor output of another Talon.
061     * <p>
062     * If Talon is in torque control, the torque is copied - which will increase
063     * the total torque applied. If Talon is in percent supply output control,
064     * the duty cycle is matched.  Motor direction either matches master's
065     * configured direction or opposes it based on OpposeMasterDirection.
066     *
067     * @param MasterID     Device ID of the master to follow.
068     * @param OpposeMasterDirection     Set to false for motor invert to match
069     *                                  the master's configured Invert - which
070     *                                  is typical when master and follower are
071     *                                  mechanically linked and spin in the same
072     *                                  direction.  Set to true for motor invert
073     *                                  to oppose the master's configured Invert
074     *                                  - this is typical where the the master
075     *                                  and follower mechanically spin in
076     *                                  opposite directions.
077     *
078     * @deprecated Classes in the phoenixpro package will be removed in 2024.
079     *             Users should instead use classes from the phoenix6 package.
080     */
081    @Deprecated(forRemoval = true)
082    public Follower(int MasterID, boolean OpposeMasterDirection)
083    {
084        super("Follower");
085        this.MasterID = MasterID;
086        this.OpposeMasterDirection = OpposeMasterDirection;
087    }
088
089    @Override
090    public String toString()
091    {
092        String ss = "class: Follower\n";
093        ss += "MasterID: " + MasterID + "\n";
094        ss += "OpposeMasterDirection: " + OpposeMasterDirection + "\n";
095        return ss;
096    }
097
098    @Override
099    public StatusCode sendRequest(String network, int deviceHash, boolean cancelOtherRequests)
100    {
101        var ref = requestReference.getNameValues();
102        ref.put("MasterID", String.valueOf(this.MasterID));
103        ref.put("OpposeMasterDirection", String.valueOf(this.OpposeMasterDirection));
104        String ss = "";
105        
106        ControlConfigJNI.JNI_RequestConfigApply(network, deviceHash, configTimeout, ss, applyConfigsOnRequest);
107        applyConfigsOnRequest = false;
108        return StatusCode.valueOf(ControlJNI.JNI_RequestControlFollower(
109                network, deviceHash, UpdateFreqHz, cancelOtherRequests, MasterID, OpposeMasterDirection));
110    }
111    
112    /**
113     * Modifies this Control Request's MasterID parameter and returns itself for
114     * method-chaining and easier to use request API.
115     *
116     * @param newMasterID Parameter to modify
117     * @return Itself
118     */
119    public Follower withMasterID(int newMasterID)
120    {
121        MasterID = newMasterID;
122        return this;
123    }
124    
125    /**
126     * Modifies this Control Request's OpposeMasterDirection parameter and returns itself for
127     * method-chaining and easier to use request API.
128     *
129     * @param newOpposeMasterDirection Parameter to modify
130     * @return Itself
131     */
132    public Follower withOpposeMasterDirection(boolean newOpposeMasterDirection)
133    {
134        OpposeMasterDirection = newOpposeMasterDirection;
135        return this;
136    }
137    /**
138     * Sets the period at which this control will update at.
139     * This is designated in Hertz, with a minimum of 20 Hz
140     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
141     * <p>
142     * If this field is set to 0 Hz, the control request will
143     * be sent immediately as a one-shot frame. This may be useful
144     * for advanced applications that require outputs to be
145     * synchronized with data acquisition. In this case, we
146     * recommend not exceeding 50 ms between control calls.
147     *
148     * @param newUpdateFreqHz Parameter to modify
149     * @return Itself
150     */
151    public Follower withUpdateFreqHz(double newUpdateFreqHz)
152    {
153        UpdateFreqHz = newUpdateFreqHz;
154        return this;
155    }
156    /**
157     * Forces configs to be applied the next time this is used in a setControl.
158     * <p>
159     * This is not necessary in the majority of cases, because Phoenix will make sure configs are
160     * properly set when they are not already set
161     */
162    public void forceApplyConfigs() { applyConfigsOnRequest = true; }
163}
164