WPILibC++ 2024.3.2
BangBangController.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 <limits>
8
9#include <wpi/SymbolExports.h>
12
13namespace frc {
14
15/**
16 * Implements a bang-bang controller, which outputs either 0 or 1 depending on
17 * whether the measurement is less than the setpoint. This maximally-aggressive
18 * control approach works very well for velocity control of high-inertia
19 * mechanisms, and poorly on most other things.
20 *
21 * <p>Note that this is an *asymmetric* bang-bang controller - it will not exert
22 * any control effort in the reverse direction (e.g. it won't try to slow down
23 * an over-speeding shooter wheel). This asymmetry is *extremely important.*
24 * Bang-bang control is extremely simple, but also potentially hazardous. Always
25 * ensure that your motor controllers are set to "coast" before attempting to
26 * control them with a bang-bang controller.
27 */
29 : public wpi::Sendable,
30 public wpi::SendableHelper<BangBangController> {
31 public:
32 /**
33 * Creates a new bang-bang controller.
34 *
35 * <p>Always ensure that your motor controllers are set to "coast" before
36 * attempting to control them with a bang-bang controller.
37 *
38 * @param tolerance Tolerance for atSetpoint.
39 */
41 double tolerance = std::numeric_limits<double>::infinity());
42
43 /**
44 * Sets the setpoint for the bang-bang controller.
45 *
46 * @param setpoint The desired setpoint.
47 */
48 void SetSetpoint(double setpoint);
49
50 /**
51 * Returns the current setpoint of the bang-bang controller.
52 *
53 * @return The current setpoint.
54 */
55 double GetSetpoint() const;
56
57 /**
58 * Returns true if the error is within the tolerance of the setpoint.
59 *
60 * @return Whether the error is within the acceptable bounds.
61 */
62 bool AtSetpoint() const;
63
64 /**
65 * Sets the error within which AtSetpoint will return true.
66 *
67 * @param tolerance Position error which is tolerable.
68 */
69 void SetTolerance(double tolerance);
70
71 /**
72 * Returns the current tolerance of the controller.
73 *
74 * @return The current tolerance.
75 */
76 double GetTolerance() const;
77
78 /**
79 * Returns the current measurement of the process variable.
80 *
81 * @return The current measurement of the process variable.
82 */
83 double GetMeasurement() const;
84
85 /**
86 * Returns the current error.
87 *
88 * @return The current error.
89 */
90 double GetError() const;
91
92 /**
93 * Returns the calculated control output.
94 *
95 * <p>Always ensure that your motor controllers are set to "coast" before
96 * attempting to control them with a bang-bang controller.
97 *
98 * @param measurement The most recent measurement of the process variable.
99 * @param setpoint The setpoint for the process variable.
100 * @return The calculated motor output (0 or 1).
101 */
102 double Calculate(double measurement, double setpoint);
103
104 /**
105 * Returns the calculated control output.
106 *
107 * @param measurement The most recent measurement of the process variable.
108 * @return The calculated motor output (0 or 1).
109 */
110 double Calculate(double measurement);
111
112 void InitSendable(wpi::SendableBuilder& builder) override;
113
114 private:
115 double m_tolerance;
116
117 double m_setpoint = 0;
118 double m_measurement = 0;
119};
120
121} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
Implements a bang-bang controller, which outputs either 0 or 1 depending on whether the measurement i...
Definition: BangBangController.h:30
double GetMeasurement() const
Returns the current measurement of the process variable.
double GetError() const
Returns the current error.
void SetSetpoint(double setpoint)
Sets the setpoint for the bang-bang controller.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
double Calculate(double measurement)
Returns the calculated control output.
void SetTolerance(double tolerance)
Sets the error within which AtSetpoint will return true.
BangBangController(double tolerance=std::numeric_limits< double >::infinity())
Creates a new bang-bang controller.
double Calculate(double measurement, double setpoint)
Returns the calculated control output.
double GetSetpoint() const
Returns the current setpoint of the bang-bang controller.
bool AtSetpoint() const
Returns true if the error is within the tolerance of the setpoint.
double GetTolerance() const
Returns the current tolerance of the controller.
Helper class for building Sendable dashboard representations.
Definition: SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
Interface for Sendable objects.
Definition: Sendable.h:16
Definition: AprilTagPoseEstimator.h:15