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 TalonSRX motor controller. 010 * 011 * Use the getSimCollection() routine inside your motor controller to create the respective sim collection. 012 */ 013public class TalonSRXSimCollection { 014 015 private int _id; 016 017 /** 018 * Constructor for TalonSRXSimCollection 019 * @param motorController Motor Controller to connect Collection to 020 */ 021 public TalonSRXSimCollection(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.TalonSRX.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.TalonSRX.value, _id, "MotorOutputLeadVoltage"); 044 } 045 046 /** 047 * Sets the simulated bus voltage of the TalonSRX. 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.TalonSRX.value, _id, "BusVoltage", vbat); 058 return ErrorCode.valueOf(retval); 059 } 060 061 /** 062 * Sets the simulated supply current of the TalonSRX. 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.TalonSRX.value, _id, "CurrentSupply", currA); 070 return ErrorCode.valueOf(retval); 071 } 072 073 /** 074 * Sets the simulated stator current of the TalonSRX. 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.TalonSRX.value, _id, "CurrentStator", currA); 082 return ErrorCode.valueOf(retval); 083 } 084 085 /** 086 * Sets the simulated forward limit switch of the TalonSRX. 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.TalonSRX.value, _id, "LimitFwd", isClosed ? 1 : 0); 094 return ErrorCode.valueOf(retval); 095 } 096 097 /** 098 * Sets the simulated reverse limit switch of the TalonSRX. 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.TalonSRX.value, _id, "LimitRev", isClosed ? 1 : 0); 106 return ErrorCode.valueOf(retval); 107 } 108 109 /** 110 * Sets the simulated analog position of the TalonSRX. 111 * 112 * @param newPos the new position in native units 113 * 114 * @return error code 115 */ 116 public ErrorCode setAnalogPosition(int newPos) { 117 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "AnalogPos", newPos); 118 return ErrorCode.valueOf(retval); 119 } 120 121 /** 122 * Adds to the simulated analog position of the TalonSRX. 123 * 124 * @param dPos the change in position in native units 125 * 126 * @return error code 127 */ 128 public ErrorCode addAnalogPosition(int dPos) { 129 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "AnalogAddPos", dPos); 130 return ErrorCode.valueOf(retval); 131 } 132 133 /** 134 * Sets the simulated analog velocity of the TalonSRX. 135 * 136 * @param newVel the new velocity in native units per 100ms 137 * 138 * @return error code 139 */ 140 public ErrorCode setAnalogVelocity(int newVel) { 141 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "AnalogVel", newVel); 142 return ErrorCode.valueOf(retval); 143 } 144 145 /** 146 * Sets if the simulated pulse width sensor is connected to the TalonSRX. 147 * 148 * @param isConnected true if the pulse width sensor is connected 149 * 150 * @return error code 151 */ 152 public ErrorCode setPulseWidthConnected(boolean isConnected) { 153 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthConnected", isConnected ? 1 : 0); 154 return ErrorCode.valueOf(retval); 155 } 156 157 /** 158 * Sets the simulated pulse width rise to rise time of the TalonSRX. 159 * 160 * @param periodUs the pulse width rise to rise time in microseconds 161 * 162 * @return error code 163 */ 164 public ErrorCode setPulseWidthRiseToRiseUs(double periodUs) { 165 int retval = setPulseWidthConnected(true).value; 166 if (retval == 0) { 167 retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthUs", periodUs); 168 } 169 return ErrorCode.valueOf(retval); 170 } 171 172 /** 173 * Sets the simulated pulse width position of the TalonSRX. 174 * 175 * @param newPos the new position in native units 176 * 177 * @return error code 178 */ 179 public ErrorCode setPulseWidthPosition(int newPos) { 180 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthPos", newPos); 181 return ErrorCode.valueOf(retval); 182 } 183 184 /** 185 * Adds to the simulated pulse width position of the TalonSRX. 186 * 187 * @param dPos the change in position in native units 188 * 189 * @return error code 190 */ 191 public ErrorCode addPulseWidthPosition(int dPos) { 192 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthAddPos", dPos); 193 return ErrorCode.valueOf(retval); 194 } 195 196 /** 197 * Sets the simulated pulse width velocity of the TalonSRX. 198 * 199 * @param newVel the new velocity in native units per 100ms 200 * 201 * @return error code 202 */ 203 public ErrorCode setPulseWidthVelocity(int newVel) { 204 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthVel", newVel); 205 return ErrorCode.valueOf(retval); 206 } 207 208 /** 209 * Sets the simulated raw quadrature position of the TalonSRX. 210 * <p> 211 * The TalonSRX integrates this to calculate the true reported quadrature 212 * position. 213 * <p> 214 * When using the WPI Sim GUI, you will notice a readonly 'position' and 215 * settable 'rawPositionInput'. The readonly signal is the emulated position 216 * which will match self-test in Tuner and the hardware API. Changes to 217 * 'rawPositionInput' will be integrated into the emulated position. This way 218 * a simulator can modify the position without overriding your 219 * hardware API calls for home-ing your sensor. 220 * <p> 221 * Inputs to this function over time should be continuous, 222 * as user calls of setSelectedSensorPosition() and setQuadraturePosition() 223 * will be accounted for in the calculation. 224 * 225 * @param newPos the new raw position in native units 226 * 227 * @return error code 228 */ 229 public ErrorCode setQuadratureRawPosition(int newPos) { 230 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "QuadEncRawPos", newPos); 231 return ErrorCode.valueOf(retval); 232 } 233 234 /** 235 * Adds to the simulated quadrature position of the TalonSRX. 236 * 237 * @param dPos the change in position in native units 238 * 239 * @return error code 240 */ 241 public ErrorCode addQuadraturePosition(int dPos) { 242 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "QuadEncAddPos", dPos); 243 return ErrorCode.valueOf(retval); 244 } 245 246 /** 247 * Sets the simulated quadrature velocity of the TalonSRX. 248 * 249 * @param newVel the new velocity in native units per 100ms 250 * 251 * @return error code 252 */ 253 public ErrorCode setQuadratureVelocity(int newVel) { 254 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "QuadEncVel", newVel); 255 return ErrorCode.valueOf(retval); 256 } 257}