• 0 Votes - 0 Average
• 1
• 2
• 3
• 4
• 5
 Difficulty in understanding the stepping control code
10-04-2015, 05:36 AM
Post: #1
 tang Junior Member  Posts: 1 Joined: Oct 2015 Reputation: 0
Difficulty in understanding the stepping control code
I am trying to understand how to control the stepper motor, especially in the B-Robot way. I have checked the A4988 datasheet, and I understand that 1/16 stepping is used, and timer1 interrupt routine is called at a rate of 25kHz.
The thing I totally don't get is the "subperiod" thing. The confusing code is below:
Code:
```ISR(TIMER1_COMPA_vect) {   counter_m++;   counter_m++;   if (counter_m >= period_m[period_m_index])     {     counter_m = 0;     if (period_m==ZERO_SPEED)       return;     if (dir_m)       SET(PORTB,4);  // DIR Motor 1     else       CLR(PORTB,4);     // We need to wait at lest 200ns to generate the Step pulse...     period_m_index = (period_m_index+1)&0x07; // period_m_index from 0 to 7     //delay_200ns();     SET(PORTD,7); // STEP Motor 1     delayMicroseconds(1);     CLR(PORTD,7);     }   if (counter_m >= period_m[period_m_index])     {     counter_m = 0;     if (period_m==ZERO_SPEED)       return;     if (dir_m)       SET(PORTC,7);   // DIR Motor 2     else       CLR(PORTC,7);     period_m_index = (period_m_index+1)&0x07;     //delay_200ns();     SET(PORTD,6); // STEP Motor 1     delayMicroseconds(1);     CLR(PORTD,6);     } } // 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]++;   if (subperiod>1)    period_m[motor]++;   if (subperiod>2)    period_m[motor]++;   if (subperiod>3)    period_m[motor]++;   if (subperiod>4)    period_m[motor]++;   if (subperiod>5)    period_m[motor]++;   if (subperiod>6)    period_m[motor]++;      // 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]);Serial.print("-");     Serial.print(period_m[motor]);Serial.print("-");     Serial.print(period_m[motor]);Serial.print("-");     Serial.println(period_m[motor]);     }   */   }```

Anyone can help me? Thank you very much
10-04-2015, 07:09 AM
Post: #2
 Pat78 Junior Member  Posts: 9 Joined: Aug 2015 Reputation: 0
RE: Difficulty in understanding the stepping control code
Hello

It was not so easy to understand for me, and more difficult to explain it in english a few months later ...
But in the last version in gitub it is no longer in the code and the robot walk fine !

An easy explanation, not really the truth :
The sub period is a sort of way to smooth the command, it's look like if insteed of integer value 1,2 3 ,4 ... you use number with one decimal like 2.0 ,2.1, 2.2 , the sum of the command of the subperiod give the decimal number.

Patrick
 « Next Oldest | Next Newest »

Forum Jump:

User(s) browsing this thread: 1 Guest(s)