Go Down

Topic: Stepper motors and drivers problem (Read 3 times) previous topic - next topic

standby

Thank you both of you!

Dear Carsten53T,
I have made the following wiring between arduino MEGA and the Drivers:

Digital Pin 2 -> PUL+ (driver 1)
Digital Pin 3 -> DIR+  (driver 1)
Digital Pin 4 -> ENA+ (driver 1)
Digital Pin 5 -> PUL+ (driver 2)
Digital Pin 6 -> DIR+  (driver 2)
Digital Pin 7 -> ENA+ (driver 2)
Digital Pin 8 -> PUL+ (driver 3)
Digital Pin 9 -> DIR+  (driver 3)
Digital Pin 10 -> ENA+ (driver 3)

And the PUL-, DIR- to the Arduino GND and ENA- unconnected.
All 3 motors are connected to A+B- and B+B- each coil.

Is there something wrong in wiring?
I have used digitalwrite directly (LOW->HIGH to start) but the speed is low and also I can't make them run in different speed each other. With accelstepper library the speed is tragically low.

Thank you!

Carsten53T

#6
Jun 08, 2012, 02:24 pm Last Edit: Jun 08, 2012, 02:35 pm by Carsten53T Reason: 1

Thank you both of you!

Dear Carsten53T,
I have made the following wiring between arduino MEGA and the Drivers:

Digital Pin 2 -> PUL+ (driver 1)
Digital Pin 3 -> DIR+  (driver 1)
Digital Pin 4 -> ENA+ (driver 1)
Digital Pin 5 -> PUL+ (driver 2)
Digital Pin 6 -> DIR+  (driver 2)
Digital Pin 7 -> ENA+ (driver 2)
Digital Pin 8 -> PUL+ (driver 3)
Digital Pin 9 -> DIR+  (driver 3)
Digital Pin 10 -> ENA+ (driver 3)

And the PUL-, DIR- to the Arduino GND and ENA- unconnected.
All 3 motors are connected to A+B- and B+B- each coil.

Is there something wrong in wiring?


It sounds reasonable. It runs, so it cann't me much off ;o)
.. maybe it's the unconnected ENA- that slows down the speed. I'm not that much of a specialist, but my impression is that EN takes time. Do you involve EN between each step or just at stepper-start and stepper-stop?

Quote

I have used digitalwrite directly (LOW->HIGH to start) but the speed is low and also I can't make them run in different speed each other. With accelstepper library the speed is tragically low.

Thank you!



About the speed .. if it works as my driver, you deside the 'delay' between each step. If it's short, the steppers steps fast. Notice below the inverse relation between velocity and 'delay' [it's not an Arduino delay(time), but rather time between two visits of cStepper::setTime()]

I'm not sure weather your driver takes care of the details of which pin to pull and what fase to choose, but I can make this code work for mine.

I've combined three steppers to work in unison ( in a cPen-class), but I'm retreating to have only 2 steppers in the pen. The last days it's fooled me and doesn't comply to my wishes. I'm trying to deal with a time discrepency when two motors does equal things .. and to code myself out of burning the drivers off (it happened once). My code is executing fine and I havent got a clue of why the steppers just buzzes instead of moves. The code is rather ugly and pretty lengthy with a lot of redundant code. I can pop part of the geometry-stuff that applies to two steppers that works on a plane if you'r interested.



#include <cStepper.h>

//-------------------------------------------------------------

const int x_EA = 41 ;
const int x_i2 = 39 ;
const int x_i1 = 38 ;  
const int x_EB = 40 ;
const int x_i4 = 37 ;
const int x_i3 = 36 ;

cStepper stpX( x_EA, x_EB, x_i1, x_i2, x_i3, x_i4 ) ;

const int y_EA = 33 ;
const int y_i2 = 31 ;
const int y_i1 = 30 ;  
const int y_EB = 32 ;
const int y_i4 = 29 ;
const int y_i3 = 28 ;

cStepper stpY( y_EA, y_EB, y_i1, y_i2, y_i3, y_i4 ) ;

const int z_EA = 49 ;
const int z_i2 = 47 ;
const int z_i1 = 46 ;
const int z_EB = 48 ;
const int z_i4 = 45 ;
const int z_i3 = 44 ;

cStepper stpZ( z_EA, z_EB, z_i1, z_i2, z_i3, z_i4 ) ;

...............

from class cStepper:

cStepper::cStepper( int EA , int EB , int i1 , int i2 , int i3 , int i4 )
{
   x_i4 = i4 ;
   x_i3 = i3 ;
   x_i2 = i2 ;
   x_i1 = i1 ;
   x_EA = EA ;
   x_EB = EB ;

     pinMode( EA  , OUTPUT ) ;
    pinMode( i2 , OUTPUT ) ;
    pinMode( i1 , OUTPUT ) ;  
   pinMode( EB , OUTPUT ) ;  
    pinMode( i4 , OUTPUT ) ;
    pinMode( i3 , OUTPUT ) ;

   deEnable() ;
   Stop() ;

   position_x = 0.0f ;
   fase = 0 ;
     mVelocity = 0.0f ; // mVelocity and mDelay  are inverse
   mDelay = 1000 ; //unsigned long
    mMinDelay = 2 ; //in millis. test this out!  unsigned long
   mMaxVelocity = 500.0f ; // in steps pr sec (1000 millis )
   oldTime = 0 ;
   mDirection = true ;
   misOn = false ;
     }// end constructor

.... and the most troublesome member: The one that is 'in the loop' and 'listens' for the time ticking:

//-------------------------------------------------------
   void cStepper::setTime(){
      if ( cStepper::misOn ){
         if(  ( (oldTime + mDelay) < millis()) ){
            cStepper::Step() ;
            cStepper::oldTime = millis() ;
         }
      }//
      
   } // end cStepper::setTime

some other members members:

   //velocity in steps pr sec (1000 millis )
   boolean cStepper::setVelocity( float velocity ){
      mVelocity = velocity ;  
      boolean res = false ;
      if( mVelocity < mMaxVelocity ){
         res = true ;
         if(  0 < mVelocity ){   mDelay = long( 1000/mVelocity) ; }
         }
      else{ mDelay = mMinDelay ; }
      return res ;
   }
   //----------------------------------

//-----------------
   void cStepper::Step()// one step at a time!
   {      
       if( cStepper::mDirection == true ){ cStepper::fase  +=1 ; cStepper::position_x += 1 ; }
       else{ cStepper::fase  -=1 ; cStepper::position_x -= 1 ; }

       if( 3 < cStepper::fase ){ cStepper::fase  = 0 ; }
       if( cStepper::fase <0 ){ cStepper::fase  = 3 ; }

       switch( cStepper::fase )
         {
          case 0:
               cStepper::x1_faseOne() ;
               break ;      
          case 1:
               cStepper::x2_faseOne() ;
               break ;      
          case 2:
               cStepper::x1_faseTwo() ;
               break ;
          case 3:
               cStepper::x2_faseTwo() ;
               break ;                
          }// end switch
      }// end Step
//-------------------------------
      void cStepper::x1_faseOne()
      {
          digitalWrite( x_i4 , LOW ) ;
          digitalWrite( x_i3 , LOW ) ;
          digitalWrite( x_i1 , LOW ) ;
          digitalWrite( x_i2 , HIGH ) ;  
      }//A_faseOne
//-------------------------------
      void cStepper::x1_faseTwo()
      {
          digitalWrite( x_i4 , LOW ) ;
          digitalWrite( x_i3 , LOW ) ;
          digitalWrite( x_i2 , LOW ) ;
          digitalWrite( x_i1 ,HIGH ) ;
      }//A_faseTwo
//------------------------------
      void cStepper::x2_faseOne()
      {
          digitalWrite( x_i2 , LOW ) ;
          digitalWrite( x_i1 , LOW ) ;
          digitalWrite( x_i3 , LOW ) ;
          digitalWrite( x_i4 , HIGH ) ;
      }//B_faseOne
//------------------------------
      void cStepper::x2_faseTwo()
      {
          digitalWrite( x_i2 , LOW ) ;
          digitalWrite( x_i1 , LOW ) ;
          digitalWrite( x_i4 , LOW ) ;
          digitalWrite( x_i3 , HIGH ) ;
      }
//------------------------------      
      void cStepper::Enable(){
          analogWrite( x_EA , 255 ) ;
         delay(100) ;
          analogWrite( x_EB , 255 ) ;
         delay(100) ;
         cStepper::misOn  = true ;
      }
      void cStepper::deEnable(){
          analogWrite( x_EA , 0 ) ;
          analogWrite( x_EB , 0 ) ;
         cStepper::mVelocity = 0.0f ;
         cStepper::misOn  = false ;
      }

      void cStepper::Stop(){
          digitalWrite( x_i1 , LOW ) ;
          digitalWrite( x_i2 , LOW ) ;
          digitalWrite( x_i3 , LOW ) ;
          digitalWrite( x_i4 , LOW ) ;
         cStepper::mVelocity = 0.0f ;
      }

//--------------------------
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

don't take it granted that this will work exactly (I've got too menĂ½ versions of the code ;o/) :

   //velocity in steps pr sec (1000 millis )
   boolean cStepper::changeSpeed( float change ){
      boolean res = true ;
      mVelocity += change ; 
      if( abs(mVelocity) < mMaxVelocity ){          
          if( 2 < mVelocity ){   
            mDelay = long( 1000/mVelocity) ;
            res = false ;
            mDirection = true ;
         }
         else if( mVelocity < -2 ){
            mDelay = long( 1000/mVelocity) ;
            res = false ;
            mDirection = false ;
         }
         else{  }
      }
      else{
         mDelay = mMinDelay ;
         if( 2 < mVelocity ){ mDirection = true ; }
         if( mVelocity < -2 ){ mDirection = false ; }
         res = false ;
      }
      return res ;
   }// cStepper::changeSpeed
//------------------------------------------------------- ~100


Take note that, if you run two motors in unison and one of them is waiting while it 'works' .. it will likely burn off the driver. Ask in if you need details.

standby

Thank you for your suggestions.
As I am not an expert in programming and I am a little confused, is there someone that can make some changes to my sketch-code so I can run my 3 steppers in different variable speeds?? :~

I also note that when I am running 1 stepper the speed is higher that the overall speed when I am running 3 steppers. Is there a problem with my code or it is just the processor limit that affects the overall speed?


Here is my simple sketch:


int stepM1pin = 2;    // motor 1 step pin
int dirM1pin = 5;     // motor 1 dir pin
int stepM2pin = 3;    // motor 2 step pin
int dirM2pin = 6;     // motor 2 dir pin
int stepM3pin = 4;    // motor 3 step pin
int dirM3pin = 7;     // motor 3 dir pin

int Steps = 10;       // define steps
int Delay = 8;        // define delay microseconds
int i;


void setup() {

pinMode(stepM1pin, OUTPUT);    // motor 1 step pin as output
pinMode(dirM1pin, OUTPUT);     // motor 1 dir pin as output
pinMode(stepM2pin, OUTPUT);    // motor 2 step pin as output
pinMode(dirM2pin, OUTPUT);     // motor 2 dir pin as output
pinMode(stepM3pin, OUTPUT);    // motor 3 step pin as output
pinMode(dirM3pin, OUTPUT);     // motor 3 dir pin as output

}


void loop(){

    digitalWrite(dirM1pin, LOW);    // Change direction of 1st motor.
    digitalWrite(dirM2pin, HIGH);   // Change direction of 2nd motor.
    digitalWrite(dirM3pin, LOW);    // Change direction of 3rd motor.
   
   
    for(i=0; i<Steps; i++){            // motor 1 run
      digitalWrite(stepM1pin, LOW); 
      digitalWrite(stepM1pin, HIGH);
    }
   
    for(i=0; i<Steps; i++){            // motor 2 run
      digitalWrite(stepM2pin, LOW); 
      digitalWrite(stepM2pin, HIGH);
    }

    for(i=0; i<Steps; i++){            // motor 3 run
      digitalWrite(stepM3pin, LOW); 
      digitalWrite(stepM3pin, HIGH);
      delayMicroseconds(Delay);        // ????? This delay affects all the motors so I can't
                                                 // make it run in different speed...
    }
}



I appreciate your help! :smiley-sweat:
Thank you!!!

Carsten53T

stanby,
If you havn't any coding-experience then you'r far from your goal. You should find some sources that focus on general programming and take your time to delve into it.

The scetch has a Setup() and a Loop() function that is guaranteed to be called on program execution ... the Loop() being repeated over and over.
You need to write your own functions .. and then call them from the Loop() when appropriate.

If you havn't employed any buttons or serial() read/write  you'll have to stiffen up on boolean controle-structures to 'controle' what happens. You can create time-dependant events in code .. as a minimum you need to call your motors steps for each motor and for each step. You test a variable (say millis()+20000 ) in the loop() to see if the 20000 milli seconds has passed. If it has, you execute (call) the particulare function say  'void Step3()' written outside the Loop().
It is possible to write all your code in the Loop(), but you'r bound to loose track of what happens.

You can call a function 'Step2( long aDelay )' with an argument ( aDelay ) that will be available in the body of the function. That way you decide at the time of calling, how fast it will go.

Take an early focus on disbanding the Delay(millis) function and substitute it with the event-thing I wrote above. The Delay() function simply stops the code and waits. Tat way you can only drive one motor at a time.

Good Luck

standby

Thank you for your reply!

Basically I understand what you mean, the project is very simple and based on 3 steppers that, after power ON of the machine, will start running at the same time.
The motor 1 & 2 at the maximum possible speed and the 3rd motor must run in lower speed so I need some help to change the code I made for the speed of the 3rd motor.

I didn't use delays for the motors 1&2 because all the motors will run at the same time and for these I need the max speed.
The change is that the 3rd motor must run at lower speed and when I use delayMicroseconds() in 3rd motors for() it makes all motors speed lower.

Could you give me some help for my code please? A piece of code or my code modified is better for me.

Thank you.

Go Up