Revision 181
directional control now works, using buttons or arrow keys (s to stop)
Colonet.java | ||
---|---|---|
12 | 12 |
import java.util.Random; |
13 | 13 |
import java.applet.*; |
14 | 14 |
|
15 |
public class Colonet extends JApplet implements ActionListener, MouseInputListener, Runnable { |
|
15 |
public class Colonet extends JApplet implements ActionListener, MouseInputListener, KeyListener, Runnable {
|
|
16 | 16 |
|
17 | 17 |
final int CANVAS_SIZE = 500; //the applet may be slow if the canvas gets too large |
18 | 18 |
final int BUFFER = 50; |
... | ... | |
302 | 302 |
btnB.addActionListener(this); |
303 | 303 |
btnL.addActionListener(this); |
304 | 304 |
btnR.addActionListener(this); |
305 |
btnF.addKeyListener(this); |
|
306 |
btnB.addKeyListener(this); |
|
307 |
btnL.addKeyListener(this); |
|
308 |
btnR.addKeyListener(this); |
|
305 | 309 |
btnActivate.addActionListener(this); |
310 |
btnActivate.addKeyListener(this); |
|
306 | 311 |
vectorController.addMouseMotionListener(this); |
307 | 312 |
vectorController.addMouseListener(this); |
308 | 313 |
btnCommand_StopTask.addActionListener(this); |
... | ... | |
339 | 344 |
paint(g); |
340 | 345 |
} |
341 | 346 |
|
342 |
public void actionPerformed (ActionEvent e) { |
|
343 |
Object source = e.getSource(); |
|
344 |
if (source == btnGraph) { |
|
345 |
btnGraph.setEnabled(false); |
|
346 |
//Start dependent threads |
|
347 |
drawThread = new Thread(this, "drawThread"); |
|
348 |
drawThread.start(); |
|
349 |
indicator.start(); |
|
350 |
packetMonitor.start(); |
|
351 |
simulator.start(); |
|
352 |
} else if (source == btnConnect) { |
|
353 |
csi.connect(txtHost.getText(), txtPort.getText()); |
|
354 |
dataUpdater = new DataUpdater(); |
|
355 |
dataUpdater.start(); |
|
356 |
} |
|
357 |
// Robot Movement Controls |
|
358 |
else if (source == btnF) { |
|
359 |
vectorController.setMaxForward(); |
|
360 |
vectorController.sendToServer(); |
|
361 |
} else if (source == btnB) { |
|
362 |
vectorController.setMaxReverse(); |
|
363 |
vectorController.sendToServer(); |
|
364 |
} else if (source == btnL) { |
|
365 |
vectorController.setMaxLeft(); |
|
366 |
vectorController.sendToServer(); |
|
367 |
} else if (source == btnR) { |
|
368 |
vectorController.setMaxRight(); |
|
369 |
vectorController.sendToServer(); |
|
370 |
} else if (source == btnActivate) { |
|
371 |
vectorController.setZero(); |
|
372 |
vectorController.sendToServer(); |
|
373 |
} |
|
374 |
// Robot Commands (non-movement) |
|
375 |
else if (source == btnCommand_StopTask) { |
|
376 |
|
|
377 |
} else if (source == btnCommand_ResumeTask) { |
|
378 |
|
|
379 |
} else if (source == btnCommand_ChargeNow) { |
|
380 |
|
|
381 |
} else if (source == btnCommand_StopCharging) { |
|
382 |
|
|
383 |
} |
|
384 |
|
|
385 |
// Queue Management |
|
386 |
else if (source == btnAddTask) { |
|
387 |
taskAddWindow.prompt(); |
|
388 |
} else if (source == btnRemoveTask) { |
|
389 |
if (taskList.getSelectedIndex() >= 0); |
|
390 |
csi.sendQueueRemove(taskList.getSelectedIndex()); |
|
391 |
csi.sendQueueUpdate(); |
|
392 |
} else if (source == btnMoveTaskUp) { |
|
393 |
csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() - 1); |
|
394 |
csi.sendQueueUpdate(); |
|
395 |
} else if (source == btnMoveTaskDown) { |
|
396 |
csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() + 1); |
|
397 |
csi.sendQueueUpdate(); |
|
398 |
} else if (source == btnUpdateTasks) { |
|
399 |
csi.sendQueueUpdate(); |
|
400 |
} |
|
401 |
} |
|
402 |
|
|
403 | 347 |
private void randomize () { |
404 | 348 |
Random r = new Random(); |
405 | 349 |
StringBuilder s = new StringBuilder(); |
... | ... | |
619 | 563 |
} |
620 | 564 |
|
621 | 565 |
// |
622 |
// MouseEvent methods
|
|
566 |
// MouseListener methods
|
|
623 | 567 |
// |
624 | 568 |
public void mouseExited(MouseEvent e) { |
625 | 569 |
} |
626 | 570 |
public void mouseEntered(MouseEvent e) { |
627 | 571 |
} |
628 | 572 |
public void mouseReleased(MouseEvent e) { |
629 |
vectorController.sendToServer();
|
|
573 |
vectorController.notifyMouseEvent(e, true);
|
|
630 | 574 |
} |
631 | 575 |
public void mouseClicked(MouseEvent e) { |
632 |
mouseDragged(e);
|
|
576 |
vectorController.notifyMouseEvent(e, false);
|
|
633 | 577 |
} |
634 | 578 |
public void mousePressed(MouseEvent e) { |
635 | 579 |
try { |
... | ... | |
643 | 587 |
|
644 | 588 |
} |
645 | 589 |
public void mouseDragged(MouseEvent e) { |
646 |
vectorController.setPoint(e.getX(), e.getY()); |
|
647 |
vectorController.repaint(); |
|
590 |
vectorController.notifyMouseEvent(e, false); |
|
648 | 591 |
} |
649 | 592 |
public void mouseMoved(MouseEvent e) { |
650 | 593 |
} |
651 | 594 |
|
595 |
// |
|
596 |
// KeyListener methods |
|
597 |
// |
|
598 |
public void keyPressed (KeyEvent e) { |
|
599 |
int code = e.getKeyCode(); |
|
600 |
if (code == KeyEvent.VK_UP) { |
|
601 |
vectorController.setMaxForward(); |
|
602 |
vectorController.sendToServer(); |
|
603 |
} else if (code == KeyEvent.VK_DOWN) { |
|
604 |
vectorController.setMaxReverse(); |
|
605 |
vectorController.sendToServer(); |
|
606 |
} else if (code == KeyEvent.VK_LEFT) { |
|
607 |
vectorController.setMaxLeft(); |
|
608 |
vectorController.sendToServer(); |
|
609 |
} else if (code == KeyEvent.VK_RIGHT) { |
|
610 |
vectorController.setMaxRight(); |
|
611 |
vectorController.sendToServer(); |
|
612 |
} else if (code == KeyEvent.VK_S) { |
|
613 |
vectorController.setZero(); |
|
614 |
vectorController.sendToServer(); |
|
615 |
} |
|
616 |
} |
|
617 |
public void keyReleased (KeyEvent e) { |
|
618 |
} |
|
619 |
public void keyTyped (KeyEvent e) { |
|
620 |
} |
|
652 | 621 |
|
622 |
|
|
623 |
// |
|
624 |
// ActionListener method |
|
625 |
// |
|
626 |
public void actionPerformed (ActionEvent e) { |
|
627 |
Object source = e.getSource(); |
|
628 |
if (source == btnGraph) { |
|
629 |
btnGraph.setEnabled(false); |
|
630 |
//Start dependent threads |
|
631 |
drawThread = new Thread(this, "drawThread"); |
|
632 |
drawThread.start(); |
|
633 |
indicator.start(); |
|
634 |
packetMonitor.start(); |
|
635 |
simulator.start(); |
|
636 |
} else if (source == btnConnect) { |
|
637 |
csi.connect(txtHost.getText(), txtPort.getText()); |
|
638 |
dataUpdater = new DataUpdater(); |
|
639 |
dataUpdater.start(); |
|
640 |
} |
|
641 |
// Robot Movement Controls |
|
642 |
else if (source == btnF) { |
|
643 |
vectorController.setMaxForward(); |
|
644 |
vectorController.sendToServer(); |
|
645 |
} else if (source == btnB) { |
|
646 |
vectorController.setMaxReverse(); |
|
647 |
vectorController.sendToServer(); |
|
648 |
} else if (source == btnL) { |
|
649 |
vectorController.setMaxLeft(); |
|
650 |
vectorController.sendToServer(); |
|
651 |
} else if (source == btnR) { |
|
652 |
vectorController.setMaxRight(); |
|
653 |
vectorController.sendToServer(); |
|
654 |
} else if (source == btnActivate) { |
|
655 |
vectorController.setZero(); |
|
656 |
vectorController.sendToServer(); |
|
657 |
} |
|
658 |
// Robot Commands (non-movement) |
|
659 |
else if (source == btnCommand_StopTask) { |
|
660 |
|
|
661 |
} else if (source == btnCommand_ResumeTask) { |
|
662 |
|
|
663 |
} else if (source == btnCommand_ChargeNow) { |
|
664 |
|
|
665 |
} else if (source == btnCommand_StopCharging) { |
|
666 |
|
|
667 |
} |
|
668 |
|
|
669 |
// Queue Management |
|
670 |
else if (source == btnAddTask) { |
|
671 |
taskAddWindow.prompt(); |
|
672 |
} else if (source == btnRemoveTask) { |
|
673 |
if (taskList.getSelectedIndex() >= 0); |
|
674 |
csi.sendQueueRemove(taskList.getSelectedIndex()); |
|
675 |
csi.sendQueueUpdate(); |
|
676 |
} else if (source == btnMoveTaskUp) { |
|
677 |
csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() - 1); |
|
678 |
csi.sendQueueUpdate(); |
|
679 |
} else if (source == btnMoveTaskDown) { |
|
680 |
csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() + 1); |
|
681 |
csi.sendQueueUpdate(); |
|
682 |
} else if (source == btnUpdateTasks) { |
|
683 |
csi.sendQueueUpdate(); |
|
684 |
} |
|
685 |
} |
|
686 |
|
|
653 | 687 |
/* |
654 | 688 |
* SelectionIndicator thread. |
655 | 689 |
* Graphical representation of the selection marker |
... | ... | |
973 | 1007 |
return (xterm + yterm <= 1); |
974 | 1008 |
} |
975 | 1009 |
|
1010 |
public void notifyMouseEvent (MouseEvent e, boolean send) { |
|
1011 |
if (!isValidPoint(e.getX(), e.getY())) |
|
1012 |
return; |
|
1013 |
vectorController.setPoint(e.getX(), e.getY()); |
|
1014 |
vectorController.repaint(); |
|
1015 |
if (send) |
|
1016 |
vectorController.sendToServer(); |
|
1017 |
} |
|
1018 |
|
|
976 | 1019 |
public int getVelocity () { |
977 | 1020 |
int dx = x - cx; |
978 | 1021 |
int dy = y - cy; |
... | ... | |
1024 | 1067 |
} |
1025 | 1068 |
|
1026 | 1069 |
public void sendToServer () { |
1027 |
if (csi != null) |
|
1070 |
//log.append("Attempitng to send angle = " + vectorController.getAngle() + ", velocity = " + vectorController.getVelocity() + "\n"); |
|
1071 |
if (csi != null) { |
|
1072 |
if (cx == 0 && cy == 0) { |
|
1073 |
csi.sendData(ColonetServerInterface.MOTORS_OFF, ColonetServerInterface.GLOBAL_DEST); |
|
1074 |
} else { |
|
1075 |
//Directional commands |
|
1076 |
if (x > cx && y == cy) { //move right |
|
1077 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 160", ColonetServerInterface.GLOBAL_DEST); |
|
1078 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 160", ColonetServerInterface.GLOBAL_DEST); |
|
1079 |
} else if (x < cx && y == cy) { //move left |
|
1080 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 160", ColonetServerInterface.GLOBAL_DEST); |
|
1081 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 160", ColonetServerInterface.GLOBAL_DEST); |
|
1082 |
} else if (x == cx && y > cy) { //move forward |
|
1083 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST); |
|
1084 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST); |
|
1085 |
} else if (x == cx && y < cy) { //move backward |
|
1086 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST); |
|
1087 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST); |
|
1088 |
} else if (x == cx && y == cy) { //stop! |
|
1089 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 0", ColonetServerInterface.GLOBAL_DEST); |
|
1090 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 0", ColonetServerInterface.GLOBAL_DEST); |
|
1091 |
} |
|
1092 |
|
|
1093 |
/* |
|
1094 |
//Work on this later. |
|
1095 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " " + "0" + " " + |
|
1096 |
(vectorController.getVelocity() + vectorController.getAngle()), ColonetServerInterface.GLOBAL_DEST); |
|
1097 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " " + "0" + " " + |
|
1098 |
(vectorController.getVelocity() - vectorController.getAngle()), ColonetServerInterface.GLOBAL_DEST); |
|
1099 |
*/ |
|
1100 |
} |
|
1101 |
} |
|
1102 |
/* |
|
1103 |
//The move command doesn't really work. |
|
1028 | 1104 |
csi.sendData(ColonetServerInterface.MOVE + " " + |
1029 | 1105 |
vectorController.getAngle() + " " + |
1030 | 1106 |
vectorController.getVelocity(), ColonetServerInterface.GLOBAL_DEST); |
1107 |
*/ |
|
1108 |
|
|
1031 | 1109 |
} |
1032 | 1110 |
|
1033 | 1111 |
} |
Also available in: Unified diff