Browse Source

added function: bluetooth communication

fstange 6 years ago
parent
commit
27621a603f
1 changed files with 40 additions and 26 deletions
  1. 40 26
      Zumo32U4/Hauptprojekt/Hauptprojekt.ino

+ 40 - 26
Zumo32U4/Hauptprojekt/Hauptprojekt.ino

@@ -42,13 +42,10 @@ int16_t   moveRate;
42 42
 int16_t   compassOffset;                
43 43
 uint16_t  compassLastUpdate;
44 44
 
45
-uint8_t   randy;                        //Richtungsauswahl Abbiegen geradeaus => 0
46
-bool      leftCode;                     //                          links => 1
47
-bool      rightCode;                    //                          rechts => 2
48
-
49 45
 int       maxSpeed = 200;               //Maximale Geschwindigkeit (möglich 400)
50
-char      dir = '0';                    //Fahrtrichtung, Ereignis
46
+char      dir = "0";                    //Fahrtrichtung, Ereignis
51 47
 char      report[200];                  //Ausgabe
48
+char      attention;                    //zeigt Überhol-/Abbiegevorgang an
52 49
 
53 50
 /*-----------------------------------------------------------------*/
54 51
 
@@ -133,10 +130,10 @@ void setup()
133 130
 {
134 131
   //Initialisierung der Bluetoothverbindung
135 132
   Serial1.begin(9600);                                              
136
-  if(Serial1.available()) Serial1.println("bluetooth available");  
133
+  if(Serial1.available()) Serial1.println("Bluetooth verfügbar");  
137 134
 
138 135
   //Initialisierung und Kalibrierung der Sensoren
139
-  Serial1.println("sensor calibration, dont touch");   
136
+  Serial1.println("Sensorkalibrierung");   
140 137
   Wire.begin();                       
141 138
   delay(500);
142 139
   ProxSetup();
@@ -145,7 +142,7 @@ void setup()
145 142
   CompassSetup();
146 143
   
147 144
   //Zumo bereit zu starten
148
-  Serial1.println("Zumo ready to start, press A");
145
+  Serial1.println("Zumo bereit, drücke A");
149 146
   ledGreen(1);
150 147
   buttonA.waitForButton();
151 148
   randomSeed(millis());
@@ -203,7 +200,7 @@ void CompassUpdate()
203 200
 void EncoderUpdate()
204 201
 {
205 202
   encoderCounts[0] = encoders.getCountsLeft();
206
-  driveRotation[0] = encoderCounts[0] / 910;                                //12cpr (Motorwelle) * 75,81:1 (Getriebe) = 909,7cpr (Antriebszahnrad)
203
+  driveRotation[0] = encoderCounts[0] / 910;                         //12cpr (Motorwelle) * 75,81:1 (Getriebe) = 909,7cpr (Antriebszahnrad)
207 204
   encoderCounts[1] = encoders.getCountsRight();
208 205
   driveRotation[1] = encoderCounts[1] / 910;
209 206
 }
@@ -213,21 +210,21 @@ void EncoderUpdate()
213 210
  //Funktion Kollisionserkennung
214 211
 void CollisionDetection()                                            
215 212
 {
216
-  dir = 'X';
217
-  Serial1.println("impact detected");
213
+  dir = "X";
214
+  Serial1.println("Aufprall erkannt");
218 215
   ledRed(1);
219 216
 
220 217
   motors.setSpeeds(0, 0);
221 218
   delay(500);
222 219
   motors.setSpeeds(-maxSpeed/2, -maxSpeed/2);
223
-  delay(1000); 
220
+  delay(500); 
224 221
 }
225 222
 
226 223
 //Funktion Hindernisumfahrung
227 224
 void ObstacleAvoidance()                                              
228 225
 {
229
-  dir = 'U';
230
-  Serial1.println("obstacle avoidance");  
226
+  dir = "U";
227
+  Serial1.println("Überholen");  
231 228
   ledYellow(1);
232 229
 
233 230
   //Schritt 1: Spurwechsel links   
@@ -263,7 +260,6 @@ void ObstacleAvoidance()
263 260
   if(proxValue[1] < 4 || proxValue[2] < 4)
264 261
   {
265 262
     //Schritt 2: Hindernis passieren
266
-    maxSpeed = 400;
267 263
     motors.setSpeeds(maxSpeed, maxSpeed);                    
268 264
     delay(1000);
269 265
     proxSensors.read();
@@ -275,7 +271,6 @@ void ObstacleAvoidance()
275 271
       proxValue[3] = proxSensors.countsRightWithRightLeds();  
276 272
       TrackKeeper();
277 273
     }
278
-    maxSpeed = 200;
279 274
   }
280 275
   
281 276
   //Schritt 3: Spurwechsel rechts     
@@ -303,6 +298,8 @@ void ObstacleAvoidance()
303 298
   }
304 299
   //geradeaus fahren
305 300
   motors.setSpeeds(maxSpeed, maxSpeed); 
301
+
302
+  Serial1.println("Überholen beendet");  
306 303
 }
307 304
 
308 305
 //Funktion Spurhalten
@@ -314,7 +311,7 @@ void TrackKeeper()
314 311
     motors.setSpeeds(maxSpeed, maxSpeed/2);
315 312
     ledYellow(1);
316 313
     delay(100);    
317
-    dir = 'R';
314
+    dir = "R";
318 315
   }
319 316
 
320 317
   //rechte Linie erreicht, nach links fahren
@@ -323,7 +320,7 @@ void TrackKeeper()
323 320
     motors.setSpeeds(maxSpeed/2, maxSpeed);  
324 321
     ledYellow(1); 
325 322
     delay(100); 
326
-    dir = 'L';     
323
+    dir = "L";     
327 324
   }
328 325
 
329 326
   //Linie überfahren, zurücksetzen
@@ -335,7 +332,7 @@ void TrackKeeper()
335 332
       motors.setSpeeds(-maxSpeed/2, -maxSpeed/2);
336 333
     }
337 334
     delay(500); 
338
-    dir = 'B';     
335
+    dir = "B";     
339 336
   }  
340 337
 
341 338
   //normale Fahrt
@@ -343,7 +340,7 @@ void TrackKeeper()
343 340
   {
344 341
     motors.setSpeeds(maxSpeed, maxSpeed);    
345 342
     ledGreen(1); 
346
-    dir = 'F';     
343
+    dir = "F";     
347 344
   }
348 345
 }
349 346
 
@@ -351,15 +348,19 @@ void TrackKeeper()
351 348
 void Crossroad()
352 349
 {
353 350
   ledYellow(1); 
354
-  dir = 'A';
351
+  dir = "A";
352
+  Serial1.println("Abbiegen");  
355 353
 
356 354
   //Kodierung analysieren
355
+  bool  leftCode;                                                         //links => 1
356
+  bool  rightCode;                                                        //rechts => 2
357 357
   if(lineValue[0] > 500 && lineValue[0] < 1500) leftCode = true;
358 358
   else leftCode = false;
359 359
   if(lineValue[2] > 500 && lineValue[2] < 1500) rightCode = true;
360 360
   else rightCode = false;
361 361
 
362 362
   //Zufallszahl erzeugen
363
+  uint8_t randy;                                                          //geradeaus => 0
363 364
   if(leftCode == true && rightCode == true) randy = random(1, 3);         //1, 2
364 365
   else if(leftCode == true && rightCode == false) randy = random(0, 2);   //0, 1
365 366
   else if(leftCode == false && rightCode == true)
@@ -408,6 +409,8 @@ void Crossroad()
408 409
 
409 410
   //nicht Abbiegen, geradeaus fahren
410 411
   else motors.setSpeeds(maxSpeed/2, maxSpeed/2); 
412
+
413
+  Serial1.println("Abbiegen beendet");  
411 414
 }
412 415
 
413 416
 //Funktion Serielle Ausgabe
@@ -437,11 +440,22 @@ void loop()
437 440
   EncoderUpdate();
438 441
 
439 442
   //Funktionsauswahl
440
-  if(moveRate > 1000) CollisionDetection();         
441
-  else if(proxValue[0] == 6 || proxValue[1] == 6) motors.setSpeeds(0, 0);  
442
-  else if(proxValue[0] > 4 || proxValue[1] > 4) ObstacleAvoidance();
443
-  else if((lineValue[0] > 500 && lineValue[0] < 1500) || (lineValue[2] > 500 && lineValue[2] < 1500)) Crossroad();
444
-  else TrackKeeper();
443
+  attention = Serial1.read();
444
+  if(attention == "Überholen" || "Abbiegen") 
445
+  {
446
+    maxSpeed = 100;
447
+    if(moveRate > 1000 || proxValue[0] > 4 || proxValue[1] > 4 || lineValue[0] > 500 && lineValue[0] < 1500) || 
448
+    lineValue[2] > 500 && lineValue[2] < 1500) motors.setSpeeds(0, 0);  
449
+    else TrackKeeper();
450
+  }
451
+  else
452
+  {
453
+    if(moveRate > 1000) CollisionDetection();         
454
+    else if(proxValue[0] == 6 || proxValue[1] == 6) motors.setSpeeds(0, 0);  
455
+    else if(proxValue[0] > 4 || proxValue[1] > 4) ObstacleAvoidance();
456
+    else if((lineValue[0] > 500 && lineValue[0] < 1500) || (lineValue[2] > 500 && lineValue[2] < 1500)) Crossroad();
457
+    else TrackKeeper();
458
+  }
445 459
 
446 460
   //Ausgabe über Bluetoothverbindung
447 461
   SerialOutput();