Robot Car Project

Hauptprojekt.ino.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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 08.05.2018
  6. //Projektthema: Untersuchungen und Implementierung eines automatisierten Fahrens mittels Pololu Zumo
  7. /*Kurzbeschreibung: Der Zumo fährt automatisch zwischen 2 Linien ohne diese zu überfahren mithilfe
  8. der Liniensensoren (3), ähnlich einer Spurhalteautomatik (heller Belag und dunkle Streifen).
  9. Außerdem werden Kollisionen verhindert durch Nutzung der vorderen Abstandssensoren. Kommt es
  10. dennoch zu einer Kollision, wird diese durch den Beschleunigungssensor (LSM303) erkannt.
  11. Für den Überholvorgang werden die seitlichen Abstandssensoren und der Drehbewegungssensor (L3G)
  12. genutzt. Mithilfe der Quadraturencoder in den Motoren können Wegstrecken vermessen werden.
  13. Für die Filterung der Messwerte werden Medianfilter genutzt. Der Zumo kommuniziert über ein
  14. Bluetooth-Modul (HC-05) mit anderen Geräten. Die Kommunikation erfolgt seriell ('SERIAL' für USB,
  15. 'SERIAL1' für Bluetooth). Das LCD kann bei Bluetoothnutzung nicht verwendet werden.*/
  16. #include <Wire.h>
  17. #include <Zumo32U4.h>
  18. #include <MedianFilter.h>
  19. Zumo32U4ProximitySensors proxSensors; //Abstandssensoren
  20. Zumo32U4LineSensors lineSensors; //Liniensensoren
  21. Zumo32U4Motors motors; //Motoren
  22. Zumo32U4ButtonA buttonA; //Taste A
  23. Zumo32U4Encoders encoders; //Motorencoder
  24. LSM303 compass; //Beschleunigungssensor x-Achse
  25. L3G gyro; //Drehbewegungssensor z-Achse
  26. MedianFilter LineFilter0(3, 0); //erstellen der Filter für Liniensensoren
  27. MedianFilter LineFilter1(3, 0); //Medianfilter geben mittleren Wert einer Reihe aus
  28. MedianFilter LineFilter2(3, 0); //Fenstergröße: 3, Basiswerte: 0 0 0
  29. MedianFilter ProxFilter0(3, 0); //erstellen der Filter für Abstandssensoren
  30. MedianFilter ProxFilter1(3, 0);
  31. MedianFilter ProxFilter2(3, 0);
  32. MedianFilter ProxFilter3(3, 0);
  33. #define MARKLINE0 150
  34. #define MARKLINE1 100
  35. #define MARKLINE2 120
  36. #define SIGN0 500
  37. #define SIGN1 300
  38. #define SIGN2 500
  39. #define MAXSPEED 400
  40. #define FULLSPEEDLEFT 104
  41. #define HALFSPEEDLEFT 52
  42. #define SLOWSPEEDLEFT 26
  43. #define FULLSPEEDRIGHT 100
  44. #define HALFSPEEDRIGHT 50
  45. #define SLOWSPEEDRIGHT 25
  46. int16_t lineValue[3]; //Liniensensoren
  47. uint16_t lineOffset[3]; //von links (0) nach rechts (2)
  48. uint8_t proxValue[4]; //Abstandssensoren v.l. (0) n.r. (3)
  49. int16_t encoderCounts[2]; //Anzahl der Umdrehungen
  50. int16_t driveRotation[2]; //von links (0) nach rechts (1)
  51. int32_t rotationAngle = 0; //Drehwinkel
  52. int32_t turnAngle = 0;
  53. int16_t turnRate;
  54. int16_t gyroOffset;
  55. uint16_t gyroLastUpdate;
  56. //int32_t moveDisplay = 0; //Beschleunigung
  57. //int32_t moveDistance = 0;
  58. int16_t moveRate;
  59. int16_t compassOffset;
  60. //uint16_t compassLastUpdate;
  61. uint16_t lastUpdate = 0; //Systemzeit
  62. uint16_t eventTime = 0; //Zeit seit letzter Geschwindigkeitsänderung
  63. char report[200]; //Ausgabe
  64. float eventSpeed = 1; //vermindert die Geschwindigkeit bei Manövern
  65. int btData = 0; //Gelesene Daten aus Bluetooth
  66. bool alarm = false; //zeigt Manöver abhängig von btData
  67. /*-----------------------------------------------------------------*/
  68. //Setup Bluetoothverbindung
  69. #line 78 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  70. void BlueSetup();
  71. #line 87 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  72. void LineSetup();
  73. #line 116 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  74. void ProxSetup();
  75. #line 122 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  76. void GyroSetup();
  77. #line 155 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  78. void CompassSetup();
  79. #line 186 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  80. void setup();
  81. #line 212 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  82. void TimeUpdate();
  83. #line 218 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  84. void LoopTiming();
  85. #line 262 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  86. void ProxUpdate();
  87. #line 272 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  88. void LineUpdate();
  89. #line 282 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  90. void GyroUpdate();
  91. #line 295 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  92. void CompassUpdate();
  93. #line 308 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  94. void EncoderUpdate();
  95. #line 319 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  96. void Kontaktvermeiden();
  97. #line 338 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  98. void Hindernisumfahren();
  99. #line 464 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  100. void Abbiegen();
  101. #line 625 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  102. void Spurhalten();
  103. #line 670 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  104. void Spurfinden();
  105. #line 688 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  106. void SerielleAusgabe();
  107. #line 703 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  108. void loop();
  109. #line 78 "c:\\Users\\Carolin\\Downloads\\VSCode\\Zumo32U4\\Zumo32U4\\Hauptprojekt\\Hauptprojekt.ino"
  110. void BlueSetup()
  111. {
  112. Serial1.begin(9600); //Initialisierung mit Datengeschwindigkeit(Baud)
  113. Serial1.setTimeout(10); //verkürzt Serial(1).read auf 10 ms statt 1000 ms
  114. if(Serial1.available()) Serial.println("Bluetoothverbindung hergestellt");
  115. if(Serial1.available() > 0) Serial1.read(); //Verwerfen der alten Informationen aus dem Puffer
  116. }
  117. //Setup Liniensensoren
  118. void LineSetup()
  119. {
  120. ledYellow(1);
  121. lineSensors.initThreeSensors(); //Initialisierung von 3 Sensoren (max 5)
  122. //Kalibrierung
  123. uint32_t total[3] = {0, 0, 0};
  124. for(uint8_t i = 0; i < 120; i++)
  125. {
  126. if (i > 30 && i <= 90) motors.setSpeeds(FULLSPEEDLEFT, -FULLSPEEDRIGHT);
  127. else motors.setSpeeds(-FULLSPEEDLEFT, FULLSPEEDRIGHT);
  128. lineSensors.read(lineOffset);
  129. total[0] += lineOffset[0];
  130. total[1] += lineOffset[1];
  131. total[2] += lineOffset[2];
  132. lineSensors.calibrate();
  133. }
  134. motors.setSpeeds(0, 0);
  135. lineOffset[0] = total[0] / 120;
  136. lineOffset[1] = total[1] / 120;
  137. lineOffset[2] = total[2] / 120;
  138. ledYellow(0);
  139. // lineOffset[0] = 240;
  140. // lineOffset[1] = 120;
  141. // lineOffset[2] = 220;
  142. }
  143. //Setup Abstandssensoren
  144. void ProxSetup()
  145. {
  146. proxSensors.initThreeSensors();
  147. }
  148. //Setup Drehbewegungssensor
  149. void GyroSetup()
  150. {
  151. ledYellow(1);
  152. gyro.init(); //Initialisierung
  153. if(!gyro.init()) //Fehlerabfrage
  154. {
  155. //Fehler beim Initialisieren des Drehbewegungssensors
  156. ledRed(1);
  157. while(1)
  158. {
  159. Serial.println("Fehler Drehbewegungssensors");
  160. delay(5000);
  161. }
  162. }
  163. gyro.writeReg(L3G::CTRL1, 0b11111111); //Ausgaberate 800Hz, Tiefpassfilter 100Hz
  164. gyro.writeReg(L3G::CTRL4, 0b00100000); //2000dps Auflösung
  165. gyro.writeReg(L3G::CTRL5, 0b00000000); //Hochpassfilter ausgeschaltet
  166. //Kalibrierung
  167. int32_t total = 0;
  168. for(uint16_t i = 0; i < 1024; i++)
  169. {
  170. while(!gyro.readReg(L3G::STATUS_REG) & 0x08); //Fehlerabfrage
  171. gyro.read();
  172. total += gyro.g.z;
  173. }
  174. gyroOffset = total / 1024;
  175. gyroLastUpdate = micros();
  176. ledYellow(0);
  177. }
  178. //Setup Beschleunigungssensor
  179. void CompassSetup()
  180. {
  181. ledYellow(1);
  182. compass.init(); //Initialisierung
  183. if(!compass.init()) //Fehlerabfrage
  184. {
  185. //Fehler beim Initialisieren des Beschleunigungssensors
  186. ledRed(1);
  187. while(1)
  188. {
  189. Serial.println("Fehler Beschleunigungssensors");
  190. delay(5000);
  191. }
  192. }
  193. compass.enableDefault();
  194. //Kalibrierung
  195. int32_t total = 0;
  196. for (uint16_t i = 0; i < 1024; i++)
  197. {
  198. compass.read();
  199. total += compass.a.x;
  200. }
  201. compassOffset = total / 1024;
  202. //compassLastUpdate = micros();
  203. ledYellow(0);
  204. }
  205. /*-----------------------------------------------------------------*/
  206. void setup()
  207. {
  208. //Initialisierung der Bluetoothverbindung
  209. BlueSetup();
  210. //Initialisierung und Kalibrierung der Sensoren
  211. //Serial1.println("Sensorkalibrierung");
  212. Wire.begin();
  213. delay(500);
  214. ProxSetup();
  215. LineSetup();
  216. GyroSetup();
  217. CompassSetup();
  218. //Zumo bereit zu starten
  219. //Serial1.println("Zumo bereit, starte mit A");
  220. ledGreen(1);
  221. buttonA.waitForButton();
  222. randomSeed(millis());
  223. delay(500);
  224. //Serial1.println("Start");
  225. }
  226. /*-----------------------------------------------------------------*/
  227. //Update Durchlaufzeit
  228. void TimeUpdate()
  229. {
  230. uint16_t m = millis();
  231. eventTime = m - lastUpdate;
  232. }
  233. void LoopTiming()
  234. {
  235. const int AL = 100; // Arraylänge, NUR GERADE Zahlen verwenden!
  236. static unsigned long LoopTime[AL];
  237. static unsigned int Index=0, Messung=0, Min=0xFFFF, Max, Avg;
  238. if (Messung % 2 == 0) // wenn Messung X gerade (0,2,4,6 usw.), entspricht immer Anfang der Loop
  239. {
  240. LoopTime[Index] = millis();
  241. Messung++;
  242. Index++;
  243. return; // Funktion sofort beenden, spart etwas Zeit
  244. }
  245. if (Messung % 2 == 1) // wenn Messung X ungerade (1,3,5,7 usw.), entspricht immer Ende der Loop
  246. {
  247. LoopTime[Index] = millis();
  248. LoopTime[Index-1] = LoopTime[Index] - LoopTime[Index-1]; // Loopdauer einen Index niedriger einspeichern wie aktuell
  249. Messung++;
  250. }
  251. if (Index >= AL) // Array voll, Daten auswerten
  252. {
  253. for (int i = 0; i<AL; i++)
  254. {
  255. Min = min(Min, LoopTime[i]);
  256. Max = max(Max, LoopTime[i]);
  257. Avg += LoopTime[i];
  258. }
  259. Avg = Avg / AL;
  260. Serial1.print(F("Minimal "));Serial1.print(Min);Serial1.println(" ms ");
  261. Serial1.print(F("Durchschnitt "));Serial1.print(Avg);Serial1.println(" ms ");
  262. Serial1.print(F("Maximal "));Serial1.print(Max);Serial1.println(" ms ");
  263. SerielleAusgabe();
  264. Min = 0xFFFF;
  265. Max = 0;
  266. Avg = 0;
  267. Messung = 0;
  268. Index = 0;
  269. }
  270. }
  271. //Update Abstandssensoren
  272. void ProxUpdate()
  273. {
  274. proxSensors.read();
  275. proxValue[0] = ProxFilter0.in(proxSensors.countsLeftWithLeftLeds());
  276. proxValue[1] = ProxFilter1.in(proxSensors.countsFrontWithLeftLeds());
  277. proxValue[2] = ProxFilter2.in(proxSensors.countsFrontWithRightLeds());
  278. proxValue[3] = ProxFilter3.in(proxSensors.countsRightWithRightLeds());
  279. }
  280. //Update Liniensensoren
  281. void LineUpdate()
  282. {
  283. uint16_t lineRaw[3];
  284. lineSensors.read(lineRaw); //lese Messwerte der Liniensensoren aus
  285. lineValue[0] = LineFilter0.in(lineRaw[0] - lineOffset[0]); //ziehe Offsetwerte von Messwerten ab und gib diese in Filter ein
  286. lineValue[1] = LineFilter1.in(lineRaw[1] - lineOffset[1]); //erhält neue mittlere Werte der Filter
  287. lineValue[2] = LineFilter2.in(lineRaw[2] - lineOffset[2]); //"LineFilter.out" um gefilterte Werte auszugeben ohne neue Werte einzugeben
  288. }
  289. //Update Drehbewegungssensor
  290. void GyroUpdate()
  291. {
  292. gyro.read(); //Rohwert 10285 entspricht 90° bzw.
  293. turnRate = gyro.g.z - gyroOffset; //8,75mdps/LSB
  294. uint16_t m = micros();
  295. uint16_t dt = m - gyroLastUpdate;
  296. gyroLastUpdate = m;
  297. int32_t d = (int32_t)turnRate * dt;
  298. turnAngle += (int64_t)d * 14680064 / 17578125;
  299. rotationAngle = (((int32_t)turnAngle >> 16) * 360) >> 16;
  300. }
  301. //Update Beschleunigungssensor
  302. void CompassUpdate()
  303. {
  304. compass.read(); //Rohwert 16384 entspricht 1g (9,81m/s²) bzw.
  305. moveRate = compass.a.x - compassOffset; //bei +/-2g Messbereich 0,61mg/LSB
  306. // uint16_t m = micros();
  307. // uint16_t dt = m - compassLastUpdate;
  308. // compassLastUpdate = m;
  309. // int32_t d = (int32_t)moveRate * dt;
  310. // moveDistance += (int64_t)d * dt >> 14;
  311. // moveDisplay = moveDistance * 9,81 / 1000;
  312. }
  313. //Update Encoder
  314. void EncoderUpdate()
  315. {
  316. encoderCounts[0] = encoders.getCountsLeft();
  317. driveRotation[0] = encoderCounts[0] / 910; //12cpr (Motorwelle) * 75,81:1 (Getriebe) = 909,7cpr (Antriebszahnrad)
  318. encoderCounts[1] = encoders.getCountsRight();
  319. driveRotation[1] = encoderCounts[1] / 910;
  320. }
  321. /*-----------------------------------------------------------------*/
  322. //Funktion Kontaktvermeiden
  323. void Kontaktvermeiden()
  324. {
  325. //Serial1.println("Kontaktvermeiden");
  326. Serial1.print(1);
  327. ledRed(1);
  328. motors.setSpeeds(0, 0);
  329. delay(1000);
  330. while(proxValue[1] == 0 || proxValue[2] == 0)
  331. {
  332. ProxUpdate();
  333. motors.setSpeeds(-HALFSPEEDLEFT, -HALFSPEEDRIGHT);
  334. if(lineValue[0] > MARKLINE0 || lineValue[2] > MARKLINE2) break;
  335. }
  336. motors.setSpeeds(0, 0);
  337. //Serial1.println("Vermeiden beendet");
  338. Serial1.print(0);
  339. }
  340. //Funktion Hindernisumfahrung
  341. void Hindernisumfahren()
  342. {
  343. //Serial1.println("Hindernisumfahren");
  344. Serial1.print(1);
  345. ledYellow(1);
  346. //Schritt 1: Spurwechsel links
  347. //links drehen
  348. turnAngle = 0;
  349. rotationAngle = 0;
  350. GyroUpdate();
  351. while(rotationAngle < 20)
  352. {
  353. GyroUpdate();
  354. LineUpdate();
  355. motors.setSpeeds(SLOWSPEEDLEFT, FULLSPEEDRIGHT);
  356. }
  357. GyroUpdate();
  358. while(rotationAngle < 45)
  359. {
  360. GyroUpdate();
  361. LineUpdate();
  362. motors.setSpeeds(SLOWSPEEDLEFT, FULLSPEEDRIGHT);
  363. if(lineValue[2] > MARKLINE2 && lineValue[2] < SIGN2) break;
  364. }
  365. //geradeaus über Mittellinie fahren
  366. LineUpdate();
  367. while(lineValue[2] < MARKLINE2)
  368. {
  369. LineUpdate();
  370. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  371. //if(lineValue[0] > MARKLINE0) break;
  372. }
  373. //rechts drehen
  374. GyroUpdate();
  375. while(rotationAngle > 10)
  376. {
  377. GyroUpdate();
  378. LineUpdate();
  379. if(lineValue[0] > MARKLINE0 && lineValue[0] < SIGN0) motors.setSpeeds(FULLSPEEDLEFT, 0);
  380. else if(lineValue[2] > MARKLINE2 && lineValue[2] < SIGN2) motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  381. else motors.setSpeeds(FULLSPEEDLEFT, SLOWSPEEDRIGHT);
  382. }
  383. //geradeaus fahren
  384. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  385. //Gegenverkehr beachten
  386. ProxUpdate();
  387. //if(proxValue[1] < 5 || proxValue[2] < 5)
  388. //{
  389. //Schritt 2: Hindernis passieren
  390. //Serial1.println("Aufholen");
  391. lastUpdate = millis();
  392. while(proxValue[3] < 6)
  393. {
  394. ProxUpdate();
  395. LineUpdate();
  396. Spurhalten();
  397. TimeUpdate();
  398. //Serial1.println(eventTime);
  399. if(eventTime > 3000) break;
  400. }
  401. //Serial1.println("Vorbeifahren");
  402. ProxUpdate();
  403. while(proxValue[3] == 6)
  404. {
  405. ProxUpdate();
  406. LineUpdate();
  407. Spurhalten();
  408. }
  409. //Serial1.println("Abstand gewinnen");
  410. lastUpdate = millis();
  411. TimeUpdate();
  412. while(eventTime < 3000)
  413. {
  414. LineUpdate();
  415. Spurhalten();
  416. TimeUpdate();
  417. //Serial1.println(eventTime);
  418. }
  419. //}
  420. //Schritt 3: Spurwechsel rechts
  421. //rechts drehen
  422. turnAngle = 0;
  423. rotationAngle = 0;
  424. GyroUpdate();
  425. while(rotationAngle > -20)
  426. {
  427. GyroUpdate();
  428. LineUpdate();
  429. motors.setSpeeds(FULLSPEEDLEFT, SLOWSPEEDRIGHT);
  430. }
  431. GyroUpdate();
  432. while(rotationAngle > -45)
  433. {
  434. GyroUpdate();
  435. LineUpdate();
  436. motors.setSpeeds(FULLSPEEDLEFT, SLOWSPEEDRIGHT);
  437. if(lineValue[0] > MARKLINE0 && lineValue[0] < SIGN0) break;
  438. }
  439. //geradeaus über Mittellinie fahren
  440. LineUpdate();
  441. while(lineValue[0] < MARKLINE0)
  442. {
  443. LineUpdate();
  444. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  445. //if(lineValue[0] > MARKLINE0) break;
  446. }
  447. //links drehen
  448. GyroUpdate();
  449. while(rotationAngle < -10)
  450. {
  451. GyroUpdate();
  452. LineUpdate();
  453. if(lineValue[2] > MARKLINE2 && lineValue[2] < SIGN2) motors.setSpeeds(0, FULLSPEEDRIGHT);
  454. else if(lineValue[0] > MARKLINE0 && lineValue[0] < SIGN0) motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  455. else motors.setSpeeds(SLOWSPEEDLEFT, FULLSPEEDRIGHT);
  456. }
  457. //geradeaus fahren
  458. //Serial1.println("Umfahren beendet");
  459. Serial1.print(0);
  460. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  461. }
  462. //Funktion Abbiegen
  463. void Abbiegen()
  464. {
  465. ledYellow(1);
  466. //Serial1.println("Abbiegen");
  467. Serial1.print(1);
  468. //Markierung analysieren
  469. LineUpdate();
  470. bool leftCode; //links => 1
  471. bool rightCode; //rechts => 2
  472. if((lineValue[0] > SIGN0 && lineValue[0] < 1600) && (lineValue[1] > SIGN1 && lineValue[1] < 800)) leftCode = true;
  473. else leftCode = false;
  474. if((lineValue[2] > SIGN2 && lineValue[2] < 1600) && (lineValue[1] > SIGN1 && lineValue[1] < 800)) rightCode = true;
  475. else rightCode = false;
  476. //Zufallszahl erzeugen
  477. uint8_t randy; //geradeaus => 0
  478. if(leftCode == true && rightCode == true) randy = random(1, 3); //1, 2
  479. else if(leftCode == true && rightCode == false) //randy = 1;
  480. {
  481. randy = random(0, 2); //0, 1
  482. //if(randy == 0) randy = random(0, 2); //erhöht Wahrscheinlickeit abzubiegen
  483. }
  484. else if(leftCode == false && rightCode == true) //randy = 2;
  485. {
  486. randy = random(0, 2); //0, 1
  487. //if(randy == 0) randy = random(0, 2); //erhöht Wahrscheinlickeit abzubiegen
  488. if(randy == 1) randy = 2; //!1 => 2
  489. }
  490. //links Abbiegen (von der Verbindungsstrecke)
  491. if(randy == 1 && rightCode == true)
  492. {
  493. //Serial1.println("links Abbiegen von der Verbindungsstrecke");
  494. //geradeaus zur Mittellinie fahren
  495. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT+10);
  496. lastUpdate = millis();
  497. TimeUpdate();
  498. while(eventTime < 1000)
  499. {
  500. TimeUpdate();
  501. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT+10);
  502. }
  503. LineUpdate();
  504. while(lineValue[0] < MARKLINE0 && lineValue[2] < MARKLINE2)
  505. {
  506. LineUpdate();
  507. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  508. }
  509. //links drehen
  510. turnAngle = 0;
  511. rotationAngle = 0;
  512. GyroUpdate();
  513. while(rotationAngle < 90)
  514. {
  515. GyroUpdate();
  516. LineUpdate();
  517. if(lineValue[2] > MARKLINE2 && lineValue[2] < SIGN2) motors.setSpeeds(0, FULLSPEEDRIGHT);
  518. else if(lineValue[0] > MARKLINE0 && lineValue[0] < SIGN0) motors.setSpeeds(HALFSPEEDLEFT, FULLSPEEDRIGHT);
  519. else motors.setSpeeds(SLOWSPEEDLEFT, FULLSPEEDRIGHT);
  520. }
  521. //geradeaus fahren
  522. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  523. }
  524. //links Abbiegen (vom Rundkurs)
  525. else if(randy == 1 && leftCode == true)
  526. {
  527. //Serial1.println("links Abbiegen vom Rundkurs");
  528. //links drehen
  529. turnAngle = 0;
  530. rotationAngle = 0;
  531. driveRotation[1] = 0;
  532. GyroUpdate();
  533. while(rotationAngle < 40)
  534. {
  535. GyroUpdate();
  536. EncoderUpdate();
  537. motors.setSpeeds(SLOWSPEEDLEFT, FULLSPEEDRIGHT);
  538. //if(driveRotation[1] > 1) break;
  539. }
  540. driveRotation[1] = 0;
  541. GyroUpdate();
  542. while(rotationAngle < 85)
  543. {
  544. GyroUpdate();
  545. EncoderUpdate();
  546. motors.setSpeeds(SLOWSPEEDLEFT, FULLSPEEDRIGHT);
  547. //if(driveRotation[1] > 1) break;
  548. }
  549. //geradeaus fahren
  550. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  551. lastUpdate = millis();
  552. TimeUpdate();
  553. while(eventTime < 1000)
  554. {
  555. TimeUpdate();
  556. LineUpdate();
  557. if(lineValue[2] > MARKLINE2 && lineValue[2] < SIGN2) break;
  558. }
  559. lastUpdate = millis();
  560. TimeUpdate();
  561. while(eventTime < 1000)
  562. {
  563. TimeUpdate();
  564. LineUpdate();
  565. Spurhalten();
  566. }
  567. }
  568. //rechts Abbiegen
  569. else if(randy == 2 && rightCode == true)
  570. {
  571. //Serial1.println("rechts Abbiegen");
  572. //rechts drehen
  573. turnAngle = 0;
  574. rotationAngle = 0;
  575. driveRotation[0] = 0;
  576. GyroUpdate();
  577. while(rotationAngle > -40)
  578. {
  579. GyroUpdate();
  580. EncoderUpdate();
  581. LineUpdate();
  582. motors.setSpeeds(FULLSPEEDLEFT, SLOWSPEEDRIGHT);
  583. if(lineValue[0] > MARKLINE0 && lineValue[0] < SIGN0) break;
  584. }
  585. GyroUpdate();
  586. lastUpdate = millis();
  587. while(rotationAngle > -85)
  588. {
  589. TimeUpdate();
  590. GyroUpdate();
  591. LineUpdate();
  592. if(eventTime > 3000) break;
  593. if(lineValue[0] > MARKLINE0 && lineValue[0] < SIGN0) motors.setSpeeds(FULLSPEEDLEFT, 0);
  594. //else if(lineValue[1] > MARKLINE1 && lineValue[1] < SIGN1) motors.setSpeeds(-SLOWSPEEDLEFT, -SLOWSPEEDRIGHT);
  595. else if(lineValue[2] > MARKLINE2 && lineValue[2] < SIGN2) motors.setSpeeds(FULLSPEEDLEFT, HALFSPEEDRIGHT);
  596. else motors.setSpeeds(FULLSPEEDLEFT, SLOWSPEEDRIGHT);
  597. }
  598. }
  599. //nicht Abbiegen, geradeaus fahren
  600. else
  601. {
  602. //Serial1.println("nicht Abbiegen");
  603. motors.setSpeeds(FULLSPEEDLEFT, FULLSPEEDRIGHT);
  604. lastUpdate = millis();
  605. TimeUpdate();
  606. while(eventTime < 1000)
  607. {
  608. TimeUpdate();
  609. LineUpdate();
  610. Spurhalten();
  611. }
  612. }
  613. //Serial1.println("Abbiegen beendet");
  614. Serial1.print(0);
  615. }
  616. //Funktion Spurhalten
  617. void Spurhalten()
  618. {
  619. uint16_t StartTime = millis();
  620. uint16_t Update = millis();
  621. uint16_t Time = Update - StartTime;
  622. //linke Linie erreicht, nach rechts fahren
  623. if(lineValue[0] > MARKLINE0 && lineValue[0] < SIGN0)
  624. {
  625. ledYellow(1);
  626. //Serial1.println("Spur nach rechts korrigieren");
  627. motors.setSpeeds(FULLSPEEDLEFT/eventSpeed, SLOWSPEEDRIGHT/eventSpeed);
  628. while(Time < 100)
  629. {
  630. Update = millis();
  631. Time = Update - StartTime;
  632. LineUpdate();
  633. if(lineValue[2] > MARKLINE2) break;
  634. }
  635. }
  636. //rechte Linie erreicht, nach links fahren
  637. else if(lineValue[2] > MARKLINE2 && lineValue[2] < SIGN2)
  638. {
  639. ledYellow(1);
  640. //Serial1.println("Spur nach links korrigieren");
  641. motors.setSpeeds(SLOWSPEEDLEFT/eventSpeed, FULLSPEEDRIGHT/eventSpeed);
  642. while(Time < 100)
  643. {
  644. Update = millis();
  645. Time = Update - StartTime;
  646. LineUpdate();
  647. if(lineValue[0] > MARKLINE0) break;
  648. }
  649. }
  650. //normale Fahrt
  651. else
  652. {
  653. ledGreen(1);
  654. motors.setSpeeds(FULLSPEEDLEFT/eventSpeed, FULLSPEEDRIGHT/eventSpeed);
  655. }
  656. }
  657. //Funktion Spurfinden
  658. void Spurfinden()
  659. {
  660. ledRed(1);
  661. //Serial1.println("Spurfinden");
  662. Serial1.print(1);
  663. lastUpdate = millis();
  664. while(lineValue[0] < MARKLINE0 && lineValue[2] < MARKLINE2) //Zumo fährt solange rückwärst bis er wieder auf der Spur steht
  665. {
  666. TimeUpdate();
  667. LineUpdate();
  668. motors.setSpeeds(-FULLSPEEDLEFT, -FULLSPEEDRIGHT);
  669. if(eventTime > 3000) break;
  670. }
  671. //Serial1.println("Spur gefunden");
  672. Serial1.print(0);
  673. }
  674. //Funktion Serielle Ausgabe
  675. void SerielleAusgabe()
  676. {
  677. snprintf_P(report, sizeof(report),
  678. PSTR("Abstand: %d %d %d %d Linie: %d %d %d"),
  679. proxValue[0], proxValue[1], proxValue[2], proxValue[3],
  680. lineValue[0], lineValue[1], lineValue[2]);
  681. Serial1.println(report);
  682. snprintf_P(report, sizeof(report),
  683. PSTR("Weg: %d %d Winkel: %d Beschleunigung: %d"),
  684. driveRotation[0], driveRotation[1], rotationAngle, moveRate);
  685. Serial1.println(report);
  686. }
  687. /*-----------------------------------------------------------------*/
  688. void loop()
  689. {
  690. //LoopTiming(); //Zykluszeit beträgt max. 20ms im Idle
  691. ledGreen(0);
  692. ledYellow(0);
  693. ledRed(0);
  694. //Abfragen der Sensordaten
  695. ProxUpdate();
  696. EncoderUpdate();
  697. GyroUpdate();
  698. CompassUpdate();
  699. LineUpdate();
  700. TimeUpdate();
  701. //Funktionsauswahl
  702. //btData = Serial1.readString();
  703. if(Serial1.available() > 0) btData = Serial1.read();
  704. if(btData == '1') alarm = true;
  705. else if(btData == '0') alarm = false;
  706. //verfügbare Funktionen bei laufenden Manövern
  707. //if(btData == "Abbiegen" || btData == "Hindernisumfahren" || btData == "Kontaktvermeiden"|| btData == "Spurfinden")
  708. if(alarm == true)
  709. {
  710. //Serial1.println("Verstanden");
  711. eventSpeed = 1.4;
  712. if(moveRate > 1500 || (proxValue[1] == 6 && proxValue[2] == 6) || lineValue[0] > SIGN0 || lineValue[2] > SIGN2 || lineValue[1] > MARKLINE1) motors.setSpeeds(0, 0);
  713. else Spurhalten();
  714. }
  715. //verfügbare Funktionen im Normalfall
  716. else
  717. {
  718. eventSpeed = 1.0;
  719. //if(abs(moveRate) > 1500 && eventTime > 1000) Kontaktvermeiden();
  720. if(proxValue[1] == 6 && proxValue[2] == 6) Hindernisumfahren();
  721. else if((lineValue[0] > SIGN0 && lineValue[0] < 1600) && (lineValue[1] > SIGN1 && lineValue[1] < 800)) Abbiegen();
  722. else if((lineValue[2] > SIGN2 && lineValue[2] < 1600) && (lineValue[1] > SIGN1 && lineValue[1] < 800)) Abbiegen();
  723. else if(lineValue[1] > MARKLINE1 && lineValue[1] < SIGN1) Spurfinden();
  724. else Spurhalten();
  725. }
  726. //LoopTiming();
  727. }