2022-04-30 22:54:22 -07:00
|
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
|
|
#include "frc/controller/LTVUnicycleController.h"
|
|
|
|
|
|
|
2023-08-17 13:56:15 -07:00
|
|
|
|
#include "frc/DARE.h"
|
|
|
|
|
|
#include "frc/system/Discretization.h"
|
2022-04-30 22:54:22 -07:00
|
|
|
|
#include "units/math.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
2024-12-07 23:00:15 -08:00
|
|
|
|
ChassisSpeeds LTVUnicycleController::Calculate(
|
|
|
|
|
|
const Pose2d& currentPose, const Pose2d& poseRef,
|
|
|
|
|
|
units::meters_per_second_t linearVelocityRef,
|
|
|
|
|
|
units::radians_per_second_t angularVelocityRef) {
|
2022-08-28 23:04:52 -07:00
|
|
|
|
// The change in global pose for a unicycle is defined by the following three
|
|
|
|
|
|
// equations.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ẋ = v cosθ
|
|
|
|
|
|
// ẏ = v sinθ
|
|
|
|
|
|
// θ̇ = ω
|
|
|
|
|
|
//
|
|
|
|
|
|
// Here's the model as a vector function where x = [x y θ]ᵀ and u = [v ω]ᵀ.
|
|
|
|
|
|
//
|
|
|
|
|
|
// [v cosθ]
|
|
|
|
|
|
// f(x, u) = [v sinθ]
|
|
|
|
|
|
// [ ω ]
|
|
|
|
|
|
//
|
|
|
|
|
|
// To create an LQR, we need to linearize this.
|
|
|
|
|
|
//
|
|
|
|
|
|
// [0 0 −v sinθ] [cosθ 0]
|
|
|
|
|
|
// ∂f(x, u)/∂x = [0 0 v cosθ] ∂f(x, u)/∂u = [sinθ 0]
|
|
|
|
|
|
// [0 0 0 ] [ 0 1]
|
|
|
|
|
|
//
|
|
|
|
|
|
// We're going to make a cross-track error controller, so we'll apply a
|
|
|
|
|
|
// clockwise rotation matrix to the global tracking error to transform it into
|
|
|
|
|
|
// the robot's coordinate frame. Since the cross-track error is always
|
|
|
|
|
|
// measured from the robot's coordinate frame, the model used to compute the
|
|
|
|
|
|
// LQR should be linearized around θ = 0 at all times.
|
|
|
|
|
|
//
|
|
|
|
|
|
// [0 0 −v sin0] [cos0 0]
|
|
|
|
|
|
// A = [0 0 v cos0] B = [sin0 0]
|
|
|
|
|
|
// [0 0 0 ] [ 0 1]
|
|
|
|
|
|
//
|
|
|
|
|
|
// [0 0 0] [1 0]
|
|
|
|
|
|
// A = [0 0 v] B = [0 0]
|
|
|
|
|
|
// [0 0 0] [0 1]
|
2023-08-17 13:56:15 -07:00
|
|
|
|
|
2024-12-07 23:00:15 -08:00
|
|
|
|
if (!m_enabled) {
|
|
|
|
|
|
return ChassisSpeeds{linearVelocityRef, 0_mps, angularVelocityRef};
|
|
|
|
|
|
}
|
2023-08-17 13:56:15 -07:00
|
|
|
|
|
2024-12-07 23:00:15 -08:00
|
|
|
|
// The DARE is ill-conditioned if the velocity is close to zero, so don't
|
|
|
|
|
|
// let the system stop.
|
|
|
|
|
|
if (units::math::abs(linearVelocityRef) < 1e-4_mps) {
|
|
|
|
|
|
linearVelocityRef = 1e-4_mps;
|
2022-05-04 22:04:08 -07:00
|
|
|
|
}
|
2022-04-30 22:54:22 -07:00
|
|
|
|
|
2024-12-07 23:00:15 -08:00
|
|
|
|
m_poseError = poseRef.RelativeTo(currentPose);
|
2022-04-30 22:54:22 -07:00
|
|
|
|
|
2024-12-07 23:00:15 -08:00
|
|
|
|
Eigen::Matrix<double, 3, 3> A{
|
|
|
|
|
|
{0.0, 0.0, 0.0}, {0.0, 0.0, linearVelocityRef.value()}, {0.0, 0.0, 0.0}};
|
|
|
|
|
|
constexpr Eigen::Matrix<double, 3, 2> B{{1.0, 0.0}, {0.0, 0.0}, {0.0, 1.0}};
|
2022-04-30 22:54:22 -07:00
|
|
|
|
|
2024-12-07 23:00:15 -08:00
|
|
|
|
Eigen::Matrix<double, 3, 3> discA;
|
|
|
|
|
|
Eigen::Matrix<double, 3, 2> discB;
|
|
|
|
|
|
DiscretizeAB(A, B, m_dt, &discA, &discB);
|
2022-04-30 22:54:22 -07:00
|
|
|
|
|
2024-12-07 23:00:15 -08:00
|
|
|
|
auto S = DARE<3, 2>(discA, discB, m_Q, m_R, false).value();
|
|
|
|
|
|
|
|
|
|
|
|
// K = (BᵀSB + R)⁻¹BᵀSA
|
|
|
|
|
|
Eigen::Matrix<double, 2, 3> K = (discB.transpose() * S * discB + m_R)
|
|
|
|
|
|
.llt()
|
|
|
|
|
|
.solve(discB.transpose() * S * discA);
|
2022-04-30 22:54:22 -07:00
|
|
|
|
|
2024-12-07 23:00:15 -08:00
|
|
|
|
Eigen::Vector3d e{m_poseError.X().value(), m_poseError.Y().value(),
|
|
|
|
|
|
m_poseError.Rotation().Radians().value()};
|
|
|
|
|
|
Eigen::Vector2d u = K * e;
|
2022-04-30 22:54:22 -07:00
|
|
|
|
|
|
|
|
|
|
return ChassisSpeeds{linearVelocityRef + units::meters_per_second_t{u(0)},
|
|
|
|
|
|
0_mps,
|
|
|
|
|
|
angularVelocityRef + units::radians_per_second_t{u(1)}};
|
|
|
|
|
|
}
|