Class Whacknet

java.lang.Object
frc.lib.lowlevel.Whacknet
All Implemented Interfaces:
AutoCloseable

public class Whacknet extends Object implements AutoCloseable
Pure Java implementation of the Whacknet UDP Vision Protocol.

Handles high-frequency vision data from coprocessors and broadcasts robot telemetry. Uses Direct ByteBuffers and the Flyweight pattern to achieve zero-allocation performance, avoiding Garbage Collector pressure during matches.

  • Method Details

    • getInstance

      public static Whacknet getInstance()
      Returns the singleton instance of Whacknet.
      Returns:
      The Whacknet instance.
    • startServer

      public void startServer(int port)
      Starts the vision server to receive observations from coprocessors.
      Parameters:
      port - The UDP port to listen on.
    • broadcastTelemetry

      public void broadcastTelemetry(long timestamp, double roll, double pitch, double yaw, double rollVel, double pitchVel, double yawVel, int port)
      Broadcasts robot telemetry to the vision coprocessor for constrained pose solving.
      Parameters:
      timestamp - FPGA timestamp in microseconds.
      roll - Robot roll in radians.
      pitch - Robot pitch in radians.
      yaw - Robot yaw in radians.
      rollVel - Roll velocity in radians per second.
      pitchVel - Pitch velocity in radians per second.
      yawVel - Yaw velocity in radians per second.
      port - The UDP port to broadcast on.
    • registerTelemetryService

      public void registerTelemetryService(int port, Frequency frequency, Supplier<Whacknet.RobotTelemetry> supplier)
      Registers a service to broadcast robot telemetry at a specified frequency. Clears the old notifier if it exists. If the consumer is not null, it will be used to process the telemetry data.
      Parameters:
      port - The UDP port to broadcast on.
      frequency - The frequency at which to broadcast.
      supplier - A supplier for obtaining robot telemetry data.
    • readPackets

      public int readPackets()
      Drains the internal ring buffer into a snapshot buffer for the main loop to process.
      Returns:
      The number of packets available to process this frame.
    • close

      public void close()
      Gracefully shuts down the Whacknet server, freeing UDP ports and stopping the receiver thread.
      Specified by:
      close in interface AutoCloseable
    • forEachPacket

      public void forEachPacket(ObjIntConsumer<Whacknet.PacketView> consumer)
      Iterates over packets received since the last call to readPackets().
      Parameters:
      consumer - A lambda to process each packet view.