WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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 (x = 0.0625 m, y = 0.125 m, heading = 2 rad)
37 * and default maximum desired control effort of (linear velocity = 1 m/s,
38 * angular velocity = 2 rad/s).
39 *
40 * @param dt Discretization timestep.
41 * @param maxVelocity The maximum velocity for the controller gain lookup
42 * table.
43 * @throws std::domain_error if maxVelocity <= 0 m/s or >= 15 m/s.
44 */
46 units::second_t dt, units::meters_per_second_t maxVelocity = 9_mps);
47
48 /**
49 * Constructs a linear time-varying unicycle controller.
50 *
51 * See
52 * https://docs.wpilib.org/en/stable/docs/software/advanced-controls/state-space/state-space-intro.html#lqr-tuning
53 * for how to select the tolerances.
54 *
55 * @param Qelems The maximum desired error tolerance for each state (x, y,
56 * heading).
57 * @param Relems The maximum desired control effort for each input (linear
58 * velocity, angular velocity).
59 * @param dt Discretization timestep.
60 * @param maxVelocity The maximum velocity for the controller gain lookup
61 * table.
62 * @throws std::domain_error if maxVelocity <= 0 m/s or >= 15 m/s.
63 */
65 const wpi::array<double, 2>& Relems, units::second_t dt,
66 units::meters_per_second_t maxVelocity = 9_mps);
67
68 /**
69 * Move constructor.
70 */
72
73 /**
74 * Move assignment operator.
75 */
77
78 /**
79 * Returns true if the pose error is within tolerance of the reference.
80 */
81 bool AtReference() const;
82
83 /**
84 * Sets the pose error which is considered tolerable for use with
85 * AtReference().
86 *
87 * @param poseTolerance Pose error which is tolerable.
88 */
89 void SetTolerance(const Pose2d& poseTolerance);
90
91 /**
92 * Returns the linear and angular velocity outputs of the LTV controller.
93 *
94 * The reference pose, linear velocity, and angular velocity should come from
95 * a drivetrain trajectory.
96 *
97 * @param currentPose The current pose.
98 * @param poseRef The desired pose.
99 * @param linearVelocityRef The desired linear velocity.
100 * @param angularVelocityRef The desired angular velocity.
101 */
102 ChassisSpeeds Calculate(const Pose2d& currentPose, const Pose2d& poseRef,
103 units::meters_per_second_t linearVelocityRef,
104 units::radians_per_second_t angularVelocityRef);
105
106 /**
107 * Returns the linear and angular velocity outputs of the LTV controller.
108 *
109 * The reference pose, linear velocity, and angular velocity should come from
110 * a drivetrain trajectory.
111 *
112 * @param currentPose The current pose.
113 * @param desiredState The desired pose, linear velocity, and angular velocity
114 * from a trajectory.
115 */
116 ChassisSpeeds Calculate(const Pose2d& currentPose,
117 const Trajectory::State& desiredState);
118
119 /**
120 * Enables and disables the controller for troubleshooting purposes.
121 *
122 * @param enabled If the controller is enabled or not.
123 */
124 void SetEnabled(bool enabled);
125
126 private:
127 // LUT from drivetrain linear velocity to LQR gain
129
130 Pose2d m_poseError;
131 Pose2d m_poseTolerance;
132 bool m_enabled = true;
133};
134
135} // 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:28
This class is a wrapper around std::array that does compile time size checking.
Definition array.h:26
Implements a table of key-value pairs with linear interpolation between values.
Definition interpolating_map.h:23
Definition CAN.h:11
Represents the speed of a robot chassis.
Definition ChassisSpeeds.h:25
Represents one point on the trajectory.
Definition Trajectory.h:34