WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
LTVUnicycleController.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <Eigen/Core>
8
13#include "wpi/units/angular_velocity.hpp"
14#include "wpi/units/math.hpp"
15#include "wpi/units/time.hpp"
16#include "wpi/units/velocity.hpp"
18#include "wpi/util/array.hpp"
19
20namespace wpi::math {
21
22/**
23 * The linear time-varying unicycle controller has a similar form to the LQR,
24 * but the model used to compute the controller gain is the nonlinear unicycle
25 * model linearized around the drivetrain's current state.
26 *
27 * See section 8.9 in Controls Engineering in FRC for a derivation of the
28 * control law we used shown in theorem 8.9.1.
29 */
31 public:
32 /**
33 * Constructs a linear time-varying unicycle controller with default maximum
34 * desired error tolerances of (x = 0.0625 m, y = 0.125 m, heading = 2 rad)
35 * and default maximum desired control effort of (linear velocity = 1 m/s,
36 * angular velocity = 2 rad/s).
37 *
38 * @param dt Discretization timestep.
39 */
40 explicit LTVUnicycleController(wpi::units::second_t dt)
41 : LTVUnicycleController{{0.0625, 0.125, 2.0}, {1.0, 2.0}, dt} {}
42
43 /**
44 * Constructs a linear time-varying unicycle controller.
45 *
46 * See
47 * https://docs.wpilib.org/en/stable/docs/software/advanced-controls/state-space/state-space-intro.html#lqr-tuning
48 * for how to select the tolerances.
49 *
50 * @param Qelems The maximum desired error tolerance for each state (x, y,
51 * heading).
52 * @param Relems The maximum desired control effort for each input (linear
53 * velocity, angular velocity).
54 * @param dt Discretization timestep.
55 */
57 const wpi::util::array<double, 2>& Relems,
58 wpi::units::second_t dt)
59 : m_Q{wpi::math::CostMatrix(Qelems)},
60 m_R{wpi::math::CostMatrix(Relems)},
61 m_dt{dt} {}
62
63 /**
64 * Move constructor.
65 */
67
68 /**
69 * Move assignment operator.
70 */
72
73 /**
74 * Returns true if the pose error is within tolerance of the reference.
75 */
76 bool AtReference() const {
77 const auto& eTranslate = m_poseError.Translation();
78 const auto& eRotate = m_poseError.Rotation();
79 const auto& tolTranslate = m_poseTolerance.Translation();
80 const auto& tolRotate = m_poseTolerance.Rotation();
81 return wpi::units::math::abs(eTranslate.X()) < tolTranslate.X() &&
82 wpi::units::math::abs(eTranslate.Y()) < tolTranslate.Y() &&
83 wpi::units::math::abs(eRotate.Radians()) < tolRotate.Radians();
84 }
85
86 /**
87 * Sets the pose error which is considered tolerable for use with
88 * AtReference().
89 *
90 * @param poseTolerance Pose error which is tolerable.
91 */
92 void SetTolerance(const Pose2d& poseTolerance) {
93 m_poseTolerance = poseTolerance;
94 }
95
96 /**
97 * Returns the linear and angular velocity outputs of the LTV controller.
98 *
99 * The reference pose, linear velocity, and angular velocity should come from
100 * a drivetrain trajectory.
101 *
102 * @param currentPose The current pose.
103 * @param poseRef The desired pose.
104 * @param linearVelocityRef The desired linear velocity.
105 * @param angularVelocityRef The desired angular velocity.
106 */
108 const Pose2d& currentPose, const Pose2d& poseRef,
109 wpi::units::meters_per_second_t linearVelocityRef,
110 wpi::units::radians_per_second_t angularVelocityRef);
111
112 /**
113 * Returns the linear and angular velocity outputs of the LTV controller.
114 *
115 * The reference pose, linear velocity, and angular velocity should come from
116 * a drivetrain trajectory.
117 *
118 * @param currentPose The current pose.
119 * @param desiredState The desired pose, linear velocity, and angular velocity
120 * from a trajectory.
121 */
123 const Trajectory::State& desiredState) {
124 return Calculate(currentPose, desiredState.pose, desiredState.velocity,
125 desiredState.velocity * desiredState.curvature);
126 }
127
128 /**
129 * Enables and disables the controller for troubleshooting purposes.
130 *
131 * @param enabled If the controller is enabled or not.
132 */
133 void SetEnabled(bool enabled) { m_enabled = enabled; }
134
135 private:
136 // LQR cost matrices
137 Eigen::Matrix<double, 3, 3> m_Q;
138 Eigen::Matrix<double, 2, 2> m_R;
139
140 wpi::units::second_t m_dt;
141
142 Pose2d m_poseError;
143 Pose2d m_poseTolerance;
144 bool m_enabled = true;
145};
146
147} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
LTVUnicycleController(const wpi::util::array< double, 3 > &Qelems, const wpi::util::array< double, 2 > &Relems, wpi::units::second_t dt)
Constructs a linear time-varying unicycle controller.
Definition LTVUnicycleController.hpp:56
void SetEnabled(bool enabled)
Enables and disables the controller for troubleshooting purposes.
Definition LTVUnicycleController.hpp:133
LTVUnicycleController(wpi::units::second_t dt)
Constructs a linear time-varying unicycle controller with default maximum desired error tolerances of...
Definition LTVUnicycleController.hpp:40
bool AtReference() const
Returns true if the pose error is within tolerance of the reference.
Definition LTVUnicycleController.hpp:76
LTVUnicycleController & operator=(LTVUnicycleController &&)=default
Move assignment operator.
LTVUnicycleController(LTVUnicycleController &&)=default
Move constructor.
ChassisVelocities Calculate(const Pose2d &currentPose, const Pose2d &poseRef, wpi::units::meters_per_second_t linearVelocityRef, wpi::units::radians_per_second_t angularVelocityRef)
Returns the linear and angular velocity outputs of the LTV controller.
ChassisVelocities Calculate(const Pose2d &currentPose, const Trajectory::State &desiredState)
Returns the linear and angular velocity outputs of the LTV controller.
Definition LTVUnicycleController.hpp:122
void SetTolerance(const Pose2d &poseTolerance)
Sets the pose error which is considered tolerable for use with AtReference().
Definition LTVUnicycleController.hpp:92
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.hpp:27
This class is a wrapper around std::array that does compile time size checking.
Definition array.hpp:26
Definition LinearSystem.hpp:20
constexpr Eigen::Matrix< double, sizeof...(Ts), sizeof...(Ts)> CostMatrix(Ts... tolerances)
Creates a cost matrix from the given vector for use with LQR.
Definition StateSpaceUtil.hpp:34
Definition CvSource.hpp:15
Represents robot chassis velocities.
Definition ChassisVelocities.hpp:26
Represents one point on the trajectory.
Definition Trajectory.hpp:33
Pose2d pose
The pose at that point of the trajectory.
Definition Trajectory.hpp:44
wpi::units::meters_per_second_t velocity
The velocity at that point of the trajectory.
Definition Trajectory.hpp:38
wpi::units::curvature_t curvature
The curvature at that point of the trajectory.
Definition Trajectory.hpp:47