90e0a2cb26bb001711b8bf632416c20d 7.8 KB

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