Go Down

Topic: Problems with a stepper motor and the AccelStepper library (Read 457 times) previous topic - next topic

Ohmyduddy

Hi there!


 I'm trying to manipulate a stepper motor to create a displacement on an axis of a CNC machine.
In order to do that, I have an Arduino UNO, a stepper motor coming from an old CD-ROM (so I have no information on that motor) and a stepper driver (A4988).

For the wiring, it is as following:
-> MS1, MS2, MS3 on the D10, D11, D12 pin of the Arduino.
-> ENABLE pin isn't wired.
-> SLEEP pin and RESET pin are connected together.
-> DIRECTION is set to D8.
-> STEP is set to D9.
The rest is wired the same as the scheme in the A4988 datasheet.

My biggest issue here is that I don't know the characteristics of the motor, so I don't have the angular resolution or the number of steps per revolution.

Well, first, I tried to make the stepper motor works by using this code (library used) :
Code: [Select]
#include <Arduino.h>
#include "BasicStepperDriver.h"

// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200
#define RPM 30

// Since microstepping is set externally, make sure this matches the selected mode
// If it doesn't, the motor will move at a different RPM than chosen
// 1=full step, 2=half step etc.
#define MICROSTEPS 16

// All the wires needed for full functionality
#define DIR 8
#define STEP 9
//Uncomment line to use enable/disable functionality
//#define SLEEP 13

// 2-wire basic config, microstepping is hardwired on the driver
BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);

//Uncomment line to use enable/disable functionality
//BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP, SLEEP);

void setup() {
   stepper.begin(RPM, MICROSTEPS);
   // if using enable/disable on ENABLE pin (active LOW) instead of SLEEP uncomment next line
   // stepper.setEnableActiveState(LOW);
   digitalWrite(10, HIGH);
   digitalWrite(11, HIGH);
   digitalWrite(12, HIGH);
}

void loop() {
 
   // energize coils - the motor will hold position
   // stepper.enable();
 
   /*
    * Moving motor one full revolution using the degree notation
    */
   
   stepper.rotate(-20);

   // pause and allow the motor to be moved by hand
   stepper.disable();
   delay(2000);
}

Note that I put D10, D11, and D12 to high because I wanted a sixteenth step resolution (for a "smooth movement").
So with this code, everything is kind of fine, I create a "negative" displacement like I wanted. But, I can't stop the motor, I tried the stepper.stop() and stepper.disable() function but none of them worked.

So I thought of using the AccelStepper library, I downloaded it and try to launch an example :
Code: [Select]
// Blocking.pde
// -*- mode: C++ -*-
//
// Shows how to use the blocking call runToNewPosition
// Which sets a new target position and then waits until the stepper has
// achieved it.
//
// Copyright (C) 2009 Mike McCauley
// $Id: Blocking.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1,9,8); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{  
    stepper.setMaxSpeed(20.0);
    stepper.setAcceleration(20.0);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
}

void loop()
{    
    stepper.runToNewPosition(0);
    stepper.runToNewPosition(18);
}

But, the motor is not moving at all, it is just making small noises.
Any ideas why?

Best regards!

Robin2

My biggest issue here is that I don't know the characteristics of the motor, so I don't have the angular resolution or the number of steps per revolution.
Start with much simpler code - for example the first example in this Simple Stepper Code. Use it to explore the number of steps pre revolution by putting some sort of simple pointer on the motor shaft.

Also start with full steps and don't try microsteps until you are sure you know how many full steps there are in a revolution.

Use a VERY SLOW step speed in all of this.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Ohmyduddy

Thanks for your help!  :D

I will carefully follow your advice.

Just,  in the event that I succeed, do you have any idea about how to do the following instruction in this order :
- Go from x to x+n;
- Stop there until a certain condition is fulfilled (for example, another stepper goes from y to y+k);
- "Restart" then do the x+n to x.

I'm not asking for code here, I'm just wondering if you have an idea about the feasibility of what I'm trying to do, and if possible, how would you go for it (calling a function, a loop in a loop)...

I thought about it a little and here is what I obtained :
 
Code: [Select]

// Let's say we need 160 steps to go from x to x+n
void loop()
{
if(Stepper1.distanceToGo()==0){ //check if motor has already finished his last move
    Stepper1.move(160*dir); //set next movement to 1600 steps (if dir is -1 it will move -1600 -> opposite direction)
    dir = dir*(-1); //negate dir to make the next movement go in opposite direction
    // Here will be my condition for my 2nd stepper to move with surely a while loop like that
    while(stepper2 hasn't finished his task)
     {
        stepper1.stop();
        stepper2.run(k); //where k = number of step between y to y+k
     }     
  }
 
  Stepper1.run();
  Stepper2.run();
}
 

If you could just tell me if the way I'm taking is completely wrong, or if what I wrote has an enormous flaw, it would be super cool!

Best regards, and many thanks.

Robin2

Just,  in the event that I succeed, do you have any idea about how to do the following instruction in this order :
- Go from x to x+n;
- Stop there until a certain condition is fulfilled (for example, another stepper goes from y to y+k);
- "Restart" then do the x+n to x.
That should be perfectly feasible.

But think very carefully about your detailed project description. As it stands, what you have in the line "- "Restart" then do the x+n to x." will move the motor from x + n to x + 2n

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Ohmyduddy

Hello Robin2

Are you sure about x ->x+2n?

I mean the variable dir should, with each turn, change the sign of the value contained in stepper1.moveTo()... Tell me if I'm wrong!

Best regards!

EDIT: I was thinking about using moveTo instead of move, this way I don't have to put a stepper.stop(), I could let my other stepper do his job and then instruct the first one to move.


Robin2

Hello Robin2

Are you sure about x ->x+2n?

I mean the variable dir should, with each turn, change the sign of the value contained in stepper1.moveTo()... Tell me if I'm wrong!
The devil is in the detail.

What you say in your Reply #2 does not mention the motor moving back to X - hence my comment.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Ohmyduddy

Thanks for your answer!

Yes, I agree with you. In fact, it was me who was not very clear about what I said. Sorry for that, English is not my primary language.

Still, I succeeded in finding my steps per revolution, so I will move on to control both of my steppers at the same time.

I greatly appreciated your help and concern.

Best regards!

Go Up