Bladeren bron

kartesioninfo
kinematicsinfo

Martin Kunz 5 jaren geleden
bovenliggende
commit
b50e2e7716

+ 7 - 12
src/main/java/at/acdp/urweb/Main.java

@@ -8,23 +8,18 @@ import java.io.IOException;
 public class Main {
 
     public static void main(String [ ] args) {
+        ScReadThread rt=new ScReadThread();
 
-        try {
-            new WebServer(8080,true).start();
-            //GetRobotRealtimeData rd=new GetRobotRealtimeData("192.168.20.107", 30003);
-            SecondaryClient rde=new SecondaryClient("192.168.20.156", 30002);
-            rde.connect();
-            rde.writeCmd("set_digital_out(1,True)\n");
+        new WebServer(8080,true,rt).start();
 
+        Thread t=new Thread(rt);
+        t.start();
 
+        // rde.writeCmd("set_digital_out(2,True)\n");
+        // rde.writeCmd("movej([-1.95,-1.58,-1.16,-1.15,-1.55,1.25], a=1.0, v=0.1)\n");
+        //rde.writeCmd("freedrive_mode()\n");
 
-            // rde.writeCmd("set_digital_out(2,True)\n");
-            // rde.writeCmd("movej([-1.95,-1.58,-1.16,-1.15,-1.55,1.25], a=1.0, v=0.1)\n");
-            //rde.writeCmd("freedrive_mode()\n");
 
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
     }
 
 }

+ 37 - 0
src/main/java/at/acdp/urweb/ScReadThread.java

@@ -0,0 +1,37 @@
+package at.acdp.urweb;
+
+import at.acdp.urweb.sclient.SecondaryClient;
+import at.acdp.urweb.sclient.data.VersionMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+public class ScReadThread implements Runnable {
+    private static final Logger log = LoggerFactory.getLogger(SecondaryClient.class);
+
+    SecondaryClient rde;
+    @Override
+    public void run() {
+        while(true) {
+            try {
+                read();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    private void read() throws IOException {
+        rde=new SecondaryClient("192.168.20.156", 30002);
+        VersionMessage vm = rde.connect();
+        while(true) {
+            rde.readPackage();
+        }
+    }
+
+    public void writeCmd(String s) {
+        rde.writeCmd(s);
+
+    }
+}

+ 27 - 9
src/main/java/at/acdp/urweb/sclient/SecondaryClient.java

@@ -1,12 +1,16 @@
 package at.acdp.urweb.sclient;
 
 import at.acdp.urweb.sclient.data.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.*;
 import java.net.Socket;
 import java.nio.charset.StandardCharsets;
 
 public class SecondaryClient {
+    private static final Logger log = LoggerFactory.getLogger(SecondaryClient.class);
+
 
     private final String ip;
     private final int port;
@@ -20,20 +24,18 @@ public class SecondaryClient {
         this.port = port;
     }
 
-    public void connect() throws IOException {
+    public VersionMessage connect() throws IOException {
         this.rt = new Socket(ip, port);
         this.os = rt.getOutputStream();
         this.in = new DataInputStream(rt.getInputStream());
         VersionMessage vm = new VersionMessage();
         vm.readVersionMessage(in);
-        System.out.println(vm);
-        readReply(in);
+        return vm;
     }
 
-    private void readReply(DataInputStream di) throws IOException {
+    public void readReply(DataInputStream di) throws IOException {
 
         int size = di.readInt(); //4
-        System.out.println("size: " + size);
         int pType = di.readByte() & 0xff; //+1=5
         switch (pType) {
             case MessageType.ROBOT_MESSAGE:
@@ -50,6 +52,15 @@ public class SecondaryClient {
                 break;
             case PackageType.CARTESIAN_INFO:
                 readCartesianInfo(di, size);
+                break;
+            case PackageType.KINEMATICS_INFO:
+                readKinemsticsInfo(di, size);
+                break;
+
+            default:
+                log.warn("unknown ptype: "+pType+", size: "+size);
+                byte[] pack=new byte[size-5];
+                di.readFully(pack);
         }
     }
 
@@ -81,9 +92,14 @@ public class SecondaryClient {
 
     CartesianInfo readCartesianInfo(DataInputStream di, int size) throws IOException {
         CartesianInfo ci = new CartesianInfo();
-        //TODO: parse
+        ci.read(di);
         return ci;
+    }
 
+    KinematicsInfo readKinemsticsInfo(DataInputStream di, int size) throws IOException {
+        KinematicsInfo ki = new KinematicsInfo();
+        ki.read(di, size);
+        return ki;
     }
 
     ModeData readRobotModeData(DataInputStream di, int size) throws IOException {
@@ -125,11 +141,13 @@ public class SecondaryClient {
         try {
             System.out.println("send cmd:" + cmd);
             os.write(cmd.getBytes(StandardCharsets.UTF_8));
-            readReply(in);
-            readReply(in);
-            readReply(in);
+
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
+
+    public void readPackage() throws IOException {
+        readReply(in);
+    }
 }

+ 23 - 0
src/main/java/at/acdp/urweb/sclient/data/CartesianInfo.java

@@ -1,4 +1,27 @@
 package at.acdp.urweb.sclient.data;
 
+import java.io.DataInputStream;
+import java.io.IOException;
+
 public class CartesianInfo {
+    public double x,y,z;
+    public double rx,ry,rz;
+    public double tcpOffsetX,tcpOffsetY, tcpOffsetZ;
+    public double tcpOffsetRX,tcpOffsetRY, tcpOffsetRZ;
+
+
+    public void read(DataInputStream di) throws IOException {
+        x=di.readDouble();
+        y=di.readDouble();
+        z=di.readDouble();
+        rx=di.readDouble();
+        ry=di.readDouble();
+        rz=di.readDouble();
+        tcpOffsetX=di.readDouble();
+        tcpOffsetY=di.readDouble();
+        tcpOffsetZ=di.readDouble();
+        tcpOffsetRX=di.readDouble();
+        tcpOffsetRY=di.readDouble();
+        tcpOffsetRZ=di.readDouble();
+    }
 }

+ 13 - 0
src/main/java/at/acdp/urweb/sclient/data/KinematicsInfo.java

@@ -0,0 +1,13 @@
+package at.acdp.urweb.sclient.data;
+
+import java.io.DataInputStream;
+import java.io.IOException;
+
+public class KinematicsInfo {
+
+    public byte[] internalData;
+    public void read(DataInputStream di, int size) throws IOException {
+        internalData=new byte[size-5];
+        di.readFully(internalData);
+    }
+}

+ 18 - 2
src/main/java/at/acdp/urweb/web/WebServer.java

@@ -1,6 +1,8 @@
 package at.acdp.urweb.web;
 
 import java.nio.file.Paths;
+
+import at.acdp.urweb.ScReadThread;
 import io.undertow.Handlers;
 import io.undertow.Undertow;
 import io.undertow.server.HttpHandler;
@@ -13,9 +15,11 @@ import static io.undertow.Handlers.resource;
 public class WebServer {
     private final static org.slf4j.Logger logger = LoggerFactory.getLogger(WebServer.class);
     private final int port;
+    private final ScReadThread sc;
     private Undertow server;
 
-    public WebServer(int port, boolean debug) {
+    public WebServer(int port, boolean debug, ScReadThread sc) {
+        this.sc = sc;
         this.port = port;
     }
 
@@ -23,13 +27,25 @@ public class WebServer {
         Undertow.Builder builder = Undertow.builder();
         builder.addHttpListener(port, "0.0.0.0");
         builder.setHandler(Handlers.routing()
+                .post("/cmd", new HttpHandler() {
+                    @Override
+                    public void handleRequest(HttpServerExchange ex) throws Exception {
+                        if (ex.isInIoThread()) {
+                            ex.dispatch(this);
+                            return;
+                        }
+                        ex.startBlocking();
+                        byte[] bytes = ex.getInputStream().readAllBytes();
+                        sc.writeCmd(new String(bytes));
+                    }
+                })
                 .get("/test/{id}", ex -> {
                     System.out.println(ex.getQueryParameters().get("id").getFirst());
                 })
                 .get("/*", resource(new PathResourceManager(Paths.get("webroot"), 100))
                         .setDirectoryListingEnabled(true))
         );
-        server=builder.build();
+        server = builder.build();
         server.start();
     }
 }

+ 7 - 0
src/main/resources/webroot/index.html

@@ -1,3 +1,10 @@
 <html>
 jo!
+
+<form action="/cmd">
+    cmd:<br>
+    <input type="text" name="cmd" value=""><br>
+
+    <input type="submit" value="Submit">
+</form>
 </html>

+ 5 - 1
webroot/index.html

@@ -1,3 +1,7 @@
 <html>
-huhu
+<form action="/cmd" method="post">
+    cmd:<br>
+    <input type="text" name="cmd" value=""><br>
+    <input type="submit" value="Submit">
+</form>
 </html>