MainProgram.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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, 10);
  54. //
  55. // for (DiscretePoint p: matriz) {
  56. //// System.out.println(n(p.midAng()));
  57. // if (p.z == 2 && p.x == 5 && p.y == 5)
  58. // p.set(1);
  59. // else
  60. // p.set(0);
  61. // }
  62. imap.showMatriz(matriz);
  63. // showHelp();
  64. }
  65. private double n (double x) {
  66. x = Math.toRadians(x);
  67. return Math.toDegrees(Math.atan2(Math.sin(x), Math.cos(x)));
  68. }
  69. private void showHelp() {
  70. String text = "1,2,3 - Change global map view\n";
  71. text += "l - Show global map trace.\n";
  72. text += "c - Clean maps.\n";
  73. text += "m - Enter robot movement.\n";
  74. text += "r - Enter robo rotation.\n";
  75. text += "a - Colect sonar data.\n";
  76. text += "z - Make sonar continuous scanner.\n";
  77. text += "i - Stop continuous scanner.\n";
  78. text += "g - Save global map image.\n";
  79. text += "f - Save scanner image.\n";
  80. text += "<arrows> - Move robot.\n";
  81. text += "h - help.\n";
  82. JOptionPane.showMessageDialog(null, text, "HELP", JOptionPane.PLAIN_MESSAGE);
  83. }
  84. public void addPoint(Pose p) {
  85. imap.addPoint(p);
  86. }
  87. @Override
  88. public void keyPressed(KeyEvent e) {
  89. char input = e.getKeyChar();
  90. switch (input) {
  91. case '1':
  92. imap.setVisual(0);
  93. break;
  94. case '2':
  95. imap.setVisual(1);
  96. break;
  97. case '3':
  98. imap.setVisual(2);
  99. break;
  100. case 'l':
  101. imap.showLine();
  102. break;
  103. case 'g':
  104. imap.save();
  105. break;
  106. case 'f':
  107. scanner.save();
  108. break;
  109. case 'c':
  110. imap.clean();
  111. scanner.clean();
  112. break;
  113. case 'm':
  114. moveRobot();
  115. break;
  116. case 'r':
  117. rotateRobot();
  118. break;
  119. case 'a':
  120. colectSonar();
  121. break;
  122. case 'h':
  123. showHelp();
  124. break;
  125. case 's':
  126. // reseta a contagem
  127. System.out.println("resetou");
  128. matriz.setAll(1);
  129. matriz.normalize();
  130. imap.repaint();
  131. break;
  132. default:
  133. break;
  134. }
  135. if (robot == null)
  136. return;
  137. switch (e.getKeyCode()) {
  138. case KeyEvent.VK_UP:
  139. robot.moveForward();
  140. scanner.clean();
  141. break;
  142. case KeyEvent.VK_DOWN:
  143. robot.moveBackward();
  144. scanner.clean();
  145. break;
  146. case KeyEvent.VK_LEFT:
  147. robot.moveLeft();
  148. scanner.clean();
  149. break;
  150. case KeyEvent.VK_RIGHT:
  151. robot.moveRight();
  152. scanner.clean();
  153. break;
  154. }
  155. }
  156. private void colectSonar() {
  157. int interval;
  158. try {
  159. String rs = JOptionPane.showInputDialog("Interval (degress):");
  160. interval = Integer.parseInt(rs);
  161. } catch (Exception e) {
  162. return;
  163. }
  164. ArrayList<DataRead> data = robot.scann(-90, 90, interval);
  165. if (data == null) return;
  166. /* PROBABILIDADES DEPOIS LEITURA */
  167. for (DiscretePoint p: matriz) {
  168. Pose pose = p.pose();
  169. double prob = model.readProbability(pose, data);
  170. p.set(prob*p.get());
  171. }
  172. matriz.normalize();
  173. imap.setMatriz(matriz);
  174. for (DataRead d : data) {
  175. robotData(d);
  176. }
  177. imap.repaint();
  178. }
  179. private void rotateRobot() {
  180. try {
  181. String rs = JOptionPane.showInputDialog("Enter rotation (degress-clockwise):");
  182. double r = Double.parseDouble(rs);
  183. robot.rotate(r);
  184. /* PROBABILIDADES DEPOIS MOVIMENTO */
  185. ProbabilityMatriz newmatriz = new ProbabilityMatriz(matriz.bounds, matriz.ssize, matriz.alphad);
  186. System.out.print("Iniciando...");
  187. for (DiscretePoint newp: newmatriz) {
  188. double sum = 0;
  189. for (DiscretePoint p: matriz) {
  190. Pose pa = p.pose();
  191. Pose pb = newp.pose();
  192. sum += model.rotateProbability(pa, pb, r)*p.get();
  193. }
  194. newp.set(sum);
  195. }
  196. System.out.println("Finalizado");
  197. matriz = newmatriz;
  198. imap.setMatriz(matriz);
  199. matriz.normalize();
  200. scanner.clean();
  201. imap.repaint();
  202. } catch (Exception e) {
  203. }
  204. }
  205. private void moveRobot() {
  206. try {
  207. String rs = JOptionPane.showInputDialog("Enter distance (cm):");
  208. double r = Double.parseDouble(rs);
  209. robot.move(r);
  210. /* PROBABILIDADES DEPOIS MOVIMENTO */
  211. ProbabilityMatriz newmatriz = new ProbabilityMatriz(matriz.bounds, matriz.ssize, matriz.alphad);
  212. System.out.print("Iniciando...");
  213. for (DiscretePoint newp: newmatriz) {
  214. double sum = 0;
  215. for (DiscretePoint p: matriz) {
  216. Pose pa = p.pose();
  217. Pose pb = newp.pose();
  218. sum += model.moveProbability(pa, pb, r)*p.get();
  219. }
  220. newp.set(sum);
  221. }
  222. System.out.println("Finalizado");
  223. matriz = newmatriz;
  224. matriz.normalize();
  225. imap.setMatriz(matriz);
  226. scanner.clean();
  227. imap.repaint();
  228. } catch (Exception e) {
  229. }
  230. }
  231. @Override
  232. public void keyReleased(KeyEvent arg0) {
  233. if (robot == null)
  234. return;
  235. robot.stop();
  236. }
  237. @Override
  238. public void keyTyped(KeyEvent arg0) {
  239. }
  240. @Override
  241. public void windowOpened(WindowEvent e) {
  242. }
  243. @Override
  244. public void windowClosing(WindowEvent e) {
  245. System.err.println("Fechando...");
  246. if (robot == null)
  247. return;
  248. robot.disconnect();
  249. }
  250. @Override
  251. public void windowClosed(WindowEvent e) {
  252. }
  253. @Override
  254. public void windowIconified(WindowEvent e) {
  255. }
  256. @Override
  257. public void windowDeiconified(WindowEvent e) {
  258. }
  259. @Override
  260. public void windowActivated(WindowEvent e) {
  261. }
  262. @Override
  263. public void windowDeactivated(WindowEvent e) {
  264. }
  265. @Override
  266. public void robotData(DataRead data) {
  267. // posicao do robo
  268. System.out.print(" sang:"+data.getSensorAngle());
  269. System.out.println(" distance:"+data.getDistance());
  270. scanner.addRead(data.getDistance(), data.getSensorAngle()-90);
  271. }
  272. public static void main(String[] args) {
  273. LineMap map = Map.makeMap();
  274. Robot robotv = new VirtualRobot(map);
  275. Robot robotbt = new BluetoothRobot(null);
  276. String s = "LCPRobot";
  277. Object[] possibleValues = { robotv, robotbt, s };
  278. Object robottmp = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.PLAIN_MESSAGE, null,
  279. possibleValues, possibleValues[0]);
  280. Robot robot;
  281. boolean result = false;
  282. if (robottmp == s) {
  283. robot = new LCPRobot();
  284. } else {
  285. robot = (Robot) robottmp;
  286. }
  287. if (robot != null)
  288. result = ((Robot) robot).connect();
  289. if (result == false) {
  290. JOptionPane.showMessageDialog(null, "Não foi possível conectar ao robô");
  291. System.exit(ERROR);
  292. }
  293. SwingUtilities.invokeLater(new Runnable() {
  294. public void run() {
  295. new MainProgram(map, (Robot) robot);
  296. }
  297. });
  298. }
  299. }