Hauptprojekt.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //Verfassser: Felix Stange MET2016
  2. //Datum: 09.04.2017
  3. //Projekt: Der Zumo fährt automatisch. Dabei vermeidet er Kollisionen mit der Umgebung durch Nutzung der Abstandssensoren
  4. //(links, vorne, rechts). Kommt es dennoch zu einer Kollision, wird diese durch die Beschleunigungssensoren erkannt.
  5. #include <Wire.h>
  6. #include <Zumo32U4.h>
  7. Zumo32U4ProximitySensors proxSensors;
  8. Zumo32U4Motors motors;
  9. Zumo32U4LCD lcd;
  10. Zumo32U4ButtonA buttonA;
  11. LSM303 compass;
  12. uint8_t proxLeft;
  13. uint8_t proxFrontLeft;
  14. uint8_t proxFrontRight;
  15. uint8_t proxFront;
  16. uint8_t proxRight;
  17. uint8_t maxspeed = 200;
  18. char dir;
  19. int8_t acc;
  20. char report[120];
  21. void setup()
  22. {
  23. lcd.clear();
  24. lcd.print("Press A");
  25. lcd.gotoXY(0, 1);
  26. lcd.print("to start");
  27. buttonA.waitForButton();
  28. delay(500);
  29. proxSensors.initThreeSensors();
  30. Wire.begin();
  31. compass.init();
  32. compass.enableDefault();
  33. }
  34. void loop()
  35. {
  36. ledGreen(0);
  37. ledYellow(0);
  38. ledRed(0);
  39. proxSensors.read();
  40. proxLeft = proxSensors.countsLeftWithLeftLeds();
  41. proxFrontLeft = proxSensors.countsFrontWithLeftLeds();
  42. proxFrontRight = proxSensors.countsFrontWithRightLeds();
  43. proxRight = proxSensors.countsRightWithRightLeds();
  44. compass.read();
  45. acc = compass.a.x;
  46. if ((proxFrontLeft > 4) && (proxFrontLeft > proxFrontRight))
  47. {
  48. motors.setLeftSpeed(maxspeed);
  49. motors.setRightSpeed(0);
  50. ledRed(1);
  51. dir = 'R';
  52. delay(100);
  53. }
  54. else if ((proxFrontRight > 4) && (proxFrontLeft < proxFrontRight))
  55. {
  56. motors.setLeftSpeed(0);
  57. motors.setRightSpeed(maxspeed);
  58. ledRed(1);
  59. dir = 'L';
  60. delay(100);
  61. }
  62. else if (((proxFrontRight > 5) && (proxFrontLeft > 5)) || (abs(acc) > 1000))
  63. {
  64. motors.setLeftSpeed(-maxspeed / 2);
  65. motors.setRightSpeed(-maxspeed / 2);
  66. ledRed(1);
  67. dir = 'B';
  68. delay(200);
  69. motors.setLeftSpeed(0);
  70. motors.setRightSpeed(-maxspeed / 2);
  71. delay(200);
  72. }
  73. else if ((proxLeft > 4) && (proxLeft > proxRight))
  74. {
  75. motors.setLeftSpeed(maxspeed);
  76. motors.setRightSpeed(maxspeed / 2);
  77. ledYellow(1);
  78. dir = 'r';
  79. delay(100);
  80. }
  81. else if (proxRight > 4)
  82. {
  83. motors.setLeftSpeed(maxspeed / 2);
  84. motors.setRightSpeed(maxspeed);
  85. ledYellow(1);
  86. dir = 'l';
  87. delay(100);
  88. }
  89. else
  90. {
  91. motors.setLeftSpeed(maxspeed);
  92. motors.setRightSpeed(maxspeed);
  93. ledGreen(1);
  94. dir = 'F';
  95. delay(100);
  96. }
  97. proxFront = (proxFrontLeft + proxFrontRight) / 2;
  98. lcd.clear();
  99. lcd.print("L");
  100. lcd.print(proxLeft);
  101. lcd.print(" F");
  102. lcd.print(proxFront);
  103. lcd.print(" R");
  104. lcd.print(proxRight);
  105. lcd.gotoXY(0, 1);
  106. lcd.print("Dir ");
  107. lcd.print(dir);
  108. snprintf_P(report, sizeof(report),
  109. PSTR("Abstand: %6d %6d %6d %6d Richtung: %6c Beschleunigung: %6d"),
  110. proxLeft, proxFrontLeft, proxFrontRight, proxRight,
  111. dir, acc);
  112. Serial.println(report);
  113. }