60767e52ddb90017124d9f37712eb7c1 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package robots;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.concurrent.Semaphore;
  7. import lejos.pc.comm.NXTComm;
  8. import lejos.pc.comm.NXTCommException;
  9. import lejos.pc.comm.NXTCommFactory;
  10. import lejos.pc.comm.NXTInfo;
  11. import lejos.robotics.navigation.Pose;
  12. public class BluetoothRobot implements Robot {
  13. private String name;
  14. private NXTComm nxtComm;
  15. private RobotReturn rr;
  16. private Semaphore semaphore;
  17. public static final byte FORWARD = 0;
  18. public static final byte STOP = 1;
  19. public static final byte EXIT = 2;
  20. public static final byte LEFT = 3;
  21. public static final byte RIGHT = 4;
  22. public static final byte BACKWARD = 5;
  23. public static final byte STOPSCANN = 6;
  24. public static final byte STARTSCANN = 7;
  25. public static final byte MOVE = 8;
  26. public static final byte ROTATE = 9;
  27. public static final byte SETPOSE = 10;
  28. public static final byte SCANN = 11;
  29. private DataOutputStream output;
  30. private DataInputStream input;
  31. private Receiver receivethread;
  32. private class Receiver extends Thread {
  33. public boolean run = true;
  34. @Override
  35. public void run() {
  36. int bytes_valiable = -1;
  37. while(run) {
  38. try {
  39. bytes_valiable = input.available();
  40. } catch (IOException e1) {
  41. // TODO Auto-generated catch block
  42. e1.printStackTrace();
  43. }
  44. if (bytes_valiable >= 0) {
  45. try {
  46. if (input.readByte() != '@') continue;
  47. int angle = input.readByte();
  48. float alpha = input.readFloat();
  49. float x = input.readFloat();
  50. int distance = input.readByte();
  51. float y = input.readFloat();
  52. DataPose d = new DataPose();
  53. d.setDistance(distance);
  54. d.setSensorAngle(angle);
  55. d.setPose(new Pose(x, y, alpha+90));
  56. rr.robotData(d);
  57. } catch (IOException e1) {
  58. continue;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. private void send(int command) {
  65. if (semaphore.tryAcquire()) {
  66. try {
  67. output.write(command);
  68. output.flush();
  69. } catch (IOException e) {
  70. System.out.println("Erro ao enviar...");
  71. }
  72. semaphore.release();
  73. }
  74. }
  75. private void send(int command, float f) {
  76. if (semaphore.tryAcquire()) {
  77. try {
  78. output.write(command);
  79. output.writeFloat(f);
  80. output.flush();
  81. } catch (IOException e) {
  82. System.out.println("Erro ao enviar...");
  83. }
  84. semaphore.release();
  85. }
  86. }
  87. private void send(int command, int i) {
  88. if (semaphore.tryAcquire()) {
  89. try {
  90. output.write(command);
  91. output.write(i);
  92. output.flush();
  93. } catch (IOException e) {
  94. System.out.println("Erro ao enviar...");
  95. }
  96. semaphore.release();
  97. }
  98. }
  99. private void send(Pose p) {
  100. if (semaphore.tryAcquire()) {
  101. try {
  102. output.write(SETPOSE);
  103. output.writeFloat(p.getX());
  104. output.writeFloat(p.getY());
  105. output.writeFloat(p.getHeading());
  106. output.flush();
  107. } catch (IOException e) {
  108. System.out.println("Erro ao enviar...");
  109. }
  110. semaphore.release();
  111. }
  112. }
  113. public BluetoothRobot (String name) {
  114. semaphore = new Semaphore(1);
  115. this.name = name;
  116. }
  117. @Override
  118. public void moveForward() {
  119. send(FORWARD);
  120. }
  121. @Override
  122. public void moveLeft() {
  123. send(LEFT);
  124. }
  125. @Override
  126. public void moveRight() {
  127. send(RIGHT);
  128. }
  129. @Override
  130. public void moveBackward() {
  131. send(BACKWARD);
  132. }
  133. @Override
  134. public boolean connect () {
  135. try {
  136. nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  137. NXTInfo[] nxtInfo = nxtComm.search(name); //find brick with NXT_ID by doing a Bluetooth inquiry
  138. if (nxtInfo.length == 0) { // failed to find a brick with the ID
  139. System.err.println("NO NXT found");
  140. return false;
  141. }
  142. if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
  143. System.err.println("Failed to open NXT");
  144. return false;
  145. }
  146. input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
  147. output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
  148. } catch (NXTCommException e) {
  149. return false;
  150. }
  151. receivethread.start();
  152. return true;
  153. }
  154. @Override
  155. public void stop() {
  156. send(STOP);
  157. }
  158. @Override
  159. public void move(double x) {
  160. send(MOVE, (float)x);
  161. }
  162. @Override
  163. public void rotate(double x) {
  164. send(ROTATE, (float)x);
  165. }
  166. @Override
  167. public ArrayList<DataPose> scann(int ini, int end, int interval) {
  168. send(SCANN, interval);
  169. return null;
  170. }
  171. @Override
  172. public void scann(RobotReturn r) {
  173. rr = r;
  174. send(STARTSCANN);
  175. }
  176. @Override
  177. public void stopScann() {
  178. send(STOPSCANN);
  179. }
  180. @Override
  181. public void disconnect() {
  182. send(EXIT);
  183. receivethread.run = false;
  184. try {
  185. receivethread.join();
  186. } catch (InterruptedException e1) {
  187. System.out.println("Nao foi possivel finalizar as threads...");
  188. }
  189. try {
  190. nxtComm.close();
  191. } catch (IOException e) {
  192. // TODO Auto-generated catch block
  193. e.printStackTrace();
  194. }
  195. }
  196. @Override
  197. public void setPose(float x, float y, float a) {
  198. send(new Pose(x, y, a));
  199. }
  200. }