f09bc35fdfb90017124d9f37712eb7c1 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. Scanner (DataOutputStream output, OdometryPoseProvider provider) {
  20. super();
  21. this.output = output;
  22. this.provider = provider;
  23. run = true;
  24. scann = false;
  25. }
  26. public void stop () {
  27. run = false;
  28. }
  29. public void run() {
  30. NXTRegulatedMotor scannerm = Motor.A;
  31. UltrasonicSensor sensor = new UltrasonicSensor(SensorPort.S1) ;
  32. int increment = 5;
  33. int position = 0;
  34. while (run) {
  35. if (scann == false) {
  36. scannerm.rotateTo(0);
  37. continue;
  38. }
  39. if (position == 90 || position == -90)
  40. increment *= -1;
  41. scannerm.rotateTo(position);
  42. scannerm.waitComplete();
  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. if (distance != 255) {
  49. try {
  50. output.write('@');
  51. output.write(position);
  52. output.writeFloat(alpha);
  53. output.writeFloat(x);
  54. output.write(distance);
  55. output.writeFloat(y);
  56. output.flush();
  57. } catch (IOException e) {
  58. }
  59. }
  60. position += increment;
  61. }
  62. scannerm.rotateTo(0);
  63. scannerm.waitComplete();
  64. }
  65. }
  66. public class Sonar {
  67. private static final byte FORWARD = 0;
  68. private static final byte STOP = 1;
  69. private static final byte EXIT = 2;
  70. private static final byte LEFT = 3;
  71. private static final byte RIGHT = 4;
  72. private static final byte BACKWARD = 5;
  73. public static final byte STOPSCANN = 6;
  74. public static final byte STARTSCANN = 7;
  75. public static final byte MOVE = 8;
  76. public static final byte ROTATE = 9;
  77. public static final byte SETPOSE = 10;
  78. public static final byte SETSCANANGLE = 11;
  79. public static void main(String[] args) throws Exception {
  80. BTConnection btc = Bluetooth.waitForConnection();
  81. //USBConnection btc = USB.waitForConnection();
  82. DataInputStream input = btc.openDataInputStream();
  83. DataOutputStream output = btc.openDataOutputStream();
  84. DifferentialPilot pilot = new DifferentialPilot(5.6f, 13.8f, Motor.B, Motor.C, false);
  85. OdometryPoseProvider provider = new OdometryPoseProvider(pilot);
  86. pilot.setRotateSpeed(5);
  87. pilot.setTravelSpeed(20);
  88. Scanner scan = new Scanner(output, provider);
  89. scan.start();
  90. int input_byte;
  91. boolean run = true;
  92. Sound.twoBeeps();
  93. while (run) {
  94. if (input.available() <= 0) {
  95. Thread.yield();
  96. continue;
  97. }
  98. input_byte = input.readByte();
  99. System.out.println(input_byte);
  100. switch (input_byte) {
  101. case FORWARD:
  102. pilot.forward();
  103. break;
  104. case STOP:
  105. pilot.stop();
  106. break;
  107. case EXIT:
  108. run = false;
  109. break;
  110. case LEFT:
  111. pilot.rotateLeft();
  112. break;
  113. case RIGHT:
  114. pilot.rotateRight();
  115. break;
  116. case BACKWARD:
  117. pilot.backward();
  118. break;
  119. case STOPSCANN:
  120. pilot.scann = true;
  121. break;
  122. }
  123. }
  124. Sound.beep();
  125. scan.stop();
  126. scan.join();
  127. }
  128. }