From 9aa22406217734ec710a83098498b7242cb733aa Mon Sep 17 00:00:00 2001 From: "Keenan D. Buckley" Date: Fri, 18 Jan 2019 19:10:26 -0700 Subject: [PATCH] Initial Commit --- robopipe/.classpath | 9 ++ robopipe/.project | 17 ++++ robopipe/.settings/org.eclipse.jdt.core.prefs | 11 +++ robopipe/src/robopipe/App.java | 90 +++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 robopipe/.classpath create mode 100644 robopipe/.project create mode 100644 robopipe/.settings/org.eclipse.jdt.core.prefs create mode 100644 robopipe/src/robopipe/App.java diff --git a/robopipe/.classpath b/robopipe/.classpath new file mode 100644 index 0000000..a58ea0d --- /dev/null +++ b/robopipe/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/robopipe/.project b/robopipe/.project new file mode 100644 index 0000000..be94da2 --- /dev/null +++ b/robopipe/.project @@ -0,0 +1,17 @@ + + + robopipe + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/robopipe/.settings/org.eclipse.jdt.core.prefs b/robopipe/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/robopipe/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/robopipe/src/robopipe/App.java b/robopipe/src/robopipe/App.java new file mode 100644 index 0000000..7b1da35 --- /dev/null +++ b/robopipe/src/robopipe/App.java @@ -0,0 +1,90 @@ +/** + * For more information on using java sockets: + * https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html + */ +package robopipe; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.math.BigDecimal; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.StandardCharsets; + +import edu.wpi.first.networktables.NetworkTable; +import edu.wpi.first.networktables.NetworkTableEntry; +import edu.wpi.first.networktables.NetworkTableInstance; + +public class App { + + NetworkTableEntry encoderEntry, navXEntry; + double encoder = 46.83583538, navX = 3.37583994; + ServerSocket serverSocket; + int portNumber = 4388; + Socket clientSocket; + OutputStream os; + InputStream in; + String output; + + public static void main( String[] args ) + { + System.out.println("Robopipe Start"); + new App().run(); + } + + public void run() { + + try { + NetworkTableInstance inst = NetworkTableInstance.getDefault(); + NetworkTable table = inst.getTable("/SmartDashboard"); + encoderEntry = table.getEntry("averageEncoder"); //Replace x with name of avg encoder value + navXEntry = table.getEntry("navX"); //Replace x with name of NavX rotation + inst.startClientTeam(4388); + inst.startDSClient(); + + System.out.println("Waiting for Unity client connection..."); + serverSocket = new ServerSocket(portNumber); + clientSocket = serverSocket.accept(); + os = clientSocket.getOutputStream(); + in = clientSocket.getInputStream(); + + System.out.println("Connected"); + + toUnity(); + System.out.println("Waiting for Response..."); + while (true) { + if (in.read() != -1) { + System.out.println("Response Received"); + toUnity(); + System.out.println("Waiting for Response..."); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void toUnity() throws IOException { + try { + Thread.sleep(1000); + } catch (InterruptedException ex) { + System.out.println("InterruptedException"); + return; + } + encoder = encoderEntry.getDouble(0.0); + navX = navXEntry.getDouble(0.0); + System.out.println("Encoder Value: " + encoder + " Rotation: " + navX); + + output = round(encoder, 3) + "|" + round(navX, 3); + byte[] byteOutput = output.getBytes(StandardCharsets.UTF_8); + System.out.println("Sending '" + output + "' of length " + byteOutput.length + " to Unity..."); + os.write(byteOutput); + } + + public static BigDecimal round(double d, int decimalPlace) { + BigDecimal bd = new BigDecimal(Double.toString(d)); + bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP); + return bd; + } +}