Unknown issue in tmc2209 drive code

I made a code for tmc2209, and I made a function.
However, the M_run() function malfunctions.
I definitely put a different value in the M_speeds value, but the motor works at the same speed.
There seems to be no problem with the code, but is it related to the motor?


void loop(){
  //bottom control
  M_run(step_B, dir_B, speeds_B, step_B_count, rounds_B);
  
  //top control
  M_run(step_T1, dir_T1, speeds_T, step_T1_count, rounds_T);
  M_run(step_T2, dir_T2, speeds_T, step_T2_count, rounds_T);
  M_run(step_T3, dir_T3, speeds_T, step_T3_count, rounds_T);
}

In the above code, the speed of bottom and top should be different.
I gave you a different value, but when you drive it, it works at the same speed.

Currently, I am using NEMA17 StepperMotor, and the motor driver uses tmc2209.
The power supply also uses 24V.


Full code

int en_B =  2;
int step_B = 3;
int dir_B = 4;

int en_T1 = 5;
int step_T1 = 6;
int dir_T1 = 7;

int en_T2 = 8;
int step_T2 = 9;
int dir_T2 = 10;

int en_T3 = 11;
int step_T3 = 12;
int dir_T3 = 13;

int switch_1 = 22;
int switch_2 = 24;
int switch_3 = 26;

//모터 속도 단위 마이크로초
int speeds_B = 10000;
int speeds_T = 100;
int spedds_T_setup = 100;

int rounds_B = 7200;
int rounds_T = 7200;

//1step_count
int step_B_count = 0;
int step_T1_count = 0;
int step_T2_count = 0;
int step_T3_count = 0;

//function
//setup top motor postion
void motorTopSetup(int sw, int M_step);

//setting pinMode bottom & top
void setting(int M_en, int M_step, int M_dir);
void setting(int M_en, int M_step, int M_dir, int sw);

//move to motor -> steppin, dirpin, motor speed, 1step check, count sppin
void M_run(int M_step, int M_dir, int M_speeds, int M_step_count, int M_rounds);

void setup(){
  //bottom setting
  setting(en_B, step_B, dir_B);
  //top setting
  setting(en_T1, step_T1, dir_T1, switch_1);
  setting(en_T2, step_T2, dir_T2, switch_2);
  setting(en_T3, step_T3, dir_T3, switch_3);

  //top motor setup
  while(1){
    motorTopSetup(switch_1, step_T2);
    motorTopSetup(switch_2, step_T3);
    motorTopSetup(switch_3, step_T1);

    if(digitalRead(switch_3) == LOW &&digitalRead(switch_1) == LOW && digitalRead(switch_2) == LOW){
      break;
    }
  }

  delay(1000);
}

void loop(){
  //bottom control
  M_run(step_B, dir_B, speeds_B, step_B_count, rounds_B);
  
  //top control
  M_run(step_T1, dir_T1, speeds_T, step_T1_count, rounds_T);
  M_run(step_T2, dir_T2, speeds_T, step_T2_count, rounds_T);
  M_run(step_T3, dir_T3, speeds_T, step_T3_count, rounds_T);
}

void motorTopSetup(int sw, int M_step){
  if(digitalRead(sw) == HIGH){
    digitalWrite(M_step, HIGH);
    delayMicroseconds(spedds_T_setup);
    digitalWrite(M_step, LOW);
    delayMicroseconds(spedds_T_setup);
  }
}

void setting(int M_en, int M_step, int M_dir){
  pinMode(M_en, OUTPUT);
  pinMode(M_step, OUTPUT);
  pinMode(M_dir, OUTPUT);

  digitalWrite(M_en, LOW);
  digitalWrite(M_dir, LOW);
}

void setting(int M_en, int M_step, int M_dir, int sw){
  pinMode(M_en, OUTPUT);
  pinMode(M_step, OUTPUT);
  pinMode(M_dir, OUTPUT);

  pinMode(sw, INPUT_PULLUP);

  digitalWrite(M_en, LOW);
  digitalWrite(M_dir, LOW);
}

void M_run(int M_step, int M_dir, int M_speeds, int M_step_count, int M_rounds){
  if(M_step_count == M_rounds){
    if(digitalRead(M_dir) == HIGH){
       digitalWrite(M_dir, LOW);
       M_step_count = 0;
    }else{
       digitalWrite(M_dir, HIGH);
       M_step_count = 0;
    }
  }else if(M_step_count == 0){
    digitalWrite(M_dir, HIGH);
  }
  
  digitalWrite(M_step, HIGH);
  delayMicroseconds(M_speeds);
  digitalWrite(M_step, LOW);
  delayMicroseconds(M_speeds);

  M_step_count++;
}

Please help me..

Use Serial.print inside the code to tell key data, and serial monitor to show them.

  M_run(step_B, dir_B, speeds_B, step_B_count, rounds_B);
  Serial.println("bottom run finish");
  
  //top control
  M_run(step_T1, dir_T1, speeds_T, step_T1_count, rounds_T);
  Serial.println("top1 run finish");
  M_run(step_T2, dir_T2, speeds_T, step_T2_count, rounds_T);
  Serial.println("top2 run finish");
  M_run(step_T3, dir_T3, speeds_T, step_T3_count, rounds_T);
  Serial.println("top3 run finish");
  digitalWrite(M_step, HIGH);
  delayMicroseconds(M_speeds);
  Serial.println(M_speeds);
  digitalWrite(M_step, LOW);
  delayMicroseconds(M_speeds);.

I put serial.print in the loop() statement and M_run() function, which are the parts I need to solve.

The output is as follows.

It prints out as well as I expected.
But when you actually run it again, the top's M_speed is the same speed as the bottom.

Good. Looks correct.
I suggest doing the same in the function void motorTopSetup(int sw, int M_step)

I don't know if I understood correctly.
Is your opinion to change M_run() to function like motorTopSetup()??

Please understand that I use a translator because my English is not good...
..

Looks good so far.

Reply: No, no, no.

Install Serial.print in that function, similar to the previous debug print!

Is it the high speed or the low speed You actually experience?

It works at low speeds.
In code, speeds_B.
That is, it operates at bottom speed.

In my code, bottom is delay Microsseconds (10000).
However, top should operate as delayMicrosseconds(100); and when you actually use Serial.println(), the value seems to be used properly, but when you run it, both bottom and top operate at delayMicrosseconds(10000) speed.

What new debug print did You try and what is the result?

I've already confirmed above that the value is right in the desired part, and I think this is a motor problem, not a code problem.

Currently, a total of six motors are being controlled with Arduino Mega Hana.
Among the things I've been through before, when I just connected all 6 motors to the driver and turned the code that only drives 3 out of 6, there was a strange phenomenon that the other 3 also moved. I feel like it's because of the noise from the motor.
I think this phenomenon feels similar.

You didn't act upon the earlier suggestions. Maybe You'll figure things out on Your own.

Thank you. I'm sure I'll find a solution

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.