Describe the bug
On DCMotorSim, ElevatorSim, etc., when the input voltage is set to 0, GetCurrentDraw() returns 0, even when there is back emf in the motor.
To Reproduce
Steps to reproduce the behavior:
- Construct an
ElevatorSim as such:
var elevatorSim = new ElevatorSim(
DCMotor.getKrakenX60Foc(2),
2.0, // gearing
2.0, // mass
0.0254, // radius
0, // min height
2, // max height
galse, // gravity
1 // starting height
);
- Call
setInputVoltage(12) followed by update(0.04)
- Print
getVelocity() and confirm it is non-zero
- Call
setInputVoltage(0)
- Print
getCurrentDraw() and note that it is 0 [BAD]
Expected behavior
Because there is back emf, the current in the motor should be non-zero.
Desktop:
- OS: Windows 11
- Project Information: WPILib 26.2.1
Additional context
Currently, GetCurrentDraw() returns the stator current of the motor. Unless the motor is coasting (which is not supported by the sim classes), any back emf is guaranteed to generate a non-zero stator current through the motor when the applied voltage is 0. However, because the code currently multiplies by wpi::sgn(m_u(0)), where m_u(0) is the applied voltage, the current draw is instead reported as 0.
- For the motoring/braking signage (which the sim classes currently use and matches the sign of supply current), an applied voltage of 0 normally results in non-positive stator current.
- For the forward/reverse signage (which matches the output of
DCMotor::Current), the multiplication by wpi::sgn(m_u(0)) is removed entirely, and sign is determined by the direction of torque output.
Note that for the purpose of converting GetCurrentDraw() into supply current for battery simulation, this issue does not have an impact on anything, as supply current = motoring/braking stator current * abs(duty cycle), and duty cycle is 0 here.
Describe the bug
On
DCMotorSim,ElevatorSim, etc., when the input voltage is set to 0,GetCurrentDraw()returns 0, even when there is back emf in the motor.To Reproduce
Steps to reproduce the behavior:
ElevatorSimas such:setInputVoltage(12)followed byupdate(0.04)getVelocity()and confirm it is non-zerosetInputVoltage(0)getCurrentDraw()and note that it is 0 [BAD]Expected behavior
Because there is back emf, the current in the motor should be non-zero.
Desktop:
Additional context
Currently,
GetCurrentDraw()returns the stator current of the motor. Unless the motor is coasting (which is not supported by the sim classes), any back emf is guaranteed to generate a non-zero stator current through the motor when the applied voltage is 0. However, because the code currently multiplies bywpi::sgn(m_u(0)), wherem_u(0)is the applied voltage, the current draw is instead reported as 0.DCMotor::Current), the multiplication bywpi::sgn(m_u(0))is removed entirely, and sign is determined by the direction of torque output.Note that for the purpose of converting
GetCurrentDraw()into supply current for battery simulation, this issue does not have an impact on anything, as supply current = motoring/braking stator current * abs(duty cycle), and duty cycle is 0 here.