e02fcc20d3b90017124d9f37712eb7c1 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 java.util.ArrayList;
  7. import javax.swing.ButtonGroup;
  8. import javax.swing.JFrame;
  9. import javax.swing.JMenu;
  10. import javax.swing.JMenuBar;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13. import javax.swing.JRadioButtonMenuItem;
  14. import javax.swing.JSplitPane;
  15. import javax.swing.SwingUtilities;
  16. import config.Map;
  17. import config.Models;
  18. import lejos.robotics.mapping.LineMap;
  19. import lejos.robotics.navigation.Pose;
  20. import robots.BluetoothRobot;
  21. import robots.DataPose;
  22. import robots.Robot;
  23. import robots.RobotReturn;
  24. import robots.VirtualRobot;
  25. public class MainProgram extends JPanel implements KeyListener, WindowListener, RobotReturn {
  26. private MapImage imap;
  27. private Robot robot;
  28. private ScannerImage scanner;
  29. private Models smodel;
  30. public static final byte FORWARD = 0;
  31. public static final byte STOP = 1;
  32. public static final byte EXIT = 2;
  33. public static final byte LEFT = 3;
  34. public static final byte RIGHT = 4;
  35. public static final byte BACKWARD = 5;
  36. public MainProgram(LineMap map, Robot robot) {
  37. this.robot = robot;
  38. JFrame frame = new JFrame("Mapa MAC0318");
  39. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. frame.setLayout(new BorderLayout());
  41. imap = new MapImage(map);
  42. scanner = new ScannerImage(map);
  43. smodel = new Models(map);
  44. // frame.add(this.map);
  45. frame.setSize(800, 800);
  46. frame.setVisible(true);
  47. frame.setFocusable(true);
  48. frame.requestFocusInWindow();
  49. frame.addKeyListener(this);
  50. frame.addWindowListener(this);
  51. JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scanner, imap);
  52. splitPane.setOneTouchExpandable(true);
  53. splitPane.setDividerLocation((int) (frame.getHeight() / 2));
  54. frame.add(splitPane);
  55. boolean result = false;
  56. if (robot != null)
  57. result = robot.connect();
  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 maps.\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 += "i - Stop continuous scanner.\n";
  73. text += "g - Save global map image.\n";
  74. text += "f - Save scanner image.\n";
  75. text += "<arrows> - Move robot.\n";
  76. text += "h - help.\n";
  77. JOptionPane.showMessageDialog(null, text, "HELP", JOptionPane.PLAIN_MESSAGE);
  78. }
  79. public void addPoint(Pose p) {
  80. imap.addPoint(p);
  81. }
  82. @Override
  83. public void keyPressed(KeyEvent e) {
  84. char input = e.getKeyChar();
  85. switch (input) {
  86. case '1':
  87. imap.setVisual(0);
  88. break;
  89. case '2':
  90. imap.setVisual(1);
  91. break;
  92. case '3':
  93. imap.setVisual(2);
  94. break;
  95. case 'l':
  96. imap.showLine();
  97. break;
  98. case 'g':
  99. imap.save();
  100. break;
  101. case 'c':
  102. imap.clean();
  103. scanner.clean();
  104. break;
  105. case 's':
  106. setRobotPose();
  107. break;
  108. case 'm':
  109. moveRobot();
  110. break;
  111. case 'r':
  112. rotateRobot();
  113. break;
  114. case 'a':
  115. colectSonar();
  116. break;
  117. case 'z':
  118. robot.scann(this);
  119. break;
  120. case 'i':
  121. robot.stopScann();
  122. break;
  123. case 'h':
  124. showHelp();
  125. break;
  126. default:
  127. break;
  128. }
  129. if (robot == null)
  130. return;
  131. scanner.clean();
  132. switch (e.getKeyCode()) {
  133. case KeyEvent.VK_UP:
  134. robot.moveForward();
  135. break;
  136. case KeyEvent.VK_DOWN:
  137. robot.moveBackward();
  138. break;
  139. case KeyEvent.VK_LEFT:
  140. robot.moveLeft();
  141. break;
  142. case KeyEvent.VK_RIGHT:
  143. robot.moveRight();
  144. break;
  145. }
  146. }
  147. private void colectSonar() {
  148. int interval;
  149. try {
  150. String rs = JOptionPane.showInputDialog("Interval (degress):");
  151. interval = Integer.parseInt(rs);
  152. } catch (Exception e) {
  153. return;
  154. }
  155. ArrayList<DataPose> data = robot.scann(-90, 90, interval);
  156. scanner.clean();
  157. }
  158. private void rotateRobot() {
  159. try {
  160. String rs = JOptionPane.showInputDialog("Enter rotation (degress-clockwise):");
  161. double r = Double.parseDouble(rs);
  162. robot.rotate(r);
  163. scanner.clean();
  164. } catch (Exception e) {
  165. }
  166. }
  167. private void moveRobot() {
  168. try {
  169. String rs = JOptionPane.showInputDialog("Enter distance (cm):");
  170. double r = Double.parseDouble(rs);
  171. robot.move(r);
  172. scanner.clean();
  173. } catch (Exception e) {
  174. }
  175. }
  176. private void setRobotPose() {
  177. try {
  178. String xs = JOptionPane.showInputDialog("Enter x (cm):");
  179. if (xs.length() == 0) return;
  180. String ys = JOptionPane.showInputDialog("Enter y (cm):");
  181. if (ys.length() == 0) return;
  182. String as = JOptionPane.showInputDialog("Enter heading (degress):");
  183. if (as.length() == 0) return;
  184. float x = Float.parseFloat(xs);
  185. float y = Float.parseFloat(ys);
  186. float a = Float.parseFloat(as);
  187. robot.setPose(x, y, a);
  188. scanner.setPose(new Pose(x, y, a));
  189. scanner.clean();
  190. } catch (Exception e) {
  191. }
  192. }
  193. @Override
  194. public void keyReleased(KeyEvent arg0) {
  195. if (robot == null)
  196. return;
  197. robot.stop();
  198. }
  199. @Override
  200. public void keyTyped(KeyEvent arg0) {
  201. }
  202. @Override
  203. public void windowOpened(WindowEvent e) {
  204. }
  205. @Override
  206. public void windowClosing(WindowEvent e) {
  207. System.err.println("Fechando...");
  208. if (robot == null)
  209. return;
  210. robot.disconnect();
  211. }
  212. @Override
  213. public void windowClosed(WindowEvent e) {
  214. }
  215. @Override
  216. public void windowIconified(WindowEvent e) {
  217. }
  218. @Override
  219. public void windowDeiconified(WindowEvent e) {
  220. }
  221. @Override
  222. public void windowActivated(WindowEvent e) {
  223. }
  224. @Override
  225. public void windowDeactivated(WindowEvent e) {
  226. }
  227. @Override
  228. public void robotData(DataPose data) {
  229. // posicao do robo
  230. Pose p = data.getPose();
  231. imap.addPoint(p);
  232. if (data.getDistance() == 255) {
  233. scanner.setPose(p);
  234. return;
  235. }
  236. // ponto do ultrasonico
  237. double sensor_ang = Math.toRadians(data.getSensorAngle() + p.getHeading());
  238. double dx = Math.cos(sensor_ang) * data.getDistance();
  239. double dy = Math.sin(sensor_ang) * data.getDistance();
  240. double expected = smodel.expectedSonarRead(p, data.getSensorAngle());
  241. imap.addRead(p.getX() + dx, p.getY() + dy);
  242. scanner.addRead(p, data.getDistance(), data.getSensorAngle(), expected);
  243. }
  244. public static void main(String[] args) {
  245. LineMap map = Map.makeMap();
  246. Robot robotv = new VirtualRobot(map);
  247. Robot robotbt = new BluetoothRobot(null);
  248. Object[] possibleValues = { robotv, robotbt };
  249. Object robot = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.PLAIN_MESSAGE, null,
  250. possibleValues, possibleValues[0]);
  251. SwingUtilities.invokeLater(new Runnable() {
  252. public void run() {
  253. new MainProgram(map, (Robot) robot);
  254. }
  255. });
  256. }
  257. }