10b4e4650db9001711c7a09600ea5d68 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import java.awt.BorderLayout;
  2. import java.awt.event.KeyEvent;
  3. import java.awt.event.KeyListener;
  4. import java.awt.event.WindowEvent;
  5. import java.awt.event.WindowListener;
  6. import javax.swing.JFrame;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JPanel;
  9. import javax.swing.JSplitPane;
  10. import javax.swing.SwingUtilities;
  11. import config.Map;
  12. import lejos.robotics.mapping.LineMap;
  13. import lejos.robotics.navigation.Pose;
  14. import robots.DataPose;
  15. import robots.Robot;
  16. import robots.RobotReturn;
  17. import robots.VirtualRobot;
  18. public class MainProgram extends JPanel implements KeyListener, WindowListener, RobotReturn {
  19. private MapImage imap;
  20. private Robot robot;
  21. private ScannerImage scanner;
  22. public static final byte FORWARD = 0;
  23. public static final byte STOP = 1;
  24. public static final byte EXIT = 2;
  25. public static final byte LEFT = 3;
  26. public static final byte RIGHT = 4;
  27. public static final byte BACKWARD = 5;
  28. public MainProgram(LineMap map, Robot robot) {
  29. this.robot = robot;
  30. JFrame frame = new JFrame("Mapa MAC0318");
  31. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. frame.setLayout(new BorderLayout());
  33. imap = new MapImage(map);
  34. scanner = new ScannerImage(map);
  35. // frame.add(this.map);
  36. frame.setSize(800, 800);
  37. frame.setVisible(true);
  38. frame.setFocusable(true);
  39. frame.requestFocusInWindow();
  40. frame.addKeyListener(this);
  41. frame.addWindowListener(this);
  42. JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scanner, imap);
  43. splitPane.setOneTouchExpandable(true);
  44. splitPane.setDividerLocation((int) (frame.getHeight() / 2));
  45. frame.add(splitPane);
  46. boolean result = false;
  47. if (robot != null)
  48. result = robot.connect(this);
  49. if (result == false) {
  50. JOptionPane.showMessageDialog(null, "Não foi possível conectar ao robô");
  51. }
  52. //
  53. // String text = "1,2,3 - Change view mode.\n";
  54. // text += "s - Save image.\n";
  55. // text += "l - Show trace.\n";
  56. // text += "c - Clean map.\n";
  57. // JOptionPane.showMessageDialog(null, text);
  58. }
  59. public void addPoint(Pose p) {
  60. imap.addPoint(p);
  61. }
  62. @Override
  63. public void keyPressed(KeyEvent e) {
  64. char input = e.getKeyChar();
  65. if (input == '1')
  66. imap.setVisual(0);
  67. else if (input == '2')
  68. imap.setVisual(1);
  69. else if (input == '3')
  70. imap.setVisual(2);
  71. else if (input == 'l')
  72. imap.showLine();
  73. else if (input == 's')
  74. imap.save();
  75. else if (input == 'c')
  76. imap.clean();
  77. if (robot == null)
  78. return;
  79. switch (e.getKeyCode()) {
  80. case KeyEvent.VK_UP:
  81. robot.moveForward();
  82. break;
  83. case KeyEvent.VK_DOWN:
  84. robot.moveBackward();
  85. break;
  86. case KeyEvent.VK_LEFT:
  87. robot.moveLeft();
  88. break;
  89. case KeyEvent.VK_RIGHT:
  90. robot.moveRight();
  91. break;
  92. }
  93. }
  94. @Override
  95. public void keyReleased(KeyEvent arg0) {
  96. if (robot == null)
  97. return;
  98. robot.stop();
  99. }
  100. @Override
  101. public void keyTyped(KeyEvent arg0) {
  102. }
  103. public static void main(String[] args) {
  104. LineMap map = Map.makeMap();
  105. Robot robot = new VirtualRobot(map);
  106. //Robot robot = new BluetoothRobot("NXJ8");
  107. SwingUtilities.invokeLater(new Runnable() {
  108. public void run() {
  109. new MainProgram(map, robot);
  110. }
  111. });
  112. }
  113. @Override
  114. public void windowOpened(WindowEvent e) {
  115. }
  116. @Override
  117. public void windowClosing(WindowEvent e) {
  118. System.err.println("Fechando...");
  119. if (robot == null)
  120. return;
  121. robot.exit();
  122. }
  123. @Override
  124. public void windowClosed(WindowEvent e) {
  125. }
  126. @Override
  127. public void windowIconified(WindowEvent e) {
  128. }
  129. @Override
  130. public void windowDeiconified(WindowEvent e) {
  131. }
  132. @Override
  133. public void windowActivated(WindowEvent e) {
  134. }
  135. @Override
  136. public void windowDeactivated(WindowEvent e) {
  137. }
  138. @Override
  139. public void robotData(DataPose data) {
  140. // posicao do robo
  141. Pose p = data.getPose();
  142. imap.addPoint(p);
  143. // ponto do ultrasonico
  144. double sensor_ang = Math.toRadians(data.getSensorAngle()+p.getHeading());
  145. double dx = Math.cos(sensor_ang)*data.getDistance();
  146. double dy = Math.sin(sensor_ang)*data.getDistance();
  147. imap.addRead(p.getX()+dx, p.getY()+dy);
  148. double expected = SonarModel();
  149. scanner.addRead(p, data.getDistance(), data.getSensorAngle(), expected);
  150. }
  151. }