瀏覽代碼

added function: bluetooth communication

fstange 6 年之前
父節點
當前提交
27621a603f
共有 1 個文件被更改,包括 40 次插入26 次删除
  1. 40 26
      Zumo32U4/Hauptprojekt/Hauptprojekt.ino

+ 40 - 26
Zumo32U4/Hauptprojekt/Hauptprojekt.ino

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