controlling stepper motor with digital sensors to stop at several positions smoothly

509 views
Skip to first unread message

Rose Carrousel

unread,
Oct 17, 2013, 7:55:43 AM10/17/13
to accels...@googlegroups.com
Hello folks,

I am working on controlling a stepper motor so that it can stop relatively smoothly at one of several (say, 5) designated positions along a rod which it is moving along.

I think I can figure out how to do this using the stepper.runSpeed(); option, that is, move at constant speed. But I am still working on whether this is achievable using the smooth acceleration and deceleration of stepper.run(); This second option would be ideal because it would lead to less jerkiness in the system. 

The thing is that I am not telling it what position to go to, I am just telling it to move, and then to stop when it hits a certain sensor. The reason for this is that the rod is smooth and so the stepper motor sometimes slips on the rod a bit and so if I gave it exact positions to go to, they would change overtime and the system would crash.

The goals for the system are for it to be able to stop at a designated sensor (or near a designated sensor if it takes a few steps to decelerate/stop) and for it to stop with as little jerkiness as possible.

Kind regards,
Heidi



Sandy Noble

unread,
Oct 17, 2013, 5:54:12 PM10/17/13
to accels...@googlegroups.com
Hello, I think the most common way to do this kind of thing is to set an extremely high target distance, then have your loop, run(), while checking the status of the sensors.  Once a sensor is triggered, then call stepper.stop(), and it'll decelerate to a halt.  If you are intent on lining up correctly, then you can issue a new move(...) command to backtrack to the point where the sensor was triggered.  Otherwise I guess you could just offset your sensors by the deceleration distance.

So to move endlessly clockwise, do 
stepper.move(2147483647L);

and anticlockwise 
stepper.move(-2147483646L);

While in your loop, you will be calling stepper.run(), and maybe checking your sensors.

#include <AccelStepper.h>
AccelStepper stepper;
long MAX_LONG = 2147483647L;
long MIN_LONG = -2147483646L;
boolean sensor1Triggered = false;
boolean sensor2Triggered = false;
boolean sensor3Triggered = false;
boolean currentlyTriggered = false;

void setup() {
    stepper.move(MAX_LONG);
}

void checkSensors() {
    // check the sensor input pins and sets the values of sensor1Triggered, sensor2Triggered and sensor3Triggered
    // not sure how you do this - depends on your type of sensor I guess
    // .....

    // but deal with the triggers
    if (sensor1Triggered || sensor2Triggered || sensor3Triggered) {
        if (currentlyTriggered) {
            // reset these so that they don't re-trigger the "stop" behaviour
            sensor1Triggered = false;
            sensor2Triggered = false;
            sensor3Triggered = false;
        }
        else {
            currentlyTriggered = false;
        }
    }
    else {
        currentlyTriggered = false;
    }
}

void loop() {
    checkSensors();
    if (sensor1Triggered || sensor2Triggered || sensor3Triggered) {
        currentlyTriggered = true;
        // stop the motor as quick as possible
        // but keep a note of where the sensor was triggered too, so we can wind back
        long exactTriggerPoint = stepper.currentPosition();
        stepper.stop();  // this sets the position that will allow a smooth stop
        while (stepper.distanceToGo() != 0) {
            stepper.run();}
        stepper.runToNewPosition(exactTriggerPoint);

        // and do some behaviour and set the next target
    }

    stepper.run();
}

The above should compile, but it is otherwise not tested, but that's something like how I'd do it. If you found that your sensor polling in checkSensors() makes your loop too slow (reading pins is sometimes slow), then you could connect the sensors to an interrupt, but that depends on what kind of sensors / signals you are expecting I suppose, as well how many you need to be able to differentiate.


hope that makes sense and isn't too much of a brain dump :)

sn





--
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

Rose Carrousel

unread,
Oct 17, 2013, 6:07:03 PM10/17/13
to accels...@googlegroups.com
Thanks so much Sandy.

That makes a lot of sense. Thanks for explaining the .stop(); function as well as your suggestion about setting a long target distance. 

I'll be working on this on the weekend so I will let you know how I go. I really appreciate your reply.

Kind regards,
Heidi

Reply all
Reply to author
Forward
0 new messages