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.sim;
008
009import com.ctre.phoenix6.StatusCode;
010import com.ctre.phoenixpro.hardware.core.CoreTalonFX;
011import com.ctre.phoenixpro.hardware.TalonFX;
012import com.ctre.phoenix6.jni.PlatformJNI;
013
014/**
015 * Class to control the state of a simulated {@link TalonFX}.
016 *
017 * @deprecated Classes in the phoenixpro package will be removed in 2024.
018 *             Users should instead use classes from the phoenix6 package.
019 */
020@Deprecated(forRemoval = true)
021public class TalonFXSimState {
022        private final DeviceType kDevType = DeviceType.PRO_TalonFXType;
023
024        private final int _id;
025
026        /**
027         * The orientation of the TalonFX relative to the robot chassis.
028         * <p>
029         * This value should not be changed based on the TalonFX invert.
030         * Rather, this value should be changed when the mechanical linkage
031         * between the TalonFX and the robot changes.
032         */
033        public ChassisReference Orientation;
034
035        /**
036         * Creates an object to control the state of the given {@link TalonFX}.
037         * <p>
038         * This constructor defaults to a counter-clockwise positive orientation
039         * relative to the robot chassis.
040         *
041         * @param device
042         *        Device to which this simulation state is attached
043     *
044     * @deprecated Classes in the phoenixpro package will be removed in 2024.
045     *             Users should instead use classes from the phoenix6 package.
046     */
047    @Deprecated(forRemoval = true)
048        public TalonFXSimState(CoreTalonFX device) {
049                this(device, ChassisReference.CounterClockwise_Positive);
050        }
051        /**
052         * Creates an object to control the state of the given {@link TalonFX}.
053         *
054         * @param device
055         *        Device to which this simulation state is attached
056         * @param orientation
057         *        Orientation of the device relative to the robot chassis
058     *
059     * @deprecated Classes in the phoenixpro package will be removed in 2024.
060     *             Users should instead use classes from the phoenix6 package.
061     */
062    @Deprecated(forRemoval = true)
063        public TalonFXSimState(CoreTalonFX device, ChassisReference orientation) {
064                _id = device.getDeviceID();
065                Orientation = orientation;
066        }
067
068        /**
069         * Gets the last status code generated by a simulation function.
070         * <p>
071         * Not all functions return a status code but can potentially report errors.
072         * This function can be used to retrieve those status codes.
073         *
074         * @return Last status code generated by a simulation function
075         */
076        public StatusCode getLastStatusCode() {
077                return StatusCode.valueOf(PlatformJNI.JNI_SimGetLastError(kDevType.value, _id));
078        }
079        /**
080         * Gets the simulated output voltage of the motor.
081         *
082         * @return Voltage applied to the motor in Volts
083         */
084        public double getMotorVoltage() {
085                double value = PlatformJNI.JNI_SimGetPhysicsValue(kDevType.value, _id, "MotorVoltage");
086                if (Orientation == ChassisReference.Clockwise_Positive) {
087                        value = -value;
088                }
089                return value;
090        }
091        /**
092         * Gets the simulated output torque current of the motor.
093         *
094         * @return Torque current applied to the motor in Amperes
095         */
096        public double getTorqueCurrent() {
097                double value = PlatformJNI.JNI_SimGetPhysicsValue(kDevType.value, _id, "TorqueCurrent");
098                if (Orientation == ChassisReference.Clockwise_Positive) {
099                        value = -value;
100                }
101                return value;
102        }
103        /**
104         * Gets the simulated supply current of the TalonFX.
105         *
106         * @return Supply current of the TalonFX in Amperes
107         */
108        public double getSupplyCurrent() {
109                double value = PlatformJNI.JNI_SimGetPhysicsValue(kDevType.value, _id, "SupplyCurrent");
110                return value;
111        }
112
113        /**
114         * Sets the simulated supply voltage of the TalonFX.
115         * <p>
116         * The minimum allowed supply voltage is 4 V - values below this
117         * will be promoted to 4 V.
118         *
119         * @param volts
120         *        The supply voltage in Volts
121         * @return Status code
122         */
123        public StatusCode setSupplyVoltage(double volts) {
124                return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "SupplyVoltage", volts));
125        }
126        /**
127         * Sets the simulated forward limit switch of the TalonFX.
128         *
129         * @param closed
130         *        Whether the limit switch is closed
131         * @return Status code
132         */
133        public StatusCode setForwardLimit(boolean closed) {
134                return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "ForwardLimit", closed ? 1 : 0));
135        }
136        /**
137         * Sets the simulated reverse limit switch of the TalonFX.
138         *
139         * @param closed
140         *        Whether the limit switch is closed
141         * @return Status code
142         */
143        public StatusCode setReverseLimit(boolean closed) {
144                return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "ReverseLimit", closed ? 1 : 0));
145        }
146        /**
147         * Sets the simulated raw rotor position of the TalonFX.
148         * <p>
149         * Inputs to this function over time should be continuous, as user calls of {@link TalonFX#setRotorPosition} will be accounted for in the callee.
150         * <p>
151         * The TalonFX integrates this to calculate the true reported rotor position.
152         * <p>
153         * When using the WPI Sim GUI, you will notice a readonly {@code position} and settable {@code rawPositionInput}.
154         * The readonly signal is the emulated position which will match self-test in Tuner and the hardware API.
155         * Changes to {@code rawPositionInput} will be integrated into the emulated position.
156         * This way a simulator can modify the position without overriding hardware API calls for home-ing the sensor.
157         *
158         * @param rotations
159         *        The raw position in rotations
160         * @return Status code
161         */
162        public StatusCode setRawRotorPosition(double rotations) {
163                if (Orientation == ChassisReference.Clockwise_Positive) {
164                        rotations = -rotations;
165                }
166                return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "RawRotorPosition", rotations));
167        }
168        /**
169         * Adds to the simulated rotor position of the TalonFX.
170         *
171         * @param dRotations
172         *        The change in position in rotations
173         * @return Status code
174         */
175        public StatusCode addRotorPosition(double dRotations) {
176                if (Orientation == ChassisReference.Clockwise_Positive) {
177                        dRotations = -dRotations;
178                }
179                return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "AddRotorPosition", dRotations));
180        }
181        /**
182         * Sets the simulated rotor velocity of the TalonFX.
183         *
184         * @param rps
185         *        The new velocity in rotations per second
186         * @return Status code
187         */
188        public StatusCode setRotorVelocity(double rps) {
189                if (Orientation == ChassisReference.Clockwise_Positive) {
190                        rps = -rps;
191                }
192                return StatusCode.valueOf(PlatformJNI.JNI_SimSetPhysicsInput(kDevType.value, _id, "RotorVelocity", rps));
193        }
194}