2025fb61ceb90017124d9f37712eb7c1 5.7 KB

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