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 16:05] – [Quellcode (engl. Sourcecode)] 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 " | ||
| Zeile 112: | Zeile 112: | ||
| // Allgemein | // Allgemein | ||
| int speed = 100; // Motor speed | int speed = 100; // Motor speed | ||
| - | unsigned long time90degree | + | unsigned long time90degree; | 
| - | // turn90Degree | + | |
| + | // turn90Degree... | ||
| bool t90d_isTurning = false; | bool t90d_isTurning = false; | ||
| unsigned long t90d_turnStartTime = 0; | unsigned long t90d_turnStartTime = 0; | ||
| - | // | + | // actionCalibration... | 
| unsigned long ac_turnStartTimeLeft | unsigned long ac_turnStartTimeLeft | ||
| unsigned long ac_turnTimeLeft = 0; | unsigned long ac_turnTimeLeft = 0; | ||
| unsigned long ac_turnStartTimeRight = 0; | unsigned long ac_turnStartTimeRight = 0; | ||
| unsigned long ac_turnTimeRight = 0; | unsigned long ac_turnTimeRight = 0; | ||
| - | static | + | bool ac_isTurning = false; | 
| - | static | + | bool ac_isLeftTurning = false; | 
| - | static | + | bool ac_isRightTurning = false; | 
| - | static | + | bool ac_calibrationDone = false; | 
| // | // | ||
| - | static | + | bool at_doneLeft = false; | 
| - | static | + | bool at_doneRight = false; | 
| - | static | + | bool at_testComplete = false; | 
| enum State { | enum State { | ||
| Zeile 168: | Zeile 169: | ||
| bool turn90Degree(bool left) { | bool turn90Degree(bool left) { | ||
| + | // step: ...start turning | ||
| if (!t90d_isTurning) { | if (!t90d_isTurning) { | ||
| motor1.stop(); | motor1.stop(); | ||
| motor2.stop(); | motor2.stop(); | ||
| t90d_isTurning | t90d_isTurning | ||
| - | int dir | + | int dir = left ? 1 : -1; | 
| - | if (!left) | + | |
| - | dir = -1; | + | |
| motor1.run(dir * speed); | motor1.run(dir * speed); | ||
| motor2.run(dir * speed); | motor2.run(dir * speed); | ||
| t90d_turnStartTime = millis(); | t90d_turnStartTime = millis(); | ||
| + | return false; | ||
| } | } | ||
| + | // step: ...in progress | ||
| + | if (millis() - t90d_turnStartTime < time90degree) | ||
| + | return false; | ||
| - |  | + |  | 
| - | motor1.stop(); | + | motor1.stop(); | 
| - | motor2.stop(); | + | motor2.stop(); | 
| - | t90d_isTurning = false; | + | t90d_isTurning = false; | 
| - | return true; | + | |
| - | } | + | |
| - | return | + | return | 
| } | } | ||
| Zeile 227: | Zeile 230: | ||
| void actionCalibration() { | void actionCalibration() { | ||
| - | if (!ac_calibrationDone) | + | if (ac_calibrationDone) | 
| - |  | + |  | 
| - | led.setColorAt(0, | + | |
| - | led.show(); | + | |
| - | int sensorState = lineFinder.readSensors(); | + | |
| - | // step - start turning left | + | led.setColorAt(1, 255, 0, 0); | 
| - | if (!ac_isTurning) { | + | led.setColorAt(0, 255, 0, 0); | 
| - | motor1.stop(); | + | led.show(); | 
| - |  | + |  | 
| - |  | + | |
| - |  | + | |
| - | ac_turnStartTimeLeft = millis(); | + | |
| - | ac_isTurning = true; | + | |
| - | ac_isRightTurning = true; | + | |
| - | do { | + | |
| - |  | + | |
| - | } while ( sensorState != S1_OUT_S2_OUT); | + | |
| - | } | + | |
| - |  | + |  | 
| - | if (ac_isRightTurning && sensorState == S1_IN_S2_IN) { | + | if (!ac_isTurning) { | 
| - | motor1.stop(); | + | motor1.stop(); | 
| - | motor2.stop(); | + | motor2.stop(); | 
| - |  | + |  | 
| + | motor2.run(speed); | ||
| + | ac_turnStartTimeLeft | ||
| + | ac_isTurning = true; | ||
| + | ac_isRightTurning = true; | ||
| + | do { | ||
| + | sensorState = lineFinder.readSensors(); | ||
| + | } while ( sensorState != S1_OUT_S2_OUT); | ||
| + | } | ||
| - | motor1.run(-speed); | + | // step - end turning left ... start turning right | 
| - |  | + |  | 
| - | ac_turnStartTimeRight | + |  | 
| - | do { | + |  | 
| - |  | + |  | 
| - | } while ( sensorState != S1_OUT_S2_OUT); | + | |
| - |  | + | |
| - | ac_isLeftTurning = true; | + | |
| - | } | + | |
| - |  | + | motor1.run(-speed); | 
| - | if (ac_isLeftTurning && sensorState == S1_IN_S2_IN) { | + | motor2.run(-speed); | 
| - |  | + |  | 
| - | motor2.stop(); | + | do { | 
| - |  | + |  | 
| - |  | + | } while ( sensorState != S1_OUT_S2_OUT); | 
| - |  | + |  | 
| - | led.setColorAt(1, 0, 0, 0); | + |  | 
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - | } | + | |
| } | } | ||
| + | |||
| + | // 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, | ||
| + | led.setColorAt(0, | ||
| + | led.show(); | ||
| + | buzzer.tone(1200, | ||
| + | } | ||
| + | |||
| } | } | ||
| void actionTest() { | void actionTest() { | ||
| - | if (!at_doneLeft) { | + | if (!at_doneLeft) { // Links-Drehung | 
| at_doneLeft = turn90Degree(true); | at_doneLeft = turn90Degree(true); | ||
| - | |||
| led.setColorAt(1, | led.setColorAt(1, | ||
| led.setColorAt(0, | led.setColorAt(0, | ||
| led.show(); | led.show(); | ||
| + | return; | ||
| } | } | ||
| - | else if (at_doneLeft && | + | |
| + |  | ||
| at_doneRight = turn90Degree(false); | at_doneRight = turn90Degree(false); | ||
| + | return; | ||
| } | } | ||
| - | else if (at_doneLeft && at_doneRight && | + | |
| + |  | ||
| motor1.stop(); | motor1.stop(); | ||
| motor2.stop(); | motor2.stop(); | ||
| Zeile 297: | Zeile 305: | ||
| buzzer.tone(1200, | buzzer.tone(1200, | ||
| at_testComplete = true; | at_testComplete = true; | ||
| + | return; | ||
| } | } | ||
| } | } | ||
| Zeile 319: | Zeile 328: | ||
| at_doneRight = false; | at_doneRight = false; | ||
| at_testComplete = false; | at_testComplete = false; | ||
| - | |||
| } | } | ||
| </ | </ | ||
| Zeile 337: | 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.1739981147.txt.gz · Zuletzt geändert:  von torsten.roehl
                
                