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.phoenix6.controls;
008
009import com.ctre.phoenix6.StatusCode;
010import com.ctre.phoenix6.controls.jni.ControlJNI;
011
012import java.util.HashMap;
013import java.util.Map;
014
015/**
016 * Follow the differential motor output of another Talon while ignoring the
017 * master's invert setting.
018 * <p>
019 * If Talon is in torque control, the torque is copied - which will increase the total torque applied. If
020 * Talon is in percent supply output control, the duty cycle is matched.  Motor direction is strictly
021 * determined by the configured invert and not the master.  If you want motor direction to match or oppose the
022 * master, use FollowerRequest instead.
023 */
024public class DifferentialStrictFollower extends ControlRequest implements Cloneable
025{
026    /**
027     * Device ID of the differential master to follow.
028     */
029    public int MasterID;
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 = 20; // Default to 20Hz
043
044    /**
045     * Follow the differential motor output of another Talon while ignoring the
046     * master's invert setting.
047     * <p>
048     * If Talon is in torque control, the torque is copied - which will increase the
049     * total torque applied. If Talon is in percent supply output control, the duty
050     * cycle is matched.  Motor direction is strictly determined by the configured
051     * invert and not the master.  If you want motor direction to match or oppose
052     * the master, use FollowerRequest instead.
053     * 
054     * @param MasterID    Device ID of the differential master to follow.
055     */
056    public DifferentialStrictFollower(int MasterID)
057    {
058        super("DifferentialStrictFollower");
059        this.MasterID = MasterID;
060    }
061
062    @Override
063    public String toString()
064    {
065        String ss = "class: DifferentialStrictFollower\n";
066        ss += "MasterID: " + MasterID + "\n";
067        return ss;
068    }
069
070    @Override
071    public StatusCode sendRequest(String network, int deviceHash, boolean cancelOtherRequests)
072    {
073        return StatusCode.valueOf(ControlJNI.JNI_RequestControlDifferentialStrictFollower(
074                network, deviceHash, UpdateFreqHz, cancelOtherRequests, MasterID));
075    }
076
077    /**
078     * Gets information about this control request.
079     *
080     * @return Map of control parameter names and corresponding applied values
081     */
082    @Override
083    public Map<String, String> getControlInfo()
084    {
085        var controlInfo = new HashMap<String, String>();
086        controlInfo.put("Name", getName());
087        controlInfo.put("MasterID", String.valueOf(this.MasterID));
088        return controlInfo;
089    }
090    
091    /**
092     * Modifies this Control Request's MasterID parameter and returns itself for
093     * method-chaining and easier to use request API.
094     * <p>
095     * Device ID of the differential master to follow.
096     *
097     * @param newMasterID Parameter to modify
098     * @return Itself
099     */
100    public DifferentialStrictFollower withMasterID(int newMasterID)
101    {
102        MasterID = newMasterID;
103        return this;
104    }
105    /**
106     * Sets the period at which this control will update at.
107     * This is designated in Hertz, with a minimum of 20 Hz
108     * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
109     * <p>
110     * If this field is set to 0 Hz, the control request will
111     * be sent immediately as a one-shot frame. This may be useful
112     * for advanced applications that require outputs to be
113     * synchronized with data acquisition. In this case, we
114     * recommend not exceeding 50 ms between control calls.
115     *
116     * @param newUpdateFreqHz Parameter to modify
117     * @return Itself
118     */
119    public DifferentialStrictFollower withUpdateFreqHz(double newUpdateFreqHz)
120    {
121        UpdateFreqHz = newUpdateFreqHz;
122        return this;
123    }
124
125    @Override
126    public DifferentialStrictFollower clone()
127    {
128        try {
129            return (DifferentialStrictFollower)super.clone();
130        } catch (CloneNotSupportedException ex) {
131            /* this should never happen */
132            throw new RuntimeException(ex);
133        }
134    }
135}
136