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:
Tyler Veness
2022-08-17 13:42:36 -07:00
committed by GitHub
parent 151dabb2af
commit ac9be78e27
139 changed files with 547 additions and 593 deletions

View File

@@ -58,7 +58,7 @@ units::meter_t CalculateDistanceToTarget(units::meter_t cameraHeight,
* @param cameraToRobot The position of the robot relative to the camera. If the
* camera was mounted 3 inches behind the "origin" (usually
* physical center) of the robot, this would be
* frc::Transform2d(3_in, 0_in, 0_deg).
* frc::Transform2d{3_in, 0_in, 0_deg}.
* @return The position of the robot in the field.
*/
WPILIB_DLLEXPORT
@@ -78,7 +78,7 @@ frc::Pose2d EstimateFieldToRobot(
* @param cameraToRobot The position of the robot relative to the camera. If
* the camera was mounted 3 inches behind the "origin"
* (usually physical center) of the robot, this would be
* frc::Transform2d(3_in, 0_in, 0_deg).
* frc::Transform2d{3_in, 0_in, 0_deg}.
* @return The position of the robot in the field.
*/
WPILIB_DLLEXPORT

View File

@@ -143,7 +143,7 @@ class ProfiledPIDController
*
* @param goal The desired unprofiled setpoint.
*/
void SetGoal(Distance_t goal) { m_goal = {goal, Velocity_t(0)}; }
void SetGoal(Distance_t goal) { m_goal = {goal, Velocity_t{0}}; }
/**
* Gets the goal for the ProfiledPIDController.
@@ -225,8 +225,8 @@ class ProfiledPIDController
* @param velocityTolerance Velocity error which is tolerable.
*/
void SetTolerance(Distance_t positionTolerance,
Velocity_t velocityTolerance =
Velocity_t(std::numeric_limits<double>::infinity())) {
Velocity_t velocityTolerance = Velocity_t{
std::numeric_limits<double>::infinity()}) {
m_controller.SetTolerance(positionTolerance.value(),
velocityTolerance.value());
}
@@ -237,14 +237,14 @@ class ProfiledPIDController
* @return The error.
*/
Distance_t GetPositionError() const {
return Distance_t(m_controller.GetPositionError());
return Distance_t{m_controller.GetPositionError()};
}
/**
* Returns the change in error per second.
*/
Velocity_t GetVelocityError() const {
return Velocity_t(m_controller.GetVelocityError());
return Velocity_t{m_controller.GetVelocityError()};
}
/**
@@ -339,7 +339,7 @@ class ProfiledPIDController
* velocity is assumed to be zero.
*/
void Reset(Distance_t measuredPosition) {
Reset(measuredPosition, Velocity_t(0));
Reset(measuredPosition, Velocity_t{0});
}
void InitSendable(wpi::SendableBuilder& builder) override {

View File

@@ -140,8 +140,8 @@ class SwerveDrivePoseEstimator {
* @return The estimated robot pose in meters.
*/
Pose2d GetEstimatedPosition() const {
return Pose2d(m_observer.Xhat(0) * 1_m, m_observer.Xhat(1) * 1_m,
Rotation2d(units::radian_t{m_observer.Xhat(2)}));
return Pose2d{m_observer.Xhat(0) * 1_m, m_observer.Xhat(1) * 1_m,
Rotation2d{units::radian_t{m_observer.Xhat(2)}}};
}
/**
@@ -274,8 +274,8 @@ class SwerveDrivePoseEstimator {
auto chassisSpeeds = m_kinematics.ToChassisSpeeds(moduleStates...);
auto fieldRelativeSpeeds =
Translation2d(chassisSpeeds.vx * 1_s, chassisSpeeds.vy * 1_s)
.RotateBy(angle);
Translation2d{chassisSpeeds.vx * 1_s, chassisSpeeds.vy * 1_s}.RotateBy(
angle);
Vectord<3> u{fieldRelativeSpeeds.X().value(),
fieldRelativeSpeeds.Y().value(), omega.value()};

View File

@@ -31,7 +31,7 @@ class WPILIB_DLLEXPORT DifferentialDriveOdometry {
* @param initialPose The starting position of the robot on the field.
*/
explicit DifferentialDriveOdometry(const Rotation2d& gyroAngle,
const Pose2d& initialPose = Pose2d());
const Pose2d& initialPose = Pose2d{});
/**
* Resets the robot's position on the field.

View File

@@ -99,7 +99,7 @@ class WPILIB_DLLEXPORT MecanumDriveKinematics {
*/
MecanumDriveWheelSpeeds ToWheelSpeeds(
const ChassisSpeeds& chassisSpeeds,
const Translation2d& centerOfRotation = Translation2d()) const;
const Translation2d& centerOfRotation = Translation2d{}) const;
/**
* Performs forward kinematics to return the resulting chassis state from the

View File

@@ -34,7 +34,7 @@ class WPILIB_DLLEXPORT MecanumDriveOdometry {
*/
explicit MecanumDriveOdometry(MecanumDriveKinematics kinematics,
const Rotation2d& gyroAngle,
const Pose2d& initialPose = Pose2d());
const Pose2d& initialPose = Pose2d{});
/**
* Resets the robot's position on the field.

View File

@@ -129,7 +129,7 @@ class SwerveDriveKinematics {
*/
wpi::array<SwerveModuleState, NumModules> ToSwerveModuleStates(
const ChassisSpeeds& chassisSpeeds,
const Translation2d& centerOfRotation = Translation2d()) const;
const Translation2d& centerOfRotation = Translation2d{}) const;
/**
* Performs forward kinematics to return the resulting chassis state from the

View File

@@ -39,7 +39,7 @@ class SwerveDriveOdometry {
*/
SwerveDriveOdometry(SwerveDriveKinematics<NumModules> kinematics,
const Rotation2d& gyroAngle,
const Pose2d& initialPose = Pose2d());
const Pose2d& initialPose = Pose2d{});
/**
* Resets the robot's position on the field.

View File

@@ -90,8 +90,8 @@ class Spline {
(dx * ddy - ddx * dy) / ((dx * dx + dy * dy) * std::hypot(dx, dy));
return {
{FromVector(combined.template block<2, 1>(0, 0)), Rotation2d(dx, dy)},
units::curvature_t(curvature)};
{FromVector(combined.template block<2, 1>(0, 0)), Rotation2d{dx, dy}},
units::curvature_t{curvature}};
}
protected:
@@ -119,7 +119,7 @@ class Spline {
* @return The Translation2d.
*/
static Translation2d FromVector(const Eigen::Vector2d& vector) {
return Translation2d(units::meter_t(vector(0)), units::meter_t(vector(1)));
return Translation2d{units::meter_t{vector(0)}, units::meter_t{vector(1)}};
}
};
} // namespace frc

View File

@@ -81,7 +81,7 @@ class WPILIB_DLLEXPORT TrajectoryParameterizer {
* the trajectory, max velocity, min acceleration and max acceleration.
*/
struct ConstrainedState {
PoseWithCurvature pose = {Pose2d(), units::curvature_t(0.0)};
PoseWithCurvature pose = {Pose2d{}, units::curvature_t{0.0}};
units::meter_t distance = 0_m;
units::meters_per_second_t maxVelocity = 0_mps;
units::meters_per_second_squared_t minAcceleration = 0_mps_sq;

View File

@@ -82,7 +82,7 @@ class TrapezoidProfile {
* @param initial The initial state (usually the current state).
*/
TrapezoidProfile(Constraints constraints, State goal,
State initial = State{Distance_t(0), Velocity_t(0)});
State initial = State{Distance_t{0}, Velocity_t{0}});
TrapezoidProfile(const TrapezoidProfile&) = default;
TrapezoidProfile& operator=(const TrapezoidProfile&) = default;

View File

@@ -46,10 +46,10 @@ TrapezoidProfile<Distance>::TrapezoidProfile(Constraints constraints,
accelerationTime * accelerationTime * m_constraints.maxAcceleration;
// Handle the case where the profile never reaches full speed
if (fullSpeedDist < Distance_t(0)) {
if (fullSpeedDist < Distance_t{0}) {
accelerationTime =
units::math::sqrt(fullTrapezoidDist / m_constraints.maxAcceleration);
fullSpeedDist = Distance_t(0);
fullSpeedDist = Distance_t{0};
}
m_endAccel = accelerationTime - cutoffBegin;
@@ -110,7 +110,7 @@ units::second_t TrapezoidProfile<Distance>::TimeLeftUntil(
Distance_t distToTarget = units::math::abs(target - position);
if (distToTarget < Distance_t(1e-6)) {
if (distToTarget < Distance_t{1e-6}) {
return 0_s;
}

View File

@@ -44,8 +44,8 @@ class EllipticalRegionConstraint : public TrajectoryConstraint {
if (IsPoseInRegion(pose)) {
return m_constraint.MaxVelocity(pose, curvature, velocity);
} else {
return units::meters_per_second_t(
std::numeric_limits<double>::infinity());
return units::meters_per_second_t{
std::numeric_limits<double>::infinity()};
}
}

View File

@@ -41,8 +41,8 @@ class RectangularRegionConstraint : public TrajectoryConstraint {
if (IsPoseInRegion(pose)) {
return m_constraint.MaxVelocity(pose, curvature, velocity);
} else {
return units::meters_per_second_t(
std::numeric_limits<double>::infinity());
return units::meters_per_second_t{
std::numeric_limits<double>::infinity()};
}
}