package at.acdp.urweb.web; import java.io.DataInputStream; import java.io.IOException; import java.net.Socket; import java.net.SocketException; import java.util.HashMap; import java.util.Map; public class GetRobotRealtimeData implements Runnable { private final String ip; private final int port; private volatile boolean _running=true; /***** * Creates a new RealTime reader * Default Constructor * Uses localhost (127.0.0.1) and port 30003 */ public GetRobotRealtimeData(String ip, int port) { this.ip=ip; this.port=port; } // Internal method that actually reads the data private void readSocket() throws IOException { try(Socket rt = new Socket(ip, port);){ rt.setSoTimeout(1000); if (rt.isConnected()){ System.out.println("Connected to UR Realtime Client"); } DataInputStream in = new DataInputStream(rt.getInputStream()); while(true) { int length = in.readInt(); double[] rtm = new double[length]; rtm[0] = length; // Calculate how much data is available from the length int data_available = (length - 4) / 8; for(int i=0; i jointStates = new HashMap(); jointStates.put((double) 236, "SHUTTING_DOWN"); jointStates.put((double) 237, "DUAL_CALIB_MODE"); jointStates.put((double) 238, "BACKDRIVE"); jointStates.put((double) 239, "POWER_OFF"); jointStates.put((double) 245, "NOT_RESPONDING"); jointStates.put((double) 246, "MOTOR_INIT"); jointStates.put((double) 247, "BOOTING"); jointStates.put((double) 248, "DUAL_CALIB_ERROR"); jointStates.put((double) 249, "BOOTLOADER"); jointStates.put((double) 250, "CALIBRATION_MODE"); jointStates.put((double) 252, "FAULT"); jointStates.put((double) 253, "RUNNING"); jointStates.put((double) 255, "IDLE"); String[] val = new String[RTinfo.jointmode.count()]; int i = 0; while (i < RTinfo.jointmode.count()){ // Read the code for given joint double code = rtm[RTinfo.jointmode.index()+i]; // Check if the key is known in the map if(jointStates.containsKey(code)){ // Read corresponding message val[i] = jointStates.get(code); } else{ // If unknown code, show "unknown" val[i] = "UNKNOWN_CODE"; } ++i; } return val; } /* * Example to read the actual safety limited speed scaling in percent */ public double getSpeedScaling(double[] rtm){ double scaling = rtm[RTinfo.speedscaling.index()]; return scaling * 100; // Converted to percent } }