20e590971cb80017145c9b6064c27648 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package robots;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.util.LinkedList;
  6. import lejos.pc.comm.NXTComm;
  7. import lejos.pc.comm.NXTCommException;
  8. import lejos.pc.comm.NXTCommFactory;
  9. import lejos.pc.comm.NXTInfo;
  10. import lejos.robotics.navigation.Pose;
  11. public class BluetoothRobot implements Robot {
  12. private String name;
  13. private NXTComm nxtComm;
  14. public static final byte FORWARD = 0;
  15. public static final byte STOP = 1;
  16. public static final byte EXIT = 2;
  17. public static final byte LEFT = 3;
  18. public static final byte RIGHT = 4;
  19. public static final byte BACKWARD = 5;
  20. private DataOutputStream output;
  21. private DataInputStream input;
  22. private Send sendthread;
  23. private Receiver receivethread;
  24. private LinkedList<DataPose> data;
  25. private class Receiver extends Thread {
  26. public boolean run = true;
  27. @Override
  28. public void run() {
  29. int bytes_valiable = -1;
  30. while(run) {
  31. try {
  32. bytes_valiable = input.available();
  33. } catch (IOException e1) {
  34. // TODO Auto-generated catch block
  35. e1.printStackTrace();
  36. }
  37. if (bytes_valiable >= 0) {
  38. try {
  39. if (input.readByte() != '@') continue;
  40. int angle = input.readByte();
  41. float alpha = input.readFloat();
  42. float x = input.readFloat();
  43. int distance = input.readByte();
  44. float y = input.readFloat();
  45. DataPose d = new DataPose();
  46. d.setDistance(distance);
  47. d.setSensorAngle(angle);
  48. d.setPose(new Pose(x, y, alpha+90));
  49. data.add(d);
  50. } catch (IOException e1) {
  51. continue;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. private class Send extends Thread {
  58. private int before;
  59. private int command;
  60. private boolean run = true;
  61. @Override
  62. public void run() {
  63. before = -1;
  64. command = STOP;
  65. while(run) {
  66. if (before != command) {
  67. try {
  68. System.out.println("Send cmd");
  69. output.write(command);
  70. output.flush();
  71. if (command == EXIT) run = false;
  72. } catch (IOException e1) {
  73. System.out.println("Erro send cmd");
  74. before = -1;
  75. send(command);
  76. }
  77. before = command;
  78. }
  79. try {
  80. Thread.sleep(50);
  81. } catch (InterruptedException e) {
  82. }
  83. }
  84. }
  85. public void send(int cmd) {
  86. command = cmd;
  87. }
  88. }
  89. BluetoothRobot (String name) {
  90. this.name = name;
  91. data = new LinkedList<DataPose>();
  92. }
  93. @Override
  94. public void moveForward() {
  95. sendthread.send(FORWARD);
  96. }
  97. @Override
  98. public void moveLeft() {
  99. sendthread.send(LEFT);
  100. }
  101. @Override
  102. public void moveRight() {
  103. sendthread.send(RIGHT);
  104. }
  105. @Override
  106. public void moveBackward() {
  107. sendthread.send(BACKWARD);
  108. }
  109. @Override
  110. public boolean connect () {
  111. try {
  112. nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  113. NXTInfo[] nxtInfo = nxtComm.search(name); //find brick with NXT_ID by doing a Bluetooth inquiry
  114. if (nxtInfo.length == 0) { // failed to find a brick with the ID
  115. System.err.println("NO NXT found");
  116. return false;
  117. }
  118. if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
  119. System.err.println("Failed to open NXT");
  120. return false;
  121. }
  122. input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
  123. output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
  124. } catch (NXTCommException e) {
  125. return false;
  126. }
  127. receivethread.start();
  128. sendthread.start();
  129. return true;
  130. }
  131. @Override
  132. public void stop() {
  133. sendthread.send(STOP);
  134. }
  135. @Override
  136. public void exit() {
  137. sendthread.send(EXIT);
  138. input.close();
  139. output.close();
  140. }
  141. @Override
  142. public DataPose getData() {
  143. return data.pollFirst();
  144. }
  145. }