AccelStepper and the XY-Plotter


#1

Diving into things I don’t understand, stepper motor software and stepper motor drivers.

In this thread Music Robot is this post:

I think you can do:

AccelStepper stepper(1,12,13);

params: 1 = driver mode
12, 13 = pins for PUL, DIR

for me this works …

Now I know that the XY plotter has …

Port 1 has pins 11 (direction) and 10 (step).
Port 2 has pins 3 (direction) and 9 (step)

So presumably to use AccelStepper I’d need to do

Stepper StepperMotorA (AccelStepper::FULL2WIRE,10,11) ;

to control the motor on port 1… I think I need FULL2WIRE because there are only 2 wires from the Orion to the driver…

I know I could just try it, but I like to think I know a not what I’m doing before I do it, and I’m also scared of exploding something!

TIA


#2

Nope. That would not have worked for two reasons:

  1. I should be using AccelStepper (not just Stepper)
  2. The type should have been AccelStepper::DRIVER, not AccelStepper::FULL2WIRE

Here is a sketch which can pilot one Orion motor at a time, for anyone who is interested:

#include <AccelStepper.h>

#define PORT1_DIR_PIN 11
#define PORT1_STEP_PIN 10

#define PORT2_DIR_PIN 3
#define PORT2_STEP_PIN 9

// Define a stepper and the pins it will use
AccelStepper Port1Stepper (AccelStepper::DRIVER, PORT1_STEP_PIN, PORT1_DIR_PIN); 

// This is how you'd do it for the second port
AccelStepper Port2Stepper (AccelStepper::DRIVER, PORT2_STEP_PIN, PORT2_DIR_PIN); 

void setup()
{  
}

void loop()
{
    if (Port1Stepper.distanceToGo() == 0)
    {
        // Random change to speed, position and acceleration
        delay(100);
        Port1Stepper.moveTo(rand() % 1600);
        Port1Stepper.setMaxSpeed((rand() % 4800) + 100);
        Port1Stepper.setAcceleration((rand() % 500) + 1);
    }

    // After having setup position, speed and accelleraton
    // actually move the stepper.
    Port1Stepper.run();
}

WARNING: Put the pen carriage in the middle before you run this test.
WARNING: Make sure all 3 DIP switches (on the driver boards) are set to HIGH, (closest to the red connector)


#3

Looks interesting, but is it possible to make two steppers run at the same time such that the pen would draw a straight line across a diagonal? it seems to me that, although distance to go for each stepper is easy to calculate, the acceleration and speeds would need to be worked out very precisely so that both steppers moved the pen correctly.


#4

Yes. In the same library is a class called MultiStepper which can be used to control several steppers (each one an AccelStepper) at the same time. I’ve not tested that yet though…


#5

Hi there, I too am trying to integrate the Accelstepper library with the XY plotter, having previously implemented with only the Makeblock motor functions.

My system takes in input from serial (Axis of movement, Direction of movement, Number of steps) parses the inputs and sends them to a Step function. Within this function I am attempting use a while loop to call the run() command, after setting the stepper variables like the supplied code above. This does not seem to work and I can confirm that my serial parsing implementation is not at fault.

Surely it is possible to operate the motors outwith the arduino’s main loop? My thinking is that a delay() is possibly required?

Thanks in advance,
Steve


#6

Do you mean outside of the main loop? If you are sending commands normally the “each” loop gathers each command, pilots the motors then starts from the beginning again. Something like

void loop() {
    GetCommandFromSerial() ;
    MoveMotorsAccordingly() ;
} 

I personally have never actual used the AccellSteper library.


#7

Thanks for your reply!

Yes, the only code I have in my main loop is for receiving of characters over serial e.g.
void loop() { // if serial data available, process it while (Serial.available () > 0) { processIncomingByte (Serial.read ()); } }

The serial data is then buffered until I see a return character. I then send the full command string to a processing function, which parses the command and inputs the correct components into a step function.

My step function is where I am trying to run the motors from.

Within the step function I am trying to do this:
int targetPos = Port1StepperX.currentPosition() + steps; Port1StepperX.moveTo(targetPos); Port1StepperX.setMaxSpeed(1.0); Port1StepperX.setAcceleration(1.0); while(Port1StepperX.currentPosition() != targetPos){ Port1StepperX.run(); }

Any help would be appreciated.

Thanks!


#8

I have found a solution to this. The problem seemed to be due to the incorrect setting or wrong ordering of the stepper motor parameters.

By removing the setMaxSpeed() parameter I was able to use the method outlined above.


#9

Thanks for the update :slight_smile:


#10