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