Browse Source

current evva deployment

Martin Kunz 2 years ago
parent
commit
15a8482533

+ 11 - 13
src/main/java/at/acdp/urweb/fhpp/FHMaster.java

@@ -52,12 +52,6 @@ public class FHMaster {
             }, Modbus.sharedExecutor());
         }
     }
-    public void prepare() {
-        byte[] sb=new byte[8];
-        sb[0]=0x00;
-        sb[1]=0x00;
-        send(sb, 4);
-    }
 
     private void send(byte[] bytes, int quantity) {
         CompletableFuture<ReadHoldingRegistersResponse> f =
@@ -65,7 +59,7 @@ public class FHMaster {
         f.join();
     }
 
-    public void direktAuftrag(int pos, int start, boolean relative) {
+    public void direktAuftrag(int pos, int speed, int start, boolean relative) {
         byte ccon=0x0;
         ccon|=(1<<0); // Enable drive
         ccon|=(1<<1); // !Stop
@@ -102,7 +96,7 @@ public class FHMaster {
         cdir|=(0<<6);   // Function Group Kurvenscheibenfunktion (0=nein)
         cdir|=(0<<7);   // 0 = normal; 1=kurvenscheibenfunktion
 
-        byte sbyte4=20; // Geschw, in % vom Basiswert (PNU540
+        byte sbyte4= (byte) speed; // Geschw, in % vom Basiswert (PNU540
         byte sbyte8= (byte)(pos& 0xff);
         byte sbyte7= (byte)((pos>>8) & 0xff);
         byte sbyte6= (byte)((pos>>16) & 0xff);
@@ -149,12 +143,16 @@ public class FHMaster {
         return  fc;
     }
 
-    private void bereitschaft(ModbusTcpMaster master) {
+    public void bereitschaft() {
         List<ModbusRequest> blist = new ArrayList<>();
-        blist.add(new WriteMultipleRegistersRequest(0,4, new byte[]{0x01,0,0,0,0,0,0,0}));
-        blist.add(new WriteMultipleRegistersRequest(0,4, new byte[]{0x11,0,0,0,0,0,0,0}));
-        blist.add(new WriteMultipleRegistersRequest(0,4, new byte[]{0x01,0,0,0,0,0,0,0}));
-        sendRequests(master, blist);
+        blist.add(new WriteMultipleRegistersRequest(0,4, new byte[]{0b01000111,0,0,0,0,0,0,0}));
+        try {
+            Thread.sleep(10);
+            sendRequests(master, blist);
+            Thread.sleep(10);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
     }
 
     public void stop() {

+ 8 - 0
src/main/java/at/acdp/urweb/fhpp/Status.java

@@ -28,6 +28,8 @@ public class Status {
     public int fnum; /** rückmeldung funktionsmodus */
     public int com; /** rückmeldung regelmodus */
     public boolean abs;
+    public int istMoment;
+    public int istPosition;
 
     @Override
     public String toString() {
@@ -52,6 +54,8 @@ public class Status {
                 .add("fnum=" + fnum)
                 .add("com=" + com)
                 .add("abs=" + abs)
+                .add(("istMoment=" + istMoment))
+                .add(("istPosition" + istPosition))
                 .toString();
     }
 
@@ -89,5 +93,9 @@ public class Status {
         fnum = (sdir >> 3) & 0x3;
         com = (sdir >> 1) & 0x3;
         abs = (sdir & (1 << 0)) > 0;
+
+        istMoment = status[3];
+        istPosition= ((status[4] &0xFF) << 24) | ((status[5] &0xFF) << 16) | ((status[6] &0xFF) << 8) | ((status[7] &0xFF));
+
     }
 }

+ 105 - 22
src/main/java/at/acdp/urweb/web/XRServer.java

@@ -5,6 +5,8 @@ import com.nmote.xr.XRMethod;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.concurrent.ExecutionException;
+
 public class XRServer {
     private final static Logger logger = LoggerFactory.getLogger(XRServer.class);
 
@@ -19,39 +21,120 @@ public class XRServer {
     }
 
     @XRMethod(value = "rel", help = "rel")
-    public static void rel(String pos) {
-        logger.info("Relative to {} Begin", pos);
-        System.out.println("rel: "+pos);
-        Main.fhm.direktAuftrag(Integer.parseInt(pos),0,true);
-        waitMC(true);
-        Main.fhm.direktAuftrag(Integer.parseInt(pos),1,true);
-        waitMC(true);
-        logger.info("Relative to {} End", pos);
+    public static boolean rel(String pos, String speedS) {
+        try {
+            logger.info("rel  {}", pos);
+            int curPos=Main.fhm.readStatus().get().istPosition;
+            int relPos=Integer.parseInt(pos);
+            int targetPos=curPos+relPos;
+            int speed=Integer.parseInt(speedS);
+            logger.info("Relative by {} from {} to {} speed {}", relPos, curPos, targetPos, speed);
+            Main.fhm.bereitschaft();
+            if(!Main.fhm.readStatus().get().enabled) {
+                logger.info("Controller not enabled");
+                return false;
+            }
+            Main.fhm.direktAuftrag(relPos, speed,0, true);
+            Thread.sleep(10);
+            Main.fhm.direktAuftrag(relPos, speed, 1, true);
+            Thread.sleep(10);
+
+
+            while(true) {
+                boolean reached=checkPos(targetPos);
+                if(reached) {
+                    logger.info("Reached {}", targetPos);
+                    return true;
+                }
+                try {
+                    Thread.sleep(100);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+        } catch (ExecutionException|InterruptedException e) {
+            logger.warn("abs failed", e);
+        }
+        return false;
     }
 
     @XRMethod(value = "abs", help = "abs")
-    public static void abs(String pos) {
-        logger.info("Absolute to {} Begin", pos);
-        Main.fhm.direktAuftrag(Integer.parseInt(pos),0, false);
-        waitMC(true);
-        Main.fhm.direktAuftrag(Integer.parseInt(pos),1, false);
-        waitMC(true);
-        logger.info("Absolute to {} End", pos);
-    }
+    public boolean abs(String pos, String speedS) {
+        try {
+
+            int targetPos=Integer.parseInt(pos);
+            int speed=Integer.parseInt(speedS);
+
+            logger.info("Absolute to {} speed {}", pos, speed);
+            Main.fhm.bereitschaft();
+            if(!Main.fhm.readStatus().get().enabled) {
+                logger.info("Controller not enabled");
+                return false;
+           }
+        Main.fhm.direktAuftrag(targetPos, speed, 0, false);
+        Thread.sleep(100);
+        Main.fhm.direktAuftrag(targetPos, speed, 1, false);
 
-    private static void waitMC(boolean what) {
-        logger.info("Waiting for MC");
         while(true) {
-            Status s = Main.fhm.readStatus().join();
-            if (s.mc == what) break;
+            boolean reached=checkPos(targetPos);
+            if(reached) {
+                logger.info("Reached {}", targetPos);
+                return true;
+            }
+            try {
+                Thread.sleep(100);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
         }
-        logger.info("Got MC");
+        } catch (ExecutionException|InterruptedException e) {
+            logger.warn("abs failed", e);
+        }
+        return false;
     }
 
-    private static void checkPos(int pos) {
+    @XRMethod(value = "getpos", help = "getpos")
+    public static int getpos() {
+        logger.info("GetPos");
+        try {
+            return Main.fhm.readStatus().get().istPosition;
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        } catch (ExecutionException e) {
+            e.printStackTrace();
+        }
+        return -1;
+    }
+
+    @XRMethod(value = "ready", help = "ready")
+    public static void ready() {
+        logger.info("Ready Begin");
+        Main.fhm.bereitschaft();
+        logger.info("Ready End");
+    }
+
+    private static boolean checkPos(int pos) {
+            try {
+                Status s= Main.fhm.readStatus().get();
+                int curPos = s.istPosition;
+
 
+                logger.info("curpos {} ({})", curPos, pos);
+                if(curPos==pos) {
+                    logger.info("reached {}", pos);
+                    return true;
+                }
+                Thread.sleep(500);
+            } catch (ExecutionException e) {
+                e.printStackTrace();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        return false;
     }
 
+
+
     @XRMethod(value = "status", help = "Returns status")
     public static String status() {
         var x=Main.fhm.readStatus();