What does setCurrentPosition(Pos) ?

4,598 views
Skip to first unread message

peter.m...@gmail.com

unread,
Sep 11, 2013, 11:23:49 AM9/11/13
to accels...@googlegroups.com
Hi,
I want to turn my steppermotor to a certain position and then define this position as zero, in order to get 0 as currentPosition().

Assuming my steppermotor returns 50 as actual position. Now I want to move it 20 steps back and call this new Position zero. What to do for this?

Or more general: If a=currentPos(). What will happen by setcurrentPosition(b)?
1. In which absolute position will the stepper move and
2. what will be the result of a following currentPosition()?


Mike McCauley

unread,
Sep 11, 2013, 4:40:11 PM9/11/13
to accels...@googlegroups.com
Hi

On Wednesday, September 11, 2013 08:23:49 AM peter.m...@gmail.com wrote:
> Hi,
> I want to turn my steppermotor to a certain position and then define this
> position as zero, in order to get 0 as currentPosition().
>
> Assuming my steppermotor returns 50 as actual position. Now I want to move
> it 20 steps back and call this new Position zero. What to do for this?
>
> Or more general: If a=currentPos(). What will happen by
> setcurrentPosition(b)?
> 1. In which absolute position will the stepper move and

it will not move as a result of setCurrentPosition(). It changes the current
position to be 0. It has side effects too, so its not meant to be something you
would do while the motor is in motion.

> 2. what will be the result of a following currentPosition()?

0

--
Mike McCauley mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474 Fax +61 7 5598-7070

peter.m...@gmail.com

unread,
Sep 12, 2013, 5:11:09 AM9/12/13
to accels...@googlegroups.com
Hi Mike,
thanks, but what's the meaning of the parameter Pos in a call like setCurrentPosition(Pos) ?

Peter

Mike McCauley

unread,
Sep 12, 2013, 5:52:27 AM9/12/13
to accels...@googlegroups.com
Hi,

Actually, the current position of the motor is set to the position argument
you pass to setCurrentPosition.
I guess my earlier remarks were for the case where you call
setCurrentPosition(0);

Documentation at:
http://www.airspayce.com/mikem/arduino/AccelStepper

Cheers.



On Thursday, September 12, 2013 02:11:09 AM peter.m...@gmail.com wrote:
> Hi Mike,
> thanks, but what's the meaning of the parameter Pos in a call like
> setCurrentPosition(Pos) ?
>
> Peter
>
> Am Mittwoch, 11. September 2013 22:40:11 UTC+2 schrieb mikem:
> > Hi
> >
> > On Wednesday, September 11, 2013 08:23:49 AM
peter.m...@gmail.com<javascript:>wrote:

peter.m...@gmail.com

unread,
Sep 13, 2013, 7:47:22 AM9/13/13
to accels...@googlegroups.com
Hi Mike,
after many test theres seems to be something wrong with setCurrentPosition().

//#include <Wire.h>
//#include <Adafruit_MotorShield.h>
#include <AccelStepper.h>

AccelStepper stepper1(1, 22,28);

void waiting()
  { while (stepper1.distanceToGo() != 0)
    {stepper1.run();}
    Serial.println(stepper1.currentPosition());
    delay(2000);
  }
 
void setup()
 { delay(5000);
   Serial.begin(9600);Serial.println("Arduino ready");
   stepper1.setMaxSpeed(1000);
   stepper1.setAcceleration(80000);
 
   stepper1.moveTo(100);
   waiting();
  
   stepper1.move(30);
   waiting();
  
   stepper1.setCurrentPosition(20);
   waiting();
 }
 
 void loop()
  { stepper1.run();}

 
The above code (only for demonstration of the problem) first moves stepper 100 steps and later on 30 steps, resulting in at last Position=130.
But the following stepper1.setCurrentPosition(20) moves(!!) the stepper to (estimated absolute Position 20) and then defines this Position as 130.

( The Arduino send by Serial the values 100    130     130 )

Can you confirm this?

Thanks Peter

Sandy Noble

unread,
Sep 13, 2013, 1:55:18 PM9/13/13
to accels...@googlegroups.com
Not sure if this is helpful, but I think this does:

1. sets target to 100
2. runs 100 steps, then prints out 100
3. sets target to 130
4. runs 30 more steps, then prints out 130
5. changes current position to 20 (does not change target - it's still at 130)
6. runs 110 steps, then prints out 130

What do you think should be happening?

sandy


--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Sandy Noble

peter.m...@gmail.com

unread,
Sep 13, 2013, 2:31:42 PM9/13/13
to accels...@googlegroups.com
Why should the single command 'setCurrentPosition(20)' produce a movement of the stepper? Procedure waiting() only finishes if Position 130 has been reached.
I expect after Point 4. ( where Position 130 has been reached ) this position to be newly defined as 20. Therefore no movement and return of
100   130   20

Sandy Noble

unread,
Sep 13, 2013, 3:17:24 PM9/13/13
to accels...@googlegroups.com
Aha, well setCurrentPosition doesn't produce a movement in itself.  So directly after your setCurrentPosition(20), the motor is at position 20, but its target is still 130, because the last move() command that was issued was telling it to go to 130.  The commands to move to a particular target position are not cancelled out by setting current position, so waiting() is still trying to get the motor to 130.  There is nowhere in your code when you set the target to be 20, and I think that is what you are expecting.

So you could do 

motor.moveTo(100);
motor.setCurrentPosition(100);

and then motor.distanceToGo() will be 0.  It won't move when doing a run(), because the current position is equal to the target.

Or
motor.moveTo(0);   // setting target as 0
motor.setCurrentPosition(100);

and then motor.distanceToGo() will be -100.  Calling run() will move 100 steps backwards to get to the target.

Does that make any sense?

sn

peter.m...@gmail.com

unread,
Sep 13, 2013, 5:19:26 PM9/13/13
to accels...@googlegroups.com
Thanks,
if setCurrentPosition() acts in this way, it's clear. I will test your example later.

My problem is perhaps best explained by a clock. Let's say, that the stepper has a pointer und this one must stay vertical at XII (12). This is the real zero position.
On initialising the pointer may stay at 10. Now I can move it for example 20 steps by stepper1.moveto(20) so that it points to twelve. How to say, that from this moment this postion shall be zero? All positions shall refer to 12 as zero Position. How to reach this?

Peter

Sandy Noble

unread,
Sep 13, 2013, 8:21:33 PM9/13/13
to accels...@googlegroups.com
I think just as you suggest:

motor.moveTo(20);
wait();
motor.setCurrentPosition(0);
motor.moveTo(motor.currentPosition());
wait();

That resets the current position to be zero from now on, and also sets the target to be the current position.

All moves after this will treat that 12 o'clock position as 0.

good luck :)

sn


peter.m...@gmail.com

unread,
Sep 14, 2013, 2:12:00 AM9/14/13
to accels...@googlegroups.com
That's it!
I defined a new function to make it more clear:

void defActPosZero()
 //Defines the actual position to be the new zero position
// May not be called while stepper is still moving
  { stepper1.setCurrentPosition(0);stepper1.moveTo(stepper1.currentPosition());}

And later on the calls in my example
 stepper1.moveTo(20);
 wait(1000);  // Stepper must be in the correct new zero position
 defActPosZero();

The limitation that stepper may not move when defActPos() is called can be simply avoided by stepper1.distanceToGo() != 0;

Many thanks!
Reply all
Reply to author
Forward
0 new messages