90dce55b1eb80017145c9b6064c27648 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. }
  92. @Override
  93. public void moveForward() {
  94. sendthread.send(FORWARD);
  95. }
  96. @Override
  97. public void moveLeft() {
  98. sendthread.send(LEFT);
  99. }
  100. @Override
  101. public void moveRight() {
  102. sendthread.send(RIGHT);
  103. }
  104. @Override
  105. public void moveBackward() {
  106. sendthread.send(BACKWARD);
  107. }
  108. @Override
  109. public boolean connect (RobotReturn r) {
  110. rr = r;
  111. try {
  112. nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  113. NXTInfo[] nxtInfo = nxtComm.search(name); //find brick with NXT_ID by doing a Bluetooth inquiry
  114. if (nxtInfo.length == 0) { // failed to find a brick with the ID
  115. System.err.println("NO NXT found");
  116. return false;
  117. }
  118. if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
  119. System.err.println("Failed to open NXT");
  120. return false;
  121. }
  122. input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
  123. output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
  124. } catch (NXTCommException e) {
  125. return false;
  126. }
  127. receivethread.start();
  128. sendthread.start();
  129. return true;
  130. }
  131. @Override
  132. public void stop() {
  133. sendthread.send(STOP);
  134. }
  135. @Override
  136. public void exit() {
  137. sendthread.send(EXIT);
  138. receivethread.run = false;
  139. sendthread.send(EXIT);
  140. try {
  141. receivethread.join();
  142. sendthread.join();
  143. } catch (InterruptedException e1) {
  144. System.out.println("Nao foi possivel finalizar as threads...");
  145. }
  146. try {
  147. nxtComm.close();
  148. } catch (IOException e) {
  149. // TODO Auto-generated catch block
  150. e.printStackTrace();
  151. }
  152. }
  153. }