0

I am using Accelstepper library to control a unipolar stepper motor. In my project, I would like to run the stepper in full-step mode when I need speed and in half-step mode when I need precision. As you know, the full-step/half-step selection is done in the very beginning of the sketch...

I found a workaround by creating two instances of Accelstepper for the same stepper. That is:

Accelstepper fast(FULL4WIRE, 2,3,4,5);

Accelstepper slow(HALF4WIRE, 2,3,4,5);

With this, when I call (skipping other details of the code) fast.run() the motor moves fast (full-step) and when I call slow.run() it moves slowly as it is in half step mode.

But;

When it comes to determine to current motor position, this workaround fails, since there are two different values returned by the two functions: fast.currentPosition() and slow.currentPosition(). So this workaroud becomes useless.

My first question is: is there a way in Accelstepper to switch from full-step to half-step and vice versa in the execution(let say in the loop)? If so, the second question can be skipped.

The second: XX.currentPosition() function returns the value of a variable, namely "_currentPos" which is calculated in Accelstepper.cpp file. As I understand, there become, as many _currentPos values as Accelstepper instances. In my case, one for fast and another for slow, and somehow they are kept and calculated individually without any confusion. Is there a way to access (extern?) and set this variable from the sketch, by referencing the specific Accelstepper instence? Briefly, is there a way to copy one motor position to another?

Many thanks.

1 Answer 1

1

The documentation notes a setCurrentPosition() function.

void AccelStepper::setCurrentPosition ( long position )

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.

Parameters

position The position in steps of wherever the motor happens to be right now.


Use the currentPosition() function to get the current position from either mode. You will have to decide whether you want "fast" steps or "slow" steps to be the actual count, and when you switch from one context to the other, grab the current position of the last used mode, and set a new position in the mode you will use next, possibly multiplying or dividing by 2 as needed.

3
  • Jose, thanks for the answer. But it doen't solve the problem. Consider, the motor is at zero, when it moves 100 steps via fast.run() and then it moves 50 steps via slow.run()...In this case, fast.currentPosition() returns 100 and slow.currentPosition() returns 50, but in fact motor is at position 150! Notice, there is only one single motor although declared 2 times. By reseting the position with setCurrentPosition, you lose your starting point. If you mean that I should record the movements and recalculate in my sketch it becomes too complex as the movements are dependent on many sub-functions
    – Nkgunel
    Jun 1, 2019 at 17:59
  • Sorry I wasn't more clear. Also use the currentPosition() function. You will have to decide whether you want "fast" steps or "slow" steps to be the actual count, and when you switch from one context to the other, grab the current position of the last used mode, and set a new position in the mode you will use next, possibly multiplying or dividing by 2 as needed. Jun 1, 2019 at 18:08
  • I thought that the setCurrentPosition() function is only for setting the current motor position to "0". However, it is possible to set current position to anything. So, with this info, it became possible to copy/add one motor position to the other just by considering the step sizes.
    – Nkgunel
    Jun 3, 2019 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.