a0960bafdeb90017124d9f37712eb7c1 5.6 KB

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