Hauptprojekt.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //Verfassser: Felix Stange MET2016
  2. //Datum: 20.07.2017
  3. //Projekt: Der Zumo fährt automatisch zwischen 2 Linien ohne diese zu überfahren mithilfe der
  4. //Liniensensoren (5), ähnlich einer Spurhalteautomatik (dunkler Belag und hellen Streifen).
  5. //Außerdem werden Kollisionen verhindert durch Nutzung der vorderen Abstandssensoren. Kommt es
  6. //dennoch zu einer Kollision, wird diese durch die Beschleunigungssensoren erkannt.
  7. //Der Zumo kommuniziert über ein Bluetooth-Modul (HC-05) mit anderen Geräten. Die Kommunikation
  8. //erfolgt seriell (SERIAL für USB, SERIAL1 für Bluetooth).
  9. #include <Wire.h>
  10. #include <Zumo32U4.h>
  11. Zumo32U4ProximitySensors proxSensors;
  12. Zumo32U4LineSensors lineSensors;
  13. Zumo32U4Motors motors;
  14. Zumo32U4LCD lcd;
  15. Zumo32U4ButtonA buttonA;
  16. LSM303 compass;
  17. uint16_t lineValues[5]; //von links (0) nach rechts (5)
  18. uint16_t lineReferences[5];
  19. uint8_t proxValues[2]; //Frontempfänger mit Werten von linker und rechter LED
  20. int16_t comValue; //Beschleunigungswerte auf x-Achse (längs des Zumo)
  21. int maxSpeed = 200; //Maximale Geschwindigkeit (möglich 400)
  22. char dir; //Fahrtrichtung
  23. char report[120]; //Ausgabe über Serial
  24. void LineSensorCalibration()
  25. {
  26. lcd.clear();
  27. lcd.print("Press A");
  28. lcd.gotoXY(0, 1);
  29. lcd.print("to calib");
  30. buttonA.waitForButton();
  31. lcd.clear();
  32. lcd.print("Calibrate");
  33. delay(500);
  34. for(uint16_t i = 0; i < 120; i++)
  35. {
  36. if (i > 30 && i <= 90) motors.setSpeeds(200, -200);
  37. else motors.setSpeeds(-200, 200);
  38. lineSensors.calibrate();
  39. }
  40. motors.setSpeeds(0, 0);
  41. }
  42. void setup()
  43. {
  44. lineSensors.initFiveSensors();
  45. lineSensors.read(lineReferences);
  46. proxSensors.initFrontSensor();
  47. Wire.begin();
  48. compass.init();
  49. compass.enableDefault();
  50. LineSensorCalibration();
  51. lcd.clear();
  52. lcd.print("Press A");
  53. lcd.gotoXY(0, 1);
  54. lcd.print("to start");
  55. buttonA.waitForButton();
  56. // Serial1.begin(9600);
  57. // if(Serial1.available()) Serial1.println("bluetooth available");
  58. Serial.begin(9600);
  59. delay(500);
  60. }
  61. void SerialOutput()
  62. {
  63. snprintf_P(report, sizeof(report),
  64. PSTR("Abstand: %3d %3d Linien: %6d %6d %6d %6d %6d Richtung: %3c Beschleunigung: %6d"),
  65. proxValues[0], proxValues[1],
  66. lineValues[0], lineValues[1], lineValues[2], lineValues[3], lineValues[4],
  67. dir, comValue);
  68. Serial.println(report);
  69. // if(Serial1.available()) Serial1.println(report);
  70. }
  71. void loop()
  72. {
  73. ledGreen(0);
  74. ledYellow(0);
  75. ledRed(0);
  76. lcd.clear();
  77. lineSensors.read(lineValues);
  78. proxSensors.read();
  79. proxValues[0] = proxSensors.countsFrontWithLeftLeds();
  80. proxValues[1] = proxSensors.countsFrontWithRightLeds();
  81. compass.read();
  82. comValue = compass.a.x;
  83. dir = 'x';
  84. SerialOutput();
  85. }