WPILibC++ 2023.4.3
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 model
24 * linearized around the drivetrain's current state.
25 *
26 * See section 8.9 in Controls Engineering in FRC for a derivation of the
27 * control law we used shown in theorem 8.9.1.
28 */
30 public:
31 /**
32 * Constructs a linear time-varying unicycle controller with default maximum
33 * desired error tolerances of (0.0625 m, 0.125 m, 2 rad) and default maximum
34 * desired control effort of (1 m/s, 2 rad/s).
35 *
36 * @param dt Discretization timestep.
37 * @param maxVelocity The maximum velocity for the controller gain lookup
38 * table.
39 * @throws std::domain_error if maxVelocity &lt;= 0.
40 */
42 units::second_t dt, units::meters_per_second_t maxVelocity = 9_mps);
43
44 /**
45 * Constructs a linear time-varying unicycle controller.
46 *
47 * @param Qelems The maximum desired error tolerance for each state.
48 * @param Relems The maximum desired control effort for each input.
49 * @param dt Discretization timestep.
50 * @param maxVelocity The maximum velocity for the controller gain lookup
51 * table.
52 * @throws std::domain_error if maxVelocity &lt;= 0.
53 */
55 const wpi::array<double, 2>& Relems, units::second_t dt,
56 units::meters_per_second_t maxVelocity = 9_mps);
57
58 /**
59 * Move constructor.
60 */
62
63 /**
64 * Move assignment operator.
65 */
67
68 /**
69 * Returns true if the pose error is within tolerance of the reference.
70 */
71 bool AtReference() const;
72
73 /**
74 * Sets the pose error which is considered tolerable for use with
75 * AtReference().
76 *
77 * @param poseTolerance Pose error which is tolerable.
78 */
79 void SetTolerance(const Pose2d& poseTolerance);
80
81 /**
82 * Returns the linear and angular velocity outputs of the LTV controller.
83 *
84 * The reference pose, linear velocity, and angular velocity should come from
85 * a drivetrain trajectory.
86 *
87 * @param currentPose The current pose.
88 * @param poseRef The desired pose.
89 * @param linearVelocityRef The desired linear velocity.
90 * @param angularVelocityRef The desired angular velocity.
91 */
92 ChassisSpeeds Calculate(const Pose2d& currentPose, const Pose2d& poseRef,
93 units::meters_per_second_t linearVelocityRef,
94 units::radians_per_second_t angularVelocityRef);
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 desiredState The desired pose, linear velocity, and angular velocity
104 * from a trajectory.
105 */
106 ChassisSpeeds Calculate(const Pose2d& currentPose,
107 const Trajectory::State& desiredState);
108
109 /**
110 * Enables and disables the controller for troubleshooting purposes.
111 *
112 * @param enabled If the controller is enabled or not.
113 */
114 void SetEnabled(bool enabled);
115
116 private:
117 // LUT from drivetrain linear velocity to LQR gain
119
120 Pose2d m_poseError;
121 Pose2d m_poseTolerance;
122 bool m_enabled = true;
123};
124
125} // 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:29
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:25
Implements a table of key-value pairs with linear interpolation between values.
Definition: interpolating_map.h:23
Definition: AprilTagFieldLayout.h:22
Represents the speed of a robot chassis.
Definition: ChassisSpeeds.h:25
Represents one point on the trajectory.
Definition: Trajectory.h:33