30e132d2d2b90017124d9f37712eb7c1 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 maps.\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. scanner.clean();
  103. break;
  104. case 's':
  105. setRobotPose();
  106. break;
  107. case 'm':
  108. moveRobot();
  109. break;
  110. case 'r':
  111. rotateRobot();
  112. break;
  113. case 'a':
  114. colectSonar();
  115. break;
  116. case 'z':
  117. robot.scann(this);
  118. break;
  119. case 'i':
  120. robot.stopScann();
  121. break;
  122. case 'h':
  123. showHelp();
  124. break;
  125. default:
  126. break;
  127. }
  128. if (robot == null)
  129. return;
  130. scanner.clean();
  131. switch (e.getKeyCode()) {
  132. case KeyEvent.VK_UP:
  133. robot.moveForward();
  134. break;
  135. case KeyEvent.VK_DOWN:
  136. robot.moveBackward();
  137. break;
  138. case KeyEvent.VK_LEFT:
  139. robot.moveLeft();
  140. break;
  141. case KeyEvent.VK_RIGHT:
  142. robot.moveRight();
  143. break;
  144. }
  145. }
  146. private void colectSonar() {
  147. try {
  148. String rs = JOptionPane.showInputDialog("Enter rotation (degress-clockwise):");
  149. int r = Integer.parseInt(rs);
  150. robot.rotate(r);
  151. scanner.clean();
  152. } catch (Exception e) {
  153. return;
  154. }
  155. }
  156. private void rotateRobot() {
  157. try {
  158. String rs = JOptionPane.showInputDialog("Enter rotation (degress-clockwise):");
  159. double r = Double.parseDouble(rs);
  160. robot.rotate(r);
  161. scanner.clean();
  162. } catch (Exception e) {
  163. }
  164. }
  165. private void moveRobot() {
  166. try {
  167. String rs = JOptionPane.showInputDialog("Enter distance (cm):");
  168. double r = Double.parseDouble(rs);
  169. robot.move(r);
  170. scanner.clean();
  171. } catch (Exception e) {
  172. }
  173. }
  174. private void setRobotPose() {
  175. try {
  176. String xs = JOptionPane.showInputDialog("Enter x (cm):");
  177. if (xs.length() == 0) return;
  178. String ys = JOptionPane.showInputDialog("Enter y (cm):");
  179. if (ys.length() == 0) return;
  180. String as = JOptionPane.showInputDialog("Enter heading (degress):");
  181. if (as.length() == 0) return;
  182. float x = Float.parseFloat(xs);
  183. float y = Float.parseFloat(ys);
  184. float a = Float.parseFloat(as);
  185. robot.setPose(x, y, a);
  186. scanner.setPose(new Pose(x, y, a));
  187. scanner.clean();
  188. } catch (Exception e) {
  189. }
  190. }
  191. @Override
  192. public void keyReleased(KeyEvent arg0) {
  193. if (robot == null)
  194. return;
  195. robot.stop();
  196. }
  197. @Override
  198. public void keyTyped(KeyEvent arg0) {
  199. }
  200. @Override
  201. public void windowOpened(WindowEvent e) {
  202. }
  203. @Override
  204. public void windowClosing(WindowEvent e) {
  205. System.err.println("Fechando...");
  206. if (robot == null)
  207. return;
  208. robot.disconnect();
  209. }
  210. @Override
  211. public void windowClosed(WindowEvent e) {
  212. }
  213. @Override
  214. public void windowIconified(WindowEvent e) {
  215. }
  216. @Override
  217. public void windowDeiconified(WindowEvent e) {
  218. }
  219. @Override
  220. public void windowActivated(WindowEvent e) {
  221. }
  222. @Override
  223. public void windowDeactivated(WindowEvent e) {
  224. }
  225. @Override
  226. public void robotData(DataPose data) {
  227. // posicao do robo
  228. Pose p = data.getPose();
  229. imap.addPoint(p);
  230. if (data.getDistance() == 255) {
  231. scanner.setPose(p);
  232. return;
  233. }
  234. // ponto do ultrasonico
  235. double sensor_ang = Math.toRadians(data.getSensorAngle() + p.getHeading());
  236. double dx = Math.cos(sensor_ang) * data.getDistance();
  237. double dy = Math.sin(sensor_ang) * data.getDistance();
  238. double expected = smodel.expectedSonarRead(p, data.getSensorAngle());
  239. imap.addRead(p.getX() + dx, p.getY() + dy);
  240. scanner.addRead(p, data.getDistance(), data.getSensorAngle(), expected);
  241. }
  242. public static void main(String[] args) {
  243. LineMap map = Map.makeMap();
  244. Robot robotv = new VirtualRobot(map);
  245. Robot robotbt = new BluetoothRobot(null);
  246. Object[] possibleValues = { robotv, robotbt };
  247. Object robot = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.PLAIN_MESSAGE, null,
  248. possibleValues, possibleValues[0]);
  249. SwingUtilities.invokeLater(new Runnable() {
  250. public void run() {
  251. new MainProgram(map, (Robot) robot);
  252. }
  253. });
  254. }
  255. }