diff --git a/wpimath/src/main/native/include/frc/StateSpaceUtil.h b/wpimath/src/main/native/include/frc/StateSpaceUtil.h index 0345b46548..a2462823e5 100644 --- a/wpimath/src/main/native/include/frc/StateSpaceUtil.h +++ b/wpimath/src/main/native/include/frc/StateSpaceUtil.h @@ -138,7 +138,8 @@ Matrixd MakeCovMatrix(Ts... stdDevs) { * Creates a cost matrix from the given vector for use with LQR. * * The cost matrix is constructed using Bryson's rule. The inverse square of - * each element in the input is taken and placed on the cost matrix diagonal. + * each element in the input is placed on the cost matrix diagonal. If a + * tolerance is infinity, its cost matrix entry is set to zero. * * @param costs An array. For a Q matrix, its elements are the maximum allowed * excursions of the states from the reference. For an R matrix, @@ -151,7 +152,11 @@ Matrixd MakeCostMatrix(const std::array& costs) { Eigen::DiagonalMatrix result; auto& diag = result.diagonal(); for (size_t i = 0; i < N; ++i) { - diag(i) = 1.0 / std::pow(costs[i], 2); + if (costs[i] == std::numeric_limits::infinity()) { + diag(i) = 0.0; + } else { + diag(i) = 1.0 / std::pow(costs[i], 2); + } } return result; }