GetRobotRealtimeData.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package at.acdp.urweb.web;
  2. import java.io.DataInputStream;
  3. import java.io.IOException;
  4. import java.net.Socket;
  5. import java.net.SocketException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. public class GetRobotRealtimeData implements Runnable {
  9. private final String ip;
  10. private final int port;
  11. private volatile boolean _running=true;
  12. /*****
  13. * Creates a new RealTime reader
  14. * Default Constructor
  15. * Uses localhost (127.0.0.1) and port 30003
  16. */
  17. public GetRobotRealtimeData(String ip, int port) {
  18. this.ip=ip;
  19. this.port=port;
  20. }
  21. // Internal method that actually reads the data
  22. private void readSocket() throws IOException {
  23. try(Socket rt = new Socket(ip, port);){
  24. rt.setSoTimeout(1000);
  25. if (rt.isConnected()){
  26. System.out.println("Connected to UR Realtime Client");
  27. }
  28. DataInputStream in = new DataInputStream(rt.getInputStream());
  29. while(true) {
  30. int length = in.readInt();
  31. double[] rtm = new double[length];
  32. rtm[0] = length;
  33. // Calculate how much data is available from the length
  34. int data_available = (length - 4) / 8;
  35. for(int i=0; i<data_available; i++){
  36. rtm[i] = in.readDouble();
  37. }
  38. }
  39. }
  40. }
  41. @Override
  42. public void run() {
  43. while(_running) {
  44. try {
  45. readSocket();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51. /*
  52. * Creating enum to map start index and lengths of data entries
  53. * According to specification of RealTimeClient in Excel sheet
  54. */
  55. private enum RTinfo {
  56. // name (index in plot, number of doubles)
  57. q_target (2, 6),
  58. qd_target (8, 6),
  59. qdd_target (14, 6),
  60. q_actual (32, 6),
  61. qd_actual (38, 6),
  62. TCP_actual (56, 6),
  63. TCPd_actual (62, 6),
  64. TCP_force (68, 6),
  65. TCP_target (74, 6),
  66. TCPd_target (80, 6),
  67. temp_joint (87, 6),
  68. robotmode (95, 1),
  69. jointmode (96, 6),
  70. safetymode (97, 1),
  71. tcp_accel (109, 3),
  72. speedscaling (118, 1),
  73. prgstate (132, 1);
  74. // More data points could be added if desired, by following above example and according to specification.
  75. private final int index;
  76. private final int count;
  77. RTinfo(int index, int count){
  78. this.index = index;
  79. this.count = count;
  80. }
  81. private int index() {return index;}
  82. private int count() {return count;}
  83. }
  84. /*****************************************************************
  85. * Methods for returning the relevant data to the calling classes
  86. *****************************************************************/
  87. /*
  88. * Example get function, to read actual joint positions as double[]
  89. */
  90. public double[] getActualJointPose(double []rtm){
  91. double[] val = new double[RTinfo.q_actual.count()];
  92. int i = 0;
  93. while (i < RTinfo.q_actual.count()){
  94. val[i] = rtm[RTinfo.q_actual.index()+i];
  95. ++i;
  96. }
  97. return val;
  98. }
  99. /*
  100. * Example get function, to read actual TCP position as double[]
  101. */
  102. public double[] getActualTcpPose(double[] rtm){
  103. double[] val = new double[RTinfo.TCP_actual.count()];
  104. int i = 0;
  105. while (i < RTinfo.TCP_actual.count()){
  106. val[i] = rtm[RTinfo.TCP_actual.index()+i];
  107. ++i;
  108. }
  109. return val;
  110. }
  111. /*
  112. * Example get function, to read joint temperatures as double[]
  113. */
  114. public double[] getJointTemperatures(double[] rtm){
  115. double[] val = new double[RTinfo.temp_joint.count()];
  116. int i = 0;
  117. while (i < RTinfo.temp_joint.count()){
  118. val[i] = rtm[RTinfo.temp_joint.index()+i];
  119. ++i;
  120. }
  121. return val;
  122. }
  123. /*
  124. * Example to read joint state as useful information
  125. */
  126. public String[] getJointStatus(double[] rtm){
  127. // Create a map binding message code to state message
  128. // According to Excel sheet sclient interface specification
  129. Map<Double, String> jointStates = new HashMap<Double, String>();
  130. jointStates.put((double) 236, "SHUTTING_DOWN");
  131. jointStates.put((double) 237, "DUAL_CALIB_MODE");
  132. jointStates.put((double) 238, "BACKDRIVE");
  133. jointStates.put((double) 239, "POWER_OFF");
  134. jointStates.put((double) 245, "NOT_RESPONDING");
  135. jointStates.put((double) 246, "MOTOR_INIT");
  136. jointStates.put((double) 247, "BOOTING");
  137. jointStates.put((double) 248, "DUAL_CALIB_ERROR");
  138. jointStates.put((double) 249, "BOOTLOADER");
  139. jointStates.put((double) 250, "CALIBRATION_MODE");
  140. jointStates.put((double) 252, "FAULT");
  141. jointStates.put((double) 253, "RUNNING");
  142. jointStates.put((double) 255, "IDLE");
  143. String[] val = new String[RTinfo.jointmode.count()];
  144. int i = 0;
  145. while (i < RTinfo.jointmode.count()){
  146. // Read the code for given joint
  147. double code = rtm[RTinfo.jointmode.index()+i];
  148. // Check if the key is known in the map
  149. if(jointStates.containsKey(code)){
  150. // Read corresponding message
  151. val[i] = jointStates.get(code);
  152. }
  153. else{
  154. // If unknown code, show "unknown"
  155. val[i] = "UNKNOWN_CODE";
  156. }
  157. ++i;
  158. }
  159. return val;
  160. }
  161. /*
  162. * Example to read the actual safety limited speed scaling in percent
  163. */
  164. public double getSpeedScaling(double[] rtm){
  165. double scaling = rtm[RTinfo.speedscaling.index()];
  166. return scaling * 100; // Converted to percent
  167. }
  168. }