Skip to content

GetCurrentDraw() on DCMotorSim and other motor sim classes returns 0 when applied voltage is 0 #8706

@bhall-ctre

Description

@bhall-ctre

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:

  1. 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
);
  1. Call setInputVoltage(12) followed by update(0.04)
  2. Print getVelocity() and confirm it is non-zero
  3. Call setInputVoltage(0)
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions