c0b0c0ed22bb001711b8bf632416c20d 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import lejos.nxt.Motor;
  5. import lejos.nxt.NXTRegulatedMotor;
  6. import lejos.nxt.SensorPort;
  7. import lejos.nxt.Sound;
  8. import lejos.nxt.UltrasonicSensor;
  9. import lejos.nxt.comm.BTConnection;
  10. import lejos.nxt.comm.Bluetooth;
  11. import lejos.robotics.navigation.DifferentialPilot;
  12. import lejos.robotics.navigation.Pose;
  13. import lejos.robotics.localization.*;
  14. class Scanner extends Thread {
  15. DataOutputStream output;
  16. OdometryPoseProvider provider;
  17. boolean run;
  18. public boolean scann;
  19. public int increment = 5;
  20. int position = 0;
  21. Scanner (DataOutputStream output, OdometryPoseProvider provider) {
  22. super();
  23. this.output = output;
  24. this.provider = provider;
  25. run = true;
  26. scann = false;
  27. }
  28. public void stop () {
  29. run = false;
  30. }
  31. public void run() {
  32. NXTRegulatedMotor scannerm = Motor.A;
  33. UltrasonicSensor sensor = new UltrasonicSensor(SensorPort.S1) ;
  34. while (run) {
  35. if (scann == false)
  36. position = 0;
  37. if (position == 90 || position == -90)
  38. increment *= -1;
  39. scannerm.rotateTo(position);
  40. scannerm.waitComplete();
  41. if (scann == false)
  42. continue;
  43. int distance = sensor.getDistance();
  44. Pose pose = provider.getPose();
  45. float x = pose.getX();
  46. float y = pose.getY();
  47. float alpha = pose.getHeading();
  48. try {
  49. output.write('@');
  50. output.write(position);
  51. output.writeFloat(alpha);
  52. output.writeFloat(x);
  53. output.write(distance);
  54. output.writeFloat(y);
  55. output.flush();
  56. } catch (IOException e) {
  57. }
  58. position += increment;
  59. }
  60. scannerm.rotateTo(0);
  61. scannerm.waitComplete();
  62. }
  63. }
  64. public class Sonar {
  65. private static final byte FORWARD = 0;
  66. private static final byte STOP = 1;
  67. private static final byte EXIT = 2;
  68. private static final byte LEFT = 3;
  69. private static final byte RIGHT = 4;
  70. private static final byte BACKWARD = 5;
  71. public static final byte STOPSCANN = 6;
  72. public static final byte STARTSCANN = 7;
  73. public static final byte MOVE = 8;
  74. public static final byte ROTATE = 9;
  75. public static final byte SETPOSE = 10;
  76. public static final byte SETSCANANGLE = 11;
  77. public static void main(String[] args) throws Exception {
  78. BTConnection btc = Bluetooth.waitForConnection();
  79. //USBConnection btc = USB.waitForConnection();
  80. DataInputStream input = btc.openDataInputStream();
  81. DataOutputStream output = btc.openDataOutputStream();
  82. DifferentialPilot pilot = new DifferentialPilot(5.6f, 13.8f, Motor.B, Motor.C, false);
  83. OdometryPoseProvider provider = new OdometryPoseProvider(pilot);
  84. pilot.setRotateSpeed(5);
  85. pilot.setTravelSpeed(20);
  86. Scanner scan = new Scanner(output, provider);
  87. scan.start();
  88. int input_byte;
  89. boolean run = true;
  90. Sound.twoBeeps();
  91. while (run) {
  92. if (input.available() <= 0) {
  93. Thread.yield();
  94. continue;
  95. }
  96. input_byte = input.readByte();
  97. System.out.println(input_byte);
  98. switch (input_byte) {
  99. case FORWARD:
  100. pilot.forward();
  101. break;
  102. case STOP:
  103. pilot.stop();
  104. break;
  105. case EXIT:
  106. run = false;
  107. break;
  108. case LEFT:
  109. pilot.rotateLeft();
  110. break;
  111. case RIGHT:
  112. pilot.rotateRight();
  113. break;
  114. case BACKWARD:
  115. pilot.backward();
  116. break;
  117. case STOPSCANN:
  118. scan.scann = false;
  119. break;
  120. case STARTSCANN:
  121. scan.position = 90;
  122. scan.increment = -Math.abs(scan.increment);
  123. scan.scann = true;
  124. break;
  125. case MOVE:
  126. float d = input.readFloat();
  127. pilot.travel(d);
  128. break;
  129. case ROTATE:
  130. float r = input.readFloat();
  131. pilot.rotate(r);
  132. break;
  133. case SETPOSE:
  134. float x = input.readFloat();
  135. float y = input.readFloat();
  136. float a = input.readFloat();
  137. System.out.println("x: "+x);
  138. System.out.println("y: "+y);
  139. System.out.println("a: "+a);
  140. provider.setPose(new Pose(x,y,a));
  141. break;
  142. case SETSCANANGLE:
  143. int i = input.readByte();
  144. System.out.println("ang: "+i);
  145. scan.increment = i;
  146. break;
  147. }
  148. }
  149. Sound.beep();
  150. scan.stop();
  151. scan.join();
  152. }
  153. }