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.

基本的客户端程序如下例所示。

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”.

然后,该实例作为具有组号的NetworkTables客户端启动(roboRIO始终是服务器)。此外,如果程序在机器操控台计算机上运行,则使用startDSClient()方法,NetworkTables将从机器操控台获取机械手IP地址。

然后,此示例程序简单地每秒循环一次,获取x和y的值并将其打印在控制台上。在现实的程序中,客户端可能正在处理或生成供机器人使用的值。

使用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.