WPILibC++ 2024.3.2
LTVUnicycleController.h
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 <wpi/SymbolExports.h>
8#include <wpi/array.h>
10
11#include "frc/EigenCore.h"
12#include "frc/geometry/Pose2d.h"
16#include "units/time.h"
17#include "units/velocity.h"
18
19namespace frc {
20
21/**
22 * The linear time-varying unicycle controller has a similar form to the LQR,
23 * but the model used to compute the controller gain is the nonlinear unicycle
24 * model linearized around the drivetrain's current state.
25 *
26 * This controller is a roughly drop-in replacement for RamseteController with
27 * more optimal feedback gains in the "least-squares error" sense.
28 *
29 * See section 8.9 in Controls Engineering in FRC for a derivation of the
30 * control law we used shown in theorem 8.9.1.
31 */
33 public:
34 /**
35 * Constructs a linear time-varying unicycle controller with default maximum
36 * desired error tolerances of (0.0625 m, 0.125 m, 2 rad) and default maximum
37 * desired control effort of (1 m/s, 2 rad/s).
38 *
39 * @param dt Discretization timestep.
40 * @param maxVelocity The maximum velocity for the controller gain lookup
41 * table.
42 * @throws std::domain_error if maxVelocity &lt;= 0.
43 */
45 units::second_t dt, units::meters_per_second_t maxVelocity = 9_mps);
46
47 /**
48 * Constructs a linear time-varying unicycle controller.
49 *
50 * See
51 * https://docs.wpilib.org/en/stable/docs/software/advanced-controls/state-space/state-space-intro.html#lqr-tuning
52 * for how to select the tolerances.
53 *
54 * @param Qelems The maximum desired error tolerance for each state.
55 * @param Relems The maximum desired control effort for each input.
56 * @param dt Discretization timestep.
57 * @param maxVelocity The maximum velocity for the controller gain lookup
58 * table.
59 * @throws std::domain_error if maxVelocity <= 0 m/s or >= 15 m/s.
60 */
62 const wpi::array<double, 2>& Relems, units::second_t dt,
63 units::meters_per_second_t maxVelocity = 9_mps);
64
65 /**
66 * Move constructor.
67 */
69
70 /**
71 * Move assignment operator.
72 */
74
75 /**
76 * Returns true if the pose error is within tolerance of the reference.
77 */
78 bool AtReference() const;
79
80 /**
81 * Sets the pose error which is considered tolerable for use with
82 * AtReference().
83 *
84 * @param poseTolerance Pose error which is tolerable.
85 */
86 void SetTolerance(const Pose2d& poseTolerance);
87
88 /**
89 * Returns the linear and angular velocity outputs of the LTV controller.
90 *
91 * The reference pose, linear velocity, and angular velocity should come from
92 * a drivetrain trajectory.
93 *
94 * @param currentPose The current pose.
95 * @param poseRef The desired pose.
96 * @param linearVelocityRef The desired linear velocity.
97 * @param angularVelocityRef The desired angular velocity.
98 */
99 ChassisSpeeds Calculate(const Pose2d& currentPose, const Pose2d& poseRef,
100 units::meters_per_second_t linearVelocityRef,
101 units::radians_per_second_t angularVelocityRef);
102
103 /**
104 * Returns the linear and angular velocity outputs of the LTV controller.
105 *
106 * The reference pose, linear velocity, and angular velocity should come from
107 * a drivetrain trajectory.
108 *
109 * @param currentPose The current pose.
110 * @param desiredState The desired pose, linear velocity, and angular velocity
111 * from a trajectory.
112 */
113 ChassisSpeeds Calculate(const Pose2d& currentPose,
114 const Trajectory::State& desiredState);
115
116 /**
117 * Enables and disables the controller for troubleshooting purposes.
118 *
119 * @param enabled If the controller is enabled or not.
120 */
121 void SetEnabled(bool enabled);
122
123 private:
124 // LUT from drivetrain linear velocity to LQR gain
126
127 Pose2d m_poseError;
128 Pose2d m_poseTolerance;
129 bool m_enabled = true;
130};
131
132} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
The linear time-varying unicycle controller has a similar form to the LQR, but the model used to comp...
Definition: LTVUnicycleController.h:32
LTVUnicycleController(const wpi::array< double, 3 > &Qelems, const wpi::array< double, 2 > &Relems, units::second_t dt, units::meters_per_second_t maxVelocity=9_mps)
Constructs a linear time-varying unicycle controller.
LTVUnicycleController(units::second_t dt, units::meters_per_second_t maxVelocity=9_mps)
Constructs a linear time-varying unicycle controller with default maximum desired error tolerances of...
void SetTolerance(const Pose2d &poseTolerance)
Sets the pose error which is considered tolerable for use with AtReference().
LTVUnicycleController & operator=(LTVUnicycleController &&)=default
Move assignment operator.
ChassisSpeeds Calculate(const Pose2d &currentPose, const Pose2d &poseRef, units::meters_per_second_t linearVelocityRef, units::radians_per_second_t angularVelocityRef)
Returns the linear and angular velocity outputs of the LTV controller.
bool AtReference() const
Returns true if the pose error is within tolerance of the reference.
void SetEnabled(bool enabled)
Enables and disables the controller for troubleshooting purposes.
LTVUnicycleController(LTVUnicycleController &&)=default
Move constructor.
ChassisSpeeds Calculate(const Pose2d &currentPose, const Trajectory::State &desiredState)
Returns the linear and angular velocity outputs of the LTV controller.
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:23
Implements a table of key-value pairs with linear interpolation between values.
Definition: interpolating_map.h:23
Definition: AprilTagPoseEstimator.h:15
Represents the speed of a robot chassis.
Definition: ChassisSpeeds.h:25
Represents one point on the trajectory.
Definition: Trajectory.h:30