RTDEClient.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package at.acdp.urweb.rtde;
  2. import org.slf4j.LoggerFactory;
  3. import java.io.*;
  4. import java.net.Socket;
  5. import java.nio.ByteBuffer;
  6. import java.nio.ByteOrder;
  7. import java.util.List;
  8. import static at.acdp.urweb.rtde.CommandType.RTDE_REQUEST_PROTOCOL_VERSION;
  9. public class RTDEClient implements Runnable {
  10. private final static org.slf4j.Logger logger = LoggerFactory.getLogger(RTDEClient.class);
  11. private final String ip;
  12. private final int port;
  13. private volatile boolean _running=true;
  14. private DataOutputStream dos;
  15. private DataInputStream dis;
  16. public static int RTDE_PROTOCOL_VERSION=2;
  17. public RTDEClient(String ip, int port) {
  18. this.ip=ip;
  19. this.port=port;
  20. }
  21. public void start() throws IOException {
  22. try (Socket rt = new Socket(ip, port);) {
  23. rt.setSoTimeout(0);
  24. rt.setReuseAddress(true);
  25. rt.setTcpNoDelay(true);
  26. if (rt.isConnected()) {
  27. System.out.println("Connected to UR Realtime Client");
  28. }
  29. dos = new DataOutputStream(rt.getOutputStream());
  30. dis = new DataInputStream(rt.getInputStream());
  31. negotiate_protocol_version();
  32. //Thread readThread = new Thread(this);
  33. //readThread.start();
  34. // send_output_setup(List.of(OutParams.timestamp), List.of(), 125);
  35. }
  36. }
  37. // Internal method that actually reads the data
  38. private void readSocket() throws IOException {
  39. while(true) {
  40. int length = dis.readInt();
  41. double[] rtm = new double[length];
  42. rtm[0] = length;
  43. // Calculate how much data is available from the length
  44. int data_available = (length - 4) / 8;
  45. for(int i=0; i<data_available; i++){
  46. rtm[i] = dis.readDouble();
  47. }
  48. }
  49. }
  50. private interface RtdeData<T> {
  51. public CommandType getType();
  52. public T read(DataInputStream di) throws IOException;
  53. public T send(DataOutputStream dos) throws IOException;
  54. }
  55. private class RtdeRequestProtocolVersion implements RtdeData{
  56. public int major, minor, bugfix, build;
  57. int size=5;
  58. @Override
  59. public CommandType getType() {
  60. return RTDE_REQUEST_PROTOCOL_VERSION;
  61. }
  62. @Override
  63. public RtdeRequestProtocolVersion read(DataInputStream d) throws IOException {
  64. major = d.readInt();
  65. minor = d.readInt();
  66. bugfix = d.readInt();
  67. build = d.readInt();
  68. return this;
  69. }
  70. @Override
  71. public RtdeRequestProtocolVersion send(DataOutputStream dos) throws IOException {
  72. dos.writeByte(5);
  73. dos.writeShort(getType().getVal());
  74. dos.writeShort(RTDE_PROTOCOL_VERSION);
  75. dos.flush();
  76. return this;
  77. }
  78. }
  79. private void receive(RtdeData cmd) throws IOException {
  80. logger.info(cmd.getType().toString());
  81. cmd.read(dis);
  82. }
  83. @Override
  84. public void run() {
  85. try {
  86. negotiate_protocol_version();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. while(_running) {
  91. try {
  92. readSocket();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. public void send_input_setup(List<String> variables, List<String> types) throws IOException {
  99. var cmd = CommandType.RTDE_CONTROL_PACKAGE_SETUP_INPUTS;
  100. var payload = String.join(",", variables);
  101. var rpv = new RtdeRequestProtocolVersion();
  102. }
  103. private void sendAndReceive(RtdeData cmd) throws IOException {
  104. cmd.send(dos);
  105. cmd.read(dis);
  106. }
  107. public void send_output_setup(List<String> variables, List<String> types, int frequency) throws IOException {
  108. var payload = String.join(",", variables);
  109. byte[] p= payload.getBytes();
  110. ByteBuffer bytes = ByteBuffer.allocate(8+p.length);
  111. bytes.putDouble(frequency);
  112. bytes.put(p);
  113. // sendAndReceive(rpv, bytes.array());
  114. }
  115. public void negotiate_protocol_version() throws IOException {
  116. var rpv = new RtdeRequestProtocolVersion();
  117. sendAndReceive(rpv);
  118. }
  119. }