Small fixes to stepper driver

This commit is contained in:
philsson 2018-09-11 16:28:07 +02:00 committed by Philip Johansson
parent b2fa98ee1b
commit aa941185c6

View File

@ -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