WPILibC++ 2023.4.3-108-ge5452e3
LTVDifferentialDriveController.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"
13#include "frc/geometry/Pose2d.h"
16#include "units/length.h"
17#include "units/time.h"
18#include "units/velocity.h"
19#include "units/voltage.h"
20
21namespace frc {
22
23/**
24 * The linear time-varying differential drive controller has a similar form to
25 * the LQR, but the model used to compute the controller gain is the nonlinear
26 * model linearized around the drivetrain's current state. We precomputed gains
27 * for important places in our state-space, then interpolated between them with
28 * a LUT to save computational resources.
29 *
30 * See section 8.7 in Controls Engineering in FRC for a derivation of the
31 * control law we used shown in theorem 8.7.4.
32 */
34 public:
35 /**
36 * Constructs a linear time-varying differential drive controller.
37 *
38 * @param plant The differential drive velocity plant.
39 * @param trackwidth The distance between the differential drive's left and
40 * right wheels.
41 * @param Qelems The maximum desired error tolerance for each state.
42 * @param Relems The maximum desired control effort for each input.
43 * @param dt Discretization timestep.
44 * @throws std::domain_error if max velocity of plant with 12 V input <= 0.
45 */
47 units::meter_t trackwidth,
48 const wpi::array<double, 5>& Qelems,
49 const wpi::array<double, 2>& Relems,
50 units::second_t dt);
51
52 /**
53 * Move constructor.
54 */
56
57 /**
58 * Move assignment operator.
59 */
61 default;
62
63 /**
64 * Returns true if the pose error is within tolerance of the reference.
65 */
66 bool AtReference() const;
67
68 /**
69 * Sets the pose error which is considered tolerable for use with
70 * AtReference().
71 *
72 * @param poseTolerance Pose error which is tolerable.
73 * @param leftVelocityTolerance Left velocity error which is tolerable.
74 * @param rightVelocityTolerance Right velocity error which is tolerable.
75 */
76 void SetTolerance(const Pose2d& poseTolerance,
77 units::meters_per_second_t leftVelocityTolerance,
78 units::meters_per_second_t rightVelocityTolerance);
79
80 /**
81 * Returns the left and right output voltages of the LTV controller.
82 *
83 * The reference pose, linear velocity, and angular velocity should come from
84 * a drivetrain trajectory.
85 *
86 * @param currentPose The current pose.
87 * @param leftVelocity The current left velocity.
88 * @param rightVelocity The current right velocity.
89 * @param poseRef The desired pose.
90 * @param leftVelocityRef The desired left velocity.
91 * @param rightVelocityRef The desired right velocity.
92 */
94 const Pose2d& currentPose, units::meters_per_second_t leftVelocity,
95 units::meters_per_second_t rightVelocity, const Pose2d& poseRef,
96 units::meters_per_second_t leftVelocityRef,
97 units::meters_per_second_t rightVelocityRef);
98
99 /**
100 * Returns the left and right output voltages of the LTV controller.
101 *
102 * The reference pose, linear velocity, and angular velocity should come from
103 * a drivetrain trajectory.
104 *
105 * @param currentPose The current pose.
106 * @param leftVelocity The left velocity.
107 * @param rightVelocity The right velocity.
108 * @param desiredState The desired pose, linear velocity, and angular velocity
109 * from a trajectory.
110 */
112 const Pose2d& currentPose, units::meters_per_second_t leftVelocity,
113 units::meters_per_second_t rightVelocity,
114 const Trajectory::State& desiredState);
115
116 private:
117 units::meter_t m_trackwidth;
118
119 // LUT from drivetrain linear velocity to LQR gain
121
122 Vectord<5> m_error;
123 Vectord<5> m_tolerance;
124};
125
126} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
The linear time-varying differential drive controller has a similar form to the LQR,...
Definition: LTVDifferentialDriveController.h:33
bool AtReference() const
Returns true if the pose error is within tolerance of the reference.
LTVDifferentialDriveController(const frc::LinearSystem< 2, 2, 2 > &plant, units::meter_t trackwidth, const wpi::array< double, 5 > &Qelems, const wpi::array< double, 2 > &Relems, units::second_t dt)
Constructs a linear time-varying differential drive controller.
DifferentialDriveWheelVoltages Calculate(const Pose2d &currentPose, units::meters_per_second_t leftVelocity, units::meters_per_second_t rightVelocity, const Trajectory::State &desiredState)
Returns the left and right output voltages of the LTV controller.
LTVDifferentialDriveController & operator=(LTVDifferentialDriveController &&)=default
Move assignment operator.
DifferentialDriveWheelVoltages Calculate(const Pose2d &currentPose, units::meters_per_second_t leftVelocity, units::meters_per_second_t rightVelocity, const Pose2d &poseRef, units::meters_per_second_t leftVelocityRef, units::meters_per_second_t rightVelocityRef)
Returns the left and right output voltages of the LTV controller.
void SetTolerance(const Pose2d &poseTolerance, units::meters_per_second_t leftVelocityTolerance, units::meters_per_second_t rightVelocityTolerance)
Sets the pose error which is considered tolerable for use with AtReference().
LTVDifferentialDriveController(LTVDifferentialDriveController &&)=default
Move constructor.
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:25
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: AprilTagPoseEstimator.h:15
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12
Motor voltages for a differential drive.
Definition: DifferentialDriveWheelVoltages.h:14
Represents one point on the trajectory.
Definition: Trajectory.h:33