Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. //Билиотека для сервопривода
  2. #include <Servo.h>
  3.  
  4. // Пины драйвера
  5. #define PIN_ENABLE1 10
  6. #define PIN_ENABLE2 12
  7. #define PIN_OUTPUT1 6
  8. #define PIN_OUTPUT2 7
  9. #define PIN_OUTPUT3 8
  10. #define PIN_OUTPUT4 9
  11. #define PIN_OUTPUT5 3
  12. #define TRIG_PIN 4
  13. #define ECHO_PIN 5
  14.  
  15. const int correction = 0;
  16. Servo rangeFinderServo;
  17.  
  18. void writeMotors(int speed1, int speed2) {
  19. // Определение направления вращения моторов
  20. bool dir1 = (speed1 >= 0);
  21. digitalWrite(PIN_OUTPUT1, dir1);
  22. digitalWrite(PIN_OUTPUT2, !dir1);
  23.  
  24. bool dir2 = (speed2 >= 0);
  25. digitalWrite(PIN_OUTPUT3, dir2);
  26. digitalWrite(PIN_OUTPUT4, !dir2);
  27.  
  28. // Если скорости нулевые - стопаем моторчики
  29. if (speed1 == 0 && speed2 == 0) {
  30. digitalWrite(PIN_ENABLE1, LOW);
  31. digitalWrite(PIN_ENABLE2, LOW);
  32. return;
  33. }
  34.  
  35. // Установка скорости (с учетом коррекции)
  36. speed1 = map(abs(speed1), 0, 100, 0, 255);
  37. speed2 = map(abs(speed2), 0, 100, 0, 255);
  38.  
  39. if (correction >= 0) {
  40. speed1 -= correction;
  41. } else {
  42. speed2 -= abs(correction);
  43. }
  44.  
  45. analogWrite(PIN_ENABLE1, speed1);
  46. analogWrite(PIN_ENABLE2, speed2);
  47. }
  48.  
  49. //Движение вперед
  50. void moveForward() {
  51. writeMotors(100, 100);
  52. }
  53.  
  54. //Движение назад
  55. void moveBackward() {
  56. writeMotors(-50, -50);
  57. }
  58.  
  59. long microsecondsToCentimeters(long microseconds) {
  60. return microseconds / 29 / 2;
  61. }
  62.  
  63. //Датчик приближения
  64. int getDistance() {
  65. digitalWrite(TRIG_PIN, LOW);
  66. delayMicroseconds(2);
  67. digitalWrite(TRIG_PIN, HIGH);
  68. delayMicroseconds(10);
  69. digitalWrite(TRIG_PIN, LOW);
  70.  
  71. return microsecondsToCentimeters(pulseIn(ECHO_PIN, HIGH));
  72. }
  73.  
  74. //Остановить моторы
  75. void stopDrives() {
  76. writeMotors(0, 0);
  77. }
  78.  
  79. void lookLeft() {
  80. rangeFinderServo.attach(3);
  81. rangeFinderServo.write(130);
  82. delay(250);
  83. rangeFinderServo.detach();
  84. }
  85.  
  86. void lookRight() {
  87. rangeFinderServo.attach(3);
  88. rangeFinderServo.write(0);
  89. delay(250);
  90. rangeFinderServo.detach();
  91. }
  92.  
  93. void lookForward() {
  94. rangeFinderServo.attach(3);
  95. rangeFinderServo.write(60);
  96. delay(250);
  97. rangeFinderServo.detach();
  98. }
  99.  
  100. void setup() {
  101. pinMode(PIN_ENABLE1, OUTPUT);
  102. pinMode(PIN_ENABLE2, OUTPUT);
  103. pinMode(PIN_OUTPUT1, OUTPUT);
  104. pinMode(PIN_OUTPUT2, OUTPUT);
  105. pinMode(PIN_OUTPUT3, OUTPUT);
  106. pinMode(PIN_OUTPUT4, OUTPUT);
  107. pinMode(TRIG_PIN, OUTPUT);
  108. pinMode(ECHO_PIN, INPUT);
  109. Serial.begin(9600);
  110. lookForward();
  111.  
  112. }
  113.  
  114. void turnRight() {
  115. writeMotors(100, -100);
  116. delay(250);
  117. stopDrives();
  118. }
  119.  
  120. void turnLeft() {
  121. writeMotors(-100, 100);
  122. delay(250);
  123. stopDrives();
  124. }
  125.  
  126. bool isNearWall() {
  127. int distance = getDistance();
  128. Serial.println(distance);
  129. if (distance < 7 && distance > 1)
  130. return true;
  131.  
  132. return false;
  133. }
  134.  
  135. bool checkLeft() {
  136. bool changedDirection = false;
  137.  
  138. lookLeft();
  139.  
  140. if (!isNearWall()) {
  141. turnLeft();
  142. changedDirection = true;
  143. }
  144.  
  145. lookForward();
  146. return changedDirection;
  147. }
  148.  
  149. bool checkRight() {
  150. bool changedDirection = false;
  151.  
  152. lookRight();
  153.  
  154. if (!isNearWall()) {
  155. turnRight();
  156. changedDirection = true;
  157. }
  158.  
  159. lookForward();
  160. return changedDirection;
  161. }
  162.  
  163. void goBack() {
  164. moveBackward();
  165. delay(500);
  166. stopDrives();
  167. delay(500);
  168. turnRight();
  169. turnRight();
  170. }
  171.  
  172. void loop() {
  173. int distance = getDistance();
  174. Serial.println(distance);
  175.  
  176. if (isNearWall()) {
  177. turnLeft();
  178. if(isNearWall()){
  179. turnRight();
  180. turnRight();
  181. if(isNearWall())
  182. turnRight();
  183. }
  184.  
  185. }
  186.  
  187. moveForward();
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement