d0078ed7ddb90017124d9f37712eb7c1 5.2 KB

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