Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Examples] Limit minimum battery voltage in sim to 0.1V #1600

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion photonlib-cpp-examples/aimandrange/src/main/cpp/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ void Robot::SimulationPeriodic() {
units::ampere_t totalCurrent = drivetrain.GetCurrentDraw();
units::volt_t loadedBattVolts =
frc::sim::BatterySim::Calculate({totalCurrent});
frc::sim::RoboRioSim::SetVInVoltage(loadedBattVolts);
// Using max(0.1, voltage) here isn't a *physically correct* solution,
// but it avoids problems with battery voltage measuring 0.
frc::sim::RoboRioSim::SetVInVoltage(units::math::max(0.1_V, loadedBattVolts));
}

#ifndef RUNNING_FRC_TESTS
Expand Down
4 changes: 3 additions & 1 deletion photonlib-cpp-examples/aimattarget/src/main/cpp/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ void Robot::SimulationPeriodic() {
units::ampere_t totalCurrent = drivetrain.GetCurrentDraw();
units::volt_t loadedBattVolts =
frc::sim::BatterySim::Calculate({totalCurrent});
frc::sim::RoboRioSim::SetVInVoltage(loadedBattVolts);
// Using max(0.1, voltage) here isn't a *physically correct* solution,
// but it avoids problems with battery voltage measuring 0.
frc::sim::RoboRioSim::SetVInVoltage(units::math::max(0.1_V, loadedBattVolts));
}

#ifndef RUNNING_FRC_TESTS
Expand Down
5 changes: 4 additions & 1 deletion photonlib-cpp-examples/poseest/src/main/cpp/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ void Robot::SimulationPeriodic() {
units::ampere_t totalCurrent = drivetrain.GetCurrentDraw();
units::volt_t loadedBattVolts =
frc::sim::BatterySim::Calculate({totalCurrent});
frc::sim::RoboRioSim::SetVInVoltage(loadedBattVolts);

// Using max(0.1, voltage) here isn't a *physically correct* solution,
// but it avoids problems with battery voltage measuring 0.
frc::sim::RoboRioSim::SetVInVoltage(units::math::max(0.1_V, loadedBattVolts));
}

#ifndef RUNNING_FRC_TESTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ public void simulationPeriodic() {
debugField.getObject("EstimatedRobotModules").setPoses(drivetrain.getModulePoses());

// Calculate battery voltage sag due to current draw
RoboRioSim.setVInVoltage(
BatterySim.calculateDefaultBatteryLoadedVoltage(drivetrain.getCurrentDraw()));
var batteryVoltage =
BatterySim.calculateDefaultBatteryLoadedVoltage(drivetrain.getCurrentDraw());

// Using max(0.1, voltage) here isn't a *physically correct* solution,
// but it avoids problems with battery voltage measuring 0.
RoboRioSim.setVInVoltage(Math.max(0.1, batteryVoltage));
}

public void resetPose() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ public void simulationPeriodic() {
debugField.getObject("EstimatedRobotModules").setPoses(drivetrain.getModulePoses());

// Calculate battery voltage sag due to current draw
RoboRioSim.setVInVoltage(
BatterySim.calculateDefaultBatteryLoadedVoltage(drivetrain.getCurrentDraw()));
var batteryVoltage =
BatterySim.calculateDefaultBatteryLoadedVoltage(drivetrain.getCurrentDraw());

// Using max(0.1, voltage) here isn't a *physically correct* solution,
// but it avoids problems with battery voltage measuring 0.
RoboRioSim.setVInVoltage(Math.max(0.1, batteryVoltage));
}

public void resetPose() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ public void simulationPeriodic() {
gpLauncher.simulationPeriodic();

// Calculate battery voltage sag due to current draw
RoboRioSim.setVInVoltage(
BatterySim.calculateDefaultBatteryLoadedVoltage(drivetrain.getCurrentDraw()));
var batteryVoltage =
BatterySim.calculateDefaultBatteryLoadedVoltage(drivetrain.getCurrentDraw());

// Using max(0.1, voltage) here isn't a *physically correct* solution,
// but it avoids problems with battery voltage measuring 0.
RoboRioSim.setVInVoltage(Math.max(0.1, batteryVoltage));
}

public void resetPose() {
Expand Down
Loading