RTDEClient.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.util.ArrayList;
  9. import java.util.List;
  10. public class RTDEClient implements Runnable {
  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 RTDEClient(String ip, int port) {
  17. this.ip=ip;
  18. this.port=port;
  19. }
  20. public void start() {
  21. Thread readThread = new Thread(this);
  22. readThread.start();
  23. send_output_setup(List.of(OutParams.speed_scaling),List.of(),125);
  24. }
  25. // Internal method that actually reads the data
  26. private void readSocket() throws IOException {
  27. try(Socket rt = new Socket(ip, port);){
  28. rt.setSoTimeout(0);
  29. if (rt.isConnected()){
  30. System.out.println("Connected to UR Realtime Client");
  31. }
  32. dis = new DataInputStream(rt.getInputStream());
  33. dos = new DataOutputStream(rt.getOutputStream());
  34. while(true) {
  35. int length = dis.readInt();
  36. double[] rtm = new double[length];
  37. rtm[0] = length;
  38. // Calculate how much data is available from the length
  39. int data_available = (length - 4) / 8;
  40. for(int i=0; i<data_available; i++){
  41. rtm[i] = dis.readDouble();
  42. }
  43. }
  44. }
  45. }
  46. @Override
  47. public void run() {
  48. while(_running) {
  49. try {
  50. readSocket();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. public void send_input_setup(List<String> variables, List<String> types) {
  57. var cmd = CommandType.RTDE_CONTROL_PACKAGE_SETUP_INPUTS;
  58. var payload = String.join(",", variables);
  59. sendAndReceive(cmd, payload.getBytes());
  60. }
  61. private void sendAndReceive(CommandType cmd, byte[] payload) {
  62. sendall(cmd, payload);
  63. }
  64. public void send_output_setup(List<String> variables, List<String> types, int frequency) {
  65. Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(64);
  66. bytes.writeDouble(frequency);
  67. var cmd = CommandType.RTDE_CONTROL_PACKAGE_SETUP_OUTPUTS;
  68. var payload = String.join(",", variables);
  69. sendAndReceive(cmd, payload.getBytes());
  70. }
  71. public void sendall(CommandType cmd, byte[] payload) {
  72. try {
  73. Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(64);
  74. int size = 3 + payload.length;
  75. bytes.writeUnsignedShort(size);
  76. bytes.writeUnsignedByte(cmd.getVal());
  77. bytes.write(payload);
  78. dos.write(bytes.toByteArray());
  79. dos.flush();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. }