905643571eb80017145c9b6064c27648 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package robots;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.util.LinkedList;
  6. import lejos.pc.comm.NXTComm;
  7. import lejos.pc.comm.NXTCommException;
  8. import lejos.pc.comm.NXTCommFactory;
  9. import lejos.pc.comm.NXTInfo;
  10. import lejos.robotics.navigation.Pose;
  11. public class BluetoothRobot implements Robot {
  12. private String name;
  13. private NXTComm nxtComm;
  14. private RobotReturn rr;
  15. public static final byte FORWARD = 0;
  16. public static final byte STOP = 1;
  17. public static final byte EXIT = 2;
  18. public static final byte LEFT = 3;
  19. public static final byte RIGHT = 4;
  20. public static final byte BACKWARD = 5;
  21. private DataOutputStream output;
  22. private DataInputStream input;
  23. private Send sendthread;
  24. private Receiver receivethread;
  25. private class Receiver extends Thread {
  26. public boolean run = true;
  27. @Override
  28. public void run() {
  29. int bytes_valiable = -1;
  30. while(run) {
  31. try {
  32. bytes_valiable = input.available();
  33. } catch (IOException e1) {
  34. // TODO Auto-generated catch block
  35. e1.printStackTrace();
  36. }
  37. if (bytes_valiable >= 0) {
  38. try {
  39. if (input.readByte() != '@') continue;
  40. int angle = input.readByte();
  41. float alpha = input.readFloat();
  42. float x = input.readFloat();
  43. int distance = input.readByte();
  44. float y = input.readFloat();
  45. DataPose d = new DataPose();
  46. d.setDistance(distance);
  47. d.setSensorAngle(angle);
  48. d.setPose(new Pose(x, y, alpha+90));
  49. rr.robotData(d);
  50. } catch (IOException e1) {
  51. continue;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. private class Send extends Thread {
  58. private int before;
  59. private int command;
  60. private boolean run = true;
  61. @Override
  62. public void run() {
  63. before = -1;
  64. command = STOP;
  65. while(run) {
  66. if (before != command) {
  67. try {
  68. System.out.println("Send cmd");
  69. output.write(command);
  70. output.flush();
  71. if (command == EXIT) run = false;
  72. } catch (IOException e1) {
  73. System.out.println("Erro send cmd");
  74. before = -1;
  75. send(command);
  76. }
  77. before = command;
  78. }
  79. try {
  80. Thread.sleep(50);
  81. } catch (InterruptedException e) {
  82. }
  83. }
  84. }
  85. public void send(int cmd) {
  86. command = cmd;
  87. }
  88. }
  89. public BluetoothRobot (String name) {
  90. this.name = name;
  91. data = new LinkedList<DataPose>();
  92. }
  93. @Override
  94. public void moveForward() {
  95. sendthread.send(FORWARD);
  96. }
  97. @Override
  98. public void moveLeft() {
  99. sendthread.send(LEFT);
  100. }
  101. @Override
  102. public void moveRight() {
  103. sendthread.send(RIGHT);
  104. }
  105. @Override
  106. public void moveBackward() {
  107. sendthread.send(BACKWARD);
  108. }
  109. @Override
  110. public boolean connect (RobotReturn r) {
  111. rr = r;
  112. try {
  113. nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  114. NXTInfo[] nxtInfo = nxtComm.search(name); //find brick with NXT_ID by doing a Bluetooth inquiry
  115. if (nxtInfo.length == 0) { // failed to find a brick with the ID
  116. System.err.println("NO NXT found");
  117. return false;
  118. }
  119. if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
  120. System.err.println("Failed to open NXT");
  121. return false;
  122. }
  123. input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
  124. output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
  125. } catch (NXTCommException e) {
  126. return false;
  127. }
  128. receivethread.start();
  129. sendthread.start();
  130. return true;
  131. }
  132. @Override
  133. public void stop() {
  134. sendthread.send(STOP);
  135. }
  136. @Override
  137. public void exit() {
  138. sendthread.send(EXIT);
  139. receivethread.run = false;
  140. sendthread.send(EXIT);
  141. try {
  142. receivethread.join();
  143. sendthread.join();
  144. } catch (InterruptedException e1) {
  145. System.out.println("Nao foi possivel finalizar as threads...");
  146. }
  147. try {
  148. nxtComm.close();
  149. } catch (IOException e) {
  150. // TODO Auto-generated catch block
  151. e.printStackTrace();
  152. }
  153. }
  154. }