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 15: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 111: | Zeile 111: | ||
int buttonCount; | int buttonCount; | ||
// Allgemein | // Allgemein | ||
- | int speed = 250; // 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 |
+ | bool ac_calibrationDone = false; | ||
// | // | ||
- | static | + | bool at_doneLeft = false; |
- | static | + | bool at_doneRight |
+ | bool at_testComplete | ||
enum State { | enum State { | ||
Zeile 166: | 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 225: | 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; | + | |
- | do { | + | |
- | | + | |
- | } while ( sensorState != S1_OUT_S2_OUT); | + | |
- | } | + | |
- | | + | |
- | if (sensorState == S1_IN_S2_IN) { | + | if (!ac_isTurning) { |
- | motor1.stop(); | + | motor1.stop(); |
- | motor2.stop(); | + | motor2.stop(); |
- | | + | motor1.run(speed); |
- | ac_isBackTurning = true; | + | motor2.run(speed); |
- | | + | |
- | motor2.run(-speed); | + | |
- | | + | ac_isRightTurning = true; |
- | do { | + | |
- | sensorState = lineFinder.readSensors(); | + | sensorState = lineFinder.readSensors(); |
- | } while ( sensorState != S1_OUT_S2_OUT); | + | } while ( sensorState != S1_OUT_S2_OUT); |
- | } | + | } |
- | | + | |
- | if (ac_isBackTurning | + | if (ac_isRightTurning |
- | motor1.stop(); | + | motor1.stop(); |
- | motor2.stop(); | + | motor2.stop(); |
- | | + | |
- | | + | |
- | | + | motor1.run(-speed); |
- | led.setColorAt(1, 0, 0, 0); | + | |
- | | + | |
- | | + | do { |
- | | + | |
- | | + | } while ( sensorState != S1_OUT_S2_OUT); |
+ | | ||
+ | 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, | ||
+ | led.setColorAt(0, | ||
+ | led.show(); | ||
+ | buzzer.tone(1200, | ||
+ | } | ||
+ | |||
} | } | ||
void actionTest() { | void actionTest() { | ||
- | | ||
- | led.setColorAt(1, | ||
- | led.setColorAt(0, | ||
- | led.show(); | ||
- | if (!at_doneLeft) { | + | if (!at_doneLeft) { // Links-Drehung |
at_doneLeft = turn90Degree(true); | at_doneLeft = turn90Degree(true); | ||
+ | led.setColorAt(1, | ||
+ | led.setColorAt(0, | ||
+ | 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 290: | Zeile 303: | ||
led.setColorAt(0, | led.setColorAt(0, | ||
led.show(); | led.show(); | ||
- | buzzer.tone(1200, | + | buzzer.tone(1200, |
+ | at_testComplete = true; | ||
+ | return; | ||
} | } | ||
} | } | ||
Zeile 298: | Zeile 313: | ||
led.setColorAt(0, | led.setColorAt(0, | ||
led.show(); | led.show(); | ||
+ | |||
// reset | // reset | ||
motor1.stop(); | motor1.stop(); | ||
motor2.stop(); | motor2.stop(); | ||
+ | |||
// reset state calibration | // reset state calibration | ||
ac_isTurning = false; | ac_isTurning = false; | ||
- | | + | |
+ | ac_isRightTurning | ||
ac_calibrationDone = false; | ac_calibrationDone = false; | ||
+ | |||
// reset state test | // reset state test | ||
at_doneLeft = false; | at_doneLeft = false; | ||
at_doneRight = false; | at_doneRight = false; | ||
- | | + | |
- | buzzer.tone(1200, | + | |
} | } | ||
</ | </ | ||
Zeile 327: | 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.1739977504.txt.gz · Zuletzt geändert: 2025/02/19 15:05 von torsten.roehl