BluetoothRobot.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 readm;
  17. private Semaphore sendm;
  18. private int lastcmd = -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. public static final byte TYPE_INT = 1;
  32. public static final byte TYPE_CMD = 2;
  33. public static final byte TYPE_POSE = 3;
  34. public static final byte TYPE_FLOAT = 4;
  35. private static final int scannangle = 5;
  36. private DataOutputStream output;
  37. private DataInputStream input;
  38. private Receiver receivethread;
  39. private Sender sendthread;
  40. private ArrayList<DataRead> reads = null;
  41. private ArrayList<SendData> tosend;
  42. private class Receiver extends Thread {
  43. public boolean run = true;
  44. @Override
  45. public void run() {
  46. int bytes_valiable = -1;
  47. while(run) {
  48. try {
  49. bytes_valiable = input.available();
  50. } catch (IOException e1) {
  51. // TODO Auto-generated catch block
  52. e1.printStackTrace();
  53. }
  54. if (bytes_valiable >= 0) {
  55. try {
  56. if (input.readByte() != '@') continue;
  57. int angle = input.readByte();
  58. float distance = input.readFloat();
  59. System.out.println(distance);
  60. DataRead d = new DataRead();
  61. d.setDistance(distance);
  62. d.setSensorAngle(-angle);
  63. if (rr != null)
  64. rr.robotData(d);
  65. if (reads != null) {
  66. readm.tryAcquire();
  67. reads.add(d);
  68. readm.release();
  69. }
  70. } catch (IOException e1) {
  71. continue;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. private class Sender extends Thread {
  78. public boolean run = true;
  79. @Override
  80. public void run() {
  81. while(run) {
  82. SendData value = null;
  83. if (sendm.tryAcquire()) {
  84. if (tosend.size() > 0) {
  85. value = tosend.get(0);
  86. tosend.remove(0);
  87. }
  88. sendm.release();
  89. }
  90. if (value != null)
  91. value.send(output);
  92. else
  93. try {
  94. Thread.sleep(100);
  95. } catch (InterruptedException e) {
  96. }
  97. }
  98. }
  99. }
  100. private class SendData {
  101. float f;
  102. int i;
  103. int cmd;
  104. Pose p;
  105. int type;
  106. SendData(int cmd) {
  107. this.cmd = cmd;
  108. this.type = TYPE_CMD;
  109. }
  110. SendData(int cmd, int i) {
  111. this.cmd = cmd;
  112. this.i = i;
  113. this.type = TYPE_INT;
  114. }
  115. SendData(int cmd, float f) {
  116. this.cmd = cmd;
  117. this.f = f;
  118. this.type = TYPE_FLOAT;
  119. }
  120. SendData(int cmd, Pose p) {
  121. this.cmd = cmd;
  122. this.p = p;
  123. this.type = TYPE_POSE;
  124. }
  125. public boolean send(DataOutputStream output) {
  126. try {
  127. switch (type) {
  128. case TYPE_CMD:
  129. output.write(cmd);
  130. break;
  131. case TYPE_INT:
  132. output.write(cmd);
  133. output.write(i);
  134. break;
  135. case TYPE_FLOAT:
  136. output.write(cmd);
  137. output.writeFloat(f);
  138. break;
  139. case TYPE_POSE:
  140. output.write(cmd);
  141. output.writeFloat(p.getX());
  142. output.writeFloat(p.getY());
  143. output.writeFloat(p.getHeading());
  144. break;
  145. default:
  146. return false;
  147. }
  148. output.flush();
  149. } catch (IOException e) {
  150. return false;
  151. }
  152. return true;
  153. }
  154. }
  155. public BluetoothRobot (String name) {
  156. readm = new Semaphore(1);
  157. sendm = new Semaphore(1);
  158. tosend = new ArrayList<SendData>();
  159. receivethread = new Receiver();
  160. sendthread = new Sender();
  161. this.name = name;
  162. }
  163. public void send (SendData sd) {
  164. if (sendm.tryAcquire()) {
  165. tosend.add(sd);
  166. sendm.release();
  167. }
  168. }
  169. @Override
  170. public void moveForward() {
  171. if (lastcmd == FORWARD) return;
  172. send(new SendData(FORWARD));
  173. lastcmd = FORWARD;
  174. }
  175. @Override
  176. public void moveLeft() {
  177. if (lastcmd == LEFT) return;
  178. send(new SendData(LEFT));
  179. lastcmd = LEFT;
  180. }
  181. @Override
  182. public void moveRight() {
  183. if (lastcmd == RIGHT) return;
  184. send(new SendData(RIGHT));
  185. lastcmd = RIGHT;
  186. }
  187. @Override
  188. public void moveBackward() {
  189. if (lastcmd == BACKWARD) return;
  190. send(new SendData(BACKWARD));
  191. lastcmd = BACKWARD;
  192. }
  193. @Override
  194. public boolean connect () {
  195. try {
  196. nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  197. NXTInfo[] nxtInfo = nxtComm.search(name); //find brick with NXT_ID by doing a Bluetooth inquiry
  198. if (nxtInfo.length == 0) { // failed to find a brick with the ID
  199. System.err.println("NO NXT found");
  200. return false;
  201. }
  202. if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
  203. System.err.println("Failed to open NXT");
  204. return false;
  205. }
  206. input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
  207. output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
  208. send(new SendData(SETSCANANGLE, scannangle)); // vai scanear em 5 em 5 graus
  209. } catch (NXTCommException e) {
  210. return false;
  211. }
  212. receivethread.start();
  213. sendthread.start();
  214. return true;
  215. }
  216. @Override
  217. public void stop() {
  218. send(new SendData(STOP));
  219. lastcmd = -1;
  220. }
  221. @Override
  222. public void move(double x) {
  223. send(new SendData(MOVE, (float)x));
  224. }
  225. @Override
  226. public void rotate(double x) {
  227. send(new SendData(ROTATE, (float)x));
  228. }
  229. @Override
  230. public ArrayList<DataRead> scann(int ini, int end, int interval) {
  231. int readsn = Math.abs(ini-end)/interval;
  232. send(new SendData(SETSCANANGLE, interval));
  233. ArrayList<DataRead> tmp;
  234. reads = new ArrayList<DataRead>();
  235. scann(null);
  236. while(true) {
  237. readm.tryAcquire();
  238. if (reads.size() >= readsn+1) break;
  239. readm.release();
  240. try {
  241. Thread.sleep(50);
  242. } catch (InterruptedException e) {
  243. }
  244. }
  245. while (readsn+1 < reads.size() ) {
  246. reads.remove(reads.size()-1);
  247. }
  248. tmp = reads;
  249. reads = null;
  250. readm.release();
  251. stopScann();
  252. send(new SendData(SETSCANANGLE, scannangle));
  253. return tmp;
  254. }
  255. @Override
  256. public void scann(RobotReturn r) {
  257. rr = r;
  258. send(new SendData(STARTSCANN));
  259. }
  260. @Override
  261. public void stopScann() {
  262. send(new SendData(STOPSCANN));
  263. }
  264. @Override
  265. public void disconnect() {
  266. send(new SendData(EXIT));
  267. if (receivethread == null) return;
  268. receivethread.run = false;
  269. try {
  270. receivethread.join();
  271. } catch (InterruptedException e1) {
  272. System.out.println("Nao foi possivel finalizar as threads...");
  273. }
  274. if (sendthread == null) return;
  275. sendthread.run = false;
  276. try {
  277. sendthread.join();
  278. } catch (InterruptedException e1) {
  279. System.out.println("Nao foi possivel finalizar as threads...");
  280. }
  281. try {
  282. nxtComm.close();
  283. } catch (IOException e) {
  284. }
  285. }
  286. @Override
  287. public void setPose(float x, float y, float a) {
  288. send(new SendData(SETPOSE, new Pose(-y, x, a)));
  289. }
  290. @Override
  291. public String toString() {
  292. return "Bluetooth Mestre/Escravo";
  293. }
  294. }