4082a00be4b90017124d9f37712eb7c1 5.8 KB

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