80f4cd6218b80017145c9b6064c27648 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.DataInputStream;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import javax.swing.JFrame;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JPanel;
  12. import javax.swing.JSplitPane;
  13. import javax.swing.SwingUtilities;
  14. import javax.swing.event.AncestorEvent;
  15. import javax.swing.event.AncestorListener;
  16. import config.Map;
  17. import lejos.pc.comm.NXTComm;
  18. import lejos.pc.comm.NXTCommException;
  19. import lejos.pc.comm.NXTCommFactory;
  20. import lejos.pc.comm.NXTInfo;
  21. import lejos.robotics.mapping.LineMap;
  22. import lejos.robotics.navigation.Pose;
  23. import robots.Robot;
  24. import robots.VirtualRobot;
  25. public class MainProgram extends JPanel implements KeyListener, WindowListener {
  26. private MapImage imap;
  27. private DataOutputStream output;
  28. private DataInputStream input;
  29. private Send sendthread;
  30. private Receiver receivethread;
  31. public static final byte FORWARD = 0;
  32. public static final byte STOP = 1;
  33. public static final byte EXIT = 2;
  34. public static final byte LEFT = 3;
  35. public static final byte RIGHT = 4;
  36. public static final byte BACKWARD = 5;
  37. private class Receiver extends Thread {
  38. public boolean run = true;
  39. @Override
  40. public void run() {
  41. int bytes_valiable = -1;
  42. while(run) {
  43. try {
  44. bytes_valiable = input.available();
  45. } catch (IOException e1) {
  46. // TODO Auto-generated catch block
  47. e1.printStackTrace();
  48. }
  49. if (bytes_valiable >= 0) {
  50. try {
  51. if (input.readByte() != '@') continue;
  52. int angle = input.readByte();
  53. float alpha = input.readFloat();
  54. float x = input.readFloat();
  55. int distance = input.readByte();
  56. float y = input.readFloat();
  57. // posicao do robo
  58. imap.addPoint(x, y, Math.toRadians(alpha+90));
  59. // ponto do ultrasonico
  60. double sensor_ang = Math.toRadians(alpha+angle);
  61. double dx = Math.cos(sensor_ang)*distance;
  62. double dy = Math.sin(sensor_ang)*distance;
  63. imap.addRead(x+dx, y+dy);
  64. } catch (IOException e1) {
  65. continue;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. private class Send extends Thread {
  72. private int before;
  73. private int command;
  74. private boolean run = true;
  75. @Override
  76. public void run() {
  77. before = -1;
  78. command = STOP;
  79. while(run) {
  80. if (before != command) {
  81. try {
  82. System.out.println("Send cmd");
  83. output.write(command);
  84. output.flush();
  85. if (command == EXIT) run = false;
  86. } catch (IOException e1) {
  87. System.out.println("Erro send cmd");
  88. before = -1;
  89. send(command);
  90. }
  91. before = command;
  92. }
  93. try {
  94. Thread.sleep(50);
  95. } catch (InterruptedException e) {
  96. }
  97. }
  98. }
  99. public void send(int cmd) {
  100. command = cmd;
  101. }
  102. }
  103. public MainProgram (LineMap map) {
  104. JFrame frame = new JFrame("Mapa MAC0318");
  105. receivethread= new Receiver();
  106. sendthread = new Send();
  107. // output = out;
  108. // input = in;
  109. // receivethread.start();
  110. // sendthread.start();
  111. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112. frame.setLayout(new BorderLayout());
  113. imap = new MapImage(map);
  114. ScannerImage scanner = new ScannerImage();
  115. // frame.add(this.map);
  116. frame.setSize(800, 800);
  117. frame.setVisible(true);
  118. frame.setFocusable(true);
  119. frame.requestFocusInWindow();
  120. frame.addKeyListener(this);
  121. frame.addWindowListener(this);
  122. JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scanner, imap);
  123. splitPane.setOneTouchExpandable(true);
  124. splitPane.setDividerLocation((int)(frame.getHeight()/2));
  125. frame.add(splitPane);
  126. //
  127. // String text = "1,2,3 - Change view mode.\n";
  128. // text += "s - Save image.\n";
  129. // text += "l - Show trace.\n";
  130. // text += "c - Clean map.\n";
  131. // JOptionPane.showMessageDialog(null, text);
  132. }
  133. public void addPoint(Pose p) {
  134. imap.addPoint(p);
  135. }
  136. @Override
  137. public void keyPressed(KeyEvent e) {
  138. char input = e.getKeyChar();
  139. if (input == '1') imap.setVisual(0);
  140. else if (input == '2') imap.setVisual(1);
  141. else if (input == '3') imap.setVisual(2);
  142. else if (input == 'l') imap.showLine();
  143. else if (input == 's') imap.save();
  144. else if (input == 'c') imap.clean();
  145. switch(e.getKeyCode()) {
  146. case KeyEvent.VK_UP:
  147. sendthread.send(FORWARD);
  148. break;
  149. case KeyEvent.VK_DOWN:
  150. sendthread.send(BACKWARD);
  151. break;
  152. case KeyEvent.VK_LEFT:
  153. sendthread.send(LEFT);
  154. break;
  155. case KeyEvent.VK_RIGHT :
  156. sendthread.send(RIGHT);
  157. break;
  158. }
  159. }
  160. @Override
  161. public void keyReleased(KeyEvent arg0) {
  162. sendthread.send(STOP);
  163. }
  164. @Override
  165. public void keyTyped(KeyEvent arg0) {
  166. }
  167. public static void main(String[] args) throws NXTCommException, IOException {
  168. LineMap map = Map.makeMap();
  169. Robot robot = new VirtualRobot(map);
  170. // NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  171. // NXTInfo[] nxtInfo = nxtComm.search("NXT8"); //find brick with NXT_ID by doing a Bluetooth inquiry
  172. // if (nxtInfo.length == 0) { // failed to find a brick with the ID
  173. // System.err.println("NO NXT found");
  174. // System.exit(1);
  175. // }
  176. // if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
  177. // System.err.println("Failed to open NXT");
  178. // System.exit(1);
  179. // }
  180. //
  181. // final DataInputStream input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
  182. // final DataOutputStream output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
  183. //
  184. SwingUtilities.invokeLater(new Runnable() {
  185. public void run() {
  186. //new Map(input, output);
  187. new MainProgram(map);
  188. }
  189. });
  190. }
  191. @Override
  192. public void windowOpened(WindowEvent e) {
  193. }
  194. @Override
  195. public void windowClosing(WindowEvent e) {
  196. System.err.println("Fechando...");
  197. receivethread.run = false;
  198. sendthread.send(EXIT);
  199. try {
  200. receivethread.join();
  201. sendthread.join();
  202. } catch (InterruptedException e1) {
  203. System.out.println("Nao foi possivel finalizar as threads...");
  204. }
  205. }
  206. @Override
  207. public void windowClosed(WindowEvent e) {
  208. }
  209. @Override
  210. public void windowIconified(WindowEvent e) {
  211. }
  212. @Override
  213. public void windowDeiconified(WindowEvent e) {
  214. }
  215. @Override
  216. public void windowActivated(WindowEvent e) {
  217. }
  218. @Override
  219. public void windowDeactivated(WindowEvent e) {
  220. }
  221. }