c0680f0ecbb90017124d9f37712eb7c1 5.1 KB

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