Project

General

Profile

Revision 702

Added by Greg Tress about 16 years ago

Added structure to send charge commands; need to implement robot side

View differences:

Colonet.java
30 30
	DataUpdater dataUpdater;
31 31

  
32 32
	// Control
33
	JPanel panelControl;
34 33
	JTabbedPane tabPaneControl;
35 34
	JPanel panelRobotControl;
36 35
	JPanel panelRobotDirection;
......
44 43
	VectorController vectorController;
45 44
	BufferedImage imageVectorControl;
46 45
	JButton btnAssignID;
46
	JButton btnLocateStation;
47
	boolean setStation;
48
	ChargingStation station;
47 49
	boolean setWaypoint;
48 50
	int setWaypointID;
49 51
  JButton btnSetBounds;
......
149 151
		panelConnect.add(txtHost);
150 152
		panelConnect.add(txtPort);
151 153
		panelConnect.add(btnConnect);
152
		//panelConnect.add(btnGetXBeeIDs);
153 154
		panelServerInterface = new JPanel();
154 155
		panelServerInterface.setLayout(new GridLayout(2,1));
155 156
		panelServerInterface.add(panelConnect);
......
192 193
		lblBattery = new JLabel(batteryIcon);
193 194
		lblSelected = new JLabel("None");
194 195
		// Management subset
196
		setStation = false;
195 197
		setWaypoint = false;
196 198
		setWaypointID = -1;
197 199
		btnAssignID = new JButton("Assign ID");
200
		btnLocateStation = new JButton("Locate Charge Station");
198 201
    boundary = new RobotBoundary();
199 202
    btnSetBounds = new JButton("Set Boundary");
200 203
    btnClearBounds = new JButton("Clear Boundary");
......
210 213
		panelRobotCommands.add(new JLabel("Selected Icon: "));
211 214
		panelRobotCommands.add(lblSelected);
212 215
		panelRobotCommands.add(btnAssignID);
213
    panelRobotCommands.add(new JLabel(" "));
216
    panelRobotCommands.add(btnLocateStation);
214 217
		panelRobotCommands.add(btnSetBounds);
215 218
    panelRobotCommands.add(btnClearBounds);
216 219
		panelRobotCommands.add(btnCommand_MoveTo);
......
253 256
		taskAddWindow = new TaskAddWindow();
254 257

  
255 258
		// Main control mechanism
256
		panelControl = new JPanel();
257
		panelControl.setLayout(new GridLayout(1,1));
258 259
		tabPaneControl = new JTabbedPane(JTabbedPane.TOP);
259 260
		tabPaneControl.setFont(new Font("arial", Font.PLAIN, 16));
260 261
		tabPaneControl.setPreferredSize(new Dimension(ColonetConstants.VECTOR_CONTROLLER_WIDTH, 0));
261 262
		tabPaneControl.addTab("Connection", panelServerInterface);
262 263
		tabPaneControl.addTab("Robots", panelRobotControl);
263 264
		//tabPaneControl.addTab("Tasks", panelTaskManager);
264
		panelControl.add(tabPaneControl);
265 265

  
266

  
267 266
		// Put all elements in the ContentPane
268 267
		this.getContentPane().setLayout(new BorderLayout());
269 268
		this.getContentPane().add(tabPaneMain, BorderLayout.CENTER);
270
		this.getContentPane().add(panelControl, BorderLayout.EAST);
269
		this.getContentPane().add(tabPaneControl, BorderLayout.EAST);
271 270
		this.setVisible(true);
272 271
		
273 272
		// Disable components before connecting
274 273
		btnConnect.setText("Connect");
275 274
    lblConnectionStatus.setText("Status: Disconnected");
276
    btnF.setEnabled(false);
277
    btnB.setEnabled(false);
278
    btnL.setEnabled(false);
279
    btnR.setEnabled(false);
280
    btnActivate.setEnabled(false);
281
    btnAssignID.setEnabled(false);
282
    btnSetBounds.setEnabled(false);
283
    btnClearBounds.setEnabled(false);
284
    btnCommand_MoveTo.setEnabled(false);
285
    btnCommand_MoveAll.setEnabled(false);
275
    setComponentsEnabled(false);
286 276

  
287 277
		/* Add all listeners here */
288 278
		// Task Management
......
312 302
		btnConnect.addActionListener(this);
313 303
		btnGetXBeeIDs.addActionListener(this);
314 304
		btnAssignID.addActionListener(this);
305
		btnLocateStation.addActionListener(this);
315 306
    btnSetBounds.addActionListener(this);
316 307
    btnClearBounds.addActionListener(this);
317 308
		panelWebcam.addMouseListener(this);
......
397 388
      public void run () {
398 389
        btnConnect.setText("Disconnect");
399 390
        lblConnectionStatus.setText("Status: Connected");
400
        btnF.setEnabled(true);
401
        btnB.setEnabled(true);
402
        btnL.setEnabled(true);
403
        btnR.setEnabled(true);
404
        btnActivate.setEnabled(true);
405
        btnAssignID.setEnabled(true);
406
        btnSetBounds.setEnabled(true);
407
        btnClearBounds.setEnabled(true);
408
        btnCommand_MoveTo.setEnabled(true);
409
        btnCommand_MoveAll.setEnabled(true);
391
        setComponentsEnabled(true);
410 392
      }
411 393
    };
412 394
    SwingUtilities.invokeLater(r);
413 395
	}
414 396

  
415 397
	public void disconnect () {
416
        try {
417
            dataUpdater.interrupt();
418
        } catch (Exception e) {
419
        }
420
        csi = null;
398
    try {
399
        dataUpdater.interrupt();
400
    } catch (Exception e) {
401
    }
402
    csi = null;
421 403
		Runnable r = new Runnable() {
422
	        public void run () {
423
		        btnConnect.setText("Connect");
424
	            lblConnectionStatus.setText("Status: Disconnected");
425
              btnF.setEnabled(false);
426
              btnB.setEnabled(false);
427
              btnL.setEnabled(false);
428
              btnR.setEnabled(false);
429
              btnActivate.setEnabled(false);
430
              btnAssignID.setEnabled(false);
431
              btnSetBounds.setEnabled(false);
432
              btnClearBounds.setEnabled(false);
433
              btnCommand_MoveTo.setEnabled(false);
434
              btnCommand_MoveAll.setEnabled(false);
435
		    }
404
      public void run () {
405
		    btnConnect.setText("Connect");
406
	      lblConnectionStatus.setText("Status: Disconnected");
407
        setComponentsEnabled(false);
408
	    }
436 409
		};
437 410
		SwingUtilities.invokeLater(r);
438 411
	}
412
	
413
	private void setComponentsEnabled (boolean enabled) {
414
    btnF.setEnabled(enabled);
415
    btnB.setEnabled(enabled);
416
    btnL.setEnabled(enabled);
417
    btnR.setEnabled(enabled);
418
    btnActivate.setEnabled(enabled);
419
    btnAssignID.setEnabled(enabled);
420
    btnLocateStation.setEnabled(enabled);
421
    btnSetBounds.setEnabled(enabled);
422
    btnClearBounds.setEnabled(enabled);
423
    btnCommand_MoveTo.setEnabled(enabled);
424
    btnCommand_MoveAll.setEnabled(enabled);
425
	}
439 426

  
440 427
	/**
441 428
	* Parses a String containing a task queue update.
......
637 624
				}
638 625
				return;
639 626
			}
627
			
628
			// If we are ordering a charge (right-click on a station)
629
			if ((e.getButton() == MouseEvent.BUTTON2 || e.getButton() == MouseEvent.BUTTON3)  
630
          && e.getID() == MouseEvent.MOUSE_PRESSED
631
          && station != null && station.contains(e.getX(), e.getY())) {
632
        //TODO: send charge command
633
				return;
634
			}
640 635

  
641 636
			// Right-click also means we are moving a robot
642 637
			if ((e.getButton() == MouseEvent.BUTTON2 || e.getButton() == MouseEvent.BUTTON3)  
......
677 672
			}
678 673
      
679 674
      // If we are drawing a boundary rectangle
680
      if (boundary.set) {
675
      if (boundary != null && boundary.isSetting()) {
681 676
        // Begin 
682 677
        if (e.getID() == MouseEvent.MOUSE_PRESSED) {
683 678
          boundary.panel_p1 = new Point(e.getX(), e.getY());
......
699 694
        }
700 695
        return;
701 696
      }
697
      
698
      // If we are locating a charging station
699
      if (setStation) {
700
        setStation = false;
701
        station = new ChargingStation(e.getX(), e.getY());
702
        station.setActive(true);
703
        return;
704
      }
702 705

  
703 706
			// Otherwise, we are selecting a bot, or doing nothing
704 707
			RobotIcon r = robotIcons.getBoundingIcon(pt);
......
780 783
				}
781 784
				selectedBot = newID;
782 785
				lblSelected.setText("" + newID);
786
		  } else if (source == btnLocateStation) {
787
		    setStation = true;
783 788
			} else if (source == btnF) { // Robot Movement Controls
784 789
				vectorController.setMaxForward();
785 790
				vectorController.sendToServer();
......
963 968
			bufferedGraphics.drawImage(imgScaled, x, y, this);
964 969

  
965 970
      // Draw boundary
966
      if (boundary.set || boundary.active) {
971
      if (boundary.isSetting() || boundary.isActive()) {
967 972
         bufferedGraphics.setColor(Color.BLUE);
968 973
         int width = boundary.panel_p2.x - boundary.panel_p1.x;
969 974
         int height = boundary.panel_p2.y - boundary.panel_p1.y;
970 975
         bufferedGraphics.drawRect(boundary.panel_p1.x, boundary.panel_p1.y, width, height);
971 976
      }
977
      
978
      // Draw charging station
979
      if (station != null && station.isActive()) {
980
        bufferedGraphics.setStroke(new BasicStroke(2));
981
        bufferedGraphics.setColor(Color.ORANGE);
982
        int width = ColonetConstants.STATION_SIZE;
983
        bufferedGraphics.drawRect(station.getX() - width/2, station.getY() - width/2, width, width);
984
      }
972 985

  
973 986
			// Draw Identifiers and battery levels
974 987
			if (robotIcons != null) {
......
1143 1156
      return active;
1144 1157
    }
1145 1158
    
1146
    public boolean isSet () {
1159
    public boolean isSetting () {
1147 1160
      return set;
1148 1161
    }
1149 1162
    
......
1152 1165
    *  in the coordinate system of the JPanel.
1153 1166
    *  Returns null if the boundary is not active.
1154 1167
    */
1155
    public Rectangle toPanelRect () {
1168
    public Rectangle toRectangle () {
1156 1169
      if (!active)
1157 1170
        return null;
1158 1171
      return new Rectangle (panel_p1.x, panel_p1.y, (panel_p2.x-panel_p1.x), (panel_p2.y-panel_p1.y));
......
1164 1177
    *  If the boundary is not active, then this method 
1165 1178
    *  returns false.
1166 1179
    */
1167
    public boolean contains (int x, int y) {
1168
      Rectangle rect = this.toPanelRect();
1180
    public boolean contains (int px, int py) {
1181
      Rectangle rect = this.toRectangle();
1169 1182
      if (rect == null || !this.active)
1170 1183
        return false;
1171
      return rect.contains(x,y);
1184
      return rect.contains(px, py);
1172 1185
    }
1173 1186
        
1174 1187
  }
1188
  
1189
  class ChargingStation {
1190
    private int x, y;
1191
    private boolean active;
1192
    
1193
    public ChargingStation () {
1194
      super();
1195
    }
1196
    
1197
    public ChargingStation (int x, int y) {
1198
      this();
1199
      setLocation(x, y);
1200
    }
1201
    
1202
    public int getX () {
1203
      return x;
1204
    }
1205
    
1206
    public int getY () {
1207
      return y;
1208
    }
1209
    
1210
    /**
1211
    *  Sets the center of the charging station to
1212
    *  the specified coordinate point.
1213
    */
1214
    public void setLocation (int x, int y) {
1215
      this.x = x;
1216
      this.y = y;
1217
    }
1218
    
1219
    public boolean isActive () {
1220
      return active;
1221
    }
1222
    
1223
    public void setActive (boolean status) {
1224
      this.active = status;
1225
    }
1226
    
1227
    /**
1228
    *  Determines whether a coordinate is contained within
1229
    *  the graphical area of the charging station.
1230
    *  Returns false if the charging station is not active.
1231
    */
1232
    public boolean contains (int px, int py) {
1233
      if (!active)
1234
        return false;
1235
      int width = ColonetConstants.STATION_SIZE;
1236
      Rectangle rect = new Rectangle(this.x - width/2, this.y - width/2, width, width);
1237
      return rect.contains(px, py);
1238
    }
1239
  
1240
  }
1175 1241

  
1176 1242
}

Also available in: Unified diff