Hauptprojekt.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //Verfassser: Felix Stange 4056379 MET2016
  2. //Datum: 19.07.2017 erstellt, 11.10.2017 zuletzt geändert
  3. //Projekt: Der Zumo fährt automatisch zwischen 2 Linien ohne diese zu überfahren mithilfe der
  4. //Liniensensoren (3), ähnlich einer Spurhalteautomatik (heller Belag und dunkle Streifen).
  5. //Außerdem werden Kollisionen verhindert durch Nutzung der vorderen Abstandssensoren. Kommt es
  6. //dennoch zu einer Kollision, wird diese durch die Beschleunigungssensoren (LSM303) erkannt.
  7. //Für den Überholvorgang werden die seitlichen Abstandssensoren und der Drehbewegungssensor (L3G)
  8. //genutzt. Mithilfe der Quadraturencoder in den Motoren können Wegstrecken vermessen werden.
  9. //Der Zumo kommuniziert über ein Bluetooth-Modul (HC-05) mit anderen Geräten. Die
  10. //Kommunikation erfolgt seriell ('SERIAL' für USB, 'SERIAL1' für Bluetooth).
  11. //das LCD kann bei Bluetoothkommunikation nicht genutzt werden.
  12. #include <Wire.h>
  13. #include <Zumo32U4.h>
  14. Zumo32U4ProximitySensors proxSensors; //Abstandssensoren
  15. Zumo32U4LineSensors lineSensors; //Liniensensoren
  16. Zumo32U4Motors motors;
  17. Zumo32U4ButtonA buttonA;
  18. Zumo32U4Encoders encoders;
  19. LSM303 compass; //Beschleunigungssensor x-Achse
  20. L3G gyro; //Drehbewegungssensor z-Achse
  21. uint16_t lineValues[3]; //von links (0) nach rechts (2)
  22. uint16_t lineOffset[3];
  23. uint8_t proxValues[4]; //von links (0) nach rechts (3)
  24. int16_t countsLeft; //Encoder
  25. int16_t countsRight;
  26. uint16_t turnAngle = 0; //Drehwinkel
  27. int16_t turnRate;
  28. int16_t gyroOffset;
  29. uint16_t gyroLastUpdate;
  30. uint16_t moveDistance = 0; //Beschleunigung
  31. int16_t moveRate;
  32. int16_t compassOffset;
  33. uint16_t compassLastUpdate;
  34. uint8_t randy; //Zufallszahl für Richtung
  35. int maxSpeed = 200; //Maximale Geschwindigkeit (möglich 400)
  36. char dir = '0'; //Fahrtrichtung, Ereignis
  37. char report[120]; //Ausgabe
  38. /*-----------------------------------------------------------------*/
  39. //Setup Liniensensoren
  40. void LineSensorSetup()
  41. {
  42. ledYellow(1);
  43. delay(500);
  44. //Kalibrierung
  45. uint32_t total[3] = {0, 0, 0};
  46. for(uint8_t i = 0; i < 120; i++)
  47. {
  48. if (i > 30 && i <= 90) motors.setSpeeds(200, -200);
  49. else motors.setSpeeds(-200, 200);
  50. lineSensors.read(lineOffset);
  51. total[0] += lineOffset[0];
  52. total[1] += lineOffset[1];
  53. total[2] += lineOffset[2];
  54. lineSensors.calibrate();
  55. }
  56. ledYellow(0);
  57. motors.setSpeeds(0, 0);
  58. lineOffset[0] = total[0] / 120;
  59. lineOffset[1] = total[1] / 120;
  60. lineOffset[2] = total[2] / 120;
  61. }
  62. //Setup Drehbewegungssensor
  63. void TurnSensorSetup()
  64. {
  65. ledYellow(1);
  66. delay(500);
  67. //800Hz Ausgaberate
  68. gyro.writeReg(L3G::CTRL1, 0b11111111); //Tiefpassfilter bei 100Hz
  69. gyro.writeReg(L3G::CTRL4, 0b00100000); //2000dps Auflösung
  70. gyro.writeReg(L3G::CTRL5, 0b00000000); //Hochpassfilter ausgeschaltet
  71. //Kalibrierung
  72. int32_t total = 0;
  73. for (uint16_t i = 0; i < 1024; i++)
  74. {
  75. while(!gyro.readReg(L3G::STATUS_REG) & 0x08);
  76. gyro.read();
  77. total += gyro.g.z;
  78. }
  79. ledYellow(0);
  80. gyroOffset = total / 1024;
  81. }
  82. //Setup Beschleunigungssensor
  83. void MoveSensorSetup()
  84. {
  85. ledYellow(1);
  86. delay(500);
  87. //Kalibrierung
  88. int32_t total = 0;
  89. for (uint16_t i = 0; i < 1024; i++)
  90. {
  91. compass.read();
  92. total += compass.a.x;
  93. }
  94. ledYellow(0);
  95. compassOffset = total / 1024;
  96. }
  97. void setup()
  98. {
  99. //Initialisierung der Bluetoothverbindung
  100. Serial1.begin(9600);
  101. if(Serial1.available()) Serial1.println("bluetooth available");
  102. //Initialisierung der Sensoren
  103. lineSensors.initThreeSensors();
  104. proxSensors.initThreeSensors();
  105. Wire.begin();
  106. compass.init();
  107. compass.enableDefault();
  108. gyro.init();
  109. //Kalibrierung der Sensoren
  110. Serial1.println("sensor calibration");
  111. buttonA.waitForButton();
  112. LineSensorSetup();
  113. TurnSensorSetup();
  114. gyroLastUpdate = micros();
  115. MoveSensorSetup();
  116. compassLastUpdate = micros();
  117. //Zumo bereit zu starten
  118. Serial1.println("Zumo ready to start");
  119. buttonA.waitForButton();
  120. delay(500);
  121. }
  122. /*-----------------------------------------------------------------*/
  123. //Update Drehbewegungssensor
  124. void TurnSensorUpdate()
  125. {
  126. gyro.read();
  127. turnRate = gyro.g.z - gyroOffset;
  128. uint16_t m = micros();
  129. uint16_t dt = m - gyroLastUpdate;
  130. gyroLastUpdate = m;
  131. int32_t d = (int32_t)turnRate * dt;
  132. turnAngle += (int64_t)d * 14680064 / 17578125;
  133. turnAngle = (((int32_t)turnAngle >> 16) * 360) >> 16;
  134. }
  135. // Update Beschleunigungssensor
  136. void MoveSensorUpdate()
  137. {
  138. compass.read();
  139. moveRate = compass.a.x - compassOffset;
  140. uint16_t m = micros();
  141. uint16_t dt = m - compassLastUpdate;
  142. compassLastUpdate = m;
  143. int32_t d = (int32_t)moveRate * dt;
  144. moveDistance += (int64_t)d * dt / 16384;
  145. moveDistance = (moveDistance * 1000) / 9,81;
  146. }
  147. //Update Encoder
  148. //12cpr (Motorwelle) * 75,81:1 (Getriebe) = 909,7cpr (Antriebszahnrad)
  149. void EncoderUpdate()
  150. {
  151. static uint8_t lastDisplayTime;
  152. if ((uint8_t)(millis() - lastDisplayTime) >= 100)
  153. {
  154. lastDisplayTime = millis();
  155. countsLeft = encoders.getCountsLeft();
  156. countsRight = encoders.getCountsRight();
  157. }
  158. }
  159. /*-----------------------------------------------------------------*/
  160. //Funktion Kollisionserkennung
  161. void CollisionDetection()
  162. {
  163. dir = 'X';
  164. Serial1.println("impact detected");
  165. ledRed(1);
  166. motors.setSpeeds(0, 0);
  167. delay(500);
  168. motors.setSpeeds(-maxSpeed/2, -maxSpeed/2);
  169. delay(1000);
  170. }
  171. //Funktion Hindernisumfahrung
  172. void ObstacleAvoidance()
  173. {
  174. dir = 'U';
  175. Serial1.println("obstacle avoidance");
  176. ledYellow(1);
  177. //Schritt 1: links bis über Mittellinie fahren
  178. motors.setSpeeds(maxSpeed/2, maxSpeed);
  179. turnAngle = 0;
  180. TurnSensorUpdate();
  181. while(lineValues[2] < (lineOffset[2] -200))
  182. {
  183. lineSensors.read(lineValues);
  184. }
  185. //Schritt 2: rechts fahren bis Fahrzeug gerade steht ohne dabei weitere Linien zu überfahren
  186. motors.setSpeeds(maxSpeed, maxSpeed/2);
  187. while(turnAngle != 0)
  188. {
  189. TurnSensorUpdate();
  190. }
  191. //Gegenverkehr beachten
  192. proxSensors.read();
  193. proxValues[1] = proxSensors.countsFrontWithLeftLeds();
  194. proxValues[2] = proxSensors.countsFrontWithRightLeds();
  195. if((proxValues[1] || proxValues[2]) < 4)
  196. {
  197. //Schritt 3: geradeaus fahren bis Hindernis passiert
  198. maxSpeed = 400;
  199. motors.setSpeeds(maxSpeed, maxSpeed);
  200. delay(1000);
  201. proxSensors.read();
  202. proxValues[3] = proxSensors.countsRightWithRightLeds();
  203. while(proxValues[3] > 4)
  204. {
  205. proxSensors.read();
  206. proxValues[3] = proxSensors.countsRightWithRightLeds();
  207. TrackKeeper();
  208. }
  209. maxSpeed = 200;
  210. }
  211. //Schritt 4: rechts bis über Mittellinie fahren
  212. motors.setSpeeds(maxSpeed/2, maxSpeed);
  213. ledYellow(1);
  214. turnAngle = 0;
  215. TurnSensorUpdate();
  216. while(lineValues[0] < (lineOffset[0] -200))
  217. {
  218. lineSensors.read(lineValues);
  219. }
  220. //Schritt 5: links fahren bis Fahrzeug gerade steht ohne dabei weitere Linien zu überfahren
  221. motors.setSpeeds(maxSpeed/2, maxSpeed);
  222. while(turnAngle != 0)
  223. {
  224. TurnSensorUpdate();
  225. }
  226. }
  227. //Funktion Spurhalten
  228. void TrackKeeper()
  229. {
  230. //linke Linie erreicht, nach rechts fahren
  231. if(lineValues[0] > (lineOffset[0] + 200))
  232. {
  233. motors.setSpeeds(maxSpeed, 0);
  234. ledYellow(1);
  235. delay(200);
  236. dir = 'R';
  237. }
  238. //rechte Linie erreicht, nach links fahren
  239. else if(lineValues[2] > (lineOffset[2] + 200))
  240. {
  241. motors.setSpeeds(0, maxSpeed);
  242. ledYellow(1);
  243. delay(200);
  244. dir = 'L';
  245. }
  246. //Linie überfahren, zurücksetzen
  247. else if(lineValues[1] > (lineOffset[1] + 200))
  248. {
  249. motors.setSpeeds(-maxSpeed/2, -maxSpeed/2);
  250. ledRed(1);
  251. delay(1000);
  252. dir = 'B';
  253. }
  254. //Normale Fahrt
  255. else
  256. {
  257. motors.setSpeeds(maxSpeed, maxSpeed);
  258. ledGreen(1);
  259. delay(100);
  260. dir = 'F';
  261. }
  262. }
  263. //Funktion Abbiegen
  264. void Crossroad()
  265. {
  266. ledYellow(1);
  267. dir = 'A';
  268. //Zufallszahl erzeugen
  269. randy = random(3);
  270. //Kodierung analysieren
  271. //links Abbiegen
  272. if(randy == 1)
  273. {
  274. //zur Kreuzungsmitte fahren
  275. while((countsLeft != 200) && (countsRight != 200))
  276. {
  277. EncoderUpdate();
  278. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  279. }
  280. //links drehen
  281. turnAngle = 0;
  282. TurnSensorUpdate();
  283. while(turnAngle != 90)
  284. {
  285. motors.setSpeeds(0, maxSpeed);
  286. TurnSensorUpdate();
  287. }
  288. //geradeaus fahren
  289. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  290. }
  291. //rechts Abbiegen
  292. else if(randy == 2)
  293. {
  294. //rechts drehen
  295. turnAngle = 0;
  296. TurnSensorUpdate();
  297. while(turnAngle != 90)
  298. {
  299. motors.setSpeeds(maxSpeed, 0);
  300. TurnSensorUpdate();
  301. }
  302. //geradeaus fahren
  303. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  304. }
  305. }
  306. //Funktion Serielle Ausgabe
  307. void SerialOutput()
  308. {
  309. snprintf_P(report, sizeof(report),
  310. PSTR("Abstand: %3d %3d %3d %3d Linie: %6d %6d %6d Richtung: %3c Beschleunigung: %6d Winkel: %6d"),
  311. proxValues[0], proxValues[1], proxValues[2], proxValues[3],
  312. lineValues[0], lineValues[1], lineValues[2],
  313. dir, moveDistance, turnAngle);
  314. Serial1.println(report);
  315. }
  316. /*-----------------------------------------------------------------*/
  317. void loop()
  318. {
  319. ledGreen(0);
  320. ledYellow(0);
  321. ledRed(0);
  322. //Abfragen der Sensordaten
  323. lineSensors.read(lineValues);
  324. proxSensors.read();
  325. proxValues[0] = proxSensors.countsLeftWithLeftLeds();
  326. proxValues[1] = proxSensors.countsFrontWithLeftLeds();
  327. proxValues[2] = proxSensors.countsFrontWithRightLeds();
  328. proxValues[3] = proxSensors.countsRightWithRightLeds();
  329. TurnSensorUpdate();
  330. MoveSensorUpdate();
  331. //Funktionsauswahl
  332. /* if((compass.a.x - compassOffset) > 4000) CollisionDetection();
  333. else if((proxValues[0] || proxValues[1]) > 4) ObstacleAvoidance();
  334. else if((lineValues[0] > (lineOffset[0] + 200)) && (lineValues[1] > (lineOffset[1] + 200))
  335. && (lineValues[2] > (lineOffset[2] + 200))) Crossroad();
  336. else TrackKeeper(); */
  337. //Ausgabe über Bluetoothverbindung
  338. SerialOutput();
  339. }