FestoXRServer.java 6.7 KB

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