RunSpeedToPosition

3,584 views
Skip to first unread message

MasseyMan

unread,
Mar 12, 2012, 12:25:35 PM3/12/12
to accelstepper
Mike,
Thanks for letting me know about the group and the updated version
that includes the fix for, "runSpeedToPosition"

For documentation purposes, I am copying our conversation into here.

MasseyMan wrote;
First, Thank you for creating this library! I am using it to control
my automatic fish feeder.

I have a question about how to use "runSpeedToPosition"

I could not get it to work without it stopping the code in its tracks.

could you please give me a short example of it's usage?

I am currently using "runToNewPosition", but was looking to avoid the
Acceleration and deceleration that comes with it, yet still be able to
code an easy one liner way to move the stepper a specific amount of
steps.

Thank You,


MikeM wrote back;
thanks for your note.
While investigating this, I realised that runSpeedToPosition() does
not really
do what I intended it to.
New version 1.15 has been uploaded with a fix, that will work with
this sample
code:


#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to 4 pins on 2, 3, 4, 5

void setup()
{
stepper.setMaxSpeed(1000);
}

void loop()
{
if (stepper.distanceToGo() == 0)
{
// Random change to position

stepper.moveTo(rand() % 200);
stepper.setSpeed(50); // moveTo can change the speed
}

stepper.runSpeedToPosition();

}

Sandy Noble

unread,
Mar 12, 2012, 12:43:52 PM3/12/12
to accels...@googlegroups.com
Oh cool, I wondered what I was missing to make sense of
runSpeedToPosition. Does this take reverse speed into account? That
is, if I do

stepper.setSpeed(50);
stepper.moveTo(stepper.currentPosition()-50)
stepper.runSpeedToPosition();

Will the stepper run backwards to get to the position? Last time I
tried this I ended up needing to do .setSpeed(-50) to get it to run
backwards.


Thanks
Sandy Noble


--
Sandy Noble

Mike McCauley

unread,
Mar 12, 2012, 5:43:38 PM3/12/12
to accels...@googlegroups.com
Hello,

On Monday, March 12, 2012 04:43:52 PM Sandy Noble wrote:
> Oh cool, I wondered what I was missing to make sense of
> runSpeedToPosition. Does this take reverse speed into account? That
> is, if I do
>
> stepper.setSpeed(50);
> stepper.moveTo(stepper.currentPosition()-50)
> stepper.runSpeedToPosition();
>
> Will the stepper run backwards to get to the position?

Yes, if you have the latest version.


> Last time I
> tried this I ended up needing to do .setSpeed(-50) to get it to run
> backwards.

Yes, that was the bug I fixed.

Cheers.

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

Radiator: the most portable, flexible and configurable RADIUS server
anywhere. SQL, proxy, DBM, files, LDAP, NIS+, password, NT, Emerald,
Platypus, Freeside, TACACS+, PAM, external, Active Directory, EAP, TLS,
TTLS, PEAP, TNC, WiMAX, RSA, Vasco, Yubikey, MOTP, HOTP, TOTP,
DIAMETER etc. Full source on Unix, Windows, MacOSX, Solaris, VMS, NetWare etc.

MasseyMan

unread,
Mar 14, 2012, 2:12:50 AM3/14/12
to accelstepper

Mike,

I was able to get your Example to work. I was not able to get Sandy's
idea to work or what I had in mind to work.
Sandy's example just ends up in a loop and the stepper never stops
turning.
I was able to get this example to work;

#include <AccelStepper.h>
AccelStepper stepper; // Defaults to 4 pins on 2, 3, 4, 5

void setup()
{
stepper.setMaxSpeed(1000);
}

void loop()
{
stepper.moveTo(100);
stepper.setSpeed(50);
if (stepper.distanceToGo() != 0)
{
stepper.runSpeedToPosition();
} else
{
delay(2000); // Pause then reset position and do it again
stepper.setCurrentPosition(1);
}
}

I was hoping for a command more like "runToNewPosition()". I realize
"runSpeedToPosition" is not a blocking, but if you want to just move
the Stepper a certian amount without Acceleration is there a simple
command for that like "runSpeedToNewPosition"?

The reason for this is, I am using your library (which I love by the
way) for my automatic fish feeder. I have a IR position sensor that is
used to stop the feeder at certian positions (in case of jitter or
stalling) and when the stop tab reaches the sensor I want it to turn a
little more to fully cover the sensor.

I have it working and if you would like to see pictures of it, that
might make more sense of what I'm trying to describe, let me know

Vance

Mike McCauley

unread,
Mar 14, 2012, 2:51:07 AM3/14/12
to accels...@googlegroups.com
Hi,

I think you want something like:

#include <AccelStepper.h>
AccelStepper stepper; // Defaults to 4 pins on 2, 3, 4, 5

void setup()
{
stepper.setMaxSpeed(1000);
}

void blockingRunSpeedToPosition(long position)
{
stepper.moveTo(position);
stepper.setSpeed(50);
while (stepper.distanceToGo() != 0)
stepper.runSpeedToPosition();
}

void loop()
{
stepper.moveTo(100);
stepper.setSpeed(50);
blockingRunSpeedToPosition(100);
delay(2000);
blockingRunSpeedToPosition(0);
// done, stop here
while (1) ;
}

MasseyMan

unread,
Mar 16, 2012, 12:27:07 AM3/16/12
to accelstepper
Thanks Mike, your example works great. Now I can call the function
from anywhere in the code and move it any amount of steps I want.
I was playing around with it and did this;
I hooked up a IR position sensor (TLP832 photointerrupter) and used
this code to control the stepper.

#include <AccelStepper.h>
int sensor = A1; // position sensor attached to analog A1
int positionsensor = 0;
AccelStepper stepper; // Defaults to 4 pins on 2, 3, 4, 5

void setup()
{
stepper.setMaxSpeed(100);
Serial.begin(9600);
}

void blockingRunSpeedToPosition(long position)
{
stepper.setCurrentPosition(1); // had to set this to make
it go forward from
stepper.moveTo(position); // current position when
called
stepper.setSpeed(50);
while (stepper.distanceToGo() != 0)
stepper.runSpeedToPosition();
}

void loop()
{
positionsensor = analogRead(sensor);
stepper.setSpeed(50);
if (positionsensor < 50) // if sensor is covered do nothing
{ goloop(); }
else
{
positionsensor = analogRead(sensor); // when sensor is uncovered
run
while (positionsensor > 50) // Stepper motor until it
gets covered
{ stepper.runSpeed();
positionsensor = analogRead(sensor);
}
}

stepper.setSpeed(50);
blockingRunSpeedToPosition(30); // after sensor gets covered
Serial.println(positionsensor); // run it a little a few steps
more
delay(2000);
Serial.println("go to loop");
goloop();
}

void goloop() // go to the top of loop
{
delay(500);
Serial.println("loop it");
loop();
}


If any one knows of a different way or better way I'd love to see it
so I could try it out.
Reply all
Reply to author
Forward
0 new messages