WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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 <gcem.hpp>
10#include <wpi/SymbolExports.h>
13
14#include "wpimath/MathShared.h"
15
16namespace frc {
17
18/**
19 * Implements a bang-bang controller, which outputs either 0 or 1 depending on
20 * whether the measurement is less than the setpoint. This maximally-aggressive
21 * control approach works very well for velocity control of high-inertia
22 * mechanisms, and poorly on most other things.
23 *
24 * <p>Note that this is an *asymmetric* bang-bang controller - it will not exert
25 * any control effort in the reverse direction (e.g. it won't try to slow down
26 * an over-speeding shooter wheel). This asymmetry is *extremely important.*
27 * Bang-bang control is extremely simple, but also potentially hazardous. Always
28 * ensure that your motor controllers are set to "coast" before attempting to
29 * control them with a bang-bang controller.
30 */
32 : public wpi::Sendable,
33 public wpi::SendableHelper<BangBangController> {
34 public:
35 /**
36 * Creates a new bang-bang controller.
37 *
38 * <p>Always ensure that your motor controllers are set to "coast" before
39 * attempting to control them with a bang-bang controller.
40 *
41 * @param tolerance Tolerance for atSetpoint.
42 */
43 constexpr explicit BangBangController(
44 double tolerance = std::numeric_limits<double>::infinity())
45 : m_tolerance(tolerance) {
46 if (!std::is_constant_evaluated()) {
47 ++instances;
50 }
51 }
52
53 /**
54 * Sets the setpoint for the bang-bang controller.
55 *
56 * @param setpoint The desired setpoint.
57 */
58 constexpr void SetSetpoint(double setpoint) { m_setpoint = setpoint; }
59
60 /**
61 * Returns the current setpoint of the bang-bang controller.
62 *
63 * @return The current setpoint.
64 */
65 constexpr double GetSetpoint() const { return m_setpoint; }
66
67 /**
68 * Returns true if the error is within the tolerance of the setpoint.
69 *
70 * @return Whether the error is within the acceptable bounds.
71 */
72 constexpr bool AtSetpoint() const {
73 return gcem::abs(m_setpoint - m_measurement) < m_tolerance;
74 }
75
76 /**
77 * Sets the error within which AtSetpoint will return true.
78 *
79 * @param tolerance Position error which is tolerable.
80 */
81 constexpr void SetTolerance(double tolerance) { m_tolerance = tolerance; }
82
83 /**
84 * Returns the current tolerance of the controller.
85 *
86 * @return The current tolerance.
87 */
88 constexpr double GetTolerance() const { return m_tolerance; }
89
90 /**
91 * Returns the current measurement of the process variable.
92 *
93 * @return The current measurement of the process variable.
94 */
95 constexpr double GetMeasurement() const { return m_measurement; }
96
97 /**
98 * Returns the current error.
99 *
100 * @return The current error.
101 */
102 constexpr double GetError() const { return m_setpoint - m_measurement; }
103
104 /**
105 * Returns the calculated control output.
106 *
107 * <p>Always ensure that your motor controllers are set to "coast" before
108 * attempting to control them with a bang-bang controller.
109 *
110 * @param measurement The most recent measurement of the process variable.
111 * @param setpoint The setpoint for the process variable.
112 * @return The calculated motor output (0 or 1).
113 */
114 constexpr double Calculate(double measurement, double setpoint) {
115 m_measurement = measurement;
116 m_setpoint = setpoint;
117
118 return measurement < setpoint ? 1 : 0;
119 }
120
121 /**
122 * Returns the calculated control output.
123 *
124 * @param measurement The most recent measurement of the process variable.
125 * @return The calculated motor output (0 or 1).
126 */
127 constexpr double Calculate(double measurement) {
128 return Calculate(measurement, m_setpoint);
129 }
130
131 void InitSendable(wpi::SendableBuilder& builder) override;
132
133 private:
134 double m_tolerance;
135
136 double m_setpoint = 0;
137 double m_measurement = 0;
138
139 // Usage reporting instances
140 inline static int instances = 0;
141};
142
143} // 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:33
constexpr BangBangController(double tolerance=std::numeric_limits< double >::infinity())
Creates a new bang-bang controller.
Definition BangBangController.h:43
constexpr bool AtSetpoint() const
Returns true if the error is within the tolerance of the setpoint.
Definition BangBangController.h:72
constexpr double GetTolerance() const
Returns the current tolerance of the controller.
Definition BangBangController.h:88
constexpr double Calculate(double measurement, double setpoint)
Returns the calculated control output.
Definition BangBangController.h:114
constexpr double GetMeasurement() const
Returns the current measurement of the process variable.
Definition BangBangController.h:95
constexpr double Calculate(double measurement)
Returns the calculated control output.
Definition BangBangController.h:127
constexpr void SetTolerance(double tolerance)
Sets the error within which AtSetpoint will return true.
Definition BangBangController.h:81
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
constexpr void SetSetpoint(double setpoint)
Sets the setpoint for the bang-bang controller.
Definition BangBangController.h:58
constexpr double GetSetpoint() const
Returns the current setpoint of the bang-bang controller.
Definition BangBangController.h:65
constexpr double GetError() const
Returns the current error.
Definition BangBangController.h:102
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:21
Interface for Sendable objects.
Definition Sendable.h:16
static void ReportUsage(MathUsageId id, int count)
Definition MathShared.h:75
Definition CAN.h:11
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition abs.hpp:40