FestoXRServer.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package at.acdp.urweb.web;
  2. import at.acdp.urweb.Main;
  3. import at.acdp.urweb.fhpp.Status;
  4. import at.acdp.urweb.rtde.RTDEClient;
  5. import com.nmote.xr.XRMethod;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.concurrent.ExecutionException;
  11. public class FestoXRServer {
  12. private final static Logger logger = LoggerFactory.getLogger(FestoXRServer.class);
  13. private final RTDEClient rtde;
  14. public FestoXRServer(RTDEClient r) {
  15. this.rtde=r;
  16. }
  17. @XRMethod(value = "example.helloWorld", help = "Returns 'Helo ' + argument")
  18. public static String hello(Object s) {
  19. return "Hello '" + s + "'";
  20. }
  21. @XRMethod(value = "ref2", help = "ref")
  22. public static String ref(String a, String b) {
  23. return "ref";
  24. }
  25. @XRMethod(value = "rel", help = "rel")
  26. public static String rel(int relPos, int speed) {
  27. try {
  28. logger.info("rel {}", relPos);
  29. int curPos=Main.fhm.readStatus().get().istPosition;
  30. int targetPos=curPos+relPos;
  31. logger.info("Relative by {} from {} to {} speed {}", relPos, curPos, targetPos, speed);
  32. Main.fhm.bereitschaft();
  33. if(!Main.fhm.readStatus().get().enabled) {
  34. logger.info("Controller not enabled");
  35. return "Controller not enabled";
  36. }
  37. Main.fhm.direktAuftrag(relPos, speed,0, true);
  38. Thread.sleep(10);
  39. Main.fhm.direktAuftrag(relPos, speed, 1, true);
  40. Thread.sleep(10);
  41. while(true) {
  42. boolean reached=checkPos(targetPos);
  43. if(reached) {
  44. logger.info("Reached {}", targetPos);
  45. return "OK";
  46. }
  47. Thread.sleep(100);
  48. }
  49. } catch (ExecutionException|InterruptedException e) {
  50. logger.warn("abs failed", e);
  51. return "abs failed: "+e.toString() ;
  52. }
  53. }
  54. @XRMethod(value = "abs", help = "abs")
  55. public static String abs(int targetPos, int speed) {
  56. try {
  57. logger.info("Absolute to {} speed {}", targetPos, speed);
  58. Main.fhm.bereitschaft();
  59. Main.fhm.direktAuftrag(targetPos, speed, 0, false);
  60. Thread.sleep(100);
  61. Main.fhm.direktAuftrag(targetPos, speed, 1, false);
  62. while(true) {
  63. boolean reached=checkPos(targetPos);
  64. if(reached) {
  65. logger.info("Reached {}", targetPos);
  66. return "OK";
  67. }
  68. Thread.sleep(100);
  69. }
  70. } catch (InterruptedException e) {
  71. logger.warn("abs failed", e);
  72. return "abs failed: "+e.toString();
  73. }
  74. }
  75. @XRMethod(value = "getpos", help = "getpos")
  76. public static int getpos() {
  77. logger.info("GetPos");
  78. try {
  79. return Main.fhm.readStatus().get().istPosition;
  80. } catch (InterruptedException e) {
  81. e.printStackTrace();
  82. } catch (ExecutionException e) {
  83. e.printStackTrace();
  84. }
  85. return -1;
  86. }
  87. @XRMethod(value = "ready", help = "ready")
  88. public static String ready() {
  89. try {
  90. logger.info("Ready called");
  91. Main.fhm.bereitschaft();
  92. for(int i=0;i<5;i++) {
  93. Status s=Main.fhm.readStatus().get();
  94. if(s.enabled) {
  95. return "OK";
  96. }
  97. logger.info("Controller not enabled.." + i);
  98. Thread.sleep(100);
  99. }
  100. } catch (InterruptedException|ExecutionException e) {
  101. logger.warn("ready failed", e);
  102. }
  103. return "Controller not enabled";
  104. }
  105. private static boolean checkPos(int pos) {
  106. try {
  107. Status s= Main.fhm.readStatus().get();
  108. int curPos = s.istPosition;
  109. logger.info("curpos {} ({})", curPos, pos);
  110. if(curPos==pos) {
  111. logger.info("reached {}", pos);
  112. return true;
  113. }
  114. if(s.istMoment==0 && Math.abs(curPos-pos) <5) {
  115. logger.info("reached {} (moment=0)", pos);
  116. return true;
  117. }
  118. Thread.sleep(500);
  119. } catch (ExecutionException|InterruptedException e) {
  120. logger.warn("checkPos failed", e);
  121. }
  122. return false;
  123. }
  124. @XRMethod(value = "status", help = "Returns status")
  125. public static Map<String, Object> status() {
  126. var x=Main.fhm.readStatus();
  127. try {
  128. if(x==null)
  129. return null;
  130. var ret= x.get();
  131. Map<String, Object> m=new HashMap<>();
  132. m.put("opm1", ret.opm1);
  133. m.put("opm2", ret.opm2);
  134. m.put("fct", ret.fct);
  135. m.put("rdyen", ret.rdyen);
  136. m.put("fault", ret.fault);
  137. m.put("warn", ret.warn);
  138. m.put("open", ret.open);
  139. m.put("enabled", ret.enabled);
  140. m.put("ref", ret.ref);
  141. m.put("still", ret.still);
  142. m.put("dev", ret.dev);
  143. m.put("mov", ret.mov);
  144. m.put("teach", ret.teach);
  145. m.put("mc", ret.mc);
  146. m.put("ack", ret.ack);
  147. m.put("halt", ret.halt);
  148. m.put("func", ret.func);
  149. m.put("fgrp", ret.fgrp);
  150. m.put("fnum", ret.fnum);
  151. m.put("com", ret.com);
  152. m.put("abs", ret.abs);
  153. m.put("istMoment", ret.istMoment);
  154. m.put("istPosition", ret.istPosition);
  155. m.put("halt", ret.halt);
  156. return m;
  157. } catch (Exception e) {
  158. e.printStackTrace();
  159. return null;
  160. }
  161. }
  162. }