Accelstepper.h library: 3 questions

When reading the Accelstepper.h following lines appear (from 476 to 489):

    long    currentPosition();  

    /// Resets the current position of the motor, so that wherever the motor
    /// happens to be right now is considered to be the new 0 position. Useful
    /// for setting a zero position on a stepper after an initial hardware
    /// positioning move.
    /// Has the side effect of setting the current motor speed to 0.
    /// \param[in] position The position in steps of wherever the motor
    /// happens to be right now.
    void    setCurrentPosition(long position);  
    
    /// Moves the motor (with acceleration/deceleration)
    /// to the target position and blocks until it is at
    /// position. Dont use this in event loops, since it blocks.

However, in Accelstepper.cpp following shows (lines 82 to 95:

long AccelStepper::currentPosition()
{
    return _currentPos;
}

// Useful during initialisations or after initial positioning
// Sets speed to 0
void AccelStepper::setCurrentPosition(long position)
{
    _targetPos = _currentPos = position;
    _n = 0;
    _stepInterval = 0;
    _speed = 0.0;
}
  1. Rather than resetting the current position to the new 0 position, does currentPosition not return the actually stored current position?

  2. setCurrentPosition: can this not also be used to store a new value in setCurrentPosition? For example to assign a new value to currentPosition (without moving the stepper)

  3. If the latter is not true, how can (with this library) a new value be assigned the currentPosition? For example, on startup or reset of the program) to assign a value to the current position of the stepper. Such as if, before reset, a specific current stepper position needs to be restored after reset.

  1. mystepper.currentPosition() reports the current position, it might well have been named getCurrentPosition(), but was not. The get/set pair of accessor functions for the private variable _currentPos are currentPosition() and setCurrentPosition()

  2. is true. myStepper.setCurrentPosition(-1000); will change the current idea of its position to -1000. it can happen without stepper movement.

3 (2 is true) so you use setCurrentPosition to assign a new value.

Thanks f@DaveX or such a speedy reply!

"... it can happen without stepper movement..."

How to get the stepper not to move?

And what would make the stepper to move?

You get the stepper not to move by not telling it to move, or, if it is already in motion from a previous command, try .stop()

You make the stepper move by giving it a movement command.
https://www.airspayce.com/mikem/arduino/AccelStepper/classMultiStepper.html#a26c2f53b1e7ddf5d5dfb333f6fb7fb92

1 Like

Top! Thank you, understood.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.