Creating a Client-side Program

If all you need to do is have your robot program communicate with a COTS coprocessor or a dashboard running on the Driver Station laptop, then the previous examples of writing robot programs are sufficient. But if you would like to write some custom client code that would run on the drivers station or on a coprocessor then you need to know how to build NetworkTables programs for those (non-roboRIO) platforms.

Un programa personalizado básico es como el siguiente ejemplo.

import edu.wpi.first.networktables.DoubleSubscriber;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.NetworkTablesJNI;
import edu.wpi.first.util.CombinedRuntimeLoader;

import java.io.IOException;

import edu.wpi.first.cscore.CameraServerJNI;
import edu.wpi.first.math.WPIMathJNI;
import edu.wpi.first.util.WPIUtilJNI;


public class Program {
    public static void main(String[] args) throws IOException {
        NetworkTablesJNI.Helper.setExtractOnStaticLoad(false);
        WPIUtilJNI.Helper.setExtractOnStaticLoad(false);
        WPIMathJNI.Helper.setExtractOnStaticLoad(false);
        CameraServerJNI.Helper.setExtractOnStaticLoad(false);

        CombinedRuntimeLoader.loadLibraries(Program.class, "wpiutiljni", "wpimathjni", "ntcorejni",
                "cscorejnicvstatic");
        new Program().run();
    }

    public void run() {
        NetworkTableInstance inst = NetworkTableInstance.getDefault();
        NetworkTable table = inst.getTable("datatable");
        DoubleSubscriber xSub = table.getDoubleTopic("x").subscribe(0.0);
        DoubleSubscriber ySub = table.getDoubleTopic("y").subscribe(0.0);
        inst.startClient4("example client");
        inst.setServer("localhost"); // where TEAM=190, 294, etc, or use inst.setServer("hostname") or similar
        inst.startDSClient(); // recommended if running on DS computer; this gets the robot IP from the DS
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                System.out.println("interrupted");
                return;
            }
            double x = xSub.get();
            double y = ySub.get();
            System.out.println("X: " + x + " Y: " + y);
        }
    }
}

In this example an instance of NetworkTables is created and subscribers are created to reference the values of «x» and «y» from a table called «datatable».

A continuación, esta instancia se inicia como cliente de NetworkTables con el número de equipo (el roboRIO es siempre el servidor). Además, si el programa se ejecuta en la Driver Station, mediante el método startDSClient(), NetworkTables obtendrá la dirección IP del robot de la Driver Station.

Luego este programa de muestra simplemente se repite una vez por segundo y obtiene los valores de “x” y “y” y los captura en la consola. En un programa más realista, el cliente podría estar procesando o generando valores para que el robot los consuma.

Compilando usando using Gradle

Example build.gradle files are provided in the StandaloneAppSamples Repository Update the GradleRIO version to correspond to the desired WPILib version.

 1plugins {
 2    id "java"
 3    id 'application'
 4    id 'com.github.johnrengelman.shadow' version '7.1.2'
 5    id "edu.wpi.first.GradleRIO" version "2023.2.1"
 6    id 'edu.wpi.first.WpilibTools' version '1.1.0'
 7}
 8
 9mainClassName = 'Program'
10
11wpilibTools.deps.wpilibVersion = wpi.versions.wpilibVersion.get()
12
13def nativeConfigName = 'wpilibNatives'
14def nativeConfig = configurations.create(nativeConfigName)
15
16def nativeTasks = wpilibTools.createExtractionTasks {
17    configurationName = nativeConfigName
18}
19
20nativeTasks.addToSourceSetResources(sourceSets.main)
21nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpimath")
22nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpinet")
23nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpiutil")
24nativeConfig.dependencies.add wpilibTools.deps.wpilib("ntcore")
25nativeConfig.dependencies.add wpilibTools.deps.cscore()
26
27dependencies {
28    implementation wpilibTools.deps.wpilibJava("wpiutil")
29    implementation wpilibTools.deps.wpilibJava("wpimath")
30    implementation wpilibTools.deps.wpilibJava("wpinet")
31    implementation wpilibTools.deps.wpilibJava("ntcore")
32    implementation wpilibTools.deps.wpilibJava("cscore")
33
34    implementation group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: wpi.versions.jacksonVersion.get()
35    implementation group: "com.fasterxml.jackson.core", name: "jackson-core", version: wpi.versions.jacksonVersion.get()
36    implementation group: "com.fasterxml.jackson.core", name: "jackson-databind", version: wpi.versions.jacksonVersion.get()
37
38    implementation group: "org.ejml", name: "ejml-simple", version: wpi.versions.ejmlVersion.get()
39}
40
41shadowJar {
42    archiveBaseName = "TestApplication"
43    archiveVersion = ""
44    exclude("module-info.class")
45    archiveClassifier.set(wpilibTools.currentPlatform.platformName)
46}
47
48wrapper {
49    gradleVersion = '7.5.1'
50}

Building Python

For Python, refer to the RobotPy pyntcore install documentation.