Help with sketch to drive 2 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
GRNDPNDR
 
Posts: 11
Joined: Fri Oct 05, 2012 8:10 pm

Help with sketch to drive 2 steppers.

Post by GRNDPNDR »

I just finished assembling my Adafruit motor driver shield and tested out some example code to drive 2x reduction stepper motors (5V).

Now here's where I need some help, I'm not much of a programmer but I need to drive these motors in a specific way and don't know how.

The two motors will be operating at different speeds, but still EXTREMELY slowly.

Motor #1 will take X number of steps every X number of secs (actual value not determined yet) and will only rotate to a certain spot. I believe it needs to rotate 338 degrees.
Once it's reached it's "stop point" it will wait for a few secs then begin rotating the opposite direction.

The second motor will make 1/4 rotation forward when the first motor reaches it's stop point, and when the first motor starts going backwards the second motor will make another 1/4 rotation forward then will start moving backwards to it's original position OR just keep going forwards to it's original position.

I know this sounds more confusing than it really is. I'll try and make a video using our solidworks model to show what the motors need to do.

GRNDPNDR
 
Posts: 11
Joined: Fri Oct 05, 2012 8:10 pm

Re: Help with sketch to drive 2 steppers.

Post by GRNDPNDR »

Ok here is a video.

I did actually see it working once, but it keeps showing me a black screen so if you cant see the video keep trying to refresh or I can send you the video.

I cant attach it because it's slighty over 1MB.

http://www.youtube.com/watch?v=qKkMR6Tp ... e=youtu.be

GRNDPNDR
 
Posts: 11
Joined: Fri Oct 05, 2012 8:10 pm

Re: Help with sketch to drive 2 steppers.

Post by GRNDPNDR »

I can compress it with BANNED. So if you have that here is the video.
Attachments
Trap Master Assembly.rar
(441.37 KiB) Downloaded 49 times

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

Re: Help with sketch to drive 2 steppers.

Post by adafruit_support_bill »

The first thing you need to do is figure out how many steps there are in each move. To do that you need to know the number of degrees per step for your motor.

Making sequential moves is simple using the AF_Motor Library. To move two motors at the same time, you can either interleave the steps using AF_Motor's OneStep() function. Or you can use the Adafruit version of AccelStepper which will do concurrent moves.

I'd suggest getting familiar with all the examples in the library first.

GRNDPNDR
 
Posts: 11
Joined: Fri Oct 05, 2012 8:10 pm

Re: Help with sketch to drive 2 steppers.

Post by GRNDPNDR »

I've loaded the one example, and it has one motor turning 10,000 steps before stopping and another that reverses direction.

The motors I'm using have 48 steps, but are gearing motors so 48*16 = 768 steps.

With 48 steps there is 7.5 degrees between each step (not sure how the gearing affects the angles)

one motor has to rotate 338 degrees forward, then 338 degrees backwards.

The second motor will rotate 90 degrees, then another 90 degrees, then 180 degrees.

So how can I make the motors do this? I'm on a timeline and don't really have a bunch of time to sit here and tinker with it while trying to learn a programming language and what all this can do, not only that this isn't for hobby and I have to find time in between doing hours of homework every night to get this done on time.

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

Re: Help with sketch to drive 2 steppers.

Post by adafruit_support_bill »

With 48 steps there is 7.5 degrees between each step (not sure how the gearing affects the angles)
You divide the per-step angle by the reduction ratio.
So how can I make the motors do this?
Set the speed you want to move (in rpm) using motor.setSpeed().
Calculate the number of steps to move.
Then call motor.step() with the number of steps.

GRNDPNDR
 
Posts: 11
Joined: Fri Oct 05, 2012 8:10 pm

Re: Help with sketch to drive 2 steppers.

Post by GRNDPNDR »

I'm a little confused, and I'm basically just playing with the numbers in the following code since I don't really understand it too much.

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(768, 1);
AF_Stepper motor2(768, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {  
  motor1.onestep(FORWARD, DOUBLE);
}
void backwardstep1() {  
  motor1.onestep(BACKWARD, DOUBLE);
}
// 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(20.0);
    stepper1.moveTo(256);
    
    stepper2.setMaxSpeed(200.0);
    stepper2.setAcceleration(45.0);
    stepper2.moveTo(150);
    
}

void loop()
{
    // Change direction at the limits
    if (stepper1.distanceToGo() == 0)
	stepper1.moveTo(-stepper1.currentPosition());
    if (stepper2.distanceToGo() == 0)
	stepper2.moveTo(-stepper1.currentPosition());
    stepper1.run();
    stepper2.run();
}
I know that motor 1 needs to move 338 degrees, then delay for 3 secs, and move 338 degree backwards.
During the 3 secs, motor number 2 needs to rotate 90 degrees forward, then hold for X seconds before rotating 90 degrees backward, hold for 1 second, then move another 90 degrees backwards. It's basically moving back and forth 180 degrees but holding every 90 degrees.

Problem is I don't know how to translate these movements into code. Also is there anyway to index the motors so they always have an initial start position?

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

Re: Help with sketch to drive 2 steppers.

Post by adafruit_support_bill »

Problem is I don't know how to translate these movements into code.
Have you calculated the number of steps in each move? First you want to get all the individual motor movements working. Then you will need to construct a state machine to put all the moves together in sequence.
Also is there anyway to index the motors so they always have an initial start position?
Typically, this is done with a limit-switch or an optical slot-detector.

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

Return to “Arduino Shields from Adafruit”