RUNNING 2 stepper motors Simultaneously

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
2011TRD
 
Posts: 2
Joined: Sun Apr 28, 2013 4:26 am

RUNNING 2 stepper motors Simultaneously

Post by 2011TRD »

This is my first post, and really happy to finally on the forum boards.

I just bought a Adafruit motor shield and I am trying to develop code that will run two stepper motors at the same time. I looked at the tutorial but I kinda don't fully understand it. If i used the following example code from the site below what would I have to add to drive two motors at the same time.

void doublestep (int steps, int direction, int style) {
while (steps--) {
motor1.step(1, direction, style);
motor2.step(1, direction, style);
}
}

How can I implement this example function for driving two motors in their example code below



Code: Select all

    #include <AFMotor.h>


    AF_Stepper motor(48, 2);


    void setup() {
      Serial.begin(9600);           // set up Serial library at 9600 bps
      Serial.println("Stepper test!");

      motor.setSpeed(10);  // 10 rpm   

      motor.step(100, FORWARD, SINGLE); 
      motor.release();
      delay(1000);
    }

    void loop() {
      motor.step(100, FORWARD, SINGLE); 
      motor.step(100, BACKWARD, SINGLE); 

      motor.step(100, FORWARD, DOUBLE); 
      motor.step(100, BACKWARD, DOUBLE);

      motor.step(100, FORWARD, INTERLEAVE); 
      motor.step(100, BACKWARD, INTERLEAVE); 

      motor.step(100, FORWARD, MICROSTEP); 
      motor.step(100, BACKWARD, MICROSTEP); 
    }
Last edited by adafruit_support_bill on Sun Apr 28, 2013 7:21 am, edited 1 time in total.
Reason: Please use the 'code' button when submitting code

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: RUNNING 2 stepper motors Simultaneously

Post by adafruit_support_bill »

The easiest way to drive multiple steppers simultaneously is with the Adafruit version of AccelStepper: https://github.com/adafruit/AccelStepper
This works with the AF_Motor library to support simultaneous stepper operation.

This is the AF_Motor_MultiStepper example from the library:

Code: Select all

// MultiStepper
// -*- mode: C++ -*-
//
// Control both Stepper motors at the same time with different speeds
// and accelerations. 
// Requires the AFMotor library (https://github.com/adafruit/Adafruit-Motor-Shield-library)
// And AccelStepper with AFMotor support (https://github.com/adafruit/AccelStepper)
// Public domain!

#include <AccelStepper.h>
#include <AFMotor.h>

// two stepper motors one on each port
AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {  
  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  motor1.onestep(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {  
  motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() {  
  motor2.onestep(BACKWARD, SINGLE);
}

// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()
{  
    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(100.0);
    stepper1.moveTo(24);
    
    stepper2.setMaxSpeed(300.0);
    stepper2.setAcceleration(100.0);
    stepper2.moveTo(1000000);
    
}

void loop()
{
    // Change direction at the limits
    if (stepper1.distanceToGo() == 0)
	stepper1.moveTo(-stepper1.currentPosition());
    stepper1.run();
    stepper2.run();
}

2011TRD
 
Posts: 2
Joined: Sun Apr 28, 2013 4:26 am

Re: RUNNING 2 stepper motors Simultaneously

Post by 2011TRD »

Thank You for the prompt response, your help is greatly appreciated!

What does that comment mean?
// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: RUNNING 2 stepper motors Simultaneously

Post by adafruit_support_bill »

It means that we create two AccelStepper objects (stepper1 and stepper2) that control the two stepper motor ports.

barneystone
 
Posts: 20
Joined: Sun Aug 18, 2013 10:58 am

Re: RUNNING 2 stepper motors Simultaneously

Post by barneystone »

Is the Motor Shield V2 compatible with AccelStepper? I can't find an example for that combination. I keep ending up at examples that work with the older Motor Shield. I need a way to run a stepper in non-blocking mode, so I can run a servo at the same time. Any help would be appreciated.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: RUNNING 2 stepper motors Simultaneously

Post by adafruit_support_bill »

We have not implemented this for Version 2 of the shield. You can implement non-blocking stepper motion with periodic calls to onestep() either with a timer interrupt or by polling "millis()" as in AccelStepper.

What kind of stepper motion are you trying to achieve?

barneystone
 
Posts: 20
Joined: Sun Aug 18, 2013 10:58 am

Re: RUNNING 2 stepper motors Simultaneously

Post by barneystone »

I am spinning something back and forth for a visual effect. The exact position is not critical. I want to rock another object back and forth at the same time with a servo. Can you give me an example of how to use millis() and onestep() for that? Thanks!

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: RUNNING 2 stepper motors Simultaneously

Post by adafruit_support_bill »

Something like this:

Code: Select all

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

int stepInterval = 10;  // 10 milliseconds is 100 steps per second
int maxSteps = 100;  // number of steps in each direction
int stepsTaken = 0;
long lastStepTime = 0;

uint8_t  dir = FORWARD;
uint8_t  style = DOUBLE;

void setup() 
{
  AFMS.begin();  // create with the default frequency 1.6KHz
  lastStepTime = millis();
}

void loop() 
{
  if (millis() - lastStepTime >= stepInterval) // Time to take another step
  {
    myMotor->onestep(dir, style);
    lastStepTime = 0;  // reset time
    if (++stepsTaken >= maxSteps) // reverse direction
    {
      if (dir == FORWARD)
        dir = BACKWARD;
      else
        dir = FORWARD;
      stepsTaken = 0;
    }
  }
  
  // do other stiff
}

barneystone
 
Posts: 20
Joined: Sun Aug 18, 2013 10:58 am

Re: RUNNING 2 stepper motors Simultaneously

Post by barneystone »

That's great, Bill! Thanks for the excellent support, especially on the weekend!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”