Benutzer-Werkzeuge

Webseiten-Werkzeuge


hello_mbot_kalibrierung

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
hello_mbot_kalibrierung [2025/02/19 10:35] – [Quellcode (engl. Sourcecode)] torsten.roehlhello_mbot_kalibrierung [2025/02/19 19:55] (aktuell) – [Bedienung] torsten.roehl
Zeile 97: Zeile 97:
 ==== Quellcode (engl. Sourcecode) ==== ==== Quellcode (engl. Sourcecode) ====
  
-FIXME  Code noch nicht fertig!+
 <Code c linenums:1 | Listing 1:MinimalesProgramm.ino> <Code c linenums:1 | Listing 1:MinimalesProgramm.ino>
 #include "MeMCore.h" #include "MeMCore.h"
  
 +// Hardware
 +MeBuzzer buzzer;
 MeLineFollower lineFinder(PORT_2); MeLineFollower lineFinder(PORT_2);
 MeRGBLed led(0, 2);   // must be fixed! MeRGBLed led(0, 2);   // must be fixed!
Zeile 106: Zeile 108:
 MeDCMotor motor2(M2); MeDCMotor motor2(M2);
 int PIN_BUTTON = 7;   // must be fixed! int PIN_BUTTON = 7;   // must be fixed!
- 
 int threshold  = 500; // Analoger Schwellenwert! int threshold  = 500; // Analoger Schwellenwert!
 int buttonCount; int buttonCount;
 +// Allgemein
 +int speed = 100; // Motor speed
 unsigned long time90degree; unsigned long time90degree;
-bool isTurning = false; + 
-unsigned long turnStartTime = 0; +// turn90Degree... 
-int speed 250; // Motor speed+bool t90d_isTurning = false; 
 +unsigned long t90d_turnStartTime = 0; 
 +// actionCalibration... 
 +unsigned long ac_turnStartTimeLeft  0; 
 +unsigned long ac_turnTimeLeft = 0; 
 +unsigned long ac_turnStartTimeRight = 0; 
 +unsigned long ac_turnTimeRight = 0; 
 +bool ac_isTurning = false; 
 +bool ac_isLeftTurning = false; 
 +bool ac_isRightTurning = false; 
 +bool ac_calibrationDone = false; 
 +//actionTest... 
 +bool at_doneLeft = false; 
 +bool at_doneRight = false; 
 +bool at_testComplete = false;
  
 enum State { enum State {
Zeile 152: Zeile 169:
  
 bool turn90Degree(bool left) { bool turn90Degree(bool left) {
- +  // step: ...start turning 
-   +  if (!t90d_isTurning) {
-  if (!isTurning) { +
-    turnStartTime = millis(); +
-    isTurning = true; +
- +
-    if (left) { +
-      motor1.run(speed); +
-      motor2.run(speed); +
-    } else { +
-      motor1.run(-speed); +
-      motor2.run(-speed); +
-    } +
-  } +
- +
-  if (isTurning && millis() - turnStartTime >= time90degree) {+
     motor1.stop();     motor1.stop();
     motor2.stop();     motor2.stop();
-    isTurning false+    t90d_isTurning     true; 
-    return true;+    int dir = left ? 1 : -1; 
 + 
 +    motor1.run(dir * speed); 
 +    motor2.run(dir * speed); 
 +    t90d_turnStartTime = millis()
 +    return false;
   }   }
 +  // step: ...in progress
 +  if (millis() - t90d_turnStartTime < time90degree)
 +    return false;
  
-  return false;+  // step: ... finished 
 +  motor1.stop(); 
 +  motor2.stop(); 
 +  t90d_isTurning = false
 + 
 +  return true;
 } }
  
Zeile 213: Zeile 229:
  
 void actionCalibration() { void actionCalibration() {
 +
 +  if (ac_calibrationDone)
 +    return;
 +
   led.setColorAt(1, 255, 0, 0);   led.setColorAt(1, 255, 0, 0);
   led.setColorAt(0, 255, 0, 0);   led.setColorAt(0, 255, 0, 0);
   led.show();   led.show();
-  // TODO non-blocking +  int sensorState = lineFinder.readSensors();
  
 +  // step - start turning left
 +  if (!ac_isTurning) {
 +    motor1.stop();
 +    motor2.stop();
 +    motor1.run(speed);
 +    motor2.run(speed);
 +    ac_turnStartTimeLeft = millis();
 +    ac_isTurning = true;
 +    ac_isRightTurning = true;
 +    do {
 +      sensorState = lineFinder.readSensors();
 +    } while ( sensorState != S1_OUT_S2_OUT);
 +  }
 +
 +  // step - end turning left ... start turning right
 +  if (ac_isRightTurning && sensorState == S1_IN_S2_IN) {
 +    motor1.stop();
 +    motor2.stop();
 +    ac_turnTimeLeft = millis() - ac_turnStartTimeLeft;
 +
 +    motor1.run(-speed);
 +    motor2.run(-speed);
 +    ac_turnStartTimeRight = millis();
 +    do {
 +      sensorState = lineFinder.readSensors();
 +    } while ( sensorState != S1_OUT_S2_OUT);
 +    ac_isRightTurning = false;
 +    ac_isLeftTurning = true;
 +  }
 +
 +  // step - end turing right
 +  if (ac_isLeftTurning && sensorState == S1_IN_S2_IN) {
 +    motor1.stop();
 +    motor2.stop();
 +    ac_turnTimeRight = millis() - ac_turnStartTimeRight;
 +    time90degree = (ac_turnTimeLeft + ac_turnTimeRight) / 2.0;
 +    ac_calibrationDone = true;
 +    led.setColorAt(1, 0, 0, 0);
 +    led.setColorAt(0, 0, 0, 0);
 +    led.show();
 +    buzzer.tone(1200, 600);
 +  }
  
 } }
 void actionTest() { void actionTest() {
-  led.setColorAt(1, 0, 255, 0); 
-  led.setColorAt(0, 0, 255, 0); 
-  led.show(); 
  
-  static bool doneLeft false+  if (!at_doneLeft) { // Links-Drehung 
-  static bool doneRight = false;+    at_doneLeft turn90Degree(true)
 +    led.setColorAt(1, 0, 255, 0); 
 +    led.setColorAt(0, 0, 255, 0); 
 +    led.show(); 
 +    return; 
 +  }
  
-  if (!doneLeft) { +  if (!at_doneRight) { // Rechts-Drehung 
-    doneLeft = turn90Degree(true);+    at_doneRight = turn90Degree(false)
 +    return;
   }   }
-  else if (doneLeft && !doneRight) { + 
-    doneRight = turn90Degree(false); +  if (!at_testComplete) { // fertig 
-  } +    motor1.stop(); 
-  else if (doneLeft && doneRight{ +    motor2.stop(); 
-    state STATE_OFF;+    led.setColorAt(1, 0, 0, 0); 
 +    led.setColorAt(0, 0, 0, 0); 
 +    led.show(); 
 +    buzzer.tone(1200, 600); 
 +    at_testComplete true; 
 +    return;
   }   }
 } }
Zeile 243: Zeile 313:
   led.setColorAt(0, 0, 0, 0);   led.setColorAt(0, 0, 0, 0);
   led.show();   led.show();
 +
 +  // reset
   motor1.stop();   motor1.stop();
   motor2.stop();   motor2.stop();
-   + 
 +  // reset state calibration 
 +  ac_isTurning = false; 
 +  ac_isLeftTurning = false; 
 +  ac_isRightTurning = false; 
 +  ac_calibrationDone = false; 
 + 
 +  // reset state test 
 +  at_doneLeft = false; 
 +  at_doneRight = false; 
 +  at_testComplete = false; 
 +}
 </Code> </Code>
 ==== Bedienung ==== ==== Bedienung ====
Zeile 262: Zeile 345:
  
 ⚡ **Schritt 3:**   ⚡ **Schritt 3:**  
-▶ **Nach dem Testlauf** kehrt der mBot in den **Ruhezustand** zurück.  +▶ **Nach dem Testlauf** kehrt der mBot durch Drückens des Tasters in den **Ruhezustand** zurück.  
    * Der Vorgang kann **von vorne** gestartet werden.    * Der Vorgang kann **von vorne** gestartet werden.
  
hello_mbot_kalibrierung.1739961301.txt.gz · Zuletzt geändert: 2025/02/19 10:35 von torsten.roehl