Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add correction of sofware error #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions src/AccelStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,28 @@ boolean AccelStepper::runSpeed()
if (!_stepInterval)
return false;

unsigned long time = micros();
if (time - _lastStepTime >= _stepInterval)
unsigned long r_elsp= micros() - this->_lastStepTime; // software delay

if (this->_stepInterval <= r_elsp - this->_integralError)
{
if (_direction == DIRECTION_CW)
{
// Clockwise
_currentPos += 1;
}
else
{
// Anticlockwise
_currentPos -= 1;
}
step(_currentPos);
if (_direction == DIRECTION_CW)
{
// Clockwise
_currentPos += 1;
}
else
{
// Anticlockwise
_currentPos -= 1;
}
step(_currentPos);


this->_lastStepTime = micros();

_lastStepTime = time; // Caution: does not account for costs in step()
this->_integralError += this->_stepInterval- r_elsp ; //software error integreted

return true;
return true;
}
else
{
Expand Down Expand Up @@ -92,6 +96,7 @@ void AccelStepper::setCurrentPosition(long position)
_n = 0;
_stepInterval = 0;
_speed = 0.0;
this->_integralError = 0;
}

void AccelStepper::computeNewSpeed()
Expand Down Expand Up @@ -200,6 +205,7 @@ AccelStepper::AccelStepper(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_
_minPulseWidth = 1;
_enablePin = 0xff;
_lastStepTime = 0;
this->_integralError =0;
_pin[0] = pin1;
_pin[1] = pin2;
_pin[2] = pin3;
Expand Down Expand Up @@ -241,6 +247,7 @@ AccelStepper::AccelStepper(void (*forward)(), void (*backward)())
_pin[3] = 0;
_forward = forward;
_backward = backward;
this->_integralError =0;

// NEW
_n = 0;
Expand Down Expand Up @@ -306,8 +313,12 @@ void AccelStepper::setSpeed(float speed)
{
_stepInterval = fabs(1000000.0 / speed);
_direction = (speed > 0.0) ? DIRECTION_CW : DIRECTION_CCW;

this->_integralError = 0;
}
_speed = speed;


}

float AccelStepper::speed()
Expand Down Expand Up @@ -611,9 +622,12 @@ void AccelStepper::setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3In

// Blocks until the target position is reached and stopped
void AccelStepper::runToPosition()
{
{
this->_integralError = 0;
while (run())
YIELD; // Let system housekeeping occur

this->_integralError = 0 ;
}

boolean AccelStepper::runSpeedToPosition()
Expand Down Expand Up @@ -643,6 +657,8 @@ void AccelStepper::stop()
move(stepsToStop);
else
move(-stepsToStop);

this->_integralError = 0 ;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/AccelStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ class AccelStepper
private:
/// Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a
/// bipolar, and 4 pins is a unipolar.

uint8_t _interface; // 0, 1, 2, 4, 8, See MotorInterfaceType

/// Arduino pin number assignments for the 2 or 4 pins required to interface to the
Expand Down Expand Up @@ -656,6 +657,9 @@ class AccelStepper

/// The last step time in microseconds
unsigned long _lastStepTime;

/// the integral error correction
unsigned long _integralError;

/// The minimum allowed pulse width in microseconds
unsigned int _minPulseWidth;
Expand Down