4015b9dc0db9001711c7a09600ea5d68 4.1 KB

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