From c1c5d60e6f79eafec07752065da1d4aa9f07b39b Mon Sep 17 00:00:00 2001 From: Maxim Kukushkin Date: Sat, 20 Jun 2020 01:41:20 +0100 Subject: [PATCH 1/4] Fixed ESP restarts by watch dog during long series of steps by adding yield() between the steps --- CheapStepper.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CheapStepper.cpp b/CheapStepper.cpp index 914cd91..a6aa233 100644 --- a/CheapStepper.cpp +++ b/CheapStepper.cpp @@ -162,6 +162,11 @@ void CheapStepper::step(bool clockwise){ void CheapStepper::off() { for (int p=0; p<4; p++) digitalWrite(pins[p], 0); + #if defined(ESP8266) || defined(ARDUINO_ESP8266_NODEMCU) + // return control to the system between the steps so that long series of those + // doesn't trigger watch dog restart on ESP + yield(); + #endif } From 255b7b9580b93d6014adbf192a04e44324fb4e6d Mon Sep 17 00:00:00 2001 From: Maxim Kukushkin Date: Sat, 20 Jun 2020 01:44:25 +0100 Subject: [PATCH 2/4] Fixed incorrect placement of yield() --- CheapStepper.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/CheapStepper.cpp b/CheapStepper.cpp index a6aa233..e3ac821 100644 --- a/CheapStepper.cpp +++ b/CheapStepper.cpp @@ -157,11 +157,7 @@ void CheapStepper::step(bool clockwise){ if (clockwise) seqCW(); else seqCCW(); -} -void CheapStepper::off() { - for (int p=0; p<4; p++) - digitalWrite(pins[p], 0); #if defined(ESP8266) || defined(ARDUINO_ESP8266_NODEMCU) // return control to the system between the steps so that long series of those // doesn't trigger watch dog restart on ESP @@ -169,6 +165,11 @@ void CheapStepper::off() { #endif } +void CheapStepper::off() { + for (int p=0; p<4; p++) + digitalWrite(pins[p], 0); +} + ///////////// // PRIVATE // From 6d1daf34cb43176ae7a5e164ff6c9f2d949c30bc Mon Sep 17 00:00:00 2001 From: Maxim Kukushkin Date: Fri, 25 Sep 2020 14:50:44 +0100 Subject: [PATCH 3/4] Reduced the amount of calls to yield() during motor rotation --- CheapStepper.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/CheapStepper.cpp b/CheapStepper.cpp index e3ac821..845f5d3 100644 --- a/CheapStepper.cpp +++ b/CheapStepper.cpp @@ -25,6 +25,11 @@ #include "Arduino.h" #include "CheapStepper.h" +#if defined(ESP8266) || defined(ARDUINO_ESP8266_NODEMCU) + #define YIELD_TIME 1000 // Source https://www.sigmdel.ca/michel/program/esp8266/arduino/watchdogs_en.html#ESP8266_WDT_TIMEOUT +#else + #define YIELD_TIME (0UL - 1UL) // Large number to only call it rarely +#endif CheapStepper::CheapStepper () : pins({8,9,10,11}) { for (int pin=0; pin<4; pin++){ @@ -158,11 +163,10 @@ void CheapStepper::step(bool clockwise){ if (clockwise) seqCW(); else seqCCW(); - #if defined(ESP8266) || defined(ARDUINO_ESP8266_NODEMCU) - // return control to the system between the steps so that long series of those - // doesn't trigger watch dog restart on ESP + // return control to the system between the steps so that long series of those + // doesn't trigger watch dog restart on ESP + if (millis() - lastYieldTime >= YIELD_TIME) yield(); - #endif } void CheapStepper::off() { From b22b6366ccfa13e1711d4a04341b94b864117685 Mon Sep 17 00:00:00 2001 From: Maxim Kukushkin Date: Fri, 25 Sep 2020 14:57:32 +0100 Subject: [PATCH 4/4] Fixed compilation errors --- CheapStepper.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CheapStepper.cpp b/CheapStepper.cpp index 845f5d3..de63d15 100644 --- a/CheapStepper.cpp +++ b/CheapStepper.cpp @@ -166,7 +166,10 @@ void CheapStepper::step(bool clockwise){ // return control to the system between the steps so that long series of those // doesn't trigger watch dog restart on ESP if (millis() - lastYieldTime >= YIELD_TIME) + { yield(); + lastYieldTime = millis(); + } } void CheapStepper::off() {