RTDEClient.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package at.acdp.urweb.rtde;
  2. import org.slf4j.LoggerFactory;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.Socket;
  7. import java.nio.ByteBuffer;
  8. import java.util.List;
  9. import static at.acdp.urweb.rtde.CommandType.RTDE_GET_URCONTROL_VERSION;
  10. import static at.acdp.urweb.rtde.CommandType.RTDE_REQUEST_PROTOCOL_VERSION;
  11. public class RTDEClient implements Runnable {
  12. private final static org.slf4j.Logger logger = LoggerFactory.getLogger(RTDEClient.class);
  13. public static int RTDE_PROTOCOL_VERSION = 2;
  14. private final String ip;
  15. private final int port;
  16. private final boolean _running = true;
  17. private DataOutputStream dos;
  18. private DataInputStream dis;
  19. public RTDEClient(String ip, int port) {
  20. this.ip = ip;
  21. this.port = port;
  22. }
  23. public void start() throws IOException {
  24. try (Socket rt = new Socket(ip, port)) {
  25. rt.setSoTimeout(0);
  26. rt.setReuseAddress(true);
  27. rt.setTcpNoDelay(true);
  28. if (rt.isConnected()) {
  29. System.out.println("Connected to UR Realtime Client");
  30. }
  31. dos = new DataOutputStream(rt.getOutputStream());
  32. dis = new DataInputStream(rt.getInputStream());
  33. init();
  34. //Thread readThread = new Thread(this);
  35. //readThread.start();
  36. // send_output_setup(List.of(OutParams.timestamp), List.of(), 125);
  37. }
  38. }
  39. // Internal method that actually reads the data
  40. private void readSocket() throws IOException {
  41. while (true) {
  42. int length = dis.readInt();
  43. double[] rtm = new double[length];
  44. rtm[0] = length;
  45. // Calculate how much data is available from the length
  46. int data_available = (length - 4) / 8;
  47. for (int i = 0; i < data_available; i++) {
  48. rtm[i] = dis.readDouble();
  49. }
  50. }
  51. }
  52. private void receive(RtdeData cmd) throws IOException {
  53. logger.info(cmd.getType().toString());
  54. cmd.read(dis);
  55. }
  56. @Override
  57. public void run() {
  58. try {
  59. init();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. while (_running) {
  64. try {
  65. readSocket();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }
  71. public void send_input_setup(List<String> variables, List<String> types) throws IOException {
  72. var cmd = CommandType.RTDE_CONTROL_PACKAGE_SETUP_INPUTS;
  73. var payload = String.join(",", variables);
  74. var rpv = new RtdeRequestProtocolVersion();
  75. }
  76. private void sendAndReceive(RtdeData cmd) throws IOException {
  77. cmd.send(dos);
  78. cmd.read(dis);
  79. }
  80. public void send_output_setup(List<String> variables, List<String> types, int frequency) throws IOException {
  81. var payload = String.join(",", variables);
  82. byte[] p = payload.getBytes();
  83. ByteBuffer bytes = ByteBuffer.allocate(8 + p.length);
  84. bytes.putDouble(frequency);
  85. bytes.put(p);
  86. // sendAndReceive(rpv, bytes.array());
  87. }
  88. public void init() throws IOException {
  89. var rpv = new RtdeRequestProtocolVersion();
  90. sendAndReceive(rpv);
  91. var ruv = new RtdeRequestURVersion();
  92. sendAndReceive(ruv);
  93. }
  94. private interface RtdeData<T> {
  95. CommandType getType();
  96. int getSize();
  97. void setReplySize(int i);
  98. T read(DataInputStream di) throws IOException;
  99. T send(DataOutputStream dos) throws IOException;
  100. default void sendHeader(DataOutputStream dos) throws IOException {
  101. dos.writeShort(getSize());
  102. dos.writeByte(getType().getVal());
  103. }
  104. default void readHeader(DataInputStream dis) throws IOException {
  105. setReplySize(dis.readShort());
  106. int cmd = dis.readByte();
  107. if(cmd==getType().getVal())
  108. return;
  109. if (cmd==77) {
  110. int mLength=dis.readByte();
  111. byte[] mText = new byte[mLength];
  112. dis.readFully(mText);
  113. String m=new String(mText);
  114. logger.info(m);
  115. int sLength=dis.readByte();
  116. byte[] sText = new byte[sLength];
  117. dis.readFully(sText);
  118. String s=new String(sText);
  119. logger.info(s);
  120. int warning=dis.readByte();
  121. String w=String.valueOf(warning);
  122. } else throw new IOException(String.format("Expected %d, got %d", getType().getVal(), cmd));
  123. }
  124. }
  125. private class RtdeRequestURVersion implements RtdeData {
  126. private int replySize;
  127. private int major, minor, bugfix, build;
  128. @Override
  129. public CommandType getType() {
  130. return RTDE_GET_URCONTROL_VERSION;
  131. }
  132. @Override
  133. public int getSize() {
  134. return 3;
  135. }
  136. @Override
  137. public void setReplySize(int s) {
  138. replySize = s;
  139. }
  140. @Override
  141. public RtdeRequestURVersion read(DataInputStream di) throws IOException {
  142. readHeader(di);
  143. major=di.readInt();
  144. minor=di.readInt();
  145. bugfix=di.readInt();
  146. build=di.readInt();
  147. return this;
  148. }
  149. @Override
  150. public RtdeRequestURVersion send(DataOutputStream dos) throws IOException {
  151. sendHeader(dos);
  152. return this;
  153. }
  154. }
  155. private class RtdeRequestProtocolVersion implements RtdeData {
  156. public boolean success;
  157. private int replySize;
  158. @Override
  159. public CommandType getType() {
  160. return RTDE_REQUEST_PROTOCOL_VERSION;
  161. }
  162. @Override
  163. public int getSize() {
  164. return 5;
  165. }
  166. @Override
  167. public void setReplySize(int size) {
  168. this.replySize = size;
  169. }
  170. @Override
  171. public RtdeRequestProtocolVersion read(DataInputStream d) throws IOException {
  172. readHeader(dis);
  173. success = dis.readBoolean();
  174. return this;
  175. }
  176. @Override
  177. public RtdeRequestProtocolVersion send(DataOutputStream dos) throws IOException {
  178. sendHeader(dos);
  179. dos.writeShort(RTDE_PROTOCOL_VERSION);
  180. dos.flush();
  181. return this;
  182. }
  183. }
  184. }