0

I am bhanuka and i am new to Stepper motors and to this library.

I am trying to control 4 NEMA 17 motors ( Different speeds in same time ) using 4 (four) DM320t stepper driver. Actually i want to move the robot ( Robot has omni wheels) to coordinates according to the Gcode. It seem like code is ok, but when i enter a G-code, motors start to rotate to correct direction but they don't give smooth rotation ( I can hear clicking sound , motor seems jumping from step to step and motor get hot too much ).

I think the problem for this case is motor miss some steps. here i have attach the code. Can someone help me to overcome this problem. Thank you.

#include <AccelStepper.h>
#include <math.h>

int Gval;
int Xval, Yval, Zval;
int Fval;
int Ival, Jval;
String Gvalstr, Xvalstr, Yvalstr, Zvalstr, Fvalstr, Ivalstr, Jvalstr;
char Key;
String code; // parameters of G code Translator, It will execute the code
String ab; // Para of G code translator, For come to Gcode function

AccelStepper w1(1, 2, 3); // pin 3 = step, pin 6 = direction
AccelStepper w2(1, 49, 51); // pin 4 = step, pin 7 = direction
AccelStepper w3(1, 5, 6); // pin 3 = step, pin 6 = direction
AccelStepper w4(1, 8, 9); // pin 4 = step, pin 7 = direction

int w1_enb = 4;
int w2_enb = 53;
int w3_enb = 7;
int w4_enb = 10;
char x ; // x cordinate
char y ; // x cordinate
char command; // case vari.
char com;



String inString = "";


void setup() {
  digitalWrite(w1_enb, HIGH);
  digitalWrite(w2_enb, HIGH);
  digitalWrite(w3_enb, HIGH);
  digitalWrite(w4_enb, HIGH);

  Serial.begin(9600);
  Serial.println("G code");

}

void loop() {
  Gvalstr = "";
  Xvalstr = "";
  Yvalstr = "";
  Zvalstr = "";
  Fvalstr = "";
  Ivalstr = "";
  Jvalstr = "";

  String code;
  if (Serial.available() > 0 ) {
    ab = Serial.readString();
    Gcode();
  }
  else {}

}

void Gcode () {
  code = ab;
  Serial.println("code is " + ab);
  //Serial.println(code);
  //String code = "G01 X1 Y1 F200 T01 M03 S500";
  int len = code.length();
  for (int i = 0; i < len; i++) {

    if (code[i] == 'G')Key = 'G';
    else if (code[i] == 'X')Key = 'X';
    else if (code[i] == 'Y')Key = 'Y';
    else if (code[i] == 'Z')Key = 'Z';
    else if (code[i] == 'F')Key = 'F';
    else if (code[i] == 'i')Key = 'i';
    else if (code[i] == 'j')Key = 'j';
    else if (isdigit(code[i]) || code[i] == '-') {
      if (Key == 'G') {
        Gvalstr += code[i];
      }
      else if (Key == 'X') {
        Xvalstr += code[i];
      }
      else if (Key == 'Y') {
        Yvalstr += code[i];
      }
      else if (Key == 'Z') {
        Zvalstr += code[i];
      }
      else if (Key == 'F') {
        Fvalstr += code[i];
      }
      else if (Key == 'i') {
        Ivalstr += code[i];
      }
      else if (Key == 'j') {
        Jvalstr += code[i];
      }

    }
    else
    {
      Key = '-1';
    }

  }


  Gval = Gvalstr.toInt();
  Xval = Xvalstr.toInt();
  Yval = Yvalstr.toInt();
  Fval = Fvalstr.toInt();


  Serial.println( Gval);
  Serial.println (Xval);
  Serial.println (Yval);
  Serial.println (Fval);



  switch (Gval) {

    case 0:
      motor_setup(Xval, Yval);
      //Serial.println("**********");
      break;
    case 1:
      motor_setup(Xval, Yval);
      //Serial.println("**-------");
      break;

    case 2:
      motor_setup(Xval, Yval);
      break;

    case 3:
      motor_setup(Xval, Yval);
      break;

    case 90:
      motor_setup(Xval, Yval);
      break;

    case 91:
      motor_setup(Xval, Yval);
      break;

    case 92:
      motor_setup(Xval, Yval);
      break;

  }
}


void motor_setup (int Xval , int Yval) {

  double Xprojection;
  double Yprojection;

  if (Yval == 0 ) {

    if (Xval > 0) {
      Xprojection = '1';
    }

    else if (Xval == 0) {
      Xprojection = 0;

    }

    else {

      Xprojection = -1;
    }
    Serial.println("Xprojection in if ");
    Serial.println(Xprojection);

    Yprojection = 0;

  }
  else {
    float alpha = Yval / Xval;
    Serial.println("CALCULATING");
    Serial.println(alpha);

    double atanval = atan(alpha);
    Xprojection = cos(atanval);
    Yprojection = sin(atanval);

    Serial.println("X_projection : ");
    Serial.println(Xprojection);

    Serial.println("Y_projection : ");
    Serial.println(Yprojection);
  }


  float W1, W2, W3, W4;  // Radial Velocity required to move to Position

  W1 = float ( -0.35 * Xprojection + 0.35 * Yprojection) ;   
  W2 = float (-0.35 * Xprojection - 0.35 * Yprojection );
  W3 = float ( 0.35 * Xprojection - 0.35 * Yprojection );
  W4 = float (0.35 * Xprojection + 0.35 * Yprojection );

  Serial.println ("Velocity components ");
  Serial.println (W1);
  Serial.println (W2);
  Serial.println (W3);
  Serial.println (W4);
  Serial.println('\n');

  long c = 20;    // these values for testing 
  long d = 20;
  long e = 20;
  long f = 20;

  w1.setMaxSpeed(2);
  w1.setAcceleration(12);
  w1.moveTo( Xval );

  w2.setMaxSpeed(2);
  w2.setAcceleration(12);
  w2.moveTo( Yval);

  w3.setMaxSpeed(2);
  w3.setAcceleration(12);
  w3.moveTo( -Xval);

  w4.setMaxSpeed(2);
  w4.setAcceleration(12);
  w4.moveTo( -Yval );

  while (w1.distanceToGo() != 0 || w2.distanceToGo() != 0 || w3.distanceToGo() != 0 || w4.distanceToGo() != 0) {
    w1.run();
    w2.run();
    w3.run();
    w4.run();





  }

  Serial.println("Executed");
  Serial.println("Next GCODE");
  Serial.flush();



}

  • 2
    reduce your code to control only one motor ... remove all other code ... run motor for 2 seconds and stop ... update your post with the results – jsotola Jan 9 at 17:13
0

The problem was the Supply Voltage. I have connected it to 24DC V. when i connect it to 12V problem was solved.

| improve this answer | |
  • It seems that you see too high current at the driver. You should always use minimum current, that smoothly moves your device. Try decreasing the current, it then will work at lower voltages – Zhenek Jan 12 at 19:01
  • It's OK to accept you're own answer as the solution to the problem. – VE7JRO Feb 9 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.