From aa941185c6a306af14972f7507103e3504554e53 Mon Sep 17 00:00:00 2001 From: philsson Date: Tue, 11 Sep 2018 16:28:07 +0200 Subject: [PATCH] Small fixes to stepper driver --- src/drivers/stepper.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/drivers/stepper.cpp b/src/drivers/stepper.cpp index 532868a..9558797 100644 --- a/src/drivers/stepper.cpp +++ b/src/drivers/stepper.cpp @@ -20,8 +20,10 @@ Stepper::Stepper(PinName stepPin, , m_lastDirection(1) , m_latestSpeed(0.0f) { - m_step.pulsewidth_us(1); + m_step.period_us(1000000); + m_step.pulsewidth_us(5.0f); + // Start controller deactivated m_en.write(1); m_dir.write(m_configuredDirection); } @@ -51,8 +53,8 @@ bool Stepper::isEnabled() void Stepper::setDirection(int dir) { + m_lastDirection = -1; // Deem invalid m_configuredDirection = dir; - m_lastDirection = dir; m_dir.write(dir == 1 ? 1 : 0); } @@ -77,15 +79,16 @@ float Stepper::limitAcceleration(float DPS) void Stepper::setSpeed(const float& DPS) { m_latestSpeed = limitAcceleration(DPS); - + float revPerSecond = abs(m_latestSpeed)/(float)360.0; - revPerSecond = constrain(revPerSecond, 1000000.0f); + revPerSecond = constrain(revPerSecond, 100.0f); float stepsPerSecond = m_stepsPerRevolution*m_microStepResolution*revPerSecond; float usPerSecond = 1000000.0f; + // Some high value to make motors "stop" | And to avoid division by zero float periodUs = (stepsPerSecond == 0.0) ? 100000.0 : usPerSecond/stepsPerSecond; // Precaution. Don't know how close the period can be to