30f7b5eac1b90017124d9f37712eb7c1 3.8 KB

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