Pedro Pathing

repository·main·Indexed 41 days ago

https://github.com/pedro-pathing/pedropathing

A library for control systems providing a functional approach to defining and composing control algorithms. It features a Controller interface for implementing PID, integral, and derivative controllers, as well as feedforward and piecewise controllers. The library supports composing controllers through arithmetic operations like plus, minus, and times.

Tokens
1.1K
Snippets
7
Records
7
Agent score
3%

What's inside Pedro Pathing

  1. Create feedforward controllers

    main

    Feedforward controllers provide output based on the target or error without waiting for a feedback loop.

    Available types:

    • staticFeedforward(double kS): Constant output in the direction of the error: kS * signum(error).
    • staticTargetFeedforward(double kS): Constant output in the direction of the target: kS * signum(target).
    • proportionalFeedforward(double kV): Output proportional to the target: kV * target.

    All these methods accept either a double or a Supplier<Double> for the gain/constant.

    Controller ff = Controller.staticFeedforward(0.5);
    Controller targetFf = Controller.proportionalFeedforward(0.2);
  2. Compose controllers using arithmetic operations

    main

    You can combine multiple Controller instances using arithmetic-like methods to build complex control laws from simple components. These operations return new Controller instances that manage the lifecycle (resetting) of their constituents.

    Available operations:

    • plus(Controller other): Sums the outputs of two controllers.
    • minus(Controller other): Subtracts the output of the second controller from the first.
    • times(double scalar): Scales the output of the controller by a constant factor.
    • Controller.sum(Controller... controllers): A static utility to sum an arbitrary number of controllers.
    Controller p = Controller.proportional(1.0);
    Controller feedforward = Controller.staticFeedforward(0.5);
    
    // Combine them into a single controller
    Controller combined = p.plus(feedforward);
  3. Use the Controller interface for control logic

    main

    The Controller interface is the core abstraction for all control algorithms in the library. It is a @FunctionalInterface that defines how to calculate a control output based on a target value and the current error.

    Key methods:

    • calculate(double target, double error): Computes the control output.
    • calculate(double target, double error, double derivative): A specialized version that accepts a pre-calculated derivative.
    • reset(): Resets the internal state of the controller (e.g., clearing integral accumulation or time tracking).
    Controller myController = (target, error) -> error * 0.5;
    double output = myController.calculate(10.0, 2.0);
  4. Create an integral controller with zone and decay

    main

    For more advanced integral control, use the overloaded integral method. This version allows you to prevent integral windup and manage accumulation more precisely using an 'iZone', a decay factor, and a maximum cap.

    Parameters:

    • kI: Supplier for the integral gain.
    • iZone: Supplier for the error threshold; accumulation only occurs when |error| < iZone.
    • decay: Supplier for a decay factor applied to the integral when within the zone.
    • maxI: Supplier for the maximum absolute value the integral can reach (clamping).
    Controller advancedIntegral = Controller.integral(
        () -> 0.5,   // kI
        () -> 0.1,   // iZone
        () -> 0.9,   // decay
        () -> 10.0   // maxI
    );