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 TalonSRX motor controller. 011 * 012 * Use the getSimCollection() routine inside your motor controller to create the respective sim collection. 013 */ 014public class TalonSRXSimCollection { 015 016 private int _id; 017 018 /** 019 * Constructor for TalonSRXSimCollection 020 * @param motorController Motor Controller to connect Collection to 021 */ 022 public TalonSRXSimCollection(BaseTalon motorController) { 023 _id = motorController.getDeviceID(); 024 } 025 026 /** 027 * Gets the last error generated by this object. Not all functions return an 028 * error code but can potentially report errors. This function can be used 029 * to retrieve those error codes. 030 * 031 * @return Last Error Code generated by a function. 032 */ 033 public ErrorCode getLastError() { 034 int retval = PlatformJNI.JNI_SimGetLastError(DeviceType.TalonSRX.value, _id); 035 return ErrorCode.valueOf(retval); 036 } 037 038 /** 039 * Gets the simulated output voltage across M+ and M- for the motor. 040 * 041 * @return applied voltage to the motor in volts 042 */ 043 public double getMotorOutputLeadVoltage() { 044 return PlatformJNI.JNI_SimGetPhysicsValue(DeviceType.TalonSRX.value, _id, "MotorOutputLeadVoltage"); 045 } 046 047 /** 048 * Sets the simulated bus voltage of the TalonSRX. 049 * <p> 050 * The minimum allowed bus voltage is 4 V - values 051 * below this will be promoted to 4 V. 052 * 053 * @param vbat the bus voltage in volts 054 * 055 * @return error code 056 */ 057 public ErrorCode setBusVoltage(double vbat) { 058 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "BusVoltage", vbat); 059 return ErrorCode.valueOf(retval); 060 } 061 062 /** 063 * Sets the simulated supply current of the TalonSRX. 064 * 065 * @param currA the supply current in amps 066 * 067 * @return error code 068 */ 069 public ErrorCode setSupplyCurrent(double currA) { 070 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "CurrentSupply", currA); 071 return ErrorCode.valueOf(retval); 072 } 073 074 /** 075 * Sets the simulated stator current of the TalonSRX. 076 * 077 * @param currA the stator current in amps 078 * 079 * @return error code 080 */ 081 public ErrorCode setStatorCurrent(double currA) { 082 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "CurrentStator", currA); 083 return ErrorCode.valueOf(retval); 084 } 085 086 /** 087 * Sets the simulated forward limit switch of the TalonSRX. 088 * 089 * @param isClosed true if the limit switch is closed 090 * 091 * @return error code 092 */ 093 public ErrorCode setLimitFwd(boolean isClosed) { 094 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "LimitFwd", isClosed ? 1 : 0); 095 return ErrorCode.valueOf(retval); 096 } 097 098 /** 099 * Sets the simulated reverse limit switch of the TalonSRX. 100 * 101 * @param isClosed true if the limit switch is closed 102 * 103 * @return error code 104 */ 105 public ErrorCode setLimitRev(boolean isClosed) { 106 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "LimitRev", isClosed ? 1 : 0); 107 return ErrorCode.valueOf(retval); 108 } 109 110 /** 111 * Sets the simulated analog position of the TalonSRX. 112 * 113 * @param newPos the new position in native units 114 * 115 * @return error code 116 */ 117 public ErrorCode setAnalogPosition(int newPos) { 118 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "AnalogPos", newPos); 119 return ErrorCode.valueOf(retval); 120 } 121 122 /** 123 * Adds to the simulated analog position of the TalonSRX. 124 * 125 * @param dPos the change in position in native units 126 * 127 * @return error code 128 */ 129 public ErrorCode addAnalogPosition(int dPos) { 130 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "AnalogAddPos", dPos); 131 return ErrorCode.valueOf(retval); 132 } 133 134 /** 135 * Sets the simulated analog velocity of the TalonSRX. 136 * 137 * @param newVel the new velocity in native units per 100ms 138 * 139 * @return error code 140 */ 141 public ErrorCode setAnalogVelocity(int newVel) { 142 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "AnalogVel", newVel); 143 return ErrorCode.valueOf(retval); 144 } 145 146 /** 147 * Sets if the simulated pulse width sensor is connected to the TalonSRX. 148 * 149 * @param isConnected true if the pulse width sensor is connected 150 * 151 * @return error code 152 */ 153 public ErrorCode setPulseWidthConnected(boolean isConnected) { 154 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthConnected", isConnected ? 1 : 0); 155 return ErrorCode.valueOf(retval); 156 } 157 158 /** 159 * Sets the simulated pulse width rise to rise time of the TalonSRX. 160 * 161 * @param periodUs the pulse width rise to rise time in microseconds 162 * 163 * @return error code 164 */ 165 public ErrorCode setPulseWidthRiseToRiseUs(double periodUs) { 166 int retval = setPulseWidthConnected(true).value; 167 if (retval == 0) { 168 retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthUs", periodUs); 169 } 170 return ErrorCode.valueOf(retval); 171 } 172 173 /** 174 * Sets the simulated pulse width position of the TalonSRX. 175 * 176 * @param newPos the new position in native units 177 * 178 * @return error code 179 */ 180 public ErrorCode setPulseWidthPosition(int newPos) { 181 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthPos", newPos); 182 return ErrorCode.valueOf(retval); 183 } 184 185 /** 186 * Adds to the simulated pulse width position of the TalonSRX. 187 * 188 * @param dPos the change in position in native units 189 * 190 * @return error code 191 */ 192 public ErrorCode addPulseWidthPosition(int dPos) { 193 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthAddPos", dPos); 194 return ErrorCode.valueOf(retval); 195 } 196 197 /** 198 * Sets the simulated pulse width velocity of the TalonSRX. 199 * 200 * @param newVel the new velocity in native units per 100ms 201 * 202 * @return error code 203 */ 204 public ErrorCode setPulseWidthVelocity(int newVel) { 205 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "PulseWidthVel", newVel); 206 return ErrorCode.valueOf(retval); 207 } 208 209 /** 210 * Sets the simulated raw quadrature position of the TalonSRX. 211 * <p> 212 * The TalonSRX integrates this to calculate the true reported quadrature 213 * position. 214 * <p> 215 * When using the WPI Sim GUI, you will notice a readonly 'position' and 216 * settable 'rawPositionInput'. The readonly signal is the emulated position 217 * which will match self-test in Tuner and the hardware API. Changes to 218 * 'rawPositionInput' will be integrated into the emulated position. This way 219 * a simulator can modify the position without overriding your 220 * hardware API calls for home-ing your sensor. 221 * <p> 222 * Inputs to this function over time should be continuous, 223 * as user calls of setSelectedSensorPosition() and setQuadraturePosition() 224 * will be accounted for in the calculation. 225 * 226 * @param newPos the new raw position in native units 227 * 228 * @return error code 229 */ 230 public ErrorCode setQuadratureRawPosition(int newPos) { 231 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "QuadEncRawPos", newPos); 232 return ErrorCode.valueOf(retval); 233 } 234 235 /** 236 * Adds to the simulated quadrature position of the TalonSRX. 237 * 238 * @param dPos the change in position in native units 239 * 240 * @return error code 241 */ 242 public ErrorCode addQuadraturePosition(int dPos) { 243 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "QuadEncAddPos", dPos); 244 return ErrorCode.valueOf(retval); 245 } 246 247 /** 248 * Sets the simulated quadrature velocity of the TalonSRX. 249 * 250 * @param newVel the new velocity in native units per 100ms 251 * 252 * @return error code 253 */ 254 public ErrorCode setQuadratureVelocity(int newVel) { 255 int retval = PlatformJNI.JNI_SimSetPhysicsInput(DeviceType.TalonSRX.value, _id, "QuadEncVel", newVel); 256 return ErrorCode.valueOf(retval); 257 } 258}