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: 19.07.2017 erstellt, 01.02.2018 zuletzt geändert
  6. //Projekt: Der Zumo fährt automatisch zwischen 2 Linien ohne diese zu überfahren mithilfe der
  7. //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. int warning = 0; //1 zeigt Überhol-/Abbiegevorgang an
  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 321 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  74. void Abbiegen();
  75. #line 398 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  76. void Spurhalten();
  77. #line 445 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  78. void SerialOutput();
  79. #line 457 "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 erkannt");
  232. Serial1.println("1");
  233. ledRed(1);
  234. motors.setSpeeds(0, 0);
  235. delay(500);
  236. while(lineValue[0] < 300 && lineValue[2] < 300)
  237. {
  238. motors.setSpeeds(-maxSpeed/2, -maxSpeed/2);
  239. }
  240. delay(500);
  241. }
  242. //Funktion Hindernisumfahrung
  243. void Hindernisumfahren()
  244. {
  245. dir = 'U';
  246. Serial1.println("Hindernis umfahren");
  247. Serial1.println("1");
  248. ledYellow(1);
  249. //Schritt 1: Spurwechsel links
  250. //links drehen
  251. rotationAngle = 0;
  252. GyroUpdate();
  253. while(rotationAngle < 45)
  254. {
  255. GyroUpdate();
  256. motors.setSpeeds(maxSpeed/2, maxSpeed);
  257. }
  258. //geradeaus über Mittellinie fahren
  259. LineUpdate();
  260. while(lineValue[2] < 300)
  261. {
  262. LineUpdate();
  263. motors.setSpeeds(maxSpeed, maxSpeed);
  264. }
  265. //rechts drehen
  266. GyroUpdate();
  267. while(rotationAngle > 0)
  268. {
  269. GyroUpdate();
  270. motors.setSpeeds(maxSpeed, maxSpeed/2);
  271. }
  272. //geradeaus fahren
  273. motors.setSpeeds(maxSpeed, maxSpeed);
  274. //Gegenverkehr beachten
  275. proxSensors.read();
  276. proxValue[1] = proxSensors.countsFrontWithLeftLeds();
  277. proxValue[2] = proxSensors.countsFrontWithRightLeds();
  278. if(proxValue[1] < 4 || proxValue[2] < 4)
  279. {
  280. //Schritt 2: Hindernis passieren
  281. motors.setSpeeds(maxSpeed, maxSpeed);
  282. delay(1000);
  283. proxSensors.read();
  284. proxValue[3] = proxSensors.countsRightWithRightLeds();
  285. while(proxValue[3] > 4)
  286. {
  287. motors.setSpeeds(maxSpeed, maxSpeed);
  288. proxSensors.read();
  289. proxValue[3] = proxSensors.countsRightWithRightLeds();
  290. Spurhalten();
  291. }
  292. }
  293. //Schritt 3: Spurwechsel rechts
  294. //rechts drehen
  295. rotationAngle = 0;
  296. GyroUpdate();
  297. while(rotationAngle > -45)
  298. {
  299. GyroUpdate();
  300. motors.setSpeeds(maxSpeed, maxSpeed/2);
  301. }
  302. //geradeaus über Mittellinie fahren
  303. LineUpdate();
  304. while(lineValue[0] < 300)
  305. {
  306. LineUpdate();
  307. motors.setSpeeds(maxSpeed, maxSpeed);
  308. }
  309. //links drehen
  310. GyroUpdate();
  311. while(rotationAngle < 0)
  312. {
  313. GyroUpdate();
  314. motors.setSpeeds(maxSpeed/2, maxSpeed);
  315. }
  316. //geradeaus fahren
  317. motors.setSpeeds(maxSpeed, maxSpeed);
  318. Serial1.println("Umfahren beendet");
  319. }
  320. //Funktion Abbiegen
  321. void Abbiegen()
  322. {
  323. dir = 'A';
  324. ledYellow(1);
  325. Serial1.println("Abbiegen");
  326. Serial1.println("1");
  327. //Kodierung analysieren
  328. bool leftCode; //links => 1
  329. bool rightCode; //rechts => 2
  330. if(lineValue[0] > 1000) leftCode = true;
  331. else leftCode = false;
  332. if(lineValue[2] > 1000) rightCode = true;
  333. else rightCode = false;
  334. //Zufallszahl erzeugen
  335. uint8_t randy; //geradeaus => 0
  336. if(leftCode == true && rightCode == true) randy = random(1, 3); //1, 2
  337. else if(leftCode == true && rightCode == false) randy = random(0, 2); //0, 1
  338. else if(leftCode == false && rightCode == true)
  339. {
  340. randy = random(3); //0, (1), 2
  341. while(randy == 1) randy = random(3); //!1 => 0, 2
  342. }
  343. //links Abbiegen
  344. if(randy == 1 && leftCode == true)
  345. {
  346. //zur Kreuzungsmitte fahren
  347. driveRotation[0] = 0;
  348. driveRotation[1] = 0;
  349. while(driveRotation[0] != 1 && driveRotation[1] != 1 && lineValue[0] < 300 && lineValue[2] < 300)
  350. {
  351. EncoderUpdate();
  352. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  353. }
  354. //links drehen
  355. rotationAngle = 0;
  356. GyroUpdate();
  357. while(rotationAngle != 90)
  358. {
  359. GyroUpdate();
  360. if(lineValue[0] > 300 && lineValue[0] < 500) motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  361. else if(lineValue[1] > 300 && lineValue[1] < 500) motors.setSpeeds(-maxSpeed/4, -maxSpeed/4);
  362. else if(lineValue[2] > 300 && lineValue[2] < 500) motors.setSpeeds(0, maxSpeed/2);
  363. else motors.setSpeeds(maxSpeed/4, maxSpeed/2);
  364. }
  365. //geradeaus fahren
  366. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  367. }
  368. //rechts Abbiegen
  369. else if(randy == 2 && rightCode == true)
  370. {
  371. //rechts drehen
  372. rotationAngle = 0;
  373. GyroUpdate();
  374. while(rotationAngle != -90)
  375. {
  376. GyroUpdate();
  377. if(lineValue[0] > 300 && lineValue[0] < 500) motors.setSpeeds(maxSpeed/2, 0);
  378. else if(lineValue[1] > 300 && lineValue[1] < 500) motors.setSpeeds(-maxSpeed/4, -maxSpeed/4);
  379. else if(lineValue[2] > 300 && lineValue[2] < 500) motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  380. else motors.setSpeeds(maxSpeed/2, maxSpeed/4);
  381. }
  382. //geradeaus fahren
  383. motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  384. }
  385. //nicht Abbiegen, geradeaus fahren
  386. else motors.setSpeeds(maxSpeed/2, maxSpeed/2);
  387. delay(500);
  388. Serial1.println("Abbiegen beendet");
  389. }
  390. //Funktion Spurhalten
  391. void Spurhalten()
  392. {
  393. //linke Linie erreicht, nach rechts fahren
  394. if(lineValue[0] > 300 && lineValue[0] < 500)
  395. {
  396. dir = 'R';
  397. ledYellow(1);
  398. Serial1.println("Spur nach rechts korrigieren");
  399. motors.setSpeeds(maxSpeed, maxSpeed/2);
  400. delay(100);
  401. }
  402. //rechte Linie erreicht, nach links fahren
  403. else if(lineValue[2] > 300 && lineValue[2] < 500)
  404. {
  405. dir = 'L';
  406. ledYellow(1);
  407. Serial1.println("Spur nach links korrigieren");
  408. motors.setSpeeds(maxSpeed/2, maxSpeed);
  409. delay(100);
  410. }
  411. //Linie überfahren, zurücksetzen
  412. else if(lineValue[1] > 300 && lineValue[1] < 500)
  413. {
  414. dir = 'B';
  415. ledRed(1);
  416. Serial1.println("Spur verlassen");
  417. Serial1.println("1");
  418. while(lineValue[0] < 300 && lineValue[2] < 300) //Zumo fährt solange rückwärst bis er wieder auf der Spur steht
  419. {
  420. motors.setSpeeds(-maxSpeed/2, -maxSpeed/2);
  421. }
  422. delay(500);
  423. }
  424. //normale Fahrt
  425. else
  426. {
  427. dir = 'F';
  428. ledGreen(1);
  429. Serial1.println("Spur folgen");
  430. motors.setSpeeds(maxSpeed, maxSpeed);
  431. }
  432. }
  433. //Funktion Serielle Ausgabe
  434. void SerialOutput()
  435. {
  436. snprintf_P(report, sizeof(report),
  437. PSTR("Abstand: %d %d %d %d Linie: %d %d %d Weg: %d %d Winkel: %d Zeit: %d"),
  438. proxValue[0], proxValue[1], proxValue[2], proxValue[3],
  439. lineValue[0], lineValue[1], lineValue[2],
  440. driveRotation[0], driveRotation[1], rotationAngle, CycleTime);
  441. Serial1.println(report);
  442. }
  443. /*-----------------------------------------------------------------*/
  444. void loop()
  445. {
  446. ledGreen(0);
  447. ledYellow(0);
  448. ledRed(0);
  449. //Abfragen der Sensordaten
  450. TimeUpdate();
  451. LineUpdate();
  452. ProxUpdate();
  453. GyroUpdate();
  454. CompassUpdate();
  455. EncoderUpdate();
  456. //Funktionsauswahl
  457. warning = Serial1.read();
  458. if(warning == 1) //verfügbare Funktionen bei laufenden Manövern
  459. {
  460. maxSpeed = 100;
  461. if(moveRate > 1000 || proxValue[1] > 4 || proxValue[2] > 4 || lineValue[0] > 1000 ||
  462. lineValue[2] > 1000) motors.setSpeeds(0, 0);
  463. else Spurhalten();
  464. }
  465. else //verfügbare Funktionen im Normalfall
  466. {
  467. if(moveRate > 1000) Kollisionserkennung();
  468. else if(proxValue[1] == 6 || proxValue[2] == 6) motors.setSpeeds(0, 0);
  469. else if(proxValue[1] == 5 || proxValue[2] == 5) Hindernisumfahren();
  470. else if(lineValue[0] > 1000 || lineValue[2] > 1000) Abbiegen();
  471. else Spurhalten();
  472. }
  473. //Ausgabe über Bluetoothverbindung
  474. SerialOutput();
  475. }