Skip to content

Commit 04198c3

Browse files
authored
Update NT Client to use StandaloneAppSamples (#2173)
* Update NT Client to use StandaloneAppSamples * Add inspector config
1 parent e5c48c3 commit 04198c3

File tree

2 files changed

+73
-73
lines changed

2 files changed

+73
-73
lines changed

.github/workflows/inspector.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"baseUrl": "https://raw.githubusercontent.com/wpilibsuite/vscode-wpilib/",
2020
"versionScheme": "v\\d{4}\\.\\d\\.\\d(?:-(?:alpha|beta)-\\d)?|[0-9a-f]{40}",
2121
"latestVersion":"v2023.2.1"
22+
},
23+
{
24+
"baseUrl": "https://raw.githubusercontent.com/wpilibsuite/StandaloneAppSamples/",
25+
"versionScheme": "v\\d{4}\\.\\d\\.\\d(?:-(?:alpha|beta)-\\d)?|[0-9a-f]{40}",
26+
"latestVersion":"3b64aadee717c9f0566497a40fd0be7d0eaed96d"
2227
}
2328
],
2429
"ignoredFiles": []

source/docs/software/networktables/client-side-program.rst

Lines changed: 68 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,52 @@ A basic client program looks like the following example.
1313

1414
.. code-block:: java
1515
16-
package networktablesdesktopclient;
17-
18-
import edu.wpi.first.networktables.DoubleSubscriber;
19-
import edu.wpi.first.networktables.NetworkTable;
20-
import edu.wpi.first.networktables.NetworkTableInstance;
21-
22-
public class NetworkTablesDesktopClient {
23-
public static void main(String[] args) {
24-
new NetworkTablesDesktopClient().run();
25-
}
26-
27-
public void run() {
28-
NetworkTableInstance inst = NetworkTableInstance.getDefault();
29-
NetworkTable table = inst.getTable("datatable");
30-
DoubleSubscriber xSub = table.getDoubleTopic("x").subscribe(0.0);
31-
DoubleSubscriber ySub = table.getDoubleTopic("y").subscribe(0.0);
32-
inst.startClient4("example client");
33-
inst.setServerTeam(TEAM); // where TEAM=190, 294, etc, or use inst.setServer("hostname") or similar
34-
inst.startDSClient(); // recommended if running on DS computer; this gets the robot IP from the DS
35-
while (true) {
36-
try {
37-
Thread.sleep(1000);
38-
} catch (InterruptedException ex) {
39-
System.out.println("interrupted");
40-
return;
41-
}
42-
double x = xSub.get();
43-
double y = ySub.get();
44-
System.out.println("X: " + x + " Y: " + y);
45-
}
46-
}
47-
}
16+
import edu.wpi.first.networktables.DoubleSubscriber;
17+
import edu.wpi.first.networktables.NetworkTable;
18+
import edu.wpi.first.networktables.NetworkTableInstance;
19+
import edu.wpi.first.networktables.NetworkTablesJNI;
20+
import edu.wpi.first.util.CombinedRuntimeLoader;
21+
22+
import java.io.IOException;
23+
24+
import edu.wpi.first.cscore.CameraServerJNI;
25+
import edu.wpi.first.math.WPIMathJNI;
26+
import edu.wpi.first.util.WPIUtilJNI;
27+
28+
29+
public class Program {
30+
public static void main(String[] args) throws IOException {
31+
NetworkTablesJNI.Helper.setExtractOnStaticLoad(false);
32+
WPIUtilJNI.Helper.setExtractOnStaticLoad(false);
33+
WPIMathJNI.Helper.setExtractOnStaticLoad(false);
34+
CameraServerJNI.Helper.setExtractOnStaticLoad(false);
35+
36+
CombinedRuntimeLoader.loadLibraries(Program.class, "wpiutiljni", "wpimathjni", "ntcorejni",
37+
"cscorejnicvstatic");
38+
new Program().run();
39+
}
40+
41+
public void run() {
42+
NetworkTableInstance inst = NetworkTableInstance.getDefault();
43+
NetworkTable table = inst.getTable("datatable");
44+
DoubleSubscriber xSub = table.getDoubleTopic("x").subscribe(0.0);
45+
DoubleSubscriber ySub = table.getDoubleTopic("y").subscribe(0.0);
46+
inst.startClient4("example client");
47+
inst.setServer("localhost"); // where TEAM=190, 294, etc, or use inst.setServer("hostname") or similar
48+
inst.startDSClient(); // recommended if running on DS computer; this gets the robot IP from the DS
49+
while (true) {
50+
try {
51+
Thread.sleep(1000);
52+
} catch (InterruptedException ex) {
53+
System.out.println("interrupted");
54+
return;
55+
}
56+
double x = xSub.get();
57+
double y = ySub.get();
58+
System.out.println("X: " + x + " Y: " + y);
59+
}
60+
}
61+
}
4862
4963
.. group-tab:: C++
5064

@@ -60,8 +74,8 @@ A basic client program looks like the following example.
6074
int main() {
6175
auto inst = nt::NetworkTableInstance::GetDefault();
6276
auto table = inst.GetTable("datatable");
63-
auto xSub = table->GetDoubleTopic("x").subscribe(0.0);
64-
auto ySub = table->GetDoubleTopic("y").subscribe(0.0);
77+
auto xSub = table->GetDoubleTopic("x").Subscribe(0.0);
78+
auto ySub = table->GetDoubleTopic("y").Subscribe(0.0);
6579
inst.StartClient4("example client");
6680
inst.SetServerTeam(TEAM); // where TEAM=190, 294, etc, or use inst.setServer("hostname") or similar
6781
inst.StartDSClient(); // recommended if running on DS computer; this gets the robot IP from the DS
@@ -81,17 +95,17 @@ A basic client program looks like the following example.
8195
#include <chrono>
8296
#include <thread>
8397
#include <fmt/format.h>
84-
#include <networktables/ntcore.h>
98+
#include <ntcore_cpp.h>
8599
86100
int main() {
87-
NT_Instance inst = nt::GetDefaultInstance();
101+
NT_Inst inst = nt::GetDefaultInstance();
88102
NT_Subscriber xSub =
89103
nt::Subscribe(nt::GetTopic(inst, "/datatable/x"), NT_DOUBLE, "double");
90104
NT_Subscriber ySub =
91105
nt::Subscribe(nt::GetTopic(inst, "/datatable/y"), NT_DOUBLE, "double");
92106
nt::StartClient4(inst, "example client");
93-
nt::SetServerTeam(inst, TEAM); // where TEAM=190, 294, etc, or use inst.setServer("hostname") or similar
94-
nt::StartDSClient(inst); // recommended if running on DS computer; this gets the robot IP from the DS
107+
nt::SetServerTeam(inst, TEAM, 0); // where TEAM=190, 294, etc, or use inst.setServer("hostname") or similar
108+
nt::StartDSClient(inst, 0); // recommended if running on DS computer; this gets the robot IP from the DS
95109
while (true) {
96110
using namespace std::chrono_literals;
97111
std::this_thread::sleep_for(1s);
@@ -159,49 +173,30 @@ Then this instance is started as a NetworkTables client with the team number (th
159173

160174
Then this sample program simply loops once a second and gets the values for x and y and prints them on the console. In a more realistic program, the client might be processing or generating values for the robot to consume.
161175

162-
Building the program
163-
--------------------
164-
When building and running the program you will need some additional libraries to include with your client-side program. For Java these are:
165-
166-
https://frcmaven.wpi.edu/artifactory/development/edu/wpi/first/ntcore/ntcore-java/ (ntcore Java files)
167-
168-
https://frcmaven.wpi.edu/artifactory/development/edu/wpi/first/ntcore/ntcore-jni/ (ntcore native libs for all desktop platforms)
169-
170-
https://frcmaven.wpi.edu/artifactory/development/edu/wpi/first/wpiutil/wpiutil-java/ (wpiutil Java files)
171-
172-
.. note:: The desktop platform jar is for Windows, macOS, and Linux.
173-
174-
For Python, refer to the `RobotPy pyntcore install documentation <https://robotpy.readthedocs.io/en/stable/install/pynetworktables.html>`__.
175-
176176
Building using Gradle
177177
^^^^^^^^^^^^^^^^^^^^^
178178

179-
The dependencies above can be added to the ``dependencies`` block in a ``build.gradle`` file. The ``ntcore-java`` and ``wpiutil-java`` libraries are required at compile-time and the JNI dependencies are required at runtime. The JNI dependencies for all supported platforms should be added to the ``build.gradle`` if cross-platform support for the application is desired.
179+
Example build.gradle files are provided in the `StandaloneAppSamples Repository <https://github.com/wpilibsuite/StandaloneAppSamples>`__ Update the GradleRIO version to correspond to the desired WPILib version.
180180

181-
First, the FRC\ |reg| Maven repository should be added to the ``repositories`` block. Note that this is not required if you are using the GradleRIO plugin with your application.
181+
.. tabs::
182182

183-
.. code-block:: groovy
183+
.. group-tab:: Java
184184

185-
repositories {
186-
maven { url "https://frcmaven.wpi.edu/artifactory/development/" }
187-
}
185+
.. rli:: https://raw.githubusercontent.com/wpilibsuite/StandaloneAppSamples/3b64aadee717c9f0566497a40fd0be7d0eaed96d/Java/build.gradle
186+
:language: groovy
187+
:linenos:
188+
:emphasize-lines: 5
188189

189-
Then, the dependencies can be added to the ``dependencies`` block. Here, ``VERSION`` should be replaced with the latest version number of the following dependencies. This usually corresponds to the version number of the latest WPILib release.
190+
.. group-tab:: C++
190191

191-
.. code-block:: groovy
192+
Uncomment the appropriate platform as highlighted.
192193

193-
dependencies {
194-
// Add ntcore-java
195-
implementation "edu.wpi.first.ntcore:ntcore-java:VERSION"
194+
.. rli:: https://raw.githubusercontent.com/wpilibsuite/StandaloneAppSamples/3b64aadee717c9f0566497a40fd0be7d0eaed96d/Cpp/build.gradle
195+
:language: groovy
196+
:linenos:
197+
:emphasize-lines: 3, 20-22
196198

197-
// Add wpiutil-java
198-
implementation "edu.wpi.first.wpiutil:wpiutil-java:VERSION"
199+
Building Python
200+
^^^^^^^^^^^^^^^
199201

200-
// Add ntcore-jni for runtime. We are adding all supported platforms
201-
// so that our application will work on all supported platforms.
202-
implementation "edu.wpi.first.ntcore:ntcore-jni:VERSION:windowsx86"
203-
implementation "edu.wpi.first.ntcore:ntcore-jni:VERSION:windowsx86-64"
204-
implementation "edu.wpi.first.ntcore:ntcore-jni:VERSION:linuxx86-64"
205-
implementation "edu.wpi.first.ntcore:ntcore-jni:VERSION:linuxraspbian"
206-
implementation "edu.wpi.first.ntcore:ntcore-jni:VERSION:osxx86-64"
207-
}
202+
For Python, refer to the `RobotPy pyntcore install documentation <https://robotpy.readthedocs.io/en/stable/install/pynetworktables.html>`__.

0 commit comments

Comments
 (0)