Revision 459
updated some colonet stuff
trunk/code/projects/colonet/ColonetGUI/Colonet.java | ||
---|---|---|
525 | 525 |
} |
526 | 526 |
} |
527 | 527 |
|
528 |
/** |
|
529 |
* Parses a String containing visual robot position information along with |
|
530 |
* canonical ID assignments. |
|
531 |
*/ |
|
532 |
public void parsePositions (String line) { |
|
533 |
String [] str = line.split(" "); |
|
534 |
java.util.List <RobotIcon> newList = new ArrayList <RobotIcon> (); |
|
535 |
|
|
536 |
for (int i = 2; i < str.length; i+=3) { |
|
537 |
int id = Integer.parseInt(str[i]); |
|
538 |
int x = Integer.parseInt(str[i+1]); |
|
539 |
int y = Integer.parseInt(str[i+2]); |
|
540 |
RobotIcon newIcon = new RobotIcon(x, y); |
|
541 |
newIcon.id = id; |
|
542 |
newList.add(newIcon); |
|
543 |
} |
|
544 |
|
|
545 |
robotIcons = newList; |
|
546 |
|
|
547 |
} |
|
548 |
|
|
549 |
|
|
528 | 550 |
// |
529 | 551 |
// MouseListener methods |
530 | 552 |
// |
... | ... | |
642 | 664 |
return; |
643 | 665 |
} |
644 | 666 |
// Assign new ID and update display |
667 |
csi.sendIDAssignment(curID, newID); |
|
645 | 668 |
robotIcons.get(selectedBot).id = newID; |
646 | 669 |
robotIcons.get(selectedBot).color = Color.GREEN; |
647 | 670 |
lblSelected.setText(" " + newID); |
... | ... | |
939 | 962 |
super(); |
940 | 963 |
} |
941 | 964 |
|
942 |
public synchronized void setImage (BufferedImage newimg) {
|
|
965 |
public void setImage (BufferedImage newimg) { |
|
943 | 966 |
if (img != null) { |
944 | 967 |
img.flush(); |
945 |
img = null; |
|
946 |
img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); |
|
947 |
txtMatrix.append("c"); |
|
948 | 968 |
} |
949 | 969 |
System.gc(); |
950 | 970 |
img = newimg; |
951 | 971 |
} |
952 | 972 |
|
953 |
public synchronized void setPoints (Point [] newpoints) {
|
|
973 |
public void setPoints (Point [] newpoints) { |
|
954 | 974 |
// test code -- need to remove later |
955 | 975 |
if (newpoints == null) { |
956 | 976 |
Point [] p = new Point [3]; |
... | ... | |
1027 | 1047 |
*/ |
1028 | 1048 |
class WebcamLoader extends Thread |
1029 | 1049 |
{ |
1030 |
final int WEBCAMLOADER_DELAY = 1000;
|
|
1050 |
final int WEBCAMLOADER_DELAY = 200;
|
|
1031 | 1051 |
final String IMAGE_PATH = "http://roboclub9.frc.ri.cmu.edu/colonet.jpg"; |
1032 |
final String LOCATIONS_PATH = "http://roboclub9.frc.ri.cmu.edu/colonet/locations.txt"; |
|
1033 |
|
|
1052 |
|
|
1034 | 1053 |
URL imagePath; |
1035 |
URI locationsPath; |
|
1036 | 1054 |
|
1037 | 1055 |
MediaTracker mt; |
1038 | 1056 |
BufferedImage image; |
... | ... | |
1047 | 1065 |
} catch (MalformedURLException e) { |
1048 | 1066 |
System.out.println("Malformed URL: could not form URL from: [" + IMAGE_PATH + "]\n"); |
1049 | 1067 |
} |
1050 |
try { |
|
1051 |
locationsPath = new URI(LOCATIONS_PATH); |
|
1052 |
} catch (URISyntaxException x) { |
|
1053 |
System.out.println("Malformed URI: could not form URI from: [" + LOCATIONS_PATH + "]\n"); |
|
1054 |
} |
|
1055 | 1068 |
|
1056 | 1069 |
} |
1057 | 1070 |
|
... | ... | |
1071 | 1084 |
mt.removeImage(image); |
1072 | 1085 |
// Save |
1073 | 1086 |
panelWebcam.setImage(image); |
1074 |
parseLocations(locationsPath.toURL()); |
|
1075 |
//panelWebcam.setPoints(null); //temporary simulation code |
|
1076 | 1087 |
} catch (InterruptedException e) { |
1077 | 1088 |
return; |
1078 | 1089 |
} catch (java.security.AccessControlException e) { |
... | ... | |
1084 | 1095 |
} |
1085 | 1096 |
} |
1086 | 1097 |
|
1087 |
private void parseLocations (URL url) { |
|
1088 |
URLConnection conn = null; |
|
1089 |
DataInputStream data = null; |
|
1090 |
String line; |
|
1091 |
String [] lines = new String[30]; |
|
1092 |
StringBuffer buf = new StringBuffer(); |
|
1093 |
int i = 0; |
|
1098 |
} |
|
1099 |
|
|
1100 |
/* |
|
1101 |
* PositionsLoader class |
|
1102 |
* Handles the loading of the robot positions as seen by the vision technology. |
|
1103 |
*/ |
|
1104 |
class PositionsLoader extends Thread |
|
1105 |
{ |
|
1106 |
|
|
1107 |
final int POSITIONSLOADER_DELAY = 600; |
|
1108 |
|
|
1109 |
public PositionsLoader () { |
|
1110 |
super("PositionsLoader"); |
|
1111 |
} |
|
1094 | 1112 |
|
1095 |
try { |
|
1096 |
conn = url.openConnection(); |
|
1097 |
conn.connect(); |
|
1098 |
data = new DataInputStream(new BufferedInputStream(conn.getInputStream())); |
|
1099 |
while ((line = data.readLine()) != null) { |
|
1100 |
buf.append(line + ";"); |
|
1101 |
lines[i] = line; |
|
1102 |
i++; |
|
1113 |
public void run () { |
|
1114 |
while (true) { |
|
1115 |
try { |
|
1116 |
Thread.sleep(POSITIONSLOADER_DELAY); |
|
1117 |
if (csi != null) |
|
1118 |
csi.sendPositionRequest(); |
|
1119 |
} catch (InterruptedException e) { |
|
1103 | 1120 |
} |
1104 |
data.close(); |
|
1105 |
} catch (IOException e) { |
|
1106 |
System.out.println("IOException:" + e.getMessage()); |
|
1107 | 1121 |
} |
1108 |
//log.append("Got robot locations: " + buf + "\n"); |
|
1109 |
|
|
1110 |
// Get Point values from strings |
|
1111 |
Point [] points = new Point[i]; |
|
1112 |
for (int j = 0; j < i; j++) { |
|
1113 |
String [] parts = lines[j].split(","); |
|
1114 |
int xval = Integer.parseInt(parts[0]); |
|
1115 |
int yval = Integer.parseInt(parts[1]); |
|
1116 |
Point p = new Point(xval, yval); |
|
1117 |
points[j] = p; |
|
1118 |
} |
|
1119 |
if (points.length != 0) |
|
1120 |
panelWebcam.setPoints(points); |
|
1121 |
|
|
1122 | 1122 |
} |
1123 |
|
|
1123 | 1124 |
} |
1124 | 1125 |
|
1125 | 1126 |
/* |
... | ... | |
1128 | 1129 |
*/ |
1129 | 1130 |
class RobotIcon { |
1130 | 1131 |
public final int RADIUS = 30; |
1131 |
public final int CLOSE = 70;
|
|
1132 |
public final int CLOSE = 80;
|
|
1132 | 1133 |
|
1133 | 1134 |
public int x, y; |
1134 | 1135 |
public int destx, desty; |
... | ... | |
1517 | 1518 |
g2d.drawString(level + "%", greenX + greenWidth/2 - 10, greenY + greenHeight/2 + 5); |
1518 | 1519 |
|
1519 | 1520 |
g2d.dispose(); |
1520 |
System.out.println("Painted icon"); |
|
1521 | 1521 |
} |
1522 | 1522 |
|
1523 | 1523 |
/** |
trunk/code/projects/colonet/ColonetGUI/ColonetServerInterface.java | ||
---|---|---|
66 | 66 |
public static final String COLONET_REQUEST = "14"; //0x0E |
67 | 67 |
public static final String CORONET_RESPONSE = "15"; //0x0F |
68 | 68 |
public static final String GLOBAL_DEST = "200"; |
69 |
public static final String CLIENT_REQUEST_ROBOT_POSITIONS = "86"; |
|
70 |
public static final String CLIENT_ASSIGN_ROBOT_ID = "87"; |
|
69 | 71 |
|
70 | 72 |
//Queue instructions |
71 | 73 |
public static final String COLONET_QUEUE = "100"; |
... | ... | |
289 | 291 |
String packet = ""; |
290 | 292 |
packet += ColonetServerInterface.REQUEST_FROM_SERVER; |
291 | 293 |
packet += " " + robotNumber; |
292 |
//packet += " " + ColonetServerInterface.COLONET_REQUEST; |
|
293 | 294 |
packet += " " + s; //add the command code here |
294 | 295 |
packet += "\n"; |
295 | 296 |
sendString(packet); |
... | ... | |
340 | 341 |
sendString(packet); |
341 | 342 |
} |
342 | 343 |
|
344 |
/** |
|
345 |
* Requests a list of all robot positions and correspondind robot IDs. |
|
346 |
*/ |
|
347 |
public void sendPositionRequest () { |
|
348 |
sendRequest(ColonetServerInterface.CLIENT_REQUEST_ROBOT_POSITIONS, ""); |
|
349 |
} |
|
350 |
|
|
351 |
/** |
|
352 |
* Send a robot ID assignment update to store on the server. |
|
353 |
*/ |
|
354 |
public void sendIDAssignment (int oldID, int newID) { |
|
355 |
String packet = ""; |
|
356 |
packet += ColonetServerInterface.REQUEST_FROM_SERVER; |
|
357 |
packet += " " + oldID + " " + newID; |
|
358 |
packet += "\n"; |
|
359 |
sendString(packet); |
|
360 |
txtMatrix.append("ID " + oldID + " => " + newID + "\n"); |
|
361 |
} |
|
362 |
|
|
363 |
|
|
343 | 364 |
/* |
344 | 365 |
* Queue management |
345 | 366 |
*/ |
... | ... | |
494 | 515 |
else if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + |
495 | 516 |
ColonetServerInterface.BATTERY)) |
496 | 517 |
colonet.parseBattery(line); |
518 |
// Robot Positions |
|
519 |
else if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + |
|
520 |
ColonetServerInterface.CLIENT_REQUEST_ROBOT_POSITIONS)) |
|
521 |
colonet.parsePositions(line); |
|
497 | 522 |
// Unknown type |
498 | 523 |
else |
499 | 524 |
System.out.println("Got data:" + line); |
Also available in: Unified diff