Acceleration and deceleration of stepper motor

I am using 3 stepper motors with DM542 driver to build a pick and place robot using Nucleo-F746ZG. For the movement of stepper motor I am using this library :-

#include "stepperMotor.h"

#include "mbed.h"

int motorSpeed; // Steper speed

sMotor::sMotor(PinName A0, PinName A1, PinName A2, PinName A3) : _A0(A0), _A1(A1), _A2(A2), _A3(A3) { // Defenition of motor pins
    _A0=0;
    _A1=0;
    _A2=0;
    _A3=0;
}


void sMotor::anticlockwise() { // rotate the motor 1 step anticlockwise 
     
    
     for (int i = 0; i < 8; i++) {

        switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
            case 0: {
                _A0=0;
                _A1=0;
                _A2=1;
                _A3=0;
            }
            break;
            case 1: {
           _A0=0;
         _A1=0;
         _A2=0;
       _A3=0;
            }
           break;
            case 2: {
              _A0=0;
             _A1=0;
             _A2=0;
            _A3=1;
            }
     break;
         case 3: {
               _A0=0;
                _A1=1;
                _A2=0;
               _A3=1;
           }
            break;
            case 4: {
                _A0=0;
                _A1=1;
                _A2=1;
               _A3=1;
            }
            break;
        case 5: {
                _A0=1;
                _A1=1;
             _A2=1;
                _A3=1;
            }
            break;
            case 6: {
                _A0=1;
                _A1=0;
                _A2=1;
                _A3=1;
            }
            break;
            case 7: {
                _A0=1;
                _A1=0;
                _A2=1;
               _A3=0;
            }
            break;
        }


        wait_us(motorSpeed); // wait time defines the speed 
    }
}

void sMotor::clockwise() { // rotate the motor 1 step clockwise 
    for (int i = 7; i >= 0; i--) {
                
        switch (i) {
         case 0: {
                _A0=0;
                _A1=0;
                _A2=0;
                _A3=1;
          }
            break;
          case 1: {
                _A0=0;
             _A1=0;
              _A2=1;
                _A3=1;
            }
            break;
         case 2: {
                _A0=0;
                _A1=0;
                _A2=1;
                _A3=0;
           }
            break;
            case 3: {
                _A0=0;
                _A1=1;
                _A2=1;
                 _A3=0;
         }
           break;
            case 4: {
                _A0=0;
                _A1=1;
                _A2=0;
                _A3=0;
            }
           break;
           case 5: {
                _A0=1;
                _A1=1;
               _A2=0;
               _A3=0;
         }
            break;
            case 6: {
                _A0=1;
                  _A1=0;
                   _A2=0;
                 _A3=0;
            }
            break;
         case 7: {
                _A0=1;
                _A1=0;
                _A2=0;
              _A3=1;
            }
            break;
        }


        wait_us(motorSpeed); // wait time defines the speed 
    }
}
void sMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200)
    int count=0; // initalize step count
    motorSpeed=speed; //set motor speed
    if (direction==0) // turn clockwise
        do {
            clockwise();
            count++;
        } while (count<num_steps); // turn number of steps applied 
        
    else if (direction==1)// turn anticlockwise
    { 
        count=0;
        do {
            anticlockwise();
            count++;
 
         } while (count<num_steps);// turn number of steps applied 
   }
}

Now the problem is the arm moves with jerks in start and end (inertia) and I want to smooth the movement. For that I want to acceleration while the motor starts and deceleration when the motor stops. Kindly help me with this problem to make stepper motor move smoothly.

Hello,

I am not sure but mabe try change speed value. Something like

void sMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200)
    int count=0; // initalize step count
    if (direction==0) // turn clockwise
        do {
            if(count < 5 || count > (num_steps-6)) motorSpeed = (speed/2)
            else motorSpeed = speed;
            clockwise();
            count++;
        } while (count<num_steps); // turn number of steps applied 
        
    else if (direction==1)// turn anticlockwise
    { 
        count=0;
        do {
            if(count < 5 || count > (num_steps-6)) motorSpeed = (speed/2)
            else motorSpeed = speed;
            anticlockwise();
            count++;
 
         } while (count<num_steps);// turn number of steps applied 
   }
}

BR, Jan

@JohnnyK I tried different methods to change the speed (delay of stepper motor) but it is not working.
I am trying to get the motion as shown in this video Pick and place robot

@JohnnyK After some more research I found that this smooth motion is possible by S-curve motion. Is there any way to get this s-curve motion.

Hello,

well I understand what you want and does not matter how this is named. I do not play with a stepper for a while so I am not sure but I belive it is about a timing.

If you are able to change speed of the motor then you just need to do in you move function.
You need to a normal and initial speed. Then in your move function you need start from and initial speed and increes it with every step up to normal speed. And same thing vise wersa.

BR, Jan

I Finally got the library that helped me in smoothing the motion. Check out this Stepper with Smooth Motion

1 Like