Robot Car Project

Hauptprojekt.ino.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. #include <Arduino.h>
  2. #line 1 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  3. #line 1 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  4. //Verfassser: Felix Stange, 4056379, MET2016
  5. //Datum: erstellt am 19.07.2017, zuletzt geändert am 15.02.2018
  6. //Projektbeschreibung: Der Zumo fährt automatisch zwischen 2 Linien ohne diese zu überfahren mithilfe
  7. //der Liniensensoren (3), ähnlich einer Spurhalteautomatik (heller Belag und dunkle Streifen).
  8. //Außerdem werden Kollisionen verhindert durch Nutzung der vorderen Abstandssensoren. Kommt es
  9. //dennoch zu einer Kollision, wird diese durch die Beschleunigungssensoren (LSM303) erkannt.
  10. //Für den Überholvorgang werden die seitlichen Abstandssensoren und der Drehbewegungssensor (L3G)
  11. //genutzt. Mithilfe der Quadraturencoder in den Motoren können Wegstrecken vermessen werden.
  12. //Der Zumo kommuniziert über ein Bluetooth-Modul (HC-05) mit anderen Geräten. Die
  13. //Kommunikation erfolgt seriell ('SERIAL' für USB, 'SERIAL1' für Bluetooth).
  14. //Das LCD kann bei Bluetoothnutzung nicht verwendet werden.
  15. #include <Wire.h>
  16. #include <Zumo32U4.h>
  17. Zumo32U4ProximitySensors proxSensors; //Abstandssensoren
  18. Zumo32U4LineSensors lineSensors; //Liniensensoren
  19. Zumo32U4Motors motors;
  20. Zumo32U4ButtonA buttonA;
  21. Zumo32U4Encoders encoders; //Motorencoder
  22. LSM303 compass; //Beschleunigungssensor x-Achse
  23. L3G gyro; //Drehbewegungssensor z-Achse
  24. int16_t lineValue[3]; //von links (0) nach rechts (2)
  25. uint16_t lineOffset[3];
  26. uint8_t proxValue[4]; //von links (0) nach rechts (3)
  27. int16_t encoderCounts[2]; //von links (0) nach rechts (1)
  28. int16_t driveRotation[2];
  29. int32_t rotationAngle = 0; //Drehwinkel
  30. int32_t turnAngle = 0;
  31. int16_t turnRate;
  32. int16_t gyroOffset;
  33. uint16_t gyroLastUpdate;
  34. int32_t moveDisplay = 0; //Beschleunigung
  35. int32_t moveDistance = 0;
  36. int16_t moveRate;
  37. int16_t compassOffset;
  38. uint16_t compassLastUpdate;
  39. uint16_t LastUpdate = 0; //Systemzeit
  40. uint16_t CycleTime = 0; //Zykluszeit
  41. int maxSpeed = 200; //Maximale Geschwindigkeit (möglich 400)
  42. char dir; //Fahrtrichtung, Ereignis
  43. char report[200]; //Ausgabe
  44. String btData;
  45. /*-----------------------------------------------------------------*/
  46. //Setup Liniensensoren
  47. #line 54 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  48. void LineSetup();
  49. #line 79 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  50. void ProxSetup();
  51. #line 85 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  52. void GyroSetup();
  53. #line 109 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  54. void CompassSetup();
  55. #line 130 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  56. void setup();
  57. #line 157 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  58. void TimeUpdate();
  59. #line 165 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  60. void ProxUpdate();
  61. #line 175 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  62. void LineUpdate();
  63. #line 185 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  64. void GyroUpdate();
  65. #line 198 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  66. void CompassUpdate();
  67. #line 211 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  68. void EncoderUpdate();
  69. #line 222 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  70. void Kollisionserkennung();
  71. #line 238 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  72. void Hindernisumfahren();
  73. #line 320 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  74. void Abbiegen();
  75. #line 396 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  76. void Spurhalten();
  77. #line 443 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  78. void SerialOutput();
  79. #line 455 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  80. void loop();
  81. #line 54 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  82. void LineSetup()
  83. {
  84. ledYellow(1);
  85. lineSensors.initThreeSensors();
  86. //Kalibrierung
  87. uint32_t total[3] = {0, 0, 0};
  88. for(uint8_t i = 0; i < 120; i++)
  89. {
  90. if (i > 30 && i <= 90) motors.setSpeeds(maxSpeed, -maxSpeed);
  91. else motors.setSpeeds(-maxSpeed, maxSpeed);
  92. lineSensors.read(lineOffset);
  93. total[0] += lineOffset[0];
  94. total[1] += lineOffset[1];
  95. total[2] += lineOffset[2];
  96. lineSensors.calibrate();
  97. }
  98. ledYellow(0);
  99. motors.setSpeeds(0, 0);
  100. lineOffset[0] = total[0] / 120;
  101. lineOffset[1] = total[1] / 120;
  102. lineOffset[2] = total[2] / 120;
  103. }
  104. //Setup Abstandssensoren
  105. void ProxSetup()
  106. {
  107. proxSensors.initThreeSensors();
  108. }
  109. //Setup Drehbewegungssensor
  110. void GyroSetup()
  111. {
  112. ledYellow(1);
  113. gyro.init();
  114. gyro.writeReg(L3G::CTRL1, 0b11111111); //Ausgaberate 800Hz, Tiefpassfilter 100Hz
  115. gyro.writeReg(L3G::CTRL4, 0b00100000); //2000dps Auflösung
  116. gyro.writeReg(L3G::CTRL5, 0b00000000); //Hochpassfilter ausgeschaltet
  117. //Kalibrierung
  118. int32_t total = 0;
  119. for (uint16_t i = 0; i < 1024; i++)
  120. {
  121. while(!gyro.readReg(L3G::STATUS_REG) & 0x08);
  122. gyro.read();
  123. total += gyro.g.z;
  124. }
  125. gyroOffset = total / 1024;
  126. gyroLastUpdate = micros();
  127. ledYellow(0);
  128. }
  129. //Setup Beschleunigungssensor
  130. void CompassSetup()
  131. {
  132. ledYellow(1);
  133. compass.init();
  134. compass.enableDefault();
  135. //Kalibrierung
  136. int32_t total = 0;
  137. for (uint16_t i = 0; i < 1024; i++)
  138. {
  139. compass.read();
  140. total += compass.a.x;
  141. }
  142. compassOffset = total / 1024;
  143. compassLastUpdate = micros();
  144. ledYellow(0);
  145. }
  146. /*-----------------------------------------------------------------*/
  147. void setup()
  148. {
  149. //Initialisierung der Bluetoothverbindung
  150. Serial1.begin(9600);
  151. if(Serial1.available()) Serial1.println("Verbindung hergestellt");
  152. //Initialisierung und Kalibrierung der Sensoren
  153. Serial1.println("Sensorkalibrierung");
  154. Wire.begin();
  155. delay(500);
  156. LastUpdate = micros();
  157. ProxSetup();
  158. LineSetup();
  159. GyroSetup();
  160. CompassSetup();
  161. //Zumo bereit zu starten
  162. Serial1.println("Zumo bereit, starte mit A");
  163. ledGreen(1);
  164. buttonA.waitForButton();
  165. randomSeed(millis());
  166. delay(500);
  167. }
  168. /*-----------------------------------------------------------------*/
  169. //Update Durchlaufzeit
  170. void TimeUpdate()
  171. {
  172. uint16_t m = micros();
  173. CycleTime = m - LastUpdate;
  174. LastUpdate = m;
  175. }
  176. //Update Abstandssensoren
  177. void ProxUpdate()
  178. {
  179. proxSensors.read();
  180. proxValue[0] = proxSensors.countsLeftWithLeftLeds();
  181. proxValue[1] = proxSensors.countsFrontWithLeftLeds();
  182. proxValue[2] = proxSensors.countsFrontWithRightLeds();
  183. proxValue[3] = proxSensors.countsRightWithRightLeds();
  184. }
  185. //Updaten Liniensensoren
  186. void LineUpdate()
  187. {
  188. uint16_t lineRaw[3];
  189. lineSensors.read(lineRaw);
  190. lineValue[0] = lineRaw[0] - lineOffset[0];
  191. lineValue[1] = lineRaw[1] - lineOffset[1];
  192. lineValue[2] = lineRaw[2] - lineOffset[2];
  193. }
  194. //Update Drehbewegungssensor
  195. void GyroUpdate()
  196. {
  197. gyro.read();
  198. turnRate = gyro.g.z - gyroOffset;
  199. uint16_t m = micros();
  200. uint16_t dt = m - gyroLastUpdate;
  201. gyroLastUpdate = m;
  202. int32_t d = (int32_t)turnRate * dt;
  203. turnAngle += (int64_t)d * 14680064 / 17578125;
  204. rotationAngle = (((int32_t)turnAngle >> 16) * 360) >> 16;
  205. }
  206. // Update Beschleunigungssensor
  207. void CompassUpdate()
  208. {
  209. compass.read();
  210. moveRate = compass.a.x - compassOffset;
  211. uint16_t m = micros();
  212. uint16_t dt = m - compassLastUpdate;
  213. compassLastUpdate = m;
  214. int32_t d = (int32_t)moveRate * dt;
  215. moveDistance += (int64_t)d * dt >> 14;
  216. moveDisplay = moveDistance * 1000 / 9,81;
  217. }
  218. //Update Encoder
  219. void EncoderUpdate()
  220. {
  221. encoderCounts[0] = encoders.getCountsLeft();
  222. driveRotation[0] = encoderCounts[0] / 910; //12cpr (Motorwelle) * 75,81:1 (Getriebe) = 909,7cpr (Antriebszahnrad)
  223. encoderCounts[1] = encoders.getCountsRight();
  224. driveRotation[1] = encoderCounts[1] / 910;
  225. }
  226. /*-----------------------------------------------------------------*/
  227. //Funktion Kollisionserkennung
  228. void Kollisionserkennung()
  229. {
  230. dir = 'X';
  231. Serial1.println("Kollision");
  232. ledRed(1);
  233. motors.setSpeeds(0, 0);
  234. delay(500);
  235. while(lineValue[0] < 300 && lineValue[2] < 300)
  236. {
  237. motors.setSpeeds(-maxSpeed/2, -maxSpeed/2);
  238. }
  239. delay(500);
  240. }
  241. //Funktion Hindernisumfahrung
  242. void Hindernisumfahren()
  243. {
  244. dir = 'U';
  245. Serial1.println("Hindernisumfahren");
  246. ledYellow(1);
  247. //Schritt 1: Spurwechsel links
  248. //links drehen
  249. rotationAngle = 0;
  250. GyroUpdate();
  251. while(rotationAngle < 45)
  252. {
  253. GyroUpdate();
  254. motors.setSpeeds(maxSpeed/2, maxSpeed);
  255. }
  256. //geradeaus über Mittellinie fahren
  257. LineUpdate();
  258. while(lineValue[2] < 300)
  259. {
  260. LineUpdate();
  261. motors.setSpeeds(maxSpeed, maxSpeed);
  262. }
  263. //rechts drehen
  264. GyroUpdate();
  265. while(rotationAngle > 0)
  266. {
  267. GyroUpdate();
  268. motors.setSpeeds(maxSpeed, maxSpeed/2);
  269. }
  270. //geradeaus fahren
  271. motors.setSpeeds(maxSpeed, maxSpeed);
  272. //Gegenverkehr beachten
  273. proxSensors.read();
  274. proxValue[1] = proxSensors.countsFrontWithLeftLeds();
  275. proxValue[2] = proxSensors.countsFrontWithRightLeds();
  276. if(proxValue[1] < 5 || proxValue[2] < 5)
  277. {
  278. //Schritt 2: Hindernis passieren
  279. motors.setSpeeds(maxSpeed, maxSpeed);
  280. delay(1000);
  281. proxSensors.read();
  282. proxValue[3] = proxSensors.countsRightWithRightLeds();
  283. while(proxValue[3] > 4)
  284. {
  285. motors.setSpeeds(maxSpeed, maxSpeed);
  286. proxSensors.read();
  287. proxValue[3] = proxSensors.countsRightWithRightLeds();
  288. Spurhalten();
  289. }
  290. }
  291. //Schritt 3: Spurwechsel rechts
  292. //rechts drehen
  293. rotationAngle = 0;
  294. GyroUpdate();
  295. while(rotationAngle > -45)
  296. {
  297. GyroUpdate();
  298. motors.setSpeeds(maxSpeed, maxSpeed/2);
  299. }
  300. //geradeaus über Mittellinie fahren
  301. LineUpdate();
  302. while(lineValue[0] < 300)
  303. {
  304. LineUpdate();
  305. motors.setSpeeds(maxSpeed, maxSpeed);
  306. }
  307. //links drehen
  308. GyroUpdate();
  309. while(rotationAngle < 0)
  310. {
  311. GyroUpdate();
  312. motors.setSpeeds(maxSpeed/2, maxSpeed);
  313. }
  314. //geradeaus fahren
  315. motors.setSpeeds(maxSpeed, maxSpeed);
  316. Serial1.println("Umfahren beendet");
  317. }
  318. //Funktion Abbiegen
  319. void Abbiegen()
  320. {
  321. dir = 'A';
  322. ledYellow(1);
  323. Serial1.println("Abbiegen");
  324. //Kodierung analysieren
  325. bool leftCode; //links => 1
  326. bool rightCode; //rechts => 2
  327. if(lineValue[0] > 1000) leftCode = true;
  328. else leftCode = false;
  329. if(lineValue[2] > 1000) rightCode = true;
  330. else rightCode = false;
  331. //Zufallszahl erzeugen
  332. uint8_t randy; //geradeaus => 0
  333. if(leftCode == true && rightCode == true) randy = random(1, 3); //1, 2
  334. else if(leftCode == true && rightCode == false) randy = random(0, 2); //0, 1
  335. else if(leftCode == false && rightCode == true)
  336. {
  337. randy = random(3); //0, (1), 2
  338. while(randy == 1) randy = random(3); //!1 => 0, 2
  339. }
  340. //links Abbiegen
  341. if(randy == 1 && leftCode == true)
  342. {
  343. //zur Kreuzungsmitte fahren
  344. driveRotation[0] = 0;
  345. driveRotation[1] = 0;
  346. while(driveRotation[0] != 1 && driveRotation[1] != 1 && lineValue[0] < 300 && lineValue[2] < 300)
  347. {
  348. EncoderUpdate();
  349. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  350. }
  351. //links drehen
  352. rotationAngle = 0;
  353. GyroUpdate();
  354. while(rotationAngle != 90)
  355. {
  356. GyroUpdate();
  357. if(lineValue[0] > 300 && lineValue[0] < 500) motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  358. else if(lineValue[1] > 300 && lineValue[1] < 500) motors.setSpeeds(-maxSpeed/4, -maxSpeed/4);
  359. else if(lineValue[2] > 300 && lineValue[2] < 500) motors.setSpeeds(0, maxSpeed/2);
  360. else motors.setSpeeds(maxSpeed/4, maxSpeed/2);
  361. }
  362. //geradeaus fahren
  363. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  364. }
  365. //rechts Abbiegen
  366. else if(randy == 2 && rightCode == true)
  367. {
  368. //rechts drehen
  369. rotationAngle = 0;
  370. GyroUpdate();
  371. while(rotationAngle != -90)
  372. {
  373. GyroUpdate();
  374. if(lineValue[0] > 300 && lineValue[0] < 500) motors.setSpeeds(maxSpeed/2, 0);
  375. else if(lineValue[1] > 300 && lineValue[1] < 500) motors.setSpeeds(-maxSpeed/4, -maxSpeed/4);
  376. else if(lineValue[2] > 300 && lineValue[2] < 500) motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  377. else motors.setSpeeds(maxSpeed/2, maxSpeed/4);
  378. }
  379. //geradeaus fahren
  380. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  381. }
  382. //nicht Abbiegen, geradeaus fahren
  383. else motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  384. delay(500);
  385. Serial1.println("Abbiegen beendet");
  386. }
  387. //Funktion Spurhalten
  388. void Spurhalten()
  389. {
  390. //linke Linie erreicht, nach rechts fahren
  391. if(lineValue[0] > 300 && lineValue[0] < 500)
  392. {
  393. dir = 'R';
  394. ledYellow(1);
  395. Serial1.println("Spur nach rechts korrigieren");
  396. motors.setSpeeds(maxSpeed, maxSpeed/2);
  397. delay(100);
  398. }
  399. //rechte Linie erreicht, nach links fahren
  400. else if(lineValue[2] > 300 && lineValue[2] < 500)
  401. {
  402. dir = 'L';
  403. ledYellow(1);
  404. Serial1.println("Spur nach links korrigieren");
  405. motors.setSpeeds(maxSpeed/2, maxSpeed);
  406. delay(100);
  407. }
  408. //Linie überfahren, zurücksetzen
  409. else if(lineValue[1] > 300 && lineValue[1] < 500)
  410. {
  411. dir = 'B';
  412. ledRed(1);
  413. Serial1.println("Spur verlassen");
  414. Serial1.println("1");
  415. while(lineValue[0] < 300 && lineValue[2] < 300) //Zumo fährt solange rückwärst bis er wieder auf der Spur steht
  416. {
  417. motors.setSpeeds(-maxSpeed/2, -maxSpeed/2);
  418. }
  419. delay(500);
  420. }
  421. //normale Fahrt
  422. else
  423. {
  424. dir = 'F';
  425. ledGreen(1);
  426. Serial1.println("Spur folgen");
  427. motors.setSpeeds(maxSpeed, maxSpeed);
  428. }
  429. }
  430. //Funktion Serielle Ausgabe
  431. void SerialOutput()
  432. {
  433. snprintf_P(report, sizeof(report),
  434. PSTR("Abstand: %d %d %d %d Linie: %d %d %d Weg: %d %d Winkel: %d Zeit: %d"),
  435. proxValue[0], proxValue[1], proxValue[2], proxValue[3],
  436. lineValue[0], lineValue[1], lineValue[2],
  437. driveRotation[0], driveRotation[1], rotationAngle, CycleTime);
  438. Serial1.println(report);
  439. }
  440. /*-----------------------------------------------------------------*/
  441. void loop()
  442. {
  443. ledGreen(0);
  444. ledYellow(0);
  445. ledRed(0);
  446. //Abfragen der Sensordaten
  447. TimeUpdate();
  448. LineUpdate();
  449. ProxUpdate();
  450. GyroUpdate();
  451. CompassUpdate();
  452. EncoderUpdate();
  453. //Funktionsauswahl
  454. btData = Serial1.readString();
  455. //verfügbare Funktionen bei laufenden Manövern
  456. if(btData == "Abbiegen" || btData == "Hindernisumfahren" || btData == "Kollision")
  457. {
  458. maxSpeed = 100;
  459. if(moveRate > 1000 || proxValue[1] > 4 || proxValue[2] > 4 || lineValue[0] > 1000 ||
  460. lineValue[2] > 1000) motors.setSpeeds(0, 0);
  461. else Spurhalten();
  462. }
  463. //verfügbare Funktionen im Normalfall
  464. else
  465. {
  466. if(moveRate > 1000) Kollisionserkennung();
  467. else if(proxValue[1] == 6 || proxValue[2] == 6) motors.setSpeeds(0, 0);
  468. else if(proxValue[1] == 5 || proxValue[2] == 5) Hindernisumfahren();
  469. else if(lineValue[0] > 1000 || lineValue[2] > 1000) Abbiegen();
  470. else Spurhalten();
  471. }
  472. //Ausgabe über Bluetoothverbindung
  473. SerialOutput();
  474. }