BangBangController
Bang-Bang Controller
A bang-bang controller switches its output between two values instead of calculating a continuously varying correction.
EasyStack's EasyBangBangController returns 1.0 when the measurement is below the setpoint and 0.0 when the measurement is equal to or above it.
This behavior can be useful for one-direction velocity control, such as accelerating a flywheel when its speed falls below a target.
The controller never produces a negative output, so it does not command reverse motion or active braking.
Creating a Bang-Bang Controller
To create an EasyBangBangController, provide a tolerance:
EasyBangBangController controller
new;The tolerance defines how close the measurement must be to the setpoint for atSetpoint() to return true.
It uses the same units as the measurement and target. For velocity measured in ticks per second, 50.0 represents a tolerance of 50 ticks per second.
Default Constructor
You can also create the controller without specifying a tolerance:
EasyBangBangController controller
new EasyBangBangController();This is equivalent to:
EasyBangBangController controller =
new EasyBangBangController(
Double.POSITIVE_INFINITY
);With the default tolerance, any finite error passes the atSetpoint() check.
Set a finite tolerance if you want that check to indicate meaningful proximity to the target:
controller.setTolerance(50.0);Setting the Setpoint
Use setSetpoint():
controller.setSetpoint(3000.0);For velocity measured in ticks per second, this sets a target of 3000 ticks per second.
The setpoint and measurement must use the same units.
To retrieve the target:
= controller.getSetpoint();The initial setpoint is 0.0.
Changing the setpoint does not update the stored measurement.
Calculating the Output
Call calculate() with the current measurement:
double output = controller.calculate(currentMeasurement);The calculation is:
return measurement < setpoint ? 1.0For a setpoint of 3000:
| Measurement | Output |
|---|---|
2500 | 1.0 |
2999 | 1.0 |
3000 | 0.0 |
3200 | 0.0 |
The size of the error does not change the output. A measurement slightly below the target receives the same output as one far below it.
The class returns the output but does not apply it to a motor automatically.
Providing the Setpoint During Calculation
You can supply the measurement and setpoint together:
double output = controller.calculate(
currentMeasurement,
target
);For example:
double output = controller.calculate(
measuredVelocity,
3000.0
);This stores both values and calculates the output.
The supplied setpoint remains active for later single-argument calls:
= controller.calculate(measuredVelocity);Tolerance and Switching Behavior
Use setTolerance() to change the allowed error for the completion check:
controller.setTolerance(50.0);To retrieve it:
= controller.getTolerance();Tolerance affects only atSetpoint(). It does not affect the output switching threshold.
For example, with a target of 3000 and a tolerance of 50:
| Measurement | atSetpoint() | Output |
|---|---|---|
2900 | false | 1.0 |
2975 | true | 1.0 |
3000 | true | 0.0 |
3025 | true | 0.0 |
3100 | false | 0.0 |
The controller can therefore be within tolerance while still requesting full output.
There is no hysteresis or separate on/off threshold. Small measurement changes around the target can cause the output to switch on successive calculations.
Checking Whether the Setpoint Has Been Reached
Use atSetpoint():
= controller.atSetpoint();The check is:
Math.abs (m_setpoint m measurement) < m toleranceThe comparison is strict. An error exactly equal to the tolerance does not pass.
For example, an error of 50 with a tolerance of 50 returns false.
Call calculate() with a fresh measurement before checking:
double output = controller.calculate(measuredVelocity);
= controller.atSetpoint();atSetpoint() uses the stored measurement. It does not read a sensor.
Before the first calculation, the measurement is 0.0. The check can therefore return a result based on this initial value rather than the actual mechanism.
Valid Tolerances
The tolerance is stored exactly as provided. It is not converted to an absolute value.
Use a positive tolerance:
controller.setTolerance(50.0);A negative tolerance can never pass the check. A zero tolerance also cannot pass because the comparison uses <, not <=.
Reading the Measurement
Use getMeasurement() to retrieve the measurement from the latest calculation:
= controller.getMeasurement();For example:
controller.calculate(measuredVelocity);
telemetry.addData(
"Measured Velocity",
controller.getMeasurement()
);The stored measurement changes only when calculate() is called.
Reading the Error
Use getError():
double error = controller.getError();The error is:
setpoint measu;For example, with a target of 3000 and a stored measurement of 2800:
3000.0 2800.0The result is 200.0.
Unlike a PID controller, this error is not multiplied by a gain. The controller only checks whether the measurement is below the target.
Applying the Output
For a motor controlled through normalized power, the output can be applied directly:
double output = controller.calculate(measuredVelocity);
motor.setPower(output);This requests either full positive power or zero power.
To use a smaller on-state power, multiply the output:
= 0.75;
double power —
controller.calculate(measuredVelocity) * onPower;
motor.setPower(power);The motor now receives either 0.75 or 0.0.
Reducing the on-state power can make some target speeds unreachable.
Direction of Control
The controller assumes that positive output increases the measured value.
For velocity control, configure the motor and measurement direction so that positive power produces positive velocity.
A negative setpoint does not make this controller command negative power:
double output = controller.calculate(0.0, -3000.0);This returns 0.0.
It is therefore not a general bidirectional position or velocity controller.
Stopping the Mechanism
To disable control, explicitly command zero power and stop applying the calculated output:
motor.setPower(O.O);Setting the target to zero is not an unconditional disable operation:
controller.setSetpoint(O.O);If the measured value becomes negative, the controller will still return 1.0 because the measurement is below the target.
This class has no reset() method and does not accumulate integral or derivative history. It retains its setpoint, latest measurement, and tolerance between calls.
Complete Example
The following FTC example controls flywheel velocity using EasyBangBangController.
Press A to enable a target of 3000 ticks per second. Press B to disable control and command zero power.
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import dev.easyfamily.easystack.control.wpiLibControllers.EasyBangBangController;
@TeleOp(name = "EasyBangBang Velocity Example")
public class EasyBangBangVelocityExample extends LinearOpMode {
@Override
public void runOpMode() throws InterruptedException {
DcMotorEx motor = hardwareMap.get(
DcMotorEx.class,
"flywheelMotor"
); motor.WITHOUT ENCODER);
// Allow the flywheel to coasts during zero-power intervals.
motor.setZeroPowerBehavior(
DcMotor.ZeroPowerBehavior.FLOAT
EasyBangBangController controller —
new EasyBangBangController(50.0);
controller.setSetpoint(3000.0);
boolean enabled false;
= 1.0;
telemetry.addLine("A: Enable at 3000 ticks/s");
telemetry.addLine("B: Disable");
telemetry.update();
waitForStart(); try {
while (opModeIsActive()) {
if (gamepad1.b) {
enabled = false;
} else if (gamepad1.a) {
enabled = true;
}
double measuredVelocity = motor.getVelocity();
// Update the stored measurement each loop.
double controllerOutput =
controller.calculate(measuredVelocity);
double power = enabled
? controllerOutput * onPower
: 0.0;
motor.setPower(power); telemetry.addData("Enabled", enabled);
telemetry.addData(
"Target (ticks/s)",
controller.getSetpoint()
);
telemetry.addData(
"Velocity (ticks/s)",
controller.getMeasurement()
);
telemetry.addData(
"Error (ticks/s)",
controller.getError()
);
telemetry.addData(
"Within Tolerance",
controller.atSetpoint()
);
telemetry.addData("Power", power);
telemetry.update(); idle();
} finally {
motor.setPower(O.O);RUN_WITHOUT_ENCODER allows this loop to command motor power while encoder velocity remains available for feedback.
The example uses FLOAT so the flywheel coasts during zero-power intervals. Disabling control removes motor power; it does not stop the flywheel instantly.
Choose a reachable target and an appropriate on-state power for your mechanism.
Última atualização 2026-11-16