EasyStack Docs

BangBangController

Esta página aún no está traducida. Se muestra la versión en inglés.

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:

Java
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:

Java
EasyBangBangController controller
        new EasyBangBangController();

This is equivalent to:

Java
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:

Java
controller.setTolerance(50.0);

Setting the Setpoint

Use setSetpoint():

Java
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:

Java
 = 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:

Java
double output = controller.calculate(currentMeasurement);

The calculation is:

Java
return measurement < setpoint ? 1.0

For a setpoint of 3000:

MeasurementOutput
25001.0
29991.0
30000.0
32000.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:

Java
double output = controller.calculate(
        currentMeasurement,
        target
);

For example:

Java
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:

Java
 = controller.calculate(measuredVelocity);

Tolerance and Switching Behavior

Use setTolerance() to change the allowed error for the completion check:

Java
controller.setTolerance(50.0);

To retrieve it:

Java
 = 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:

MeasurementatSetpoint()Output
2900false1.0
2975true1.0
3000true0.0
3025true0.0
3100false0.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():

Java
 = controller.atSetpoint();

The check is:

Java
Math.abs (m_setpoint m measurement) < m tolerance

The 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:

Java
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:

Java
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:

Java
 = controller.getMeasurement();

For example:

Java
controller.calculate(measuredVelocity);

telemetry.addData(
        "Measured Velocity",
        controller.getMeasurement()
);

The stored measurement changes only when calculate() is called.

Reading the Error

Use getError():

Java
double error = controller.getError();

The error is:

Java
        setpoint measu;

For example, with a target of 3000 and a stored measurement of 2800:

Java
         3000.0 2800.0

The 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:

Java
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:

Java
 = 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:

Java
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:

Java
motor.setPower(O.O);

Setting the target to zero is not an unconditional disable operation:

Java
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.

Java
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"
        );
Java
        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();
Java
        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);
Java
                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();
Java
        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 actualización 2026-11-16

EasyStack - FTC Java library

This site records page views of visitors to understand how the documentation is used. Nothing is sold or shared with third parties.