Go Down

Topic: [Solved]Accelstepper library and Multiple stepper motors (Read 220 times) previous topic - next topic

Daeir

Hi all,

So I am working on a project right now that involves controlling 9 stepper motors at the same time. For this I am using the Accelstepper library and an Arduino mega. My code now works by using the Accelstepper library and giving commands separately to each stepper. So when I want to move my 9 steppers to a position I would say something like

Code: [Select]

stepper1.moveTo(position1);
stepper2.moveTo(position2);
stepper3.moveTo(position3);

stepper1.run();
stepper2.run();
stepper3.run();

 

And this works no problem however with 9 steppers a big part of my code already consists of repetitions. Let alone would I want to expand and control maybe 100 steppers with it. Usually I use arrays and for loops to solve this.
I would stick all steppers in an array, then I would iterate trough them using a for loop and save me a lot of space. Thinking I can use the same method with the accelstepper library I wrote something like this.

Code: [Select]

//Stick all the steppers in an array
AccelStepper stepperArray[stepperAmount] {
    
    stepper1,
    stepper2,
    stepper3,
}

for(int i = 0; i < stepperAmount; i++){
    
    stepperArray[i].moveTo(position);
}

for(int i = 0; i < stepperAmount; i++){
    
    stepperArray[i]run();
}


However, no dice. the code will compile but the steppers won't react to it. I found the Multistepper functions that solves this isuse with moving the steppers. But now I would like to use some functions only found in the Accelstepper part of the library. For example the setCurrentPosition or the currentPosition( for calibrating and using a feedback loop for correcting positions). I expect for example to receive a position of a motor(eg 512 or something) because I just set the motor to go to that position. But I get returned a 0.

I get the feeling the code compiles just fine but the steppers I put in the stepperArray are not the same I declare at the beginning of my code. Thus my steppers wont react to my code. Has anyone had more experience using the Accelstepper library and multiple stepper motors at the same time? Especially using for loops or a similar construction to avoid having to all all my steppers separately.

Regards!

Daeir

Update:

So after trying all kinds of crazy constructions I decided to look into the Multistepper library and discovered the solution to my problem.

Instead of directly placing the Accelstepper objects into an arrayI had to place pointers to those objects into an array.
I tested it and it works like a charm.
This is what I did

Code: [Select]


//First I declare the AccelStepper objects

const int stepperAmount = 2;

AccelStepper stepper1 (AccelStepper::FULL4WIRE,5,6,7,8);
AccelStepper stepper2 (AccelStepper::FULL4WIRE,9,10,11,12);

//Then I stick pointers to those AccelStepper objects into an Array

AccelStepper* steppers[stepperAmount] ={
   
    &stepper1,
    &stepper2,
};

void setup(){

    //now i can just make a for loop and iterate through the steppers array and call whatever function I want
    //Note: you have to use -> to call that function instead of a .

    for(int stepperNumber = 0; stepperNumber < stepperAmount; stepperNumber++){

        steppers[stepperNumber]->stetMaxSpeed(SPEED);
        steppers[stepperNumber]->setAcceleration(ACCELARATION);
    }
}

void loop(){

    //Same goes for setting postions and running the steppers

    for(int stepperNumber = 0; stepperNumber < stepperAmount; stepperNumber++){

        steppers[stepperNumber]->moveTo(positionArray[stepperNumber]);

    }

    for(int stepperNumber = 0; stepperNumber < stepperAmount; stepperNumber++){

        steppers[stepperNumber]->run();

    }
}


Now I can just add 9 more or even 50 more steppers by just altering a few lines of code, which makes me very happy :)
Anyways my problem is solved for now. I thought I'll just post this in case someone else runs into this problem and can't find a solution.

rpt007

Thank you for sharing your solution; might be that I can use that in the future.
You deserved a Karma point ;-) from me.
Before you ask:
Did you really read and understand "How to use this forum" ?
AND:
Do you have already some solution or is a part of the problem sitting in front of the screen?  :)

Matbot

Now I can just add 9 more or even 50 more steppers by just altering a few lines of code, which makes me very happy :)
Anyways my problem is solved for now. I thought I'll just post this in case someone else runs into this problem and can't find a solution.
I'm trying to understand your problem and the solution, it could help me or to be benefic in my project ! But I'm not good in code and english isn't my first language :) ...

However, how could you control 50 steppers with an arduino mega, whereas your code seems to use 4 wires/stepper so 50x4= 200 which is really over the pinout number arduino mega ability ?


thank's

Daeir

You are right, with the default mega you cannot use that many pinouts.
I was talking about a hypothetical number of steppers, code wise, this is possible.

Also there are a lot of ways to increase the number of pinouts on your arduino. Daisy chaining arduino's or using shift registers. You would have to alter your code a bit though. For more information on that topic, you can look at this topic on the forums. It may help you further.

http://forum.arduino.cc/index.php?topic=173357.0

rpt007

You can cut the number of required wires by half:
Just use drivers which come with dir/step connection, so you only need 2 wires/stepper
Before you ask:
Did you really read and understand "How to use this forum" ?
AND:
Do you have already some solution or is a part of the problem sitting in front of the screen?  :)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy