mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Use stricter C++ type conversions (#4357)
Now, implicit narrowing conversions are only used with wpi::Now(). This
also fixes clang-tidy warnings about C-style casts. For example:
```
== clang-tidy /__w/allwpilib/allwpilib/wpilibNewCommands/src/main/native/include/frc2/command/SwerveControllerCommand.inc ==
/__w/allwpilib/allwpilib/wpilibNewCommands/src/main/native/include/frc2/command/SwerveControllerCommand.inc:95:18: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast [google-readability-casting]
auto curTime = units::second_t(m_timer.Get());
^
```
In that case at least, the cast was removed entirely since Get() already
returns a units::second_t.
This commit is contained in:
@@ -163,7 +163,7 @@ units::volt_t RobotController::GetBrownoutVoltage() {
|
||||
int32_t status = 0;
|
||||
double retVal = HAL_GetBrownoutVoltage(&status);
|
||||
FRC_CheckErrorStatus(status, "{}", "GetBrownoutVoltage");
|
||||
return units::volt_t(retVal);
|
||||
return units::volt_t{retVal};
|
||||
}
|
||||
|
||||
void RobotController::SetBrownoutVoltage(units::volt_t brownoutVoltage) {
|
||||
|
||||
@@ -21,9 +21,9 @@ units::second_t GetTime() {
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::system_clock;
|
||||
|
||||
return units::second_t(
|
||||
return units::second_t{
|
||||
duration_cast<duration<double>>(system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
.count()};
|
||||
}
|
||||
|
||||
} // namespace frc
|
||||
@@ -78,9 +78,9 @@ bool Timer::AdvanceIfElapsed(units::second_t period) {
|
||||
|
||||
units::second_t Timer::GetFPGATimestamp() {
|
||||
// FPGA returns the timestamp in microseconds
|
||||
return units::second_t(frc::RobotController::GetFPGATime() * 1.0e-6);
|
||||
return units::second_t{frc::RobotController::GetFPGATime() * 1.0e-6};
|
||||
}
|
||||
|
||||
units::second_t Timer::GetMatchTime() {
|
||||
return units::second_t(frc::DriverStation::GetMatchTime());
|
||||
return units::second_t{frc::DriverStation::GetMatchTime()};
|
||||
}
|
||||
|
||||
@@ -27,6 +27,5 @@ units::turn_t AnalogEncoderSim::GetTurns() {
|
||||
}
|
||||
|
||||
frc::Rotation2d AnalogEncoderSim::GetPosition() {
|
||||
units::radian_t rads = GetTurns();
|
||||
return frc::Rotation2d{rads};
|
||||
return units::radian_t{GetTurns()};
|
||||
}
|
||||
|
||||
@@ -82,20 +82,20 @@ double DifferentialDrivetrainSim::GetState(int state) const {
|
||||
}
|
||||
|
||||
Rotation2d DifferentialDrivetrainSim::GetHeading() const {
|
||||
return Rotation2d(units::radian_t(GetOutput(State::kHeading)));
|
||||
return units::radian_t{GetOutput(State::kHeading)};
|
||||
}
|
||||
|
||||
Pose2d DifferentialDrivetrainSim::GetPose() const {
|
||||
return Pose2d(units::meter_t(GetOutput(State::kX)),
|
||||
units::meter_t(GetOutput(State::kY)), GetHeading());
|
||||
return Pose2d{units::meter_t{GetOutput(State::kX)},
|
||||
units::meter_t{GetOutput(State::kY)}, GetHeading()};
|
||||
}
|
||||
|
||||
units::ampere_t DifferentialDrivetrainSim::GetLeftCurrentDraw() const {
|
||||
auto loadIleft =
|
||||
m_motor.Current(
|
||||
units::radians_per_second_t(m_x(State::kLeftVelocity) *
|
||||
m_currentGearing / m_wheelRadius.value()),
|
||||
units::volt_t(m_u(0))) *
|
||||
units::radians_per_second_t{m_x(State::kLeftVelocity) *
|
||||
m_currentGearing / m_wheelRadius.value()},
|
||||
units::volt_t{m_u(0)}) *
|
||||
wpi::sgn(m_u(0));
|
||||
|
||||
return loadIleft;
|
||||
@@ -104,9 +104,9 @@ units::ampere_t DifferentialDrivetrainSim::GetLeftCurrentDraw() const {
|
||||
units::ampere_t DifferentialDrivetrainSim::GetRightCurrentDraw() const {
|
||||
auto loadIRight =
|
||||
m_motor.Current(
|
||||
units::radians_per_second_t(m_x(State::kRightVelocity) *
|
||||
m_currentGearing / m_wheelRadius.value()),
|
||||
units::volt_t(m_u(1))) *
|
||||
units::radians_per_second_t{m_x(State::kRightVelocity) *
|
||||
m_currentGearing / m_wheelRadius.value()},
|
||||
units::volt_t{m_u(1)}) *
|
||||
wpi::sgn(m_u(1));
|
||||
|
||||
return loadIRight;
|
||||
|
||||
@@ -49,11 +49,11 @@ bool ElevatorSim::WouldHitUpperLimit(units::meter_t elevatorHeight) const {
|
||||
}
|
||||
|
||||
bool ElevatorSim::HasHitLowerLimit() const {
|
||||
return WouldHitLowerLimit(units::meter_t(m_y(0)));
|
||||
return WouldHitLowerLimit(units::meter_t{m_y(0)});
|
||||
}
|
||||
|
||||
bool ElevatorSim::HasHitUpperLimit() const {
|
||||
return WouldHitUpperLimit(units::meter_t(m_y(0)));
|
||||
return WouldHitUpperLimit(units::meter_t{m_y(0)});
|
||||
}
|
||||
|
||||
units::meter_t ElevatorSim::GetPosition() const {
|
||||
@@ -96,10 +96,10 @@ Vectord<2> ElevatorSim::UpdateX(const Vectord<2>& currentXhat,
|
||||
},
|
||||
currentXhat, u, dt);
|
||||
// Check for collision after updating x-hat.
|
||||
if (WouldHitLowerLimit(units::meter_t(updatedXhat(0)))) {
|
||||
if (WouldHitLowerLimit(units::meter_t{updatedXhat(0)})) {
|
||||
return Vectord<2>{m_minHeight.value(), 0.0};
|
||||
}
|
||||
if (WouldHitUpperLimit(units::meter_t(updatedXhat(0)))) {
|
||||
if (WouldHitUpperLimit(units::meter_t{updatedXhat(0)})) {
|
||||
return Vectord<2>{m_maxHeight.value(), 0.0};
|
||||
}
|
||||
return updatedXhat;
|
||||
|
||||
@@ -39,7 +39,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterVInVoltageCallback(
|
||||
}
|
||||
|
||||
units::volt_t RoboRioSim::GetVInVoltage() {
|
||||
return units::volt_t(HALSIM_GetRoboRioVInVoltage());
|
||||
return units::volt_t{HALSIM_GetRoboRioVInVoltage()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetVInVoltage(units::volt_t vInVoltage) {
|
||||
@@ -56,7 +56,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterVInCurrentCallback(
|
||||
}
|
||||
|
||||
units::ampere_t RoboRioSim::GetVInCurrent() {
|
||||
return units::ampere_t(HALSIM_GetRoboRioVInCurrent());
|
||||
return units::ampere_t{HALSIM_GetRoboRioVInCurrent()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetVInCurrent(units::ampere_t vInCurrent) {
|
||||
@@ -73,7 +73,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserVoltage6VCallback(
|
||||
}
|
||||
|
||||
units::volt_t RoboRioSim::GetUserVoltage6V() {
|
||||
return units::volt_t(HALSIM_GetRoboRioUserVoltage6V());
|
||||
return units::volt_t{HALSIM_GetRoboRioUserVoltage6V()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetUserVoltage6V(units::volt_t userVoltage6V) {
|
||||
@@ -90,7 +90,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserCurrent6VCallback(
|
||||
}
|
||||
|
||||
units::ampere_t RoboRioSim::GetUserCurrent6V() {
|
||||
return units::ampere_t(HALSIM_GetRoboRioUserCurrent6V());
|
||||
return units::ampere_t{HALSIM_GetRoboRioUserCurrent6V()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetUserCurrent6V(units::ampere_t userCurrent6V) {
|
||||
@@ -124,7 +124,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserVoltage5VCallback(
|
||||
}
|
||||
|
||||
units::volt_t RoboRioSim::GetUserVoltage5V() {
|
||||
return units::volt_t(HALSIM_GetRoboRioUserVoltage5V());
|
||||
return units::volt_t{HALSIM_GetRoboRioUserVoltage5V()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetUserVoltage5V(units::volt_t userVoltage5V) {
|
||||
@@ -141,7 +141,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserCurrent5VCallback(
|
||||
}
|
||||
|
||||
units::ampere_t RoboRioSim::GetUserCurrent5V() {
|
||||
return units::ampere_t(HALSIM_GetRoboRioUserCurrent5V());
|
||||
return units::ampere_t{HALSIM_GetRoboRioUserCurrent5V()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetUserCurrent5V(units::ampere_t userCurrent5V) {
|
||||
@@ -175,7 +175,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserVoltage3V3Callback(
|
||||
}
|
||||
|
||||
units::volt_t RoboRioSim::GetUserVoltage3V3() {
|
||||
return units::volt_t(HALSIM_GetRoboRioUserVoltage3V3());
|
||||
return units::volt_t{HALSIM_GetRoboRioUserVoltage3V3()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetUserVoltage3V3(units::volt_t userVoltage3V3) {
|
||||
@@ -192,7 +192,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserCurrent3V3Callback(
|
||||
}
|
||||
|
||||
units::ampere_t RoboRioSim::GetUserCurrent3V3() {
|
||||
return units::ampere_t(HALSIM_GetRoboRioUserCurrent3V3());
|
||||
return units::ampere_t{HALSIM_GetRoboRioUserCurrent3V3()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetUserCurrent3V3(units::ampere_t userCurrent3V3) {
|
||||
@@ -277,7 +277,7 @@ std::unique_ptr<CallbackStore> RoboRioSim::RegisterBrownoutVoltageCallback(
|
||||
}
|
||||
|
||||
units::volt_t RoboRioSim::GetBrownoutVoltage() {
|
||||
return units::volt_t(HALSIM_GetRoboRioBrownoutVoltage());
|
||||
return units::volt_t{HALSIM_GetRoboRioBrownoutVoltage()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetBrownoutVoltage(units::volt_t vInVoltage) {
|
||||
|
||||
@@ -48,11 +48,11 @@ bool SingleJointedArmSim::WouldHitUpperLimit(units::radian_t armAngle) const {
|
||||
}
|
||||
|
||||
bool SingleJointedArmSim::HasHitLowerLimit() const {
|
||||
return WouldHitLowerLimit(units::radian_t(m_y(0)));
|
||||
return WouldHitLowerLimit(units::radian_t{m_y(0)});
|
||||
}
|
||||
|
||||
bool SingleJointedArmSim::HasHitUpperLimit() const {
|
||||
return WouldHitUpperLimit(units::radian_t(m_y(0)));
|
||||
return WouldHitUpperLimit(units::radian_t{m_y(0)});
|
||||
}
|
||||
|
||||
units::radian_t SingleJointedArmSim::GetAngle() const {
|
||||
@@ -102,9 +102,9 @@ Vectord<2> SingleJointedArmSim::UpdateX(const Vectord<2>& currentXhat,
|
||||
currentXhat, u, dt);
|
||||
|
||||
// Check for collisions.
|
||||
if (WouldHitLowerLimit(units::radian_t(updatedXhat(0)))) {
|
||||
if (WouldHitLowerLimit(units::radian_t{updatedXhat(0)})) {
|
||||
return Vectord<2>{m_minAngle.value(), 0.0};
|
||||
} else if (WouldHitUpperLimit(units::radian_t(updatedXhat(0)))) {
|
||||
} else if (WouldHitUpperLimit(units::radian_t{updatedXhat(0)})) {
|
||||
return Vectord<2>{m_maxAngle.value(), 0.0};
|
||||
}
|
||||
return updatedXhat;
|
||||
|
||||
@@ -140,7 +140,7 @@ void FieldObject2d::UpdateFromEntry() const {
|
||||
for (size_t i = 0; i < size / 3; ++i) {
|
||||
m_poses[i] =
|
||||
Pose2d{units::meter_t{arr[i * 3 + 0]}, units::meter_t{arr[i * 3 + 1]},
|
||||
Rotation2d{units::degree_t{arr[i * 3 + 2]}}};
|
||||
units::degree_t{arr[i * 3 + 2]}};
|
||||
}
|
||||
} else if (val->IsRaw()) {
|
||||
// treat it simply as an array of doubles
|
||||
@@ -163,8 +163,8 @@ void FieldObject2d::UpdateFromEntry() const {
|
||||
double rot = wpi::BitsToDouble(
|
||||
wpi::support::endian::readNext<uint64_t, wpi::support::big,
|
||||
wpi::support::unaligned>(p));
|
||||
m_poses[i] = Pose2d{units::meter_t{x}, units::meter_t{y},
|
||||
Rotation2d{units::degree_t{rot}}};
|
||||
m_poses[i] =
|
||||
Pose2d{units::meter_t{x}, units::meter_t{y}, units::degree_t{rot}};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ MechanismLigament2d::MechanismLigament2d(std::string_view name, double length,
|
||||
units::degree_t angle,
|
||||
double lineWeight,
|
||||
const frc::Color8Bit& color)
|
||||
: MechanismObject2d(name),
|
||||
: MechanismObject2d{name},
|
||||
m_length{length},
|
||||
m_angle{angle.value()},
|
||||
m_weight{lineWeight} {
|
||||
|
||||
@@ -10,7 +10,7 @@ using namespace frc;
|
||||
|
||||
MechanismRoot2d::MechanismRoot2d(std::string_view name, double x, double y,
|
||||
const private_init&)
|
||||
: MechanismObject2d(name), m_x{x}, m_y{y} {}
|
||||
: MechanismObject2d{name}, m_x{x}, m_y{y} {}
|
||||
|
||||
void MechanismRoot2d::SetPosition(double x, double y) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
|
||||
Reference in New Issue
Block a user