From 482d04ba8833a493c3f7b5079397a71c975506df Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 3 Aug 2023 18:51:20 +0200 Subject: [PATCH] Set direction pin level earlier --- .../ContinuousStepper_TeensyTimerTool.cpp | 6 ++++-- .../ContinuousStepperTests/ContinuousStepper_Timer1.cpp | 6 ++++-- .../ContinuousStepperTests/ContinuousStepper_Timer3.cpp | 6 ++++-- extras/tests/ContinuousStepperTests/powerOn.cpp | 2 +- extras/tests/ContinuousStepperTests/spin.cpp | 4 ++-- src/ContinuousStepperBase.h | 6 ++++-- src/ContinuousStepperWithOscillator.hpp | 1 - 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/extras/tests/ContinuousStepperTests/ContinuousStepper_TeensyTimerTool.cpp b/extras/tests/ContinuousStepperTests/ContinuousStepper_TeensyTimerTool.cpp index 240ab2e..bf582cc 100644 --- a/extras/tests/ContinuousStepperTests/ContinuousStepper_TeensyTimerTool.cpp +++ b/extras/tests/ContinuousStepperTests/ContinuousStepper_TeensyTimerTool.cpp @@ -27,6 +27,7 @@ TEST_CASE("ContinuousStepper_TeensyTimerTool::spin()") { REQUIRE(stepper.speed() == 10); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, HIGH)"}, {0'000, "PeriodicTimer::stop()"}, {0'000, "PeriodicTimer::setPeriod(50000)"}, {0'000, "PeriodicTimer::start()"}, @@ -40,6 +41,7 @@ TEST_CASE("ContinuousStepper_TeensyTimerTool::spin()") { REQUIRE(stepper.speed() == -10); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, LOW)"}, {0'000, "PeriodicTimer::stop()"}, {0'000, "PeriodicTimer::setPeriod(50000)"}, {0'000, "PeriodicTimer::start()"}, @@ -58,10 +60,10 @@ TEST_CASE("ContinuousStepper_TeensyTimerTool::spin()") { REQUIRE(stepper.speed() == 100); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, HIGH)"}, {0'000, "PeriodicTimer::stop()"}, {0'000, "PeriodicTimer::setPeriod(15811)"}, {0'000, "PeriodicTimer::start()"}, - {15'811, "digitalWrite(11, HIGH)"}, {15'811, "digitalWrite(10, HIGH)"}, {15'811, "PeriodicTimer::stop()"}, {15'811, "PeriodicTimer::setPeriod(10541)"}, @@ -156,6 +158,7 @@ TEST_CASE("ContinuousStepper_TeensyTimerTool::spin()") { {182'478, "PeriodicTimer::stop()"}, {182'478, "PeriodicTimer::setPeriod(15811)"}, {182'478, "PeriodicTimer::start()"}, + {198'289, "digitalWrite(11, LOW)"}, {198'289, "PeriodicTimer::stop()"}, {198'289, "PeriodicTimer::setPeriod(15811)"}, {198'289, "PeriodicTimer::start()"}, @@ -165,7 +168,6 @@ TEST_CASE("ContinuousStepper_TeensyTimerTool::spin()") { {229'911, "PeriodicTimer::stop()"}, {229'911, "PeriodicTimer::setPeriod(13064)"}, {229'911, "PeriodicTimer::start()"}, - {242'975, "digitalWrite(11, LOW)"}, {242'975, "digitalWrite(10, LOW)"}, {242'975, "PeriodicTimer::stop()"}, {242'975, "PeriodicTimer::setPeriod(9739)"}, diff --git a/extras/tests/ContinuousStepperTests/ContinuousStepper_Timer1.cpp b/extras/tests/ContinuousStepperTests/ContinuousStepper_Timer1.cpp index 464a4be..b66454f 100644 --- a/extras/tests/ContinuousStepperTests/ContinuousStepper_Timer1.cpp +++ b/extras/tests/ContinuousStepperTests/ContinuousStepper_Timer1.cpp @@ -27,6 +27,7 @@ TEST_CASE("ContinuousStepper_Timer1::spin()") { REQUIRE(stepper.speed() == 10); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, HIGH)"}, {0'000, "TimerOne::stop()"}, {0'000, "TimerOne::setPeriod(50000)"}, {0'000, "TimerOne::start()"}, @@ -40,6 +41,7 @@ TEST_CASE("ContinuousStepper_Timer1::spin()") { REQUIRE(stepper.speed() == -10); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, LOW)"}, {0'000, "TimerOne::stop()"}, {0'000, "TimerOne::setPeriod(50000)"}, {0'000, "TimerOne::start()"}, @@ -58,10 +60,10 @@ TEST_CASE("ContinuousStepper_Timer1::spin()") { REQUIRE(stepper.speed() == 100); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, HIGH)"}, {0'000, "TimerOne::stop()"}, {0'000, "TimerOne::setPeriod(15811)"}, {0'000, "TimerOne::start()"}, - {15'811, "digitalWrite(11, HIGH)"}, {15'811, "digitalWrite(10, HIGH)"}, {15'811, "TimerOne::stop()"}, {15'811, "TimerOne::setPeriod(10541)"}, @@ -156,6 +158,7 @@ TEST_CASE("ContinuousStepper_Timer1::spin()") { {182'478, "TimerOne::stop()"}, {182'478, "TimerOne::setPeriod(15811)"}, {182'478, "TimerOne::start()"}, + {198'289, "digitalWrite(11, LOW)"}, {198'289, "TimerOne::stop()"}, {198'289, "TimerOne::setPeriod(15811)"}, {198'289, "TimerOne::start()"}, @@ -165,7 +168,6 @@ TEST_CASE("ContinuousStepper_Timer1::spin()") { {229'911, "TimerOne::stop()"}, {229'911, "TimerOne::setPeriod(13064)"}, {229'911, "TimerOne::start()"}, - {242'975, "digitalWrite(11, LOW)"}, {242'975, "digitalWrite(10, LOW)"}, {242'975, "TimerOne::stop()"}, {242'975, "TimerOne::setPeriod(9739)"}, diff --git a/extras/tests/ContinuousStepperTests/ContinuousStepper_Timer3.cpp b/extras/tests/ContinuousStepperTests/ContinuousStepper_Timer3.cpp index 79e11eb..13e304a 100644 --- a/extras/tests/ContinuousStepperTests/ContinuousStepper_Timer3.cpp +++ b/extras/tests/ContinuousStepperTests/ContinuousStepper_Timer3.cpp @@ -27,6 +27,7 @@ TEST_CASE("ContinuousStepper_Timer3::spin()") { REQUIRE(stepper.speed() == 10); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, HIGH)"}, {0'000, "TimerThree::stop()"}, {0'000, "TimerThree::setPeriod(50000)"}, {0'000, "TimerThree::start()"}, @@ -40,6 +41,7 @@ TEST_CASE("ContinuousStepper_Timer3::spin()") { REQUIRE(stepper.speed() == -10); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, LOW)"}, {0'000, "TimerThree::stop()"}, {0'000, "TimerThree::setPeriod(50000)"}, {0'000, "TimerThree::start()"}, @@ -58,10 +60,10 @@ TEST_CASE("ContinuousStepper_Timer3::spin()") { REQUIRE(stepper.speed() == 100); CHECK_ARDUINO_LOG({ + {0'000, "digitalWrite(11, HIGH)"}, {0'000, "TimerThree::stop()"}, {0'000, "TimerThree::setPeriod(15811)"}, {0'000, "TimerThree::start()"}, - {15'811, "digitalWrite(11, HIGH)"}, {15'811, "digitalWrite(10, HIGH)"}, {15'811, "TimerThree::stop()"}, {15'811, "TimerThree::setPeriod(10541)"}, @@ -156,6 +158,7 @@ TEST_CASE("ContinuousStepper_Timer3::spin()") { {182'478, "TimerThree::stop()"}, {182'478, "TimerThree::setPeriod(15811)"}, {182'478, "TimerThree::start()"}, + {198'289, "digitalWrite(11, LOW)"}, {198'289, "TimerThree::stop()"}, {198'289, "TimerThree::setPeriod(15811)"}, {198'289, "TimerThree::start()"}, @@ -165,7 +168,6 @@ TEST_CASE("ContinuousStepper_Timer3::spin()") { {229'911, "TimerThree::stop()"}, {229'911, "TimerThree::setPeriod(13064)"}, {229'911, "TimerThree::start()"}, - {242'975, "digitalWrite(11, LOW)"}, {242'975, "digitalWrite(10, LOW)"}, {242'975, "TimerThree::stop()"}, {242'975, "TimerThree::setPeriod(9739)"}, diff --git a/extras/tests/ContinuousStepperTests/powerOn.cpp b/extras/tests/ContinuousStepperTests/powerOn.cpp index 6dcb84d..f86353d 100644 --- a/extras/tests/ContinuousStepperTests/powerOn.cpp +++ b/extras/tests/ContinuousStepperTests/powerOn.cpp @@ -32,7 +32,7 @@ TEST_CASE("ContinuousStepper::powerOn()") { REQUIRE(stepper.isSpinning() == true); CHECK(stepper.speed() == 100); CHECK_ARDUINO_LOG({ - {15'811, "digitalWrite(11, HIGH)"}, // DIR + {0'000, "digitalWrite(11, HIGH)"}, // DIR {15'811, "digitalWrite(10, HIGH)"}, // 10.541 ms {26'352, "digitalWrite(10, LOW)"}, // 8.624 ms {34'976, "digitalWrite(10, HIGH)"}, // 7.507 ms diff --git a/extras/tests/ContinuousStepperTests/spin.cpp b/extras/tests/ContinuousStepperTests/spin.cpp index f3a39f3..7b07ff5 100644 --- a/extras/tests/ContinuousStepperTests/spin.cpp +++ b/extras/tests/ContinuousStepperTests/spin.cpp @@ -36,7 +36,7 @@ TEST_CASE("ContinuousStepper::spin()") { REQUIRE(stepper.speed() == 100); CHECK_ARDUINO_LOG({ - {15'811, "digitalWrite(11, HIGH)"}, // DIR + {0'000, "digitalWrite(11, HIGH)"}, // DIR {15'811, "digitalWrite(10, HIGH)"}, // 10.541 ms {26'352, "digitalWrite(10, LOW)"}, // 8.624 ms {34'976, "digitalWrite(10, HIGH)"}, // 7.507 ms @@ -71,7 +71,7 @@ TEST_CASE("ContinuousStepper::spin()") { {143'218, "digitalWrite(10, HIGH)"}, // 10.325 ms {153'543, "digitalWrite(10, LOW)"}, // 13.124 ms {166'667, "digitalWrite(10, HIGH)"}, // 76.308 ms - {242'975, "digitalWrite(11, LOW)"}, // DIR + {198'289, "digitalWrite(11, LOW)"}, // DIR {242'975, "digitalWrite(10, LOW)"}, // 9.739 ms {252'714, "digitalWrite(10, HIGH)"}, // 8.186 ms {260'900, "digitalWrite(10, LOW)"}, // 7.219 ms diff --git a/src/ContinuousStepperBase.h b/src/ContinuousStepperBase.h index 0fa6f51..0fd1f88 100644 --- a/src/ContinuousStepperBase.h +++ b/src/ContinuousStepperBase.h @@ -85,7 +85,6 @@ class ContinuousStepperBase { protected: void tick() { if (_status == STEP) { - writeDir(); if (_stepLevel == LOW) { writeStep(HIGH); _stepLevel = HIGH; @@ -111,6 +110,7 @@ class ContinuousStepperBase { return _stepPin; } +private: void writeDir() { bool level = _currentSpeed >= 0 ? HIGH : LOW; if (level == _dirLevel) @@ -119,7 +119,6 @@ class ContinuousStepperBase { _dirLevel = level; } -private: void updateSpeed() { float_t speedIncrement = _period ? _acceleration * _period / oneSecond : _minSpeedForAcceleration; @@ -152,6 +151,9 @@ class ContinuousStepperBase { if (needsDoubleSpeed()) _period /= 2; + if (_period) + writeDir(); + setPeriod(_period); } diff --git a/src/ContinuousStepperWithOscillator.hpp b/src/ContinuousStepperWithOscillator.hpp index 2cda114..f2cc0ca 100644 --- a/src/ContinuousStepperWithOscillator.hpp +++ b/src/ContinuousStepperWithOscillator.hpp @@ -24,7 +24,6 @@ class ContinuousStepperWithOscillator : public ContinuousStepperBase { void setPeriod(unsigned long period) override { if (period) { - writeDir(); startOscillator(1e6 / period); } else { stopOscillator();