RTDEClient.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package at.acdp.urweb.rtde;
  2. import net.openhft.chronicle.bytes.Bytes;
  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.nio.ByteOrder;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class RTDEClient implements Runnable {
  12. private final String ip;
  13. private final int port;
  14. private volatile boolean _running=true;
  15. private DataOutputStream dos;
  16. private DataInputStream dis;
  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. if (rt.isConnected()) {
  25. System.out.println("Connected to UR Realtime Client");
  26. }
  27. dis = new DataInputStream(rt.getInputStream());
  28. dos = new DataOutputStream(rt.getOutputStream());
  29. Thread readThread = new Thread(this);
  30. readThread.start();
  31. send_output_setup(List.of(OutParams.speed_scaling), List.of(), 125);
  32. }
  33. }
  34. // Internal method that actually reads the data
  35. private void readSocket() throws IOException {
  36. while(true) {
  37. int length = dis.readInt();
  38. double[] rtm = new double[length];
  39. rtm[0] = length;
  40. // Calculate how much data is available from the length
  41. int data_available = (length - 4) / 8;
  42. for(int i=0; i<data_available; i++){
  43. rtm[i] = dis.readDouble();
  44. }
  45. }
  46. }
  47. @Override
  48. public void run() {
  49. while(_running) {
  50. try {
  51. readSocket();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. public void send_input_setup(List<String> variables, List<String> types) {
  58. var cmd = CommandType.RTDE_CONTROL_PACKAGE_SETUP_INPUTS;
  59. var payload = String.join(",", variables);
  60. sendAndReceive(cmd, payload.getBytes());
  61. }
  62. private void sendAndReceive(CommandType cmd, byte[] payload) {
  63. sendall(cmd, payload);
  64. }
  65. public void send_output_setup(List<String> variables, List<String> types, int frequency) {
  66. var cmd = CommandType.RTDE_CONTROL_PACKAGE_SETUP_OUTPUTS;
  67. var payload = String.join(",", variables);
  68. byte[] p= payload.getBytes();
  69. ByteBuffer bytes = ByteBuffer.allocate(8+p.length);
  70. bytes.putDouble(frequency);
  71. bytes.put(p);
  72. sendAndReceive(cmd, bytes.array());
  73. }
  74. public void sendall(CommandType cmd, byte[] payload) {
  75. try {
  76. int size = 3 + payload.length;
  77. ByteBuffer bytes = ByteBuffer.allocate(size);
  78. bytes.order(ByteOrder.BIG_ENDIAN);
  79. bytes.order( ByteOrder.BIG_ENDIAN);
  80. bytes.putShort((short) size);
  81. bytes.put((byte) cmd.getVal());
  82. bytes.put(payload);
  83. bytes.rewind();
  84. byte[] arr = new byte[bytes.remaining()];
  85. bytes.get(arr);
  86. dos.write(arr);
  87. dos.flush();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }