Brushless Motor Become Slower when combined with the IMU

Hi Everyone, i have a problem with brushless motor programming. I am not using ESC, but use L6234D as a motor driver. So far, i have successfully make my brushless motor rotating well.

Here is my arduino sketch for rotating Brushless Motor foy my Gimbal:

// constants won't change. They're used here to 
// set pin numbers:
const int motorPin1 =3;
const int motorPin2 =6;
const int motorPin3 =5;

// Variables will change:
boolean direct = true; // direction true=forward, false=backward

int pwmSin[] = {127,110,94,78,64,50,37,26,17,10,4,1,0,1,4,10,17,26,37,50,64,78,94,110,127,144,160,176,191,204,217,228,237,244,250,253,254,253,250,244,237,228,217,204,191,176,160,144,127
}; // array of PWM duty values for 8-bit timer - sine function

 
//int pwmSin[]={511,444,379,315,256,200,150,106,68,39,17,4,0,4,17,39,68,106,150,200,256,315,379,444,511,578,643,707,767,822,872,916,954,983,1005,1018,1022,1018,1005,983,954,916,872,822,767,707,643,578,511
//}; // array of PWM duty values for 10-bit timer - sine function
 
int increment;
int currentStepA=0;
int currentStepB=16;
int currentStepC=32;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long motorDelayActual = 0;  // the actual delay, based on pot value and motor delay set above
long lastMotorDelayTime = 0;

void initBLDC() {

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  
  //TCCR1B = TCCR1B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 9 and 10
  TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)

 

  ICR1 = 255 ; // 8 bit resolution
  //ICR1 = 1023 ; // 10 bit resolution

}

 
void setup() {
  
  initBLDC();
  
}


void loop() {

BLDCmove();
    
}
 
 
 
void BLDCmove() {
if((millis() - lastMotorDelayTime) > 0)
{ // delay time passed, move one step 
 
  if (direct==true)
  {
    increment = 1;
    
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
   
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
   
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  
  } 
   
   
  if (direct==false)
  {
    increment = -1;
    
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
   
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
   
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  } 
  
lastMotorDelayTime = millis();

}
   
analogWrite(motorPin1, pwmSin[currentStepA]);
analogWrite(motorPin2, pwmSin[currentStepB]);
analogWrite(motorPin3, pwmSin[currentStepC]);
   
}

Then, i combine the code above with my IMU sketch, The code is here:

The result is my brushless motor rotating much slower. Is IMU processing affected my brushless motor speed?

Thank You..

Could you explain more about the hardware - what kind of brushless motor? What is your approach to commutation?
What pins are controlling / sensing what? And what interrupts are you using?

There are no interrupts being used in his code posted on GitHub.

You're going to need to be more clever in how you set up the scheduling of the various functions of your gimbal. It looks like in your code you considered implementing a timer interrupt and that is probably the best way to run things. Decide how quickly you want your gimbal motors to be able to turn and set up an interrupt so that those motors are always updated with that frequency. All of the other tasks of reading from the gyros/accelerometer or calculating the kalman filter, etc. should be considered secondary; you don't need to be doing everything on every loop().

MarkT:
Could you explain more about the hardware - what kind of brushless motor? What is your approach to commutation?
What pins are controlling / sensing what? And what interrupts are you using?

I use this board:

And this is the motor:
http://www.rctimer.com/index.php?gOo=goods_details.dwt&goodsid=871&productname=

And i use L6234D as a motor driver:
http://www.st.com/st-web-ui/static/active/cn/resource/technical/document/application_note/CD00004062.pdf

I use 3 phase sine wave PWM signal to commutate the motor, i use pins 3, 6 and 5 for PWM, and I not use interrupts (may be this is the problem).

Chagrin:
There are no interrupts being used in his code posted on GitHub.

You're going to need to be more clever in how you set up the scheduling of the various functions of your gimbal. It looks like in your code you considered implementing a timer interrupt and that is probably the best way to run things. Decide how quickly you want your gimbal motors to be able to turn and set up an interrupt so that those motors are always updated with that frequency. All of the other tasks of reading from the gyros/accelerometer or calculating the kalman filter, etc. should be considered secondary; you don't need to be doing everything on every loop().

Ok, i will try to use interrupts..

Well, i try to use Interrupt, and the code is like this:

// constants won't change. They're used here to 
// set pin numbers:
const int motorPin1 =3;
const int motorPin2 =6;
const int motorPin3 =5;

// Variables will change:
boolean direct = true; // direction true=forward, false=backward

int pwmSin[] = {127,110,94,78,64,50,37,26,17,10,4,1,0,1,4,10,17,26,37,50,64,78,94,110,127,144,160,176,191,204,217,228,237,244,250,253,254,253,250,244,237,228,217,204,191,176,160,144,127
}; // array of PWM duty values for 8-bit timer - sine function

 
//int pwmSin[]={511,444,379,315,256,200,150,106,68,39,17,4,0,4,17,39,68,106,150,200,256,315,379,444,511,578,643,707,767,822,872,916,954,983,1005,1018,1022,1018,1005,983,954,916,872,822,767,707,643,578,511
//}; // array of PWM duty values for 10-bit timer - sine function
 
int increment;
int currentStepA=0;
int currentStepB=16;
int currentStepC=32;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long motorDelayActual = 0;  // the actual delay, based on pot value and motor delay set above
long lastMotorDelayTime = 0;

void initBLDC() {

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  
  //TCCR1B = TCCR1B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 9 and 10
  TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)
  //TCCR0B = TCCR0B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 5 and 6
  
  ICR1 = 255 ; // 8 bit resolution
  //ICR1 = 1023 ; // 10 bit resolution
  TIMSK2 |= _BV(TOIE1);
  sei();

  // Enable Timer1 Interrupt for Motor Control
  OCR0A = 0; 
  OCR0B = 0; 
  OCR1A = 0; 
  OCR1B = 0;  
  OCR2A = 0;  
  OCR2B = 0;  
}


ISR(TIMER2_OVF_vect) {  
 if((millis() - lastMotorDelayTime) > 0)
{ // delay time passed, move one step 
 
  if (direct==true)
  {
    increment = 1;
    
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
   
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
   
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  
  } 
   
   
  if (direct==false)
  {
    increment = -1;
    
  currentStepA = currentStepA + increment;
  if(currentStepA > 47) currentStepA = 0;
  if(currentStepA<0) currentStepA =47;
   
  currentStepB = currentStepB + increment;
  if(currentStepB > 47) currentStepB = 0;
  if(currentStepB<0) currentStepB =47;
   
    currentStepC = currentStepC + increment;
  if(currentStepC > 47) currentStepC = 0;
  if(currentStepC<0) currentStepC =47;
  } 
  
lastMotorDelayTime = millis();

}
   
analogWrite(motorPin1, pwmSin[currentStepA]);
analogWrite(motorPin2, pwmSin[currentStepB]);
analogWrite(motorPin3, pwmSin[currentStepC]);
   
};  

void setup() {
 
  initBLDC();
  
}


void loop() {

  //IMU Code Here
    
}

But, it is didn't works. Looks like the brushless motor not commutating, motor just hold it's position..

I know my code is wrong, but i can't find the wrong part, i think i need help here..

Is there anybody can help me?