1089c19fcbb90017124d9f37712eb7c1 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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();
  56. robot.start(this);
  57. }
  58. if (result == false) {
  59. JOptionPane.showMessageDialog(null, "Não foi possível conectar ao robô");
  60. }
  61. showHelp();
  62. }
  63. private void showHelp() {
  64. String text = "1,2,3 - Change global map view\n";
  65. text += "s - Set Pose.\n";
  66. text += "l - Show global map trace.\n";
  67. text += "c - Clean global map.\n";
  68. text += "m - Enter robot movement.\n";
  69. text += "r - Enter robo rotation.\n";
  70. text += "a - Colect sonar data.\n";
  71. text += "z - Make sonar continuous scanner.\n";
  72. text += "g - Save global map image.\n";
  73. text += "h - Save scanner image.\n";
  74. text += "<arrows> - Move robot.\n";
  75. JOptionPane.showMessageDialog(null, text, "HELP", JOptionPane.PLAIN_MESSAGE);
  76. }
  77. public void addPoint(Pose p) {
  78. imap.addPoint(p);
  79. }
  80. @Override
  81. public void keyPressed(KeyEvent e) {
  82. char input = e.getKeyChar();
  83. if (input == '1')
  84. imap.setVisual(0);
  85. else if (input == '2')
  86. imap.setVisual(1);
  87. else if (input == '3')
  88. imap.setVisual(2);
  89. else if (input == 'l')
  90. imap.showLine();
  91. else if (input == 'g')
  92. imap.save();
  93. else if (input == 'c')
  94. imap.clean();
  95. else if (input == 's')
  96. setRobotPose();
  97. if (robot == null)
  98. return;
  99. switch (e.getKeyCode()) {
  100. case KeyEvent.VK_UP:
  101. robot.moveForward();
  102. break;
  103. case KeyEvent.VK_DOWN:
  104. robot.moveBackward();
  105. break;
  106. case KeyEvent.VK_LEFT:
  107. robot.moveLeft();
  108. break;
  109. case KeyEvent.VK_RIGHT:
  110. robot.moveRight();
  111. break;
  112. }
  113. }
  114. private void setRobotPose() {
  115. String xs = JOptionPane.showInputDialog("Enter x (cm):");
  116. String ys = JOptionPane.showInputDialog("Enter y (cm):");
  117. String as = JOptionPane.showInputDialog("Enter heading (degress):");
  118. float x = Float.parseFloat(xs);
  119. float y = Float.parseFloat(ys);
  120. float a = Float.parseFloat(as);
  121. robot.setPose(x, y, a);
  122. }
  123. @Override
  124. public void keyReleased(KeyEvent arg0) {
  125. if (robot == null)
  126. return;
  127. robot.stop();
  128. }
  129. @Override
  130. public void keyTyped(KeyEvent arg0) {
  131. }
  132. @Override
  133. public void windowOpened(WindowEvent e) {
  134. }
  135. @Override
  136. public void windowClosing(WindowEvent e) {
  137. System.err.println("Fechando...");
  138. if (robot == null)
  139. return;
  140. robot.disconnect();
  141. }
  142. @Override
  143. public void windowClosed(WindowEvent e) {
  144. }
  145. @Override
  146. public void windowIconified(WindowEvent e) {
  147. }
  148. @Override
  149. public void windowDeiconified(WindowEvent e) {
  150. }
  151. @Override
  152. public void windowActivated(WindowEvent e) {
  153. }
  154. @Override
  155. public void windowDeactivated(WindowEvent e) {
  156. }
  157. @Override
  158. public void robotData(DataPose data) {
  159. // posicao do robo
  160. Pose p = data.getPose();
  161. imap.addPoint(p);
  162. // ponto do ultrasonico
  163. double sensor_ang = Math.toRadians(data.getSensorAngle() + p.getHeading());
  164. double dx = Math.cos(sensor_ang) * data.getDistance();
  165. double dy = Math.sin(sensor_ang) * data.getDistance();
  166. imap.addRead(p.getX() + dx, p.getY() + dy);
  167. double expected = smodel.expectedSonarRead(p, data.getSensorAngle());
  168. scanner.addRead(p, data.getDistance(), data.getSensorAngle(), expected);
  169. }
  170. public static void main(String[] args) {
  171. LineMap map = Map.makeMap();
  172. Robot robot = new VirtualRobot(map);
  173. // Robot robot = new BluetoothRobot("NXJ8");
  174. SwingUtilities.invokeLater(new Runnable() {
  175. public void run() {
  176. new MainProgram(map, robot);
  177. }
  178. });
  179. }
  180. }