hello_mbot_kalibrierung
                Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| hello_mbot_kalibrierung [2025/02/19 12:23] – torsten.roehl | hello_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: | <Code c linenums:1 | Listing 1: | ||
| #include " | #include " | ||
| + | // 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 108: | Zeile 110: | ||
| int threshold | int threshold | ||
| int buttonCount; | int buttonCount; | ||
| + | // Allgemein | ||
| + | int speed = 100; // Motor speed | ||
| unsigned long time90degree; | unsigned long time90degree; | ||
| - | bool isTurning = false; | ||
| - | int speed = 250; // Motor speed | + | // turn90Degree... | 
| - | unsigned long turnStartTime | + | bool t90d_isTurning | 
| - | unsigned long turnTime | + | unsigned long t90d_turnStartTime = 0; | 
| - | unsigned long backTurnStartTime | + | // actionCalibration... | 
| - | unsigned long backTurnTime | + | unsigned long ac_turnStartTimeLeft | 
| + | unsigned long ac_turnTimeLeft | ||
| + | unsigned long ac_turnStartTimeRight | ||
| + | unsigned long ac_turnTimeRight | ||
| + | bool ac_isTurning = false; | ||
| + | bool ac_isLeftTurning = false; | ||
| + | bool ac_isRightTurning = false; | ||
| + | bool ac_calibrationDone = false; | ||
| + | // | ||
| + | bool at_doneLeft = false; | ||
| + | bool at_doneRight = false; | ||
| + | bool at_testComplete = false; | ||
| enum State { | enum State { | ||
| Zeile 155: | Zeile 169: | ||
| bool turn90Degree(bool left) { | bool turn90Degree(bool left) { | ||
| - | + | // step: ...start turning | |
| - | + | if (!t90d_isTurning) { | |
| - | if (!isTurning) { | + | |
| - | turnStartTime = millis(); | + | |
| - | isTurning | + | |
| - | dir = 1; | + | |
| - | if(!left) | + | |
| - | dir = -1; | + | |
| - | + | ||
| - | motor1.run(dir*speed); | + | |
| - | motor2.run(dir*speed); | + | |
| - | + | ||
| - | } | + | |
| - | + | ||
| - | if (isTurning && millis() - turnStartTime >= time90degree) { | + | |
| motor1.stop(); | motor1.stop(); | ||
| motor2.stop(); | motor2.stop(); | ||
| - |  | + |  | 
| - | return | + | int dir = left ? 1 : -1; | 
| + | |||
| + | motor1.run(dir * speed); | ||
| + | motor2.run(dir * speed); | ||
| + | t90d_turnStartTime = millis(); | ||
| + | return | ||
| } | } | ||
| + | // step: ...in progress | ||
| + | if (millis() - t90d_turnStartTime < time90degree) | ||
| + | return false; | ||
| - |  | + |  | 
| + | motor1.stop(); | ||
| + | motor2.stop(); | ||
| + | t90d_isTurning = false; | ||
| + | |||
| + | return true; | ||
| } | } | ||
| Zeile 215: | Zeile 229: | ||
| void actionCalibration() { | void actionCalibration() { | ||
| + | |||
| + | if (ac_calibrationDone) | ||
| + | return; | ||
| led.setColorAt(1, | led.setColorAt(1, | ||
| led.setColorAt(0, | led.setColorAt(0, | ||
| led.show(); | led.show(); | ||
| + | int sensorState = lineFinder.readSensors(); | ||
| - |  | + |  | 
| - |  | + | if (!ac_isTurning) { | 
| - |  | + | motor1.stop(); | 
| + | motor2.stop(); | ||
| + | motor1.run(speed); | ||
| + | motor2.run(speed); | ||
| + | ac_turnStartTimeLeft | ||
| + |  | ||
| + |  | ||
| + | do { | ||
| + | sensorState = lineFinder.readSensors(); | ||
| + | } while ( sensorState != S1_OUT_S2_OUT); | ||
| + | } | ||
| - | if (!calibrationDone) { | + |  | 
| - |  | + |  | 
| + |  | ||
| + | motor2.stop(); | ||
| + | ac_turnTimeLeft = millis() - ac_turnStartTimeLeft; | ||
| - |  | + | motor1.run(-speed); | 
| - |  | + | motor2.run(-speed); | 
| - | motor2.run(-speed); | + |  | 
| - |  | + | do { | 
| - |  | + |  | 
| - | } | + |  | 
| + | ac_isRightTurning = false; | ||
| + | ac_isLeftTurning = true; | ||
| + |  | ||
| - |  | + | // step - end turing right | 
| - | motor1.stop(); | + |  | 
| - | motor2.stop(); | + | motor1.stop(); | 
| - |  | + | motor2.stop(); | 
| - |  | + |  | 
| - |  | + |  | 
| - |  | + |  | 
| - | motor1.run(-speed); | + | led.setColorAt(1, 0, 0, 0); | 
| - |  | + | led.setColorAt(0, 0, 0, 0); | 
| - |  | + |  | 
| - | + |  | |
| - | if (isBackTurning && sensorState == S1_IN_S2_IN) { | + | |
| - | motor1.stop(); | + | |
| - |  | + | |
| - | backTurnTime = millis() - backTurnStartTime; | + | |
| - | isBackTurning = false; | + | |
| - | time90degree = (turnTime + backTurnTime) / 2; | + | |
| - | calibrationDone = true; | + | |
| - | } | + | |
| } | } | ||
| + | |||
| } | } | ||
| void actionTest() { | void actionTest() { | ||
| - | led.setColorAt(1, | ||
| - | led.setColorAt(0, | ||
| - | led.show(); | ||
| - |  | + |  | 
| - |  | + | at_doneLeft | 
| + |  | ||
| + | led.setColorAt(0, | ||
| + | led.show(); | ||
| + | return; | ||
| + | } | ||
| - | if (!doneLeft) { | + | if (!at_doneRight) { // Rechts-Drehung | 
| - |  | + |  | 
| + | return; | ||
| } | } | ||
| - | else if (doneLeft && | + | |
| - |  | + |  | 
| - | } | + |  | 
| - | else if (doneLeft && doneRight) { | + |  | 
| - |  | + |  | 
| + |  | ||
| + | led.show(); | ||
| + | buzzer.tone(1200, | ||
| + | at_testComplete | ||
| + | return; | ||
| } | } | ||
| } | } | ||
| Zeile 278: | Zeile 313: | ||
| led.setColorAt(0, | led.setColorAt(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; | ||
| + | } | ||
| </ | </ | ||
| ==== Bedienung ==== | ==== Bedienung ==== | ||
| Zeile 297: | 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 | 
| * Der Vorgang kann **von vorne** gestartet werden. | * Der Vorgang kann **von vorne** gestartet werden. | ||
hello_mbot_kalibrierung.1739967813.txt.gz · Zuletzt geändert:  von torsten.roehl
                
                