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 07:51] – 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; | 
| - | MeRGBLed led(0, 2); // must be fixed! | + | MeLineFollower lineFinder(PORT_2); | 
| - | MeDCMotor motor1(M1); | + | MeRGBLed led(0, 2); // must be fixed! | 
| - | MeDCMotor motor2(M2); | + | MeDCMotor motor1(M1); | 
| - | int PIN_BUTTON = 7; // must be fixed! | + | MeDCMotor motor2(M2); | 
| - | int threshold | + | int PIN_BUTTON = 7; // must be fixed! | 
| - | int buttonCount; | + | int threshold | 
| - |  | + | int buttonCount; | 
| - |  | + | // Allgemein | 
| - | enum State { | + | int speed = 100; // Motor speed | 
| - | STATE_OFF, | + | unsigned long time90degree; | 
| - | STATE_CALIBRATION, | + | |
| - | STATE_TEST | + | // turn90Degree... | 
| - | + | bool t90d_isTurning = false; | |
| - |  | + | unsigned long t90d_turnStartTime = 0; | 
| - | State state = STATE_OFF; | + | // actionCalibration... | 
| - |  | + | unsigned long ac_turnStartTimeLeft | 
| - | void setup() { | + | unsigned long ac_turnTimeLeft = 0; | 
| - | led.setpin(13); | + | unsigned long ac_turnStartTimeRight = 0; | 
| - | pinMode(PIN_BUTTON, | + | unsigned long ac_turnTimeRight = 0; | 
| - | buttonCount = 0; | + | 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 { | ||
| + | STATE_OFF, | ||
| + | STATE_CALIBRATION, | ||
| + | STATE_TEST | ||
| + | }; | ||
| + | State state = STATE_OFF; | ||
| + | |||
| + | void setup() { | ||
| + | led.setpin(13); | ||
| + | pinMode(PIN_BUTTON, | ||
| + | buttonCount = 0; | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | // step: command | ||
| + | int cmd = read(); | ||
| + | // step: state | ||
| + | state  = decode(cmd); | ||
| + | // step: action | ||
| + | switch (state) { | ||
| + | case STATE_CALIBRATION: | ||
| + | actionCalibration(); | ||
| + | break; | ||
| + | case STATE_TEST: | ||
| + | actionTest(); | ||
| + | break; | ||
| + | case STATE_OFF: | ||
| + | actionOff(); | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | /* | ||
| + |  | ||
| + | */ | ||
| + | |||
| + | bool turn90Degree(bool left) { | ||
| + | // step: ...start turning | ||
| + | if (!t90d_isTurning) { | ||
| + | motor1.stop(); | ||
| + | motor2.stop(); | ||
| + | t90d_isTurning | ||
| + | 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; | ||
| + | |||
| + | // step: ... finished | ||
| + | motor1.stop(); | ||
| + | motor2.stop(); | ||
| + | t90d_isTurning = false; | ||
| + | |||
| + | return true; | ||
| + | } | ||
| + | |||
| + | bool isButtonPressed() { | ||
| + | static bool buttonPressed = false; | ||
| + | int value = analogRead(PIN_BUTTON); | ||
| + | |||
| + | if (value < threshold) { | ||
| + | if (!buttonPressed) { | ||
| + | buttonPressed = true; | ||
| + | return true; | ||
| } | } | ||
| - | + | } else { | |
| - | void loop() | + |  | 
| - | // step: command | + | } | 
| - | int cmd = read(); | + | return false; | 
| - |  | + | |
| - | // step: state | + | } | 
| - |  | + | |
| - |  | + | int read() | 
| - | // step: action | + | if ( isButtonPressed() ) | 
| - | switch (state) { | + | buttonCount += 1; | 
| - | case STATE_CALIBRAION: | + | |
| - | actionCalibration(); | + |  | 
| - | break; | + | buttonCount = 0; | 
| - | case STATE_TEST: | + | |
| - |  | + |  | 
| - | break; | + | } | 
| - | case STATE_OFF: | + | |
| - | actionOff(); | + | |
| - | break; | + | State decode(int cmd) { | 
| - | } | + |  | 
| - | } | + | case 1: return STATE_CALIBRATION; | 
| - | + | case 2: return STATE_TEST; | |
| - | + | } | |
| - |  | + | return STATE_OFF; | 
| - |  | + | } | 
| - | */ | + | |
| - | void turn90Degree(bool left){ | + | void actionCalibration() | 
| - |  | + | |
| - |  | + | if (ac_calibrationDone) | 
| - | + | return; | |
| - |  | + | |
| - | static bool buttonPressed = false; | + |  | 
| - | int value = analogRead(PIN_BUTTON); | + |  | 
| - | + |  | |
| - | if (value < threshold) { | + | int sensorState = lineFinder.readSensors(); | 
| - |  | + | |
| - |  | + | // step - start turning left | 
| - |  | + |  | 
| - | } | + |  | 
| - |  | + |  | 
| - |  | + |  | 
| - | } | + |  | 
| - |  | + |  | 
| - |  | + |  | 
| - |  | + |  | 
| - |  | + | do { | 
| - |  | + |  | 
| - | if ( isButtonPressed() ) | + | } while ( sensorState != S1_OUT_S2_OUT); | 
| - |  | + | } | 
| - |  | + | |
| - |  | + |  | 
| - |  | + | if (ac_isRightTurning && sensorState == S1_IN_S2_IN) { | 
| - |  | + |  | 
| - |  | + |  | 
| - | } | + |  | 
| - |  | + | |
| - |  | + |  | 
| - | State decode(int cmd) { | + |  | 
| - |  | + |  | 
| - | case 1: return STATE_CALIBRATION; | + | do { | 
| - | case 2: return STATE_TEST; | + |  | 
| - | } | + | } while ( sensorState != S1_OUT_S2_OUT); | 
| - | return STATE_OFF; | + |  | 
| - |  | + |  | 
| - | + | } | |
| - | void actionCalibration() { | + | |
| - | led.setColorAt(1, | + | // step - end turing right | 
| - | led.setColorAt(0, | + |  | 
| - | led.show(); | + |  | 
| - | + |  | |
| - |  | + |  | 
| - | } | + |  | 
| - |  | + |  | 
| - | void actionTest() { | + |  | 
| - | led.setColorAt(1, | + | led.setColorAt(0, | 
| - | led.setColorAt(0, | + | led.show(); | 
| - | led.show(); | + |  | 
| - |  | + |  | 
| - | turn90Degree(false); | + | |
| - | } | + | } | 
| - |  | + | void actionTest() { | 
| - | void actionOff() { | + | |
| - | led.setColorAt(1, | + | if (!at_doneLeft) { // Links-Drehung | 
| - | led.setColorAt(0, | + | at_doneLeft = turn90Degree(true); | 
| - | led.show(); | + |  | 
| - | } | + | led.setColorAt(0, | 
| - |  | + | led.show(); | 
| + |  | ||
| + | } | ||
| + | |||
| + | if (!at_doneRight) { // Rechts-Drehung | ||
| + |  | ||
| + |  | ||
| + |  | ||
| + | |||
| + | if (!at_testComplete) { // fertig | ||
| + |  | ||
| + | motor2.stop(); | ||
| + | led.setColorAt(1, | ||
| + | led.setColorAt(0, | ||
| + | led.show(); | ||
| + | buzzer.tone(1200, | ||
| + | at_testComplete = true; | ||
| + | return; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void actionOff() { | ||
| + | led.setColorAt(1, | ||
| + | led.setColorAt(0, | ||
| + | led.show(); | ||
| + | |||
| + | // reset | ||
| + | motor1.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 226: | 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.1739951516.txt.gz · Zuletzt geändert:  von torsten.roehl
                
                