MainProgram.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.JFrame;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JPanel;
  10. import javax.swing.JSplitPane;
  11. import javax.swing.SwingUtilities;
  12. import config.Map;
  13. import config.Models;
  14. import lejos.robotics.mapping.LineMap;
  15. import lejos.robotics.navigation.Pose;
  16. import robots.BluetoothRobot;
  17. import robots.DataRead;
  18. import robots.LCPRobot;
  19. import robots.Robot;
  20. import robots.RobotReturn;
  21. import robots.VirtualRobot;
  22. public class MainProgram extends JPanel implements KeyListener, WindowListener, RobotReturn {
  23. private MapImage imap;
  24. private Robot robot;
  25. private ScannerImage scanner;
  26. private Models model;
  27. private ProbabilityMatriz matriz;
  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();
  41. model = 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. matriz = new ProbabilityMatriz(map.getBoundingRect(), 5, 7);
  54. for (DiscretePoint p: matriz) {
  55. System.out.println(p.z);
  56. if (p.z == 3 && p.x == 10 && p.y == 10)
  57. p.set(1);
  58. else
  59. p.set(0);
  60. }
  61. imap.showMatriz(matriz);
  62. // showHelp();
  63. }
  64. private void showHelp() {
  65. String text = "1,2,3 - Change global map view\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 'f':
  102. scanner.save();
  103. break;
  104. case 'c':
  105. imap.clean();
  106. scanner.clean();
  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 'h':
  118. showHelp();
  119. break;
  120. case 's':
  121. // reseta a contagem
  122. System.out.println("resetou");
  123. matriz.setAll(1);
  124. matriz.normalize();
  125. imap.repaint();
  126. break;
  127. default:
  128. break;
  129. }
  130. if (robot == null)
  131. return;
  132. switch (e.getKeyCode()) {
  133. case KeyEvent.VK_UP:
  134. robot.moveForward();
  135. scanner.clean();
  136. break;
  137. case KeyEvent.VK_DOWN:
  138. robot.moveBackward();
  139. scanner.clean();
  140. break;
  141. case KeyEvent.VK_LEFT:
  142. robot.moveLeft();
  143. scanner.clean();
  144. break;
  145. case KeyEvent.VK_RIGHT:
  146. robot.moveRight();
  147. scanner.clean();
  148. break;
  149. }
  150. }
  151. private void colectSonar() {
  152. int interval;
  153. try {
  154. String rs = JOptionPane.showInputDialog("Interval (degress):");
  155. interval = Integer.parseInt(rs);
  156. } catch (Exception e) {
  157. return;
  158. }
  159. ArrayList<DataRead> data = robot.scann(-90, 90, interval);
  160. if (data == null) return;
  161. /* PROBABILIDADES DEPOIS LEITURA */
  162. for (DiscretePoint p: matriz) {
  163. Pose pose = p.pose();
  164. double prob = model.readProbability(pose, data);
  165. p.set(prob*p.get());
  166. }
  167. matriz.normalize();
  168. imap.setMatriz(matriz);
  169. for (DataRead d : data) {
  170. robotData(d);
  171. }
  172. imap.repaint();
  173. }
  174. private void rotateRobot() {
  175. try {
  176. String rs = JOptionPane.showInputDialog("Enter rotation (degress-clockwise):");
  177. double r = Double.parseDouble(rs);
  178. robot.rotate(r);
  179. /* PROBABILIDADES DEPOIS MOVIMENTO */
  180. ProbabilityMatriz newmatriz = new ProbabilityMatriz(matriz.bounds, matriz.ssize, matriz.alphad);
  181. System.out.print("Iniciando...");
  182. for (DiscretePoint newp: newmatriz) {
  183. double sum = 0;
  184. for (DiscretePoint p: matriz) {
  185. Pose pa = p.pose();
  186. Pose pb = newp.pose();
  187. sum += model.rotateProbability(pa, pb, r)*p.get();
  188. }
  189. newp.set(sum);
  190. }
  191. System.out.println("Finalizado");
  192. matriz = newmatriz;
  193. imap.setMatriz(matriz);
  194. matriz.normalize();
  195. scanner.clean();
  196. imap.repaint();
  197. } catch (Exception e) {
  198. }
  199. }
  200. private void moveRobot() {
  201. try {
  202. String rs = JOptionPane.showInputDialog("Enter distance (cm):");
  203. double r = Double.parseDouble(rs);
  204. robot.move(r);
  205. /* PROBABILIDADES DEPOIS MOVIMENTO */
  206. ProbabilityMatriz newmatriz = new ProbabilityMatriz(matriz.bounds, matriz.ssize, matriz.alphad);
  207. System.out.print("Iniciando...");
  208. for (DiscretePoint newp: newmatriz) {
  209. double sum = 0;
  210. for (DiscretePoint p: matriz) {
  211. Pose pa = p.pose();
  212. Pose pb = newp.pose();
  213. sum += model.moveProbability(pa, pb, r)*p.get();
  214. }
  215. newp.set(sum);
  216. }
  217. System.out.println("Finalizado");
  218. matriz = newmatriz;
  219. matriz.normalize();
  220. imap.setMatriz(matriz);
  221. scanner.clean();
  222. imap.repaint();
  223. } catch (Exception e) {
  224. }
  225. }
  226. @Override
  227. public void keyReleased(KeyEvent arg0) {
  228. if (robot == null)
  229. return;
  230. robot.stop();
  231. }
  232. @Override
  233. public void keyTyped(KeyEvent arg0) {
  234. }
  235. @Override
  236. public void windowOpened(WindowEvent e) {
  237. }
  238. @Override
  239. public void windowClosing(WindowEvent e) {
  240. System.err.println("Fechando...");
  241. if (robot == null)
  242. return;
  243. robot.disconnect();
  244. }
  245. @Override
  246. public void windowClosed(WindowEvent e) {
  247. }
  248. @Override
  249. public void windowIconified(WindowEvent e) {
  250. }
  251. @Override
  252. public void windowDeiconified(WindowEvent e) {
  253. }
  254. @Override
  255. public void windowActivated(WindowEvent e) {
  256. }
  257. @Override
  258. public void windowDeactivated(WindowEvent e) {
  259. }
  260. @Override
  261. public void robotData(DataRead data) {
  262. // posicao do robo
  263. System.out.print(" sang:"+data.getSensorAngle());
  264. System.out.println(" distance:"+data.getDistance());
  265. scanner.addRead(data.getDistance(), data.getSensorAngle()-90);
  266. }
  267. public static void main(String[] args) {
  268. LineMap map = Map.makeMap();
  269. Robot robotv = new VirtualRobot(map);
  270. Robot robotbt = new BluetoothRobot(null);
  271. String s = "LCPRobot";
  272. Object[] possibleValues = { robotv, robotbt, s };
  273. Object robottmp = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.PLAIN_MESSAGE, null,
  274. possibleValues, possibleValues[0]);
  275. Robot robot;
  276. boolean result = false;
  277. if (robottmp == s) {
  278. robot = new LCPRobot();
  279. } else {
  280. robot = (Robot) robottmp;
  281. }
  282. if (robot != null)
  283. result = ((Robot) robot).connect();
  284. if (result == false) {
  285. JOptionPane.showMessageDialog(null, "Não foi possível conectar ao robô");
  286. System.exit(ERROR);
  287. }
  288. SwingUtilities.invokeLater(new Runnable() {
  289. public void run() {
  290. new MainProgram(map, (Robot) robot);
  291. }
  292. });
  293. }
  294. }