BluetoothRobot.java 7.3 KB

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