Project

General

Profile

Revision 470

Added by Greg Tress over 14 years ago

more updates to colonet

View differences:

trunk/code/projects/colonet/ColonetGUI/Colonet.java
175 175
		txtInfo = new JTextArea();
176 176
		txtInfo.setBorder(BorderFactory.createTitledBorder("Info"));
177 177
		txtInfo.setEditable(false);
178
		txtHost = new JTextField("localhost");
178
		txtHost = new JTextField(this.getDocumentBase().getHost());
179 179
		txtHost.setBorder(BorderFactory.createTitledBorder("Host"));
180 180
		txtPort = new JTextField("10123");
181 181
		txtPort.setBorder(BorderFactory.createTitledBorder("Port"));
......
189 189
		panelConnect.add(txtHost);
190 190
		panelConnect.add(txtPort);
191 191
		panelConnect.add(btnConnect);
192
		panelConnect.add(btnGetXBeeIDs);
192
		//panelConnect.add(btnGetXBeeIDs);
193 193
		panelServerInterface = new JPanel();
194 194
		panelServerInterface.setLayout(new GridLayout(2,1));
195 195
		panelServerInterface.add(panelConnect);
......
437 437
			return;
438 438
		btnConnect.setEnabled(false);
439 439
		lblConnectionStatus.setText("Status: Connected");
440
		paintThread.start();
440
		//paintThread.start();
441 441
		dataUpdater.start();
442 442
		webcamLoader.start();
443 443
	}
......
537 537
			int id = Integer.parseInt(str[i]);
538 538
			int x = Integer.parseInt(str[i+1]);
539 539
			int y = Integer.parseInt(str[i+2]);
540
			RobotIcon newIcon = new RobotIcon(x, y);
541
			newIcon.id = id;
540
			RobotIcon newIcon = new RobotIcon(id, x, y);
542 541
			newList.add(newIcon);
543 542
		}
544 543
		
......
564 563
	    if (setWaypoint && setWaypointID  >= 0) {
565 564
	        setWaypoint = false;
566 565
	        panelWebcam.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
567
	        if (selectedBot < 0 || robotIcons.get(selectedBot).id < 0) 
566
	        if (selectedBot < 0)
568 567
	            return;
569 568
	        
570 569
	        RobotIcon r = robotIcons.get(selectedBot);
571 570
	        r.destx = e.getX();
572 571
	        r.desty = e.getY();
572
	        
573
	        csi.sendAbsoluteMove(r.id, r.destx, r.desty);
574
	        
573 575
	        return;
574 576
	    }
575 577
	    
......
579 581
	        panelWebcam.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
580 582
	        for (int i = 0; i < robotIcons.size(); i++) {
581 583
	            RobotIcon r = robotIcons.get(i);
582
	            if (r.id < 0)
583
	                continue;
584 584
	            r.destx = e.getX();
585 585
	            r.desty = e.getY();
586 586
	        }
......
592 592
		    RobotIcon r = robotIcons.get(i);
593 593
		    if (r.contains(e.getX(), e.getY())) {
594 594
		        selectedBot = i;
595
		        if (r.id < 0)
596
		            lblSelected.setText("Unidentified");
597
		        else
598
		            lblSelected.setText(" " + r.id);
595
		        lblSelected.setText(" " + r.id);
599 596
		    }
600 597
		}
601 598
		repaint();
......
906 903
				try {
907 904
					//request more data
908 905
					if (csi != null && csi.isReady()) {
909
						//csi.sendSensorDataRequest();
906
                        csi.sendPositionRequest();
910 907
						csi.sendXBeeIDRequest();
911 908
						if (cmbRobotNum.getSelectedIndex() > 0) {
912 909
						    String sel = (String) cmbRobotNum.getSelectedItem();
913 910
						    int num = Integer.parseInt(sel);
914 911
							csi.sendBatteryRequest(num);
915
						} else {
916
							csi.sendBatteryRequest(200);
917 912
						}
918 913
					}
919 914
					Thread.sleep(DATAUPDATER_DELAY);
......
970 965
			img = newimg;
971 966
		}
972 967
		
973
		public void setPoints (Point [] newpoints) {
974
			// test code -- need to remove later
975
			if (newpoints == null) {
976
				Point [] p = new Point [3];
977
				p[0] = new Point(200,200);
978
				p[1] = new Point(100,100);
979
				p[2] = new Point(400,300);
980
				setPoints(p);
968
				
969
		public void paint (Graphics g) {
970
			
971
			if (img == null)
981 972
				return;
982
			}
973
			// Place the image on the screen, inside the panel
983 974
			
984
			for (int i = 0; i < newpoints.length; i++) {
985
				Point p = newpoints[i];
986
				boolean found = false;
987
				for (int j = 0; j < robotIcons.size(); j++) {
988
					RobotIcon r = robotIcons.get(j);
989
					if (r.isClose(p.x, p.y)) {
990
						r.move(p.x, p.y);
991
						found = true;
992
					}
993
				}
994
				if (!found) {
995
					RobotIcon r = new RobotIcon(p.x, p.y);
996
					robotIcons.add(r);
997
				}
975
			int maxWidth = getWidth() - 2*BORDER;
976
			int maxHeight = getHeight() - 2*BORDER;
977
			double widthRatio = 1.0 * maxWidth / img.getWidth();
978
			double heightRatio = 1.0 * maxHeight / img.getHeight();
979
			int newWidth = 0;
980
			int newHeight = 0;
981
			int x = 0;
982
			int y = 0;
983
			
984
			if (widthRatio > heightRatio) {  //height is the limiting factor
985
			    newHeight = maxHeight;
986
			    newWidth = img.getWidth() * maxHeight / img.getHeight();
987
			    y = BORDER;
988
			    x = (maxWidth - newWidth) / 2 + BORDER;
989
			} else {  //width is the limiting factor
990
			    newWidth = maxWidth;
991
			    newHeight = img.getHeight() * maxWidth / img.getWidth();
992
			    x = BORDER;
993
			    y = (maxHeight - newHeight) / 2 + BORDER;
998 994
			}
999 995
			
1000
		}
1001
		
1002
		public synchronized void paint (Graphics g) {
996
			Image imgScaled = img.getScaledInstance(newWidth, newHeight, Image.SCALE_FAST);
997
			g.drawImage(imgScaled, x, y, this);
1003 998
			
1004
			if (img == null)
1005
				return;
1006
			// Place the image on the screen, inside the panel
1007
			g.drawImage(img, 
1008
						BORDER,	//dx1 - the x coordinate of the first corner of the destination rectangle.
1009
						BORDER,	//dy1 - the y coordinate of the first corner of the destination rectangle.
1010
						this.getWidth() - BORDER, 	//dx2 - the x coordinate of the second corner of the destination rectangle.
1011
						this.getHeight() - BORDER,	//dy2 - the y coordinate of the second corner of the destination rectangle.
1012
						0,	//sx1 - the x coordinate of the first corner of the source rectangle.
1013
						0,	//sy1 - the y coordinate of the first corner of the source rectangle.
1014
						image.getWidth(),	//sx2 - the x coordinate of the second corner of the source rectangle.
1015
						image.getHeight(),	//sy2 - the y coordinate of the second corner of the source rectangle.
1016
						null	//observer - object to be notified as more of the image is scaled and converted.
1017
						);
1018 999
						
1019 1000
			// Draw Identifiers
1020 1001
			if (robotIcons == null)
1021 1002
				return;
1003
			
1022 1004
			((Graphics2D)g).setStroke(new BasicStroke(2));
1023 1005
			for (int i = 0; i < robotIcons.size(); i++) {
1024 1006
				RobotIcon r = robotIcons.get(i);
1025 1007
				g.setColor(r.color);
1008
				if (widthRatio > heightRatio) {
1009
				    
1010
				
1011
				} else {
1012
				    
1013
				}
1026 1014
				g.drawOval(r.x-RADIUS, r.y-RADIUS, 2*r.RADIUS, 2*r.RADIUS);
1027 1015
				// If the robot has a destination, draw the vector
1028 1016
				if (r.destx >= 0) {
......
1047 1035
	*/
1048 1036
	class WebcamLoader extends Thread 
1049 1037
	{
1050
		final int WEBCAMLOADER_DELAY = 200;
1038
		final int WEBCAMLOADER_DELAY = 400;
1051 1039
		final String IMAGE_PATH = "http://roboclub9.frc.ri.cmu.edu/colonet.jpg";
1052 1040
				
1053 1041
		URL imagePath;
......
1068 1056
			
1069 1057
		}
1070 1058
		
1071
		public synchronized void run ()
1059
		public void run ()
1072 1060
		{
1073 1061
			while (true) {
1074 1062
				try {
......
1098 1086
	}
1099 1087
	
1100 1088
	/*
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
		}
1112
		
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) {
1120
				}
1121
			}
1122
		}
1123
	
1124
	}
1125
	
1126
	/*
1127 1089
	*  RobotIcon class
1128 1090
	*  Provides a means for graphically representing and keeping track of webcam bots.
1129 1091
	*/
......
1136 1098
		public int id;
1137 1099
		public Color color;
1138 1100
		
1139
		public RobotIcon (int x, int y) {
1140
			this.id = -1;
1101
		public RobotIcon (int id, int x, int y) {
1141 1102
			this.color = Color.RED;
1142 1103
			this.x = x;
1143 1104
			this.y = y;
1105
			this.id = id;
1144 1106
			this.destx = -1;
1145 1107
			this.desty = -1;
1146 1108
		}
trunk/code/projects/colonet/ColonetGUI/ColonetServerInterface.java
68 68
	public static final String GLOBAL_DEST = "200";
69 69
	public static final String CLIENT_REQUEST_ROBOT_POSITIONS = "86";
70 70
	public static final String CLIENT_ASSIGN_ROBOT_ID = "87";
71
	public static final String MOVE_TO_ABSOLUTE_POSITION = "83"; //0x53
71 72
	
72 73
	//Queue instructions
73 74
	public static final String COLONET_QUEUE = "100";
......
271 272
		String packet = "";
272 273
		packet += ColonetServerInterface.SEND_TO_ROBOT;
273 274
		if (robotNumber != null)
274
			packet += " " + robotNumber;
275
			packet += " " + 7;  //robotNumber;
275 276
		packet += " " + ColonetServerInterface.COLONET_COMMAND;
276 277
		packet += " " + s;  //add  the command code here
277 278
		packet += "\n";
278 279
		sendString(packet);
280
		txtMatrix.append("S:" + packet);
279 281
	}
280 282
	
281 283
	/**
......
359 361
		sendString(packet);
360 362
		txtMatrix.append("ID " + oldID + " => " + newID + "\n");
361 363
	}
362

  
363 364
	
365
	/** 
366
	* Order a robot to move to an absolute coordinate point.
367
	*/
368
	public void sendAbsoluteMove (int id, int x, int y) {
369
	    sendData(MOVE_TO_ABSOLUTE_POSITION + " " + x + " " + y, "" + id);
370
	}
371
	
364 372
	/*
365 373
	*	Queue management
366 374
	*/
......
449 457
	 * @param text Text to display
450 458
	 */
451 459
	public void msg (String text) {
452
		JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.INFORMATION_MESSAGE);
460
		JOptionPane.showMessageDialog(colonet, text, "Colonet", JOptionPane.INFORMATION_MESSAGE);
453 461
	}
454 462
	
455 463
	/**
......
457 465
	 * @param text Text to display
458 466
	 */
459 467
	public void warn (String text) {
460
		JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.WARNING_MESSAGE);
468
		JOptionPane.showMessageDialog(colonet, text, "Colonet", JOptionPane.WARNING_MESSAGE);
461 469
	}
462 470
	
463 471
	/**
......
465 473
	 * @param text Text to display
466 474
	 */
467 475
	public void err (String text) {
468
		JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.ERROR_MESSAGE);
476
		JOptionPane.showMessageDialog(colonet, text, "Colonet", JOptionPane.ERROR_MESSAGE);
469 477
	}
470 478
	
471 479
		
......
521 529
				colonet.parsePositions(line);
522 530
			// Unknown type
523 531
			else
524
				System.out.println("Got data:" + line);
532
				txtMatrix.setText("Got unknown data: " + line + "\n");
525 533
				
526 534
		}
527 535

  

Also available in: Unified diff