Motor Shield V2.3 - 2 Independent Steppers?

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
User avatar
Benjamin269
 
Posts: 16
Joined: Fri Mar 13, 2015 10:56 am

Motor Shield V2.3 - 2 Independent Steppers?

Post by Benjamin269 »

I have an Adafruit Motor Shield v2.3 and am attempting to get two steppers to run independently of each other. I can get them both to run, but I can't get them to not interfere with the performance of each other. For example:

Code: Select all

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

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 1);


void setup() {

  AFMS.begin();  

  myMotor->setSpeed(400);  // 10 rpm 
  myMotor1->setSpeed(400);  // 10 rpm 
  
}

void loop() {

if (digitalRead(12) == 1) { 
  myMotor->step(20, FORWARD, DOUBLE);
}
if (digitalRead(10) == 1) { 
  myMotor1->step(20, BACKWARD, DOUBLE);
}

}
If I run the above code, and press the button connected to i/o 12, myMotor spins. If I press the button connected to i/o 10, myMotor1 spins. If I press both buttons, they both spin, but in a "jittery, take-turns" kind of way.

I also tried the Accel_MultiStepper code:

Code: Select all

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

Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);

void forwardstep1() {  
  myStepper1->onestep(FORWARD, DOUBLE);
}
void backwardstep1() {  
  myStepper1->onestep(BACKWARD, DOUBLE);
}
// wrappers for the second motor!
void forwardstep2() {  
  myStepper2->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {  
  myStepper2->onestep(BACKWARD, DOUBLE);
}

AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()
{  
  AFMStop.begin(); // Start the top shield
  stepper1.setMaxSpeed(400.0);
  stepper1.setAcceleration(1000.0);
//  stepper1.moveTo(100);
    
  stepper2.setMaxSpeed(400.0);
  stepper2.setAcceleration(1000.0);
  stepper2.moveTo(300);
}

void loop()
{
    if (digitalRead(12) == 1) { 
	stepper1.moveTo(stepper1.currentPosition() + 50);
    }
    if (digitalRead(10) == 1) { 
	stepper1.moveTo(stepper1.currentPosition() - 50);
    }
    if (stepper2.distanceToGo() == 0)  {
	stepper2.moveTo(-stepper2.currentPosition());
    }
    stepper1.run();
    stepper2.run();
}
In the above code, stepper2 just spins back and forth, as in the base example. I have modified the code so that when i/0 12 is high, stepper1 spins in one direction, and when i/o 10 is high, it spins the other direction. When I press either button, the speed of stepper2 is noticeably reduced.

I need them to act independently and not affect the performance of each other. They will have 8-way type joystick input, so either one or both motors could be running at any one time.

Can anyone get me started in the right direction?

Benjamin

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

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by adafruit_support_bill »

Code: Select all

  myMotor->setSpeed(400);  // 10 rpm
  myMotor1->setSpeed(400);  // 10 rpm 
What motors are you using? The code above is setting the speed to 400 RPM - not 10 RPM as implied by the comments.

User avatar
Benjamin269
 
Posts: 16
Joined: Fri Mar 13, 2015 10:56 am

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by Benjamin269 »

Yes, I set the motor speeds to 400 and didn't bother to change the comments.

At the moment I am using some 12v, 200 step, 6-wire motors. The only info I have on them is the 13.5 ohms of resistance per phase. I have some adafruit 12v stepprs on order:

https://www.adafruit.com/products/918

and will hopefully be using them in the final project. I don't think it's a motor problem, I think it's a code problem.

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

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by adafruit_support_bill »

There may be some code issues too. But first you have to understand the limitations of the system. You won't be able to run a 200 step motor at 400 RPM - much less 2 of them.

The total aggregate step-rate is limited by the speed of the i2c bus. In practice, with the default settings, you can achieve about 80 RPM with a single 200-step motor. With some tweaking, you can get that up to about 250 RPM. This thread has some more detail on the subject: http://forums.adafruit.com/viewtopic.ph ... 1&p=292119

User avatar
Benjamin269
 
Posts: 16
Joined: Fri Mar 13, 2015 10:56 am

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by Benjamin269 »

Thanks Bill. Knowing the limitations of i2c bus (+ how to speed it up) as well as some of the other info in that thread has helped me quite a bit and has smoothed out some of the issues I was having. I will hopefully get the new steppers tomorrow and can try them out and see how they workout with the different step ratio and gearing. Since the new motors are essentially 513 steps per revolution (after gearing), I'm assuming they will run about 2.5 times slower on the same code as my 200 steps per rev motors. That will push me closer to the tweaked i2c limits, but I will see what happens. I will post an update once the new motors arrive.

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

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by adafruit_support_bill »

I haven't pushed the step-rate on the geared steppers to find their limit. But if the electromechanical limits are lower than the i2c bandwidth limits for a single motor, you may be able to squeeze more performance out of the system by interleaving steps between motors.

User avatar
Benjamin269
 
Posts: 16
Joined: Fri Mar 13, 2015 10:56 am

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by Benjamin269 »

I think the new motors will work as far as speed and torque.

What I want to do is: If button 1 is pressed, spin one direction. If button 2 is pressed, spin the other direction. If neither button is pressed, release the motor. How do I release the motors when not in use? If I use the following code, I get an error "error: 'class AccelStepper' has no member named 'release'"

Code: Select all

void loop()
{
    if (digitalRead(12) == 1) { 
	stepper1.moveTo(stepper1.currentPosition() + 10);
        stepper1.run();
    }
    else if (digitalRead(10) == 1) { 
	stepper1.moveTo(stepper1.currentPosition() - 10);
        stepper1.run();
    }
    else {
        stepper1.release();
    }
}
Last edited by adafruit_support_bill on Thu Mar 19, 2015 11:57 am, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

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

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by adafruit_support_bill »

The AccelStepper motor class is just 'wrapping' an instance of the Adafruit Motor class. Since the Adafruit library does have a "release()" function, you can call it on that:

Code: Select all

myMotor->release();

User avatar
Benjamin269
 
Posts: 16
Joined: Fri Mar 13, 2015 10:56 am

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by Benjamin269 »

Does the Raspberry Pi B+ have the same i2c bus speed limitations? I am looking at the Stepper Hat:

https://www.adafruit.com/products/2348

for a different project and would like to know if I am going to run into the same limitations as the Adruino.

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

Re: Motor Shield V2.3 - 2 Independent Steppers?

Post by adafruit_support_bill »

Yes. "standard" speed for i2c is 100KHz. Fast speed is 400KHz. Some people have pushed it as far as 500KHz.

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

Return to “Arduino Shields from Adafruit”