001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.motorcontrol;
003
004import com.ctre.phoenix.ErrorCode;
005import com.ctre.phoenix.motorcontrol.can.BaseTalon;
006import com.ctre.phoenix.platform.DeviceType;
007import com.ctre.phoenix.platform.PlatformJNI;
008
009/**
010 * Collection of simulation commands available to a TalonFX motor controller.
011 *
012 * Use the getSimCollection() routine inside your motor controller to create the respective sim collection.
013 *
014 * @deprecated This device's Phoenix 5 API is deprecated for removal in the
015 * 2025 season. Users should update to Phoenix 6 firmware and migrate to the
016 * Phoenix 6 API. A migration guide is available at
017 * https://v6.docs.ctr-electronics.com/en/stable/docs/migration/migration-guide/index.html.
018 * <p>
019 * If the Phoenix 5 API must be used for this device, the device must have 22.X
020 * firmware. This firmware is available in Tuner X after selecting Phoenix 5 in
021 * the firmware year dropdown.
022 */
023@Deprecated(since = "2024", forRemoval = true)
024public class TalonFXSimCollection {
025
026    private int _id;
027
028    /**
029     * Constructor for TalonFXSimCollection
030     * @param motorController Motor Controller to connect Collection to
031     */
032    public TalonFXSimCollection(BaseTalon motorController) {
033        _id = motorController.getDeviceID();
034    }
035
036    /**
037     * Gets the last error generated by this object. Not all functions return an
038     * error code but can potentially report errors. This function can be used
039     * to retrieve those error codes.
040     *
041     * @return Last Error Code generated by a function.
042     */
043    public ErrorCode getLastError() {
044        int retval = PlatformJNI.JNI_SimGetLastError(DeviceType.TalonFX.value, _id);
045        return ErrorCode.valueOf(retval);
046    }
047    
048    /**
049     * Gets the simulated output voltage across M+ and M- for the motor.
050     * 
051     * @return applied voltage to the motor in volts
052     */
053    public double getMotorOutputLeadVoltage() {
054        return PlatformJNI.JNI_SimGetPhysicsValue(DeviceType.TalonFX.value, _id, "MotorOutputLeadVoltage");
055    }
056
057    /**
058     * Sets the simulated bus voltage of the TalonFX.
059     * <p>
060     * The minimum allowed bus voltage is 4 V - values
061     * below this will be promoted to 4 V.
062     * 
063     * @param vbat the bus voltage in volts
064     *
065     * @return  error code
066     */
067    public ErrorCode setBusVoltage(double vbat) {
068        int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonFX.value, _id, "BusVoltage", vbat);
069        return ErrorCode.valueOf(retval);
070    }
071
072    /**
073     * Sets the simulated supply current of the TalonFX.
074     * 
075     * @param currA the supply current in amps
076     *
077     * @return  error code
078     */
079    public ErrorCode setSupplyCurrent(double currA) {
080        int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonFX.value, _id, "CurrentSupply", currA);
081        return ErrorCode.valueOf(retval);
082    }
083
084    /**
085     * Sets the simulated stator current of the TalonFX.
086     * 
087     * @param currA the stator current in amps
088     *
089     * @return  error code
090     */
091    public ErrorCode setStatorCurrent(double currA) {
092        int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonFX.value, _id, "CurrentStator", currA);
093        return ErrorCode.valueOf(retval);
094    }
095
096    /**
097     * Sets the simulated forward limit switch of the TalonFX.
098     * 
099     * @param isClosed true if the limit switch is closed
100     *
101     * @return  error code
102     */
103    public ErrorCode setLimitFwd(boolean isClosed) {
104        int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonFX.value, _id, "LimitFwd", isClosed ? 1 : 0);
105        return ErrorCode.valueOf(retval);
106    }
107
108    /**
109     * Sets the simulated reverse limit switch of the TalonFX.
110     * 
111     * @param isClosed true if the limit switch is closed
112     *
113     * @return  error code
114     */
115    public ErrorCode setLimitRev(boolean isClosed) {
116        int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonFX.value, _id, "LimitRev", isClosed ? 1 : 0);
117        return ErrorCode.valueOf(retval);
118    }
119
120    /**
121     * Sets the simulated raw integrated sensor position of the TalonFX.
122     * <p>
123     * The TalonFX integrates this to calculate the true reported integrated sensor
124     * position.
125     * <p>
126     * When using the WPI Sim GUI, you will notice a readonly 'position' and
127     * settable 'rawPositionInput'.  The readonly signal is the emulated position
128     * which will match self-test in Tuner and the hardware API.  Changes to
129     * 'rawPositionInput' will be integrated into the emulated position.  This way
130     * a simulator can modify the position without overriding your
131     * hardware API calls for home-ing your sensor.
132     * <p>
133     * Inputs to this function over time should be continuous,
134     * as user calls of setSelectedSensorPosition() and setIntegratedSensorPosition()
135     * will be accounted for in the calculation.
136     * 
137     * @param newPos the new raw position in native units
138     *
139     * @return  error code
140     */
141    public ErrorCode setIntegratedSensorRawPosition(int newPos) {
142        int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonFX.value, _id, "IntegSensRawPos", newPos);
143        return ErrorCode.valueOf(retval);
144    }
145
146    /**
147     * Adds to the simulated integrated sensor position of the TalonFX.
148     * 
149     * @param dPos the change in position in native units
150     *
151     * @return  error code
152     */
153    public ErrorCode addIntegratedSensorPosition(int dPos) {
154        int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonFX.value, _id, "IntegSensAddPos", dPos);
155        return ErrorCode.valueOf(retval);
156    }
157
158    /**
159     * Sets the simulated integrated sensor velocity of the TalonFX.
160     * 
161     * @param newVel the new velocity in native units per 100ms
162     *
163     * @return  error code
164     */
165    public ErrorCode setIntegratedSensorVelocity(int newVel) {
166        int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonFX.value, _id, "IntegSensVel", newVel);
167        return ErrorCode.valueOf(retval);
168    }
169}