a0c27aa6e2b90017124d9f37712eb7c1 5.8 KB

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