Skip to content

Commit 95cb38e

Browse files
authored
[wpimath] Fix ElevatorSim::GetCurrentDraw() in C++ (#8370)
The Kv calculation in C++ was missing a negative sign compared to the Java implementation.
1 parent b8d6bc2 commit 95cb38e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

wpilibc/src/main/native/cpp/simulation/ElevatorSim.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ units::ampere_t ElevatorSim::GetCurrentDraw() const {
8888
double kA = 1.0 / m_plant.B(1, 0);
8989
using Kv_t = units::unit_t<units::compound_unit<
9090
units::volt, units::inverse<units::meters_per_second>>>;
91-
Kv_t Kv = Kv_t{kA * m_plant.A(1, 1)};
91+
Kv_t Kv = Kv_t{-kA * m_plant.A(1, 1)};
9292
units::meters_per_second_t velocity{m_x(1)};
9393
units::radians_per_second_t motorVelocity = velocity * Kv * m_gearbox.Kv;
9494

wpilibc/src/test/native/cpp/simulation/ElevatorSimTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,15 @@ TEST(ElevatorSimTest, Stability) {
9292
frc::Vectord<1>{12.0}, 20_ms * 50)(0)},
9393
sim.GetPosition(), 1_cm);
9494
}
95+
96+
TEST(ElevatorSimTest, CurrentDraw) {
97+
auto const motor = frc::DCMotor::KrakenX60(2);
98+
frc::sim::ElevatorSim sim(motor, 20, 8_kg, 0.1_m, 0_m, 1_m, true, 0_m,
99+
{0.01, 0.0});
100+
101+
EXPECT_DOUBLE_EQ(0.0, sim.GetCurrentDraw().value());
102+
sim.SetInputVoltage(motor.Voltage(motor.Torque(60_A), 0_rad_per_s));
103+
sim.Update(100_ms);
104+
// current draw should start at 60 A and decrease as the back emf catches up
105+
EXPECT_TRUE(0_A < sim.GetCurrentDraw() && sim.GetCurrentDraw() < 60_A);
106+
}

wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/ElevatorSimTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,16 @@ void testStability() {
124124
sim.getPositionMeters(),
125125
0.01);
126126
}
127+
128+
@Test
129+
void testCurrentDraw() {
130+
var motor = DCMotor.getKrakenX60(2);
131+
var sim = new ElevatorSim(motor, 20, 8.0, 0.1, 0.0, 1.0, true, 0.0, 0.01, 0.0);
132+
133+
assertEquals(0.0, sim.getCurrentDrawAmps());
134+
sim.setInputVoltage(motor.getVoltage(motor.getTorque(60.0), 0.0));
135+
sim.update(0.100);
136+
// current draw should start at 60 A and decrease as the back emf catches up
137+
assertTrue(0.0 < sim.getCurrentDrawAmps() && sim.getCurrentDrawAmps() < 60.0);
138+
}
127139
}

0 commit comments

Comments
 (0)