b0b6886cceb90017124d9f37712eb7c1 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. case 'i':
  119. robot.stopScann();
  120. break;
  121. case 'h':
  122. showHelp();
  123. break;
  124. default:
  125. break;
  126. }
  127. if (robot == null)
  128. return;
  129. switch (e.getKeyCode()) {
  130. case KeyEvent.VK_UP:
  131. robot.moveForward();
  132. break;
  133. case KeyEvent.VK_DOWN:
  134. robot.moveBackward();
  135. break;
  136. case KeyEvent.VK_LEFT:
  137. robot.moveLeft();
  138. break;
  139. case KeyEvent.VK_RIGHT:
  140. robot.moveRight();
  141. break;
  142. }
  143. }
  144. private void setRobotPose() {
  145. String xs = JOptionPane.showInputDialog("Enter x (cm):");
  146. if (xs.length() == 0) return;
  147. String ys = JOptionPane.showInputDialog("Enter y (cm):");
  148. if (ys.length() == 0) return;
  149. String as = JOptionPane.showInputDialog("Enter heading (degress):");
  150. if (as.length() == 0) return;
  151. try {
  152. float x = Float.parseFloat(xs);
  153. float y = Float.parseFloat(ys);
  154. float a = Float.parseFloat(as);
  155. robot.setPose(x, y, a);
  156. } catch (Exception e) {
  157. }
  158. }
  159. @Override
  160. public void keyReleased(KeyEvent arg0) {
  161. if (robot == null)
  162. return;
  163. robot.stop();
  164. }
  165. @Override
  166. public void keyTyped(KeyEvent arg0) {
  167. }
  168. @Override
  169. public void windowOpened(WindowEvent e) {
  170. }
  171. @Override
  172. public void windowClosing(WindowEvent e) {
  173. System.err.println("Fechando...");
  174. if (robot == null)
  175. return;
  176. robot.disconnect();
  177. }
  178. @Override
  179. public void windowClosed(WindowEvent e) {
  180. }
  181. @Override
  182. public void windowIconified(WindowEvent e) {
  183. }
  184. @Override
  185. public void windowDeiconified(WindowEvent e) {
  186. }
  187. @Override
  188. public void windowActivated(WindowEvent e) {
  189. }
  190. @Override
  191. public void windowDeactivated(WindowEvent e) {
  192. }
  193. @Override
  194. public void robotData(DataPose data) {
  195. // posicao do robo
  196. Pose p = data.getPose();
  197. imap.addPoint(p);
  198. if (data.getDistance() == 255)
  199. return;
  200. // ponto do ultrasonico
  201. double sensor_ang = Math.toRadians(data.getSensorAngle() + p.getHeading());
  202. double dx = Math.cos(sensor_ang) * data.getDistance();
  203. double dy = Math.sin(sensor_ang) * data.getDistance();
  204. double expected = smodel.expectedSonarRead(p, data.getSensorAngle());
  205. imap.addRead(p.getX() + dx, p.getY() + dy);
  206. scanner.addRead(p, data.getDistance(), data.getSensorAngle(), expected);
  207. }
  208. public static void main(String[] args) {
  209. LineMap map = Map.makeMap();
  210. Robot robotv = new VirtualRobot(map);
  211. Robot robotbt = new BluetoothRobot(null);
  212. Object[] possibleValues = { robotv, robotbt };
  213. Object robot = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.PLAIN_MESSAGE, null,
  214. possibleValues, possibleValues[0]);
  215. SwingUtilities.invokeLater(new Runnable() {
  216. public void run() {
  217. new MainProgram(map, (Robot) robot);
  218. }
  219. });
  220. }
  221. }