c0387e3121bb001711b8bf632416c20d 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.util.ArrayList;
  10. import javax.swing.JFrame;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  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. showHelp();
  55. }
  56. private void showHelp() {
  57. String text = "1,2,3 - Change global map view\n";
  58. text += "s - Set Pose.\n";
  59. text += "l - Show global map trace.\n";
  60. text += "c - Clean maps.\n";
  61. text += "m - Enter robot movement.\n";
  62. text += "r - Enter robo rotation.\n";
  63. text += "a - Colect sonar data.\n";
  64. text += "z - Make sonar continuous scanner.\n";
  65. text += "i - Stop continuous scanner.\n";
  66. text += "g - Save global map image.\n";
  67. text += "f - Save scanner image.\n";
  68. text += "<arrows> - Move robot.\n";
  69. text += "h - help.\n";
  70. JOptionPane.showMessageDialog(null, text, "HELP", JOptionPane.PLAIN_MESSAGE);
  71. }
  72. public void addPoint(Pose p) {
  73. imap.addPoint(p);
  74. }
  75. @Override
  76. public void keyPressed(KeyEvent e) {
  77. char input = e.getKeyChar();
  78. switch (input) {
  79. case '1':
  80. imap.setVisual(0);
  81. break;
  82. case '2':
  83. imap.setVisual(1);
  84. break;
  85. case '3':
  86. imap.setVisual(2);
  87. break;
  88. case 'l':
  89. imap.showLine();
  90. break;
  91. case 'g':
  92. imap.save();
  93. break;
  94. case 'f':
  95. scanner.save();
  96. break;
  97. case 'c':
  98. imap.clean();
  99. scanner.clean();
  100. break;
  101. case 's':
  102. setRobotPose();
  103. break;
  104. case 'm':
  105. moveRobot();
  106. break;
  107. case 'r':
  108. rotateRobot();
  109. break;
  110. case 'a':
  111. colectSonar();
  112. break;
  113. case 'z':
  114. robot.scann(this);
  115. break;
  116. case 'i':
  117. robot.stopScann();
  118. break;
  119. case 'h':
  120. showHelp();
  121. break;
  122. default:
  123. break;
  124. }
  125. if (robot == null)
  126. return;
  127. switch (e.getKeyCode()) {
  128. case KeyEvent.VK_UP:
  129. robot.moveForward();
  130. scanner.clean();
  131. break;
  132. case KeyEvent.VK_DOWN:
  133. robot.moveBackward();
  134. scanner.clean();
  135. break;
  136. case KeyEvent.VK_LEFT:
  137. robot.moveLeft();
  138. scanner.clean();
  139. break;
  140. case KeyEvent.VK_RIGHT:
  141. robot.moveRight();
  142. scanner.clean();
  143. break;
  144. }
  145. }
  146. private void colectSonar() {
  147. int interval;
  148. try {
  149. String rs = JOptionPane.showInputDialog("Interval (degress):");
  150. interval = Integer.parseInt(rs);
  151. } catch (Exception e) {
  152. return;
  153. }
  154. ArrayList<DataPose> data = robot.scann(-90, 90, interval);
  155. if (data == null) return;
  156. Integer name = new Integer((int) (Math.random() * 1000000));
  157. String rand_name = name.toString() + ".txt";
  158. FileWriter fileWriter;
  159. try {
  160. fileWriter = new FileWriter(rand_name);
  161. } catch (IOException e) {
  162. return;
  163. }
  164. PrintWriter printWriter = new PrintWriter(fileWriter);
  165. printWriter.print("x,y,headinng,sonar_ang,read,expected\n");
  166. for (DataPose d : data) {
  167. robotData(d);
  168. double expected = smodel.expectedSonarRead(d.getPose(), d.getSensorAngle()+90);
  169. printWriter.print(d.getPose().getX() + ",");
  170. printWriter.print(d.getPose().getY() + ",");
  171. printWriter.print(d.getPose().getHeading() + ",");
  172. printWriter.print(d.getSensorAngle() + ",");
  173. printWriter.print(d.getDistance() + ",");
  174. printWriter.print(expected + "\n");
  175. }
  176. printWriter.close();
  177. JOptionPane.showMessageDialog(null, "Reads saved in " + rand_name);
  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. scanner.clean();
  185. } catch (Exception e) {
  186. }
  187. }
  188. private void moveRobot() {
  189. try {
  190. String rs = JOptionPane.showInputDialog("Enter distance (cm):");
  191. double r = Double.parseDouble(rs);
  192. robot.move(r);
  193. scanner.clean();
  194. } catch (Exception e) {
  195. }
  196. }
  197. private void setRobotPose() {
  198. try {
  199. String xs = JOptionPane.showInputDialog("Enter x (cm):");
  200. if (xs.length() == 0)
  201. return;
  202. String ys = JOptionPane.showInputDialog("Enter y (cm):");
  203. if (ys.length() == 0)
  204. return;
  205. String as = JOptionPane.showInputDialog("Enter heading (degress):");
  206. if (as.length() == 0)
  207. return;
  208. float x = Float.parseFloat(xs);
  209. float y = Float.parseFloat(ys);
  210. float a = Float.parseFloat(as);
  211. robot.setPose(x, y, a);
  212. scanner.setPose(new Pose(x, y, a));
  213. scanner.clean();
  214. } catch (Exception e) {
  215. }
  216. }
  217. @Override
  218. public void keyReleased(KeyEvent arg0) {
  219. if (robot == null)
  220. return;
  221. robot.stop();
  222. }
  223. @Override
  224. public void keyTyped(KeyEvent arg0) {
  225. }
  226. @Override
  227. public void windowOpened(WindowEvent e) {
  228. }
  229. @Override
  230. public void windowClosing(WindowEvent e) {
  231. System.err.println("Fechando...");
  232. if (robot == null)
  233. return;
  234. robot.disconnect();
  235. }
  236. @Override
  237. public void windowClosed(WindowEvent e) {
  238. }
  239. @Override
  240. public void windowIconified(WindowEvent e) {
  241. }
  242. @Override
  243. public void windowDeiconified(WindowEvent e) {
  244. }
  245. @Override
  246. public void windowActivated(WindowEvent e) {
  247. }
  248. @Override
  249. public void windowDeactivated(WindowEvent e) {
  250. }
  251. @Override
  252. public void robotData(DataPose data) {
  253. // posicao do robo
  254. Pose p = data.getPose();
  255. System.out.println(p);
  256. imap.addPoint(p);
  257. scanner.setPose(p);
  258. if (data.getDistance() == 255) {
  259. return;
  260. }
  261. // ponto do ultrasonico
  262. double sensor_ang = Math.toRadians(data.getSensorAngle() + p.getHeading()-90);
  263. double dx = Math.cos(sensor_ang) * data.getDistance();
  264. double dy = Math.sin(sensor_ang) * data.getDistance();
  265. double expected = smodel.expectedSonarRead(p, data.getSensorAngle()-90);
  266. imap.addRead(p.getX() + dx, p.getY() + dy);
  267. scanner.addRead(p, data.getDistance(), data.getSensorAngle()-90, expected);
  268. }
  269. public static void main(String[] args) {
  270. LineMap map = Map.makeMap();
  271. Robot robotv = new VirtualRobot(map);
  272. Robot robotbt = new BluetoothRobot(null);
  273. Robot robotlcp = new LCPRobot();
  274. Object[] possibleValues = { robotv, robotbt };
  275. Object robot = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.PLAIN_MESSAGE, null,
  276. possibleValues, possibleValues[0]);
  277. boolean result = false;
  278. if (robot != null)
  279. result = ((Robot) robot).connect();
  280. if (result == false) {
  281. JOptionPane.showMessageDialog(null, "Não foi possível conectar ao robô");
  282. System.exit(ERROR);
  283. }
  284. SwingUtilities.invokeLater(new Runnable() {
  285. public void run() {
  286. new MainProgram(map, (Robot) robot);
  287. }
  288. });
  289. }
  290. }