AdvantageKit

repository·main·Indexed 42 days ago

https://github.com/mechanical-advantage/advantagekit

A specialized logging, telemetry, and replay framework for robotics that enables high-fidelity log replay in simulation. It allows developers to debug robot behavior by running the exact same code in simulation using recorded log files, and integrates with AdvantageScope for data visualization.

Tokens
32.9K
Snippets
63
Records
140
Agent score
54%

What's inside AdvantageKit

  1. What is AdvantageKit?

    main

    AdvantageKit is a logging framework designed for FRC (FIRST Robotics Competition) robot code. Unlike traditional logging frameworks that record a limited set of user-defined values (like sensor data or PID errors), AdvantageKit records all data flowing into the robot code every loop cycle (e.g., every sensor value and button press).

    Because AdvantageKit captures the complete input stream, it enables high-fidelity log replay. This allows developers to:

    • Replay match data in a simulator to exactly replicate robot behavior.
    • Log extra fields after a match has concluded.
    • Modify control pipelines to see how they would have functioned during the actual match.
    • Use logging as a safety net to verify any part of the code's logic by re-running it against recorded inputs.
  2. Overview of AdvantageKit

    main

    AdvantageKit is a logging, telemetry, and replay framework designed for robotics. Its primary feature is log replay, which allows the full state of robot code to be replayed in a simulation environment using a recorded log file. This enables developers to debug robot behavior by running the exact same code in simulation that was used during a physical run.

    AdvantageKit is often used alongside AdvantageScope, a telemetry application that can visualize data even if the robot code does not use AdvantageKit. For teams with simpler requirements, WPILib Data Logging is an alternative, though it does not support the log replay capability in simulation.

  3. Use the Vision Template for high-performance vision systems

    main

    The Vision Template provides a starting point for creating high-performance vision or pose estimation systems that are compatible with AdvantageKit's deterministic log replay. It is designed to be a customizable platform rather than a finished product.

    Key features include:

    • Integration: Supports Limelight, PhotonVision, and vision simulation via PhotonLib.
    • Modes: Supports both simple AprilTag targeting and full pose estimation.
    • High-Frequency Sampling: Ensures every observation is processed without duplication.
    • Deterministic Replay: Guarantees accuracy when replaying vision processing and filtering logic in simulation.
    • Advanced Filtering: Includes automatic scaling of standard deviations and detailed logging for tuning.

    Warning: This template must be customized to fit your specific robot design, game strategy, and hardware configuration. It is a foundation for an optimized stack, not a complete solution.

  4. What is high-frequency odometry and why use it?

    main

    High-frequency odometry in AdvantageKit swerve templates allows data from drive motors, encoders, and gyros to be sampled at a rate significantly faster than the primary 50 Hz robot loop cycle (e.g., 250 Hz).

    While the improvement in absolute mean error may be modest, the primary benefit is a massive reduction in standard deviation. In practical terms, this means that while an autonomous path might not end at a more 'accurate' coordinate, the robot will be much more consistent and precise across multiple runs, making robot behavior more predictable.

  5. Understand AdvantageKit built-in logging

    main

    AdvantageKit automatically logs many critical robot fields as inputs or outputs without requiring manual configuration.

    Key Rules:

    • Built-in inputs are available during replay with guaranteed accuracy and can be accessed in user code without manual logging.
    • All other inputs (custom sensor data, etc.) must be logged using an IO interface.
    • Built-in outputs are available under specific tables (e.g., RealOutputs, ReplayOutputs, RadioStatus) and can be visualized in AdvantageScope.
  6. Access built-in input values

    main

    AdvantageKit automatically logs and replays several categories of inputs:

    • Timestamp: Values from RobotController.getTime() and Timer.getTimestamp() are logged for deterministic replay.
      • Warning: Do not use RobotController.getMonotonicTime() or Timer.getMonotonicTimestamp() in user code; these provide real-time non-deterministic timestamps and should only be used in IO implementations or for profiling.
    • Driver Station: All values from DriverStation or WPILib HID classes (Joystick, XboxController, etc.) are logged under the DriverStation table.
      • Danger: The waitForDsConnection method is incompatible with AdvantageKit.
    • Dashboard Inputs: Inputs accessed via AdvantageKit dashboard classes are logged under the NetworkInputs table. See dashboard inputs documentation for details.
  7. Avoid multithreading in main robot code for log replay compatibility

    main

    To ensure that log replay works correctly in simulation, the main robot code logic must remain single-threaded. Because thread execution timing cannot be consistently recreated in simulation, using extra threads in the main logic will cause discrepancies between real hardware and log replay.

    Recommended Alternatives:

    • Run control loops at a fixed frequency (e.g., 50Hz) within the main robot thread.
    • Utilize the closed-loop features provided by your motor controllers.

    If a thread is strictly necessary: Isolate the thread within an IO implementation. While high-frequency control loops or long-running tasks can exist within an IO implementation, they will not be recreated during log replay. The rest of the robot code will only see the values that are logged periodically via the IO layer.

  8. Configure the Robot class to use LoggedRobot

    main

    The main Robot class must inherit from LoggedRobot instead of TimedRobot.

    LoggedRobot behaves like TimedRobot but with two key differences:

    1. It does not support adding extra periodic functions.
    2. It provides the setUseTiming(boolean) method, which allows you to disable periodic timing to run cycles as fast as possible during replay. Even when timing is disabled, methods like Timer.getFPGATimestamp() will still return the correct timestamp from the real robot.
    public class Robot extends LoggedRobot {
        ...
    }
  9. Understanding the Butterfly Effect in Non-Deterministic Replay

    main

    In non-deterministic replay, minor differences in input timing or data arrival rates create a "Butterfly Effect" where small errors ripple through the system, leading to massive divergences in robot state.

    Example of divergence sequence:

    1. A vision measurement is delayed due to non-deterministic timing.
    2. The pose estimator calculates an incorrect robot pose.
    3. An auto-align command fails to trigger because the inaccurate pose is outside tolerance.
    4. The robot's superstructure and mechanism setpoints diverge from the real robot's state.
    5. Future control inputs are obeyed differently in replay than on the real robot, making the replay useless for debugging complex logic.

    Because replayed outputs are most useful when they don't have a real-world reference point to compare against, it is often impossible to distinguish between an accurate output and a diverged, inaccurate one in non-deterministic systems.