Package frc.lib.lowlevel
Class Whacknet
java.lang.Object
frc.lib.lowlevel.Whacknet
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassZero-allocation view into the ByteBuffer representing a single Vision Observation.static final recordA record representing the robot's telemetry data. -
Method Summary
Modifier and TypeMethodDescriptionvoidbroadcastTelemetry(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.voidclose()Gracefully shuts down the Whacknet server, freeing UDP ports and stopping the receiver thread.voidforEachPacket(ObjIntConsumer<Whacknet.PacketView> consumer) Iterates over packets received since the last call toreadPackets().static WhacknetReturns the singleton instance of Whacknet.intDrains the internal ring buffer into a snapshot buffer for the main loop to process.voidregisterTelemetryService(int port, Frequency frequency, Supplier<Whacknet.RobotTelemetry> supplier) Registers a service to broadcast robot telemetry at a specified frequency.voidstartServer(int port) Starts the vision server to receive observations from coprocessors.
-
Method Details
-
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:
closein interfaceAutoCloseable
-
forEachPacket
Iterates over packets received since the last call toreadPackets().- Parameters:
consumer- A lambda to process each packet view.
-