Stepper : ARDUINO MEGA, HELP , NO FOUND CODE

#define MOTOR_A_STEP_PIN 54
#define MOTOR_A_DIR_PIN 55
#define MOTOR_A_ENABLE_PIN 38


  
#define MOTOR_B_STEP_PIN 60
#define MOTOR_B_DIR_PIN 61
#define MOTOR_B_ENABLE_PIN 56


int16_t motor1;
int16_t motor2;
int16_t speed_m[2]; // Actual speed of motors
uint8_t dir_m[2]; // Actual direction of steppers motors
int16_t actual_robot_speed; // overall robot speed (measured from steppers speed)
int16_t actual_robot_speed_Old; // overall robot speed (measured from steppers speed)
float estimated_speed_filtered;
uint16_t counter_m[2]; // counters for periods
uint16_t period_m[2][8]; // Eight subperiods
uint8_t period_m_index[2]; // index for subperiods

#define ZERO_SPEED 65535
#define MAX_ACCEL 400



// 200ns => 4 instructions at 16Mhz
void delay_200ns()
{
__asm__ __volatile__ (
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop");
}


ISR(TIMER1_COMPA_vect)
{
  
  Serial.println("ok");
counter_m[0]++;
counter_m[1]++;
if (counter_m[0] >= period_m[0][period_m_index[0]])
{
counter_m[0] = 0;
if (period_m[0][0]==ZERO_SPEED)
return;
if (dir_m[0])
//SET(PORTB,4); // DIR Motor 1
    digitalWrite(MOTOR_A_DIR_PIN,0);
    
    


else 
//CLR(PORTB,4);
//    digitalWrite(MOTOR_A_ENABLE_PIN, HIGH);
//    digitalWrite(MOTOR_B_ENABLE_PIN, HIGH);
          digitalWrite(MOTOR_A_DIR_PIN,0);


    

// We need to wait at lest 200ns to generate the Step pulse...
period_m_index[0] = (period_m_index[0]+1)&0x07; // period_m_index from 0 to 7
//delay_200ns();
//SET(PORTD,7); // STEP Motor 1
    digitalWrite(MOTOR_A_STEP_PIN,0);


delayMicroseconds(1);
//CLR(PORTD,7);
    digitalWrite(MOTOR_A_STEP_PIN,1);

}
if (counter_m[1] >= period_m[1][period_m_index[1]])
{
counter_m[1] = 0;
if (period_m[1][0]==ZERO_SPEED)
return;
if (dir_m[1])
//SET(PORTC,7); // DIR Motor 2
    digitalWrite(MOTOR_B_DIR_PIN,0);

else
//CLR(PORTC,7);
    digitalWrite(MOTOR_B_DIR_PIN,1);

period_m_index[1] = (period_m_index[1]+1)&0x07;
//delay_200ns();
//SET(PORTD,6); // STEP Motor 1
digitalWrite(MOTOR_B_STEP_PIN,0);// STEP Motor 2



delayMicroseconds(1);
//CLR(PORTD,6);
//digitalWrite(MOTOR_B_STEP_PIN,1);
digitalWrite(MOTOR_B_STEP_PIN,1);// STEP Motor 2


}
}
// Dividimos en 8 subperiodos para aumentar la resolucion a velocidades altas (periodos pequeños)
// subperiod = ((1000 % vel)*8)/vel;
// Examples 4 subperiods:
// 1000/260 = 3.84 subperiod = 3
// 1000/240 = 4.16 subperiod = 0
// 1000/220 = 4.54 subperiod = 2
// 1000/300 = 3.33 subperiod = 1
void calculateSubperiods(uint8_t motor)
{
int subperiod;
int absSpeed;
uint8_t j;
if (speed_m[motor] == 0)
{
for (j=0;j<8;j++)
period_m[motor][j] = ZERO_SPEED;
return;
}
if (speed_m[motor] > 0 ) // Positive speed
{
dir_m[motor] = 1;
absSpeed = speed_m[motor];
}
else // Negative speed
{
dir_m[motor] = 0;
absSpeed = -speed_m[motor];
}
for (j=0;j<8;j++)
period_m[motor][j] = 1000/absSpeed;
// Calculate the subperiod. if module <0.25 => subperiod=0, if module < 0.5 => subperiod=1. if module < 0.75 subperiod=2 else subperiod=3
subperiod = ((1000 % absSpeed)*8)/absSpeed; // Optimized code to calculate subperiod (integer math)
if (subperiod>0)
period_m[motor][1]++;
if (subperiod>1)
period_m[motor][5]++;
if (subperiod>2)
period_m[motor][3]++;
if (subperiod>3)
period_m[motor][7]++;
if (subperiod>4)
period_m[motor][0]++;
if (subperiod>5)
period_m[motor][4]++;
if (subperiod>6)
period_m[motor][2]++;
// DEBUG
/*
if ((motor==0)&&((debug_counter%10)==0)){
Serial.print(1000.0/absSpeed);Serial.print("\t");Serial.print(absSpeed);Serial.print("\t");
Serial.print(period_m[motor][0]);Serial.print("-");
Serial.print(period_m[motor][1]);Serial.print("-");
Serial.print(period_m[motor][2]);Serial.print("-");
Serial.println(period_m[motor][3]);
}
*/
}
void setMotorSpeed(uint8_t motor, int16_t tspeed)
{
// WE LIMIT MAX ACCELERATION
if ((speed_m[motor] - tspeed)>MAX_ACCEL)
speed_m[motor] -= MAX_ACCEL;
else if ((speed_m[motor] - tspeed)<-MAX_ACCEL)
speed_m[motor] += MAX_ACCEL;
else
speed_m[motor] = tspeed;
calculateSubperiods(motor); // We use four subperiods to increase resolution
// To save energy when its not running...
if ((speed_m[0]==0)&&(speed_m[1]==0)){
digitalWrite(MOTOR_A_ENABLE_PIN, LOW);
digitalWrite(MOTOR_B_ENABLE_PIN, LOW);
}  else  {
       digitalWrite(MOTOR_A_ENABLE_PIN, LOW);
       digitalWrite(MOTOR_B_ENABLE_PIN, LOW);

}

//digitalWrite(4,HIGH); // Disable motors
//else
//digitalWrite(4,LOW); // Enable motors
}
//*************************


void setup() {
  Serial.begin(9600);

       pinMode(MOTOR_A_ENABLE_PIN, OUTPUT);
  digitalWrite(MOTOR_A_ENABLE_PIN, HIGH);
  
       pinMode(MOTOR_B_ENABLE_PIN, OUTPUT);
  digitalWrite(MOTOR_B_ENABLE_PIN, HIGH);
  
  
// STEPPER MOTORS INITIALIZATION
// TIMER1 CTC MODE
TCCR1B &= ~(1<<WGM13);
TCCR1B |= (1<<WGM12);
TCCR1A &= ~(1<<WGM11);
TCCR1A &= ~(1<<WGM10);
// output mode = 00 (disconnected)
TCCR1A &= ~(3<<COM1A0);
TCCR1A &= ~(3<<COM1B0);
// Set the timer pre-scaler
// Generally we use a divider of 8, resulting in a 2MHz timer on 16MHz CPU
TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
//OCR1A = 125; // 16Khz
//OCR1A = 100; // 20Khz
OCR1A = 80; // 25Khz
TCNT1 = 0;
delay(2000);
  
   TIMSK1 |= (1<<OCIE1A); // Enable Timer1 interrupt
   
   digitalWrite(MOTOR_A_ENABLE_PIN, LOW);
  
  digitalWrite(MOTOR_B_ENABLE_PIN, LOW);
  
  
 
   
}

void loop() {
setMotorSpeed(0,330);
                                    	setMotorSpeed(1,330);

Was there a question to go along with that code?

the problem that the motors do not move.
with this method I have understood that the engine will not stop, will not wait
aque to call for it to move.

I was using the library "<AccelStepper.h>" but losmotores only move if I call the motorA.runSpeed function () ;.

"

I'm doing a balasing, but the engines with the library <AccelStepper.h>,
move slowly, discontinuously, the process that makes Arduino
to reach the motorA.runSpeed function () ;.

You need some simpler test code to verify that the hardware is working. Then worry about the code.

is not the hardware, I tried it with the library ""<AccelStepper.h>"" and works perfect

Naruto128:
is not the hardware, I tried it with the library "" and works perfect

Good. What about your timer code? Has it been tested separately from the main program logic? Is there some reason for not using library functions? I see comments in two different languages... are you combining code from different sources? Are you one of the sources?

ISR(TIMER1_COMPA_vect)
{
  
  Serial.println("ok");

Serial inside an ISR has a nasty reputation for locking things up. The Serial interface relies on interrupts which are turned off during your ISR.

ok, but the engines do not move, I can use this as ISR (TIMER1_COMPA_vect) so the engines work?

i have never worked with this isr

Naruto128:
ok, but the engines do not move, I can use this as ISR (TIMER1_COMPA_vect) so the engines work?

i have never worked with this isr

My point was that perhaps the motors don't turn because the code locks up due to the use of Serial inside the ISR.

If you don't know what ISR is or what all of that means then I suggest you do some research on Arduino and Interrupts. It is not a trivial subject.

Delta_G:
My point was that perhaps the motors don't turn because the code locks up due to the use of Serial inside the ISR.

If you don't know what ISR is or what all of that means then I suggest you do some research on Arduino and Interrupts. It is not a trivial subject.

you do not know how it works? I remove the println (), but does not operate engines

  counter_m[0]++;
  counter_m[1]++;
  if (counter_m[0] >= period_m[0][period_m_index[0]])

Hmmm. You seem to be treating individual elements of an array as variables. Why not create variables for them? Also, your array indexing seems very dangerous. Where are the bounds checked, or limited?

Can you tell whether the program continues to run, or crashes?

BLOCK IS NOT ONLY IS NOT MOVING MOTORS

Naruto128:
BLOCK IS NOT ONLY IS NOT MOVING MOTORS

Sorry, the English translation didn't come across clearly. I'm asking you if there is any proof that the program continues to run, while the motors don't move.

No, there is no evidence of that program continue, working or not

aarg:
I see comments in two different languages... are you combining code from different sources? Are you one of the sources?

He seems to have copied the library code into his sketch rather than calling it as a library.

I have a fairly simple approach to this sort of problem.

Don't try to mess with hardware timers unless you know what you are doing. It is unreasonable to expect help with programs which are that complex.

Why not just use the AccelStepper library?

You seem to be using a stepper driver that uses step and direction signals so this Simple Stepper Program should work if you want to try programming your motor without using a library.

...R
Stepper Motor Basics

Robin2:
I have a fairly simple approach to this sort of problem.

Don't try to mess with hardware timers unless you know what you are doing. It is unreasonable to expect help with programs which are that complex.

Why not just use the AccelStepper library?

You seem to be using a stepper driver that uses step and direction signals so this Simple Stepper Program should work if you want to try programming your motor without using a library.

...R
Stepper Motor Basics

Because with this library, motors only move if the function is called. run ().

I need to be always moving, and its speed is changed only when needed

WORKS, HEHE!!!

#define MOTOR_A_STEP_PIN 54
#define MOTOR_A_DIR_PIN 55
#define MOTOR_A_ENABLE_PIN 38


  
#define MOTOR_B_STEP_PIN 60
#define MOTOR_B_DIR_PIN 61
#define MOTOR_B_ENABLE_PIN 56

int16_t motor1;
int16_t motor2;
int16_t speed_m[2]; // Actual speed of motors
uint8_t dir_m[2]; // Actual direction of steppers motors
int16_t actual_robot_speed; // overall robot speed (measured from steppers speed)
int16_t actual_robot_speed_Old; // overall robot speed (measured from steppers speed)
float estimated_speed_filtered;
uint16_t counter_m[2]; // counters for periods
uint16_t period_m[2][8]; // Eight subperiods
uint8_t period_m_index[2]; // index for subperiods



#define ZERO_SPEED 65535
#define MAX_ACCEL 7


// the setup routine runs once when you press reset:
void setup() {      

  
  pinMode(MOTOR_A_STEP_PIN, OUTPUT);
  pinMode(MOTOR_A_DIR_PIN, OUTPUT);
  pinMode(MOTOR_A_ENABLE_PIN, OUTPUT);
  digitalWrite(MOTOR_A_ENABLE_PIN, 1);
  
  
  pinMode(MOTOR_B_STEP_PIN, OUTPUT);
  pinMode(MOTOR_B_DIR_PIN, OUTPUT);
  pinMode(MOTOR_B_ENABLE_PIN, OUTPUT);
  digitalWrite(MOTOR_B_ENABLE_PIN, 1);


// STEPPER MOTORS INITIALIZATION
// TIMER1 CTC MODE
TCCR1B &= ~(1<<WGM13);
TCCR1B |= (1<<WGM12);
TCCR1A &= ~(1<<WGM11);
TCCR1A &= ~(1<<WGM10);
// output mode = 00 (disconnected)
TCCR1A &= ~(3<<COM1A0);
TCCR1A &= ~(3<<COM1B0);
// Set the timer pre-scaler
// Generally we use a divider of 8, resulting in a 2MHz timer on 16MHz CPU
TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
//OCR1A = 125; // 16Khz
//OCR1A = 100; // 20Khz
OCR1A = 80; // 25Khz
TCNT1 = 0;
delay(2000);
  
   TIMSK1 |= (1<<OCIE1A); // Enable Timer1 interrupt



digitalWrite(MOTOR_A_ENABLE_PIN, LOW);
 digitalWrite(MOTOR_B_ENABLE_PIN, LOW);
  
}


void delay_200ns()
{
__asm__ __volatile__ (
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop");
}

ISR(TIMER1_COMPA_vect)
{

//  digitalWrite(MOTOR_A_DIR_PIN,1);
//  delayMicroseconds(200);  
//  digitalWrite(MOTOR_A_STEP_PIN, LOW);
//  delayMicroseconds(1);
//  digitalWrite(MOTOR_A_STEP_PIN, HIGH);
  
  
  counter_m[0]++;
if (counter_m[0] >= period_m[0][period_m_index[0]])
{
counter_m[0] = 0;
if (period_m[0][0]==ZERO_SPEED)
return;
if (dir_m[0])
//SET(PORTB,4); // DIR Motor 1
    digitalWrite(MOTOR_A_DIR_PIN,0);

else
    digitalWrite(MOTOR_A_DIR_PIN,1);
// We need to wait at lest 200ns to generate the Step pulse...
period_m_index[0] = (period_m_index[0]+1)&0x07; // period_m_index from 0 to 7
//delay_200ns();
delayMicroseconds(200);  
//SET(PORTD,7); // STEP Motor 1
digitalWrite(MOTOR_A_STEP_PIN, LOW);
//delayMicroseconds(1);
//CLR(PORTD,7);
digitalWrite(MOTOR_A_STEP_PIN, HIGH);

}
}

// Dividimos en 8 subperiodos para aumentar la resolucion a velocidades altas (periodos pequeños)
// subperiod = ((1000 % vel)*8)/vel;
// Examples 4 subperiods:
// 1000/260 = 3.84 subperiod = 3
// 1000/240 = 4.16 subperiod = 0
// 1000/220 = 4.54 subperiod = 2
// 1000/300 = 3.33 subperiod = 1
void calculateSubperiods(uint8_t motor)
{
int subperiod;
int absSpeed;
uint8_t j;
if (speed_m[motor] == 0)
{
for (j=0;j<8;j++)
period_m[motor][j] = ZERO_SPEED;
return;
}
if (speed_m[motor] > 0 ) // Positive speed
{
dir_m[motor] = 1;
absSpeed = speed_m[motor];
}
else // Negative speed
{
dir_m[motor] = 0;
absSpeed = -speed_m[motor];
}
for (j=0;j<8;j++)
period_m[motor][j] = 1000/absSpeed;
// Calculate the subperiod. if module <0.25 => subperiod=0, if module < 0.5 => subperiod=1. if module < 0.75 subperiod=2 else subperiod=3
subperiod = ((1000 % absSpeed)*8)/absSpeed; // Optimized code to calculate subperiod (integer math)
if (subperiod>0)
period_m[motor][1]++;
if (subperiod>1)
period_m[motor][5]++;
if (subperiod>2)
period_m[motor][3]++;
if (subperiod>3)
period_m[motor][7]++;
if (subperiod>4)
period_m[motor][0]++;
if (subperiod>5)
period_m[motor][4]++;
if (subperiod>6)
period_m[motor][2]++;
// DEBUG
/*
if ((motor==0)&&((debug_counter%10)==0)){
Serial.print(1000.0/absSpeed);Serial.print("\t");Serial.print(absSpeed);Serial.print("\t");
Serial.print(period_m[motor][0]);Serial.print("-");
Serial.print(period_m[motor][1]);Serial.print("-");
Serial.print(period_m[motor][2]);Serial.print("-");
Serial.println(period_m[motor][3]);
}
*/
}
void setMotorSpeed(uint8_t motor, int16_t tspeed)
{
// WE LIMIT MAX ACCELERATION
if ((speed_m[motor] - tspeed)>MAX_ACCEL)
speed_m[motor] -= MAX_ACCEL;
else if ((speed_m[motor] - tspeed)<-MAX_ACCEL)
speed_m[motor] += MAX_ACCEL;
else
speed_m[motor] = tspeed;
calculateSubperiods(motor); // We use four subperiods to increase resolution
// To save energy when its not running...
//if ((speed_m[0]==0)&&(speed_m[1]==0))
//digitalWrite(4,HIGH); // Disable motors
//else
//digitalWrite(4,LOW); // Enable motors
}
  
  
  
  
  
  
  
//  seconds++;
//if(seconds >= 5000)
//{
//seconds = 0;
//    digitalWrite(13, !digitalRead(13));
//
//}




// the loop routine runs over and over again forever:
void loop() {
//digitalWrite(MOTOR_A_DIR_PIN,0);
//delayMicroseconds(200);  
//digitalWrite(MOTOR_A_STEP_PIN, LOW);
//  
//  digitalWrite(MOTOR_A_STEP_PIN, HIGH);  

	setMotorSpeed(0,123);

}

BUT because the program crashes if I put a Serial.println (¨); in the loop?

Naruto128:
the program crashes if I put a Serial.println (¨); in the loop?

Do you have a call to Serial.begin() in setup?

Delta_G:
Do you have a call to Serial.begin() in setup?

yes