mbot_streckenfahren
                Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| mbot_streckenfahren [2025/03/02 07:05] – [Quellcode (engl. Sourcecode)] torsten.roehl | mbot_streckenfahren [2025/03/02 14:38] (aktuell) – [Erklärungen zum Quellcode] torsten.roehl | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ====== mBot Streckenfahren ====== | ====== mBot Streckenfahren ====== | ||
| - | |||
| - | FIXME  <color # | ||
| - | </ | ||
| - | |||
| //Der mBot kann, gesteuert über die Infrarot-Fernbedienung, | //Der mBot kann, gesteuert über die Infrarot-Fernbedienung, | ||
| Zeile 392: | Zeile 388: | ||
| <WRAP center round tip 95%> | <WRAP center round tip 95%> | ||
| </ | </ | ||
| + | |||
| ==== Quellcode (engl. Sourcecode) ==== | ==== Quellcode (engl. Sourcecode) ==== | ||
| Zeile 407: | Zeile 404: | ||
| // IR-Entprellen | // IR-Entprellen | ||
| - | static uint8_t lastCode = 0xFF; | + | static uint8_t lastCode | 
| static unsigned long lastTime = 0; | static unsigned long lastTime = 0; | ||
| const unsigned long COOL_DOWN = 1000; // in Millisekunden, | const unsigned long COOL_DOWN = 1000; // in Millisekunden, | ||
| Zeile 416: | Zeile 413: | ||
| // ADJUST AREA START | // ADJUST AREA START | ||
| - | int calibrationDistance = 40; // cm | + | int calibrationDistance = 40; // in cm | 
| - | int speed = 180; // Motor speed | + | int speed | 
| + | int dir = 1; // change to -1 if forward drive backwards! | ||
| // ADJUST AREA END | // ADJUST AREA END | ||
| // actionCalibration... | // actionCalibration... | ||
| unsigned long ac_movingStartTimeForward | unsigned long ac_movingStartTimeForward | ||
| - | unsigned long ac_movingTimeForward = 0; | + | unsigned long ac_movingTimeForward | 
| unsigned long ac_movingStartTimeBackward = 0; | unsigned long ac_movingStartTimeBackward = 0; | ||
| - | unsigned long ac_movingTimeBackward = 0; | + | unsigned long ac_movingTimeBackward | 
| - | bool ac_isMoving = false; | + | bool ac_isMoving | 
| - | bool ac_isForwardMoving = false; | + | bool ac_isForwardMoving | 
| bool ac_isBackwardMoving = false; | bool ac_isBackwardMoving = false; | ||
| - | bool ac_calibrationDone = false; | + | bool ac_calibrationDone | 
| - | // actionForward | + | // motor movement | 
| - | bool af_firstRunMotor | + | bool mm_firstRunMotor | 
| - | bool af_motorActive | + | bool mm_motorActive | 
| - | unsigned long af_motorStartTime = 0; | + | unsigned long mm_motorStartTime | 
| - | // actionBackward | + | |
| - | bool ab_firstRunMotor = true; // erstes Starten? | + | |
| - | bool ab_motorActive | + | |
| - | unsigned long ab_motorStartTime | + | |
| //FSM | //FSM | ||
| Zeile 456: | Zeile 450: | ||
| }; | }; | ||
| ExState exState | ExState exState | ||
| + | |||
| int | int | ||
| String | String | ||
| Zeile 462: | Zeile 457: | ||
| void setup() { | void setup() { | ||
| led.setpin(13); | led.setpin(13); | ||
| - | Serial.begin(9600); | + |  | 
| ir.begin(); | ir.begin(); | ||
| } | } | ||
| Zeile 496: | Zeile 491: | ||
| --------------------------- | --------------------------- | ||
| */ | */ | ||
| + | void setLED(int r, int g, int b) { | ||
| + | led.setColorAt(0, | ||
| + | led.setColorAt(1, | ||
| + | led.show(); | ||
| + | } | ||
| int read() { | int read() { | ||
| if (ir.decode()) { | if (ir.decode()) { | ||
| uint32_t code = getIRCode(); | uint32_t code = getIRCode(); | ||
| + | |||
| // Vermeidung wiederholter Eingaben | // Vermeidung wiederholter Eingaben | ||
| unsigned long now = millis(); | unsigned long now = millis(); | ||
| Zeile 506: | Zeile 507: | ||
| lastCode = code; | lastCode = code; | ||
| lastTime = now; | lastTime = now; | ||
| + | |||
| // | // | ||
| switch (code) { | switch (code) { | ||
| Zeile 575: | Zeile 577: | ||
| return STATE_FORWARD; | return STATE_FORWARD; | ||
| } | } | ||
| - | |||
| return lastState; | return lastState; | ||
| } | } | ||
| Zeile 589: | Zeile 590: | ||
| inputNumber = 0; | inputNumber = 0; | ||
| inputBuffer = ""; | inputBuffer = ""; | ||
| + | setLED(0, 0, 255); // LED BLUE | ||
| } | } | ||
| Zeile 613: | Zeile 615: | ||
| exState = EX_IDLE; | exState = EX_IDLE; | ||
| buzzer.tone(1200, | buzzer.tone(1200, | ||
| + | setLED(0, 0, 0); // LED OFF | ||
| } | } | ||
| } | } | ||
| Zeile 621: | Zeile 624: | ||
| --------------------------- | --------------------------- | ||
| */ | */ | ||
| - | |||
| void actionCalibration() { | void actionCalibration() { | ||
| Zeile 627: | Zeile 629: | ||
| return; | return; | ||
| - |  | + |  | 
| - | led.setColorAt(0, | + | |
| - | led.show(); | + | |
| int sensorState = lineFinder.readSensors(); | int sensorState = lineFinder.readSensors(); | ||
| Zeile 636: | Zeile 636: | ||
| motor1.stop(); | motor1.stop(); | ||
| motor2.stop(); | motor2.stop(); | ||
| - | motor1.run(-speed); | + | motor1.run(-dir * speed); | 
| - | motor2.run(speed); | + | motor2.run( | 
| ac_movingStartTimeForward = millis(); | ac_movingStartTimeForward = millis(); | ||
| ac_isMoving = true; | ac_isMoving = true; | ||
| Zeile 651: | Zeile 652: | ||
| motor2.stop(); | motor2.stop(); | ||
| ac_movingTimeForward = millis() - ac_movingStartTimeForward; | ac_movingTimeForward = millis() - ac_movingStartTimeForward; | ||
| - | + |  | |
| - |  | + | motor2.run(-dir * speed); | 
| - | motor2.run(-speed); | + | |
| ac_movingStartTimeBackward = millis(); | ac_movingStartTimeBackward = millis(); | ||
| do { | do { | ||
| Zeile 669: | Zeile 669: | ||
| calibrationTravelTime = (ac_movingTimeForward + ac_movingTimeBackward) / 2.0; | calibrationTravelTime = (ac_movingTimeForward + ac_movingTimeBackward) / 2.0; | ||
| ac_calibrationDone = true; | ac_calibrationDone = true; | ||
| - | led.setColorAt(1, | ||
| - | led.setColorAt(0, | ||
| - | led.show(); | ||
| buzzer.tone(1200, | buzzer.tone(1200, | ||
| lastState = STATE_OFF; | lastState = STATE_OFF; | ||
| - | |||
| } | } | ||
| } | } | ||
| - | void actionForward() { | + | void motorMovement(int go_forward | 
| unsigned long currentMillis = millis(); | unsigned long currentMillis = millis(); | ||
| // Wenn der Motor aktuell läuft, prüfen wir, ob die Zeit abgelaufen ist | // Wenn der Motor aktuell läuft, prüfen wir, ob die Zeit abgelaufen ist | ||
| - | if (af_motorActive) { | + | if (mm_motorActive) { | 
| - | if (currentMillis - af_motorStartTime | + | if (currentMillis - mm_motorStartTime | 
| - | // Zeit ist um → Motor stoppen | + | |
| motor1.stop(); | motor1.stop(); | ||
| motor2.stop(); | motor2.stop(); | ||
| - |  | + |  | 
| - | state = STATE_OFF; | + | state          = STATE_OFF; | 
| - | lastState = STATE_OFF; | + | lastState | 
| } | } | ||
| - | // Funktion verlassen | ||
| return; | return; | ||
| } | } | ||
| - | if (!af_firstRunMotor) | + | if (!mm_firstRunMotor) | 
| return; | return; | ||
| - | motor1.run(-speed); | + |  | 
| - | motor2.run(speed); | + |  | 
| - |  | + | motor2.run( | 
| - |  | + | |
| - |  | + |  | 
| + |  | ||
| + |  | ||
| } | } | ||
| + | void actionForward() { | ||
| + | motorMovement(dir); | ||
| + | } | ||
| void actionBackward() { | void actionBackward() { | ||
| - |  | + |  | 
| - | + | ||
| - | // Wenn der Motor aktuell läuft, prüfen wir, ob die Zeit abgelaufen ist | + | |
| - | if (ab_motorActive) { | + | |
| - | if (currentMillis | + | |
| - | // Zeit ist um → Motor stoppen | + | |
| - | motor1.stop(); | + | |
| - | motor2.stop(); | + | |
| - | ab_motorActive = false; | + | |
| - | state = STATE_OFF; | + | |
| - | lastState = STATE_OFF; | + | |
| - | } | + | |
| - | // Funktion verlassen | + | |
| - | return; | + | |
| - | } | + | |
| - | + | ||
| - | if (!ab_firstRunMotor) | + | |
| - | return; | + | |
| - | + | ||
| - | motor1.run(speed); | + | |
| - | motor2.run(-speed); | + | |
| - | ab_motorStartTime = currentMillis; | + | |
| - | ab_motorActive | + | |
| - | ab_firstRunMotor | + | |
| } | } | ||
| Zeile 736: | Zeile 711: | ||
| // | // | ||
| // noch in der Eingabe? | // noch in der Eingabe? | ||
| - | if (exState == EX_WAIT_FOR_INPUT) | + | if (exState == EX_WAIT_FOR_INPUT) | 
| return; | return; | ||
| - | } | + | |
| // step: ...calculate distance | // step: ...calculate distance | ||
| - | inputDistance = inputBuffer.toInt(); | + | inputDistance = max(0, min(200, | 
| - | if (inputDistance > 200) { | + | |
| - | inputDistance = 200; | + | |
| - | } | + | |
| // step: calculate the travel time (for non blocking-method) | // step: calculate the travel time (for non blocking-method) | ||
| double v = (1.0 * calibrationDistance) / (1.0 * calibrationTravelTime); | double v = (1.0 * calibrationDistance) / (1.0 * calibrationTravelTime); | ||
| Zeile 752: | Zeile 725: | ||
| void actionOff() { | void actionOff() { | ||
| - |  | + |  | 
| - | led.setColorAt(0, | + | |
| - | led.show(); | + | |
| // reset | // reset | ||
| motor1.stop(); | motor1.stop(); | ||
| Zeile 765: | Zeile 736: | ||
| ac_calibrationDone | ac_calibrationDone | ||
| - | // reset state forward | + | // reset movement | 
| - |  | + |  | 
| - |  | + |  | 
| - |  | + |  | 
| - | // reset state backward | + | |
| - | ab_firstRunMotor = true; | + | |
| - | ab_motorActive | + | |
| - | ab_motorStartTime = 0; | + | |
| // reset state distance | // reset state distance | ||
| exState | exState | ||
| } | } | ||
| </ | </ | ||
| - | ==== Quellcode (engl. Sourcecode) ==== | + | ==== Erklärungen zum Quellcode | 
| + | === ⚙️ Setup (setup()) === | ||
| + | Initialisiert die IR-Fernbedienung und setzt den LED-Pin. | ||
| + | * '' | ||
| + | * '' | ||
| - | <Code c linenums:1 | Listing 1: | + | === 🔄 Loop (loop()) === | 
| - | #include " | + | - **Liest IR-Signale ('' | 
| + | - **Bestimmt den aktuellen Zustand ('' | ||
| + | - **Führt die passende Aktion aus (`actionXXX()`)** | ||
| - | // Hardware | + | === 🚦 Zustandsbezogene Aktionen | 
| - | MeIR ir; | + | * **'' | 
| - | MeBuzzer buzzer; | + | * **'' | 
| - | MeLineFollower lineFinder(PORT_2); | + | * **'' | 
| - | MeRGBLed led(0, 2); // must be fixed! | + | * **'' | 
| - | MeDCMotor motor1(M1); | + | |
| - | MeDCMotor motor2(M2); | + | |
| - | // IR-Entprellen | + | === ⌨️ Eingabeverarbeitung (handleInput(), | 
| - | static uint8_t lastCode = 0xFF; | + | - Ermöglicht die Eingabe einer numerischen Distanz per IR-Fernbedienung. | 
| - | static unsigned long lastTime = 0; | + | - '' | 
| - | const unsigned long COOL_DOWN = 1000; // in Millisekunden, | + | - '' | 
| - | // Allgemein | + | === ⚙️ Die ADJUST AREA === | 
| - | unsigned long calibrationTravelTime; | + | Hier können **drei zentrale Parameter** angepasst werden: | 
| - | unsigned long currentTravelTime | + | * **'' | 
| - | + | * **'' | |
| - | // ADJUST AREA START | + |  | 
| - | int calibrationDistance | + | * Falls das Fahrzeug rückwärts fährt, auf '' | 
| - | int speed = 180; // Motor speed | + | |
| - | // ADJUST AREA END | + | |
| - | + | ||
| - | // actionCalibration... | + | |
| - | unsigned long ac_movingStartTimeForward | + | |
| - | unsigned long ac_movingTimeForward | + | |
| - | unsigned long ac_movingStartTimeBackward | + | |
| - | unsigned long ac_movingTimeBackward = 0; | + | |
| - | bool ac_isMoving = false; | + | |
| - | bool ac_isForwardMoving = false; | + | |
| - | bool ac_isBackwardMoving = false; | + | |
| - | bool ac_calibrationDone = false; | + | |
| - | + | ||
| - | // actionForward | + | |
| - | bool af_firstRunMotor = true; // erstes Starten? | + | |
| - | bool af_motorActive | + | |
| - | unsigned long af_motorStartTime = 0; | + | |
| - | // actionBackward | + | |
| - | bool ab_firstRunMotor = true; // erstes Starten? | + | |
| - | bool ab_motorActive | + | |
| - | unsigned long ab_motorStartTime = 0; | + | |
| - | + | ||
| - | //FSM | + | |
| - | enum State { | + | |
| - | STATE_OFF, | + | |
| - | STATE_BACKWARD, | + | |
| - | STATE_CALIBRATION, | + | |
| - | STATE_DISTANCE, | + | |
| - | STATE_FORWARD | + | |
| - | }; | + | |
| - | + | ||
| - | State state     = STATE_OFF; | + | |
| - | State lastState = STATE_OFF; | + | |
| - | + | ||
| - | enum ExState { | + | |
| - | EX_IDLE, | + | |
| - | EX_WAIT_FOR_INPUT | + | |
| - | }; | + | |
| - | ExState exState | + | |
| - | int | + | |
| - | String | + | |
| - | int | + | |
| - | + | ||
| - | void setup() { | + | |
| - | led.setpin(13); | + | |
| - | Serial.begin(9600); | + | |
| - | ir.begin(); | + | |
| - | } | + | |
| - | + | ||
| - | void loop() { | + | |
| - | // step: command (IR-Taste einlesen) | + | |
| - | int cmd = read(); | + | |
| - |  | + | |
| - | state  = decode(cmd); | + | |
| - | // step: zustandsabhängige Aktion | + | |
| - | switch (state) { | + | |
| - | case STATE_CALIBRATION: | + | |
| - | actionCalibration(); | + | |
| - | break; | + | |
| - | case STATE_FORWARD: | + | |
| - | actionForward(); | + | |
| - | break; | + | |
| - | case STATE_BACKWARD: | + | |
| - | actionBackward(); | + | |
| - | break; | + | |
| - | case STATE_DISTANCE: | + | |
| - | actionDistance(); | + | |
| - | break; | + | |
| - | case STATE_OFF: | + | |
| - | actionOff(); | + | |
| - | break; | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | /* | + | |
| - | --------------------------- | + | |
| - | Funktionen | + | |
| - | --------------------------- | + | |
| - | */ | + | |
| - | + | ||
| - | int read() { | + | |
| - | if (ir.decode()) { | + | |
| - | uint32_t code = getIRCode(); | + | |
| - | // Vermeidung wiederholter Eingaben | + | |
| - | unsigned long now = millis(); | + | |
| - | if (code == lastCode && (now - lastTime < COOL_DOWN)) | + | |
| - | return -1; | + | |
| - | lastCode = code; | + | |
| - | lastTime = now; | + | |
| - | // | + | |
| - | switch (code) { | + | |
| - | case IR_BUTTON_E: | + | |
| - | buzzer.tone(1200, | + | |
| - | return 0; // STATE_OFF | + | |
| - | break; | + | |
| - | case IR_BUTTON_B: | + | |
| - | buzzer.tone(1200, | + | |
| - | return 1; // STATE_BACKWARD | + | |
| - | break; | + | |
| - | case IR_BUTTON_C: | + | |
| - | buzzer.tone(1200, | + | |
| - | return 2; // STATE_CALIBRATION | + | |
| - | break; | + | |
| - | case IR_BUTTON_D: | + | |
| - | initInput(); | + | |
| - | return 3; // STATE_DISTANCE | + | |
| - | break; | + | |
| - | case IR_BUTTON_F: | + | |
| - | buzzer.tone(1200, | + | |
| - | return 4; // STATE_FORWARD | + | |
| - | break; | + | |
| - | + | ||
| - | // Ziffern 0–9 | + | |
| - | case IR_BUTTON_0: | + | |
| - | case IR_BUTTON_1: | + | |
| - | case IR_BUTTON_2: | + | |
| - | case IR_BUTTON_3: | + | |
| - | case IR_BUTTON_4: | + | |
| - | case IR_BUTTON_5: | + | |
| - | case IR_BUTTON_6: | + | |
| - | case IR_BUTTON_7: | + | |
| - | case IR_BUTTON_8: | + | |
| - | case IR_BUTTON_9: | + | |
| - | + | ||
| - | // Taste 'Setting' | + | |
| - | case IR_BUTTON_SETTING: | + | |
| - | finalizeInput(); | + | |
| - | return -1; // Kein direkter Zustandswechsel | + | |
| - | break; | + | |
| - | } | + | |
| - | } | + | |
| - | return -1; // keine bekannte/ | + | |
| - | } | + | |
| - | + | ||
| - | uint32_t getIRCode() { | + | |
| - | uint32_t value = ir.value; | + | |
| - | value = (value >> 16) & 0xff; | + | |
| - |  | + | |
| - | } | + | |
| - | + | ||
| - | State decode(int cmd) { | + | |
| - | switch (cmd) { | + | |
| - | case 0: | + | |
| - | lastState = STATE_OFF; | + | |
| - | return STATE_OFF; | + | |
| - | case 1: | + | |
| - | lastState = STATE_BACKWARD; | + | |
| - | return STATE_BACKWARD; | + | |
| - | case 2: | + | |
| - | lastState = STATE_CALIBRATION; | + | |
| - | return STATE_CALIBRATION; | + | |
| - | case 3: | + | |
| - | lastState = STATE_DISTANCE; | + | |
| - | return STATE_DISTANCE; | + | |
| - | case 4: | + | |
| - | lastState = STATE_FORWARD; | + | |
| - | return STATE_FORWARD; | + | |
| - | } | + | |
| - | + | ||
| - | return lastState; | + | |
| - | } | + | |
| - | + | ||
| - | /* | + | |
| - | --------------------------- | + | |
| - | Eingabemodus für EX-Befehle | + | |
| - | --------------------------- | + | |
| - | */ | + | |
| - | void initInput() { | + | |
| - | buzzer.tone(1000, | + | |
| - | exState | + | |
| - | inputNumber = 0; | + | |
| - | inputBuffer = ""; | + | |
| - | } | + | |
| - | + | ||
| - | void handleInput(int number) { | + | |
| - | + | ||
| - | if (exState != EX_WAIT_FOR_INPUT) return; | + | |
| - | + | ||
| - | // Maximal 3 Stellen erlauben | + | |
| - | if (inputNumber >= 3) { | + | |
| - | buzzer.tone(300, | + | |
| - | delay(200); | + | |
| - | buzzer.tone(300, | + | |
| - | return; | + | |
| - | } | + | |
| - | + | ||
| - | // Zahl anhängen | + | |
| - | inputBuffer += String(number); | + | |
| - | inputNumber++; | + | |
| - | buzzer.tone(1200, | + | |
| - | } | + | |
| - | + | ||
| - | void finalizeInput() { | + | |
| - | if (exState == EX_WAIT_FOR_INPUT) { | + | |
| - | exState = EX_IDLE; | + | |
| - | buzzer.tone(1200, | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | /* | + | |
| - | --------------------------- | + | |
| - | Funktionen ...actionXXX | + | |
| - | --------------------------- | + | |
| - | */ | + | |
| - | + | ||
| - | void actionCalibration() { | + | |
| - | + | ||
| - | if (ac_calibrationDone) | + | |
| - | return; | + | |
| - | + | ||
| - | led.setColorAt(1, | + | |
| - | led.setColorAt(0, | + | |
| - | led.show(); | + | |
| - | int sensorState = lineFinder.readSensors(); | + | |
| - | + | ||
| - | // step - start moving forward | + | |
| - | if (!ac_isMoving) { | + | |
| - | motor1.stop(); | + | |
| - | motor2.stop(); | + | |
| - | motor1.run(-speed); | + | |
| - | motor2.run(speed); | + | |
| - | ac_movingStartTimeForward = millis(); | + | |
| - | ac_isMoving = true; | + | |
| - | ac_isBackwardMoving = true; | + | |
| - | do { | + | |
| - | sensorState = lineFinder.readSensors(); | + | |
| - | } while ( sensorState != S1_OUT_S2_OUT); | + | |
| - | } | + | |
| - | + | ||
| - | // step - end moving forward ... start moving backward | + | |
| - | if (ac_isBackwardMoving && sensorState == S1_IN_S2_IN) { | + | |
| - | motor1.stop(); | + | |
| - | motor2.stop(); | + | |
| - | ac_movingTimeForward = millis() - ac_movingStartTimeForward; | + | |
| - | + | ||
| - | motor1.run(speed); | + | |
| - | motor2.run(-speed); | + | |
| - | ac_movingStartTimeBackward = millis(); | + | |
| - | do { | + | |
| - | sensorState = lineFinder.readSensors(); | + | |
| - | } while ( sensorState != S1_OUT_S2_OUT); | + | |
| - | ac_isBackwardMoving = false; | + | |
| - | ac_isForwardMoving | + | |
| - | } | + | |
| - | + | ||
| - | // step - end turing right | + | |
| - | if (ac_isForwardMoving && sensorState == S1_IN_S2_IN) { | + | |
| - | motor1.stop(); | + | |
| - | motor2.stop(); | + | |
| - | ac_movingTimeBackward = millis() - ac_movingStartTimeBackward; | + | |
| - | calibrationTravelTime = (ac_movingTimeForward + ac_movingTimeBackward) / 2.0; | + | |
| - | ac_calibrationDone = true; | + | |
| - | led.setColorAt(1, | + | |
| - | led.setColorAt(0, | + | |
| - | led.show(); | + | |
| - | buzzer.tone(1200, | + | |
| - | lastState = STATE_OFF; | + | |
| - | + | ||
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | void actionForward() { | + | |
| - | unsigned long currentMillis = millis(); | + | |
| - | + | ||
| - | // Wenn der Motor aktuell läuft, prüfen wir, ob die Zeit abgelaufen ist | + | |
| - | if (af_motorActive) { | + | |
| - | if (currentMillis - af_motorStartTime >= currentTravelTime) { | + | |
| - | // Zeit ist um → Motor stoppen | + | |
| - | motor1.stop(); | + | |
| - | motor2.stop(); | + | |
| - | af_motorActive = false; | + | |
| - | state = STATE_OFF; | + | |
| - | lastState = STATE_OFF; | + | |
| - | } | + | |
| - | // Funktion verlassen | + | |
| - | return; | + | |
| - | } | + | |
| - | + | ||
| - | if (!af_firstRunMotor) | + | |
| - | return; | + | |
| - | + | ||
| - | motor1.run(-speed); | + | |
| - | motor2.run(speed); | + | |
| - | af_motorStartTime = currentMillis; | + | |
| - | af_motorActive | + | |
| - | af_firstRunMotor | + | |
| - | } | + | |
| - | + | ||
| - | void actionBackward() { | + | |
| - | unsigned long currentMillis = millis(); | + | |
| - | + | ||
| - | // Wenn der Motor aktuell läuft, prüfen wir, ob die Zeit abgelaufen ist | + | |
| - | if (ab_motorActive) { | + | |
| - | if (currentMillis - ab_motorStartTime >= currentTravelTime) { | + | |
| - | // Zeit ist um → Motor stoppen | + | |
| - | motor1.stop(); | + | |
| - | motor2.stop(); | + | |
| - | ab_motorActive = false; | + | |
| - | state = STATE_OFF; | + | |
| - | lastState = STATE_OFF; | + | |
| - | } | + | |
| - | // Funktion verlassen | + | |
| - | return; | + | |
| - | } | + | |
| - | + | ||
| - |  | + | |
| - | return; | + | |
| - | + | ||
| - | motor1.run(speed); | + | |
| - | motor2.run(-speed); | + | |
| - | ab_motorStartTime = currentMillis; | + | |
| - | ab_motorActive | + | |
| - | ab_firstRunMotor | + | |
| - | + | ||
| - | } | + | |
| - | + | ||
| - | void actionDistance() { | + | |
| - | // | + | |
| - | // noch in der Eingabe? | + | |
| - | if (exState == EX_WAIT_FOR_INPUT) { | + | |
| - | return; | + | |
| - | } | + | |
| - | // step: ...calculate distance | + | |
| - | inputDistance = inputBuffer.toInt(); | + | |
| - | if (inputDistance > 200) { | + | |
| - | inputDistance = 200; | + | |
| - | } | + | |
| - | // step: calculate the travel time (for non blocking-method) | + | |
| - | double v = (1.0 * calibrationDistance) / (1.0 * calibrationTravelTime); | + | |
| - | currentTravelTime = inputDistance / v; | + | |
| - | // Serial.print(" | + | |
| - | // Serial.println(inputDistance); | + | |
| - | } | + | |
| - | + | ||
| - | void actionOff() { | + | |
| - | led.setColorAt(1, 0, 0, 0); | + | |
| - | led.setColorAt(0, | + | |
| - | led.show(); | + | |
| - | // reset | + | |
| - | motor1.stop(); | + | |
| - | motor2.stop(); | + | |
| - | + | ||
| - | // reset state calibration | + | |
| - | ac_isMoving | + | |
| - | ac_isForwardMoving | + | |
| - | ac_isBackwardMoving = false; | + | |
| - | ac_calibrationDone | + | |
| - | + | ||
| - | // reset state forward | + | |
| - | af_firstRunMotor = true; | + | |
| - | af_motorActive | + | |
| - | af_motorStartTime = 0; | + | |
| - | // reset state backward | + | |
| - | ab_firstRunMotor = true; | + | |
| - | ab_motorActive | + | |
| - | ab_motorStartTime = 0; | + | |
| - | // reset state distance | + | |
| - | exState | + | |
| - | } | + | |
| - | </ | + | |
| + | Die Werte beeinflussen das Fahrverhalten direkt und sollten je nach Umgebung angepasst werden. | ||
mbot_streckenfahren.1740899159.txt.gz · Zuletzt geändert:  von torsten.roehl
                
                