Ejemplo n.º 1
0
/*! \brief Controls the PWM duty cycle based on speed set-point and current consumption.
 *
 *  This function controls the PWM duty cycle by calling a speed controller and a
 *  current controller. The speed controller signal is directly applied to the duty
 *  cycle. The current controller signal is used to limit the maximum duty cycle.
 */
static void PWMControl(void)
{
  signed int speedCompensation;
  static unsigned char currentCompensation = 0;
  static signed int duty = STARTUP_PWM_COMPARE_VALUE;

  // Run speed control only if a new speed measurement is available.
 if (speedUpdated)
  {
    speedCompensation = SpeedControl();
    speedUpdated = FALSE;
    duty += speedCompensation;
  }

  // Run current control only if a new current measurement is available.
  if (currentUpdated)
  {
     currentCompensation = CurrentControl();
     currentUpdated = FALSE;
  }

 // Keep duty cycle within limits.
  if (duty < MIN_PWM_COMPARE_VALUE)
  {
    duty = MIN_PWM_COMPARE_VALUE;
  }
  if (duty > (MAX_PWM_COMPARE_VALUE - currentCompensation))
  {
    duty = MAX_PWM_COMPARE_VALUE - currentCompensation;
  }

  SET_PWM_COMPARE_VALUE((unsigned char)duty);
}
Ejemplo n.º 2
0
void interrupt VectorNumber_Vpit0 pit0(void)    //1ms中断
{
     g_n1MsEventCount++;
     g_nSpeedControlPeriod++;     
     g_nDirectionControlPeriod++;
     SpeedControlOutput();
     MotorOutput();
     DirectionControlOutput();
     if(g_n1MsEventCount >= 5)
     {
         g_n1MsEventCount = 0;      
         SpeedCalculate();                    
     }
     else if(g_n1MsEventCount == 1) 
     {
          GetAngle(AngleResult);    
     }
     else if(g_n1MsEventCount == 2)
     {      
         AngleControl(); 
     }
     else if(g_n1MsEventCount == 3) 
     {
          g_nSpeedControlCount++;
          if(g_nSpeedControlCount >= 20) 
          {
              SpeedControl();
              g_nSpeedControlCount = 0;
              g_nSpeedControlPeriod = 0;     
           }
      }
      else if(g_n1MsEventCount == 4)
      {
         g_nDirectionControlCount++;
         if(g_nDirectionControlCount >= 2) 
         {
             DirectionControl();
             g_nDirectionControlCount = 0;
             g_nDirectionControlPeriod = 0;
          }
      }              
      PITTF_PTF0 = 1; 
}
Ejemplo n.º 3
0
void PIT_Signal(void)
{   
  DisableInterrupts;
  
  //--------------------------------------------------------------------------
  g_nSpeedControlPeriod++;
  SpeedControlOutput();  
  g_nDirectionControlPeriod ++;
  DirectionControlOutput();
  //---------------------------------------------------------------------------  
  g_nCarCount++;//中断服务片选
  switch(g_nCarCount)
  {
    //---------------------------计数:0   
  case 1:
   {   
      asm("nop"); 
      
   }; break;
    
     //---------------------------计数:1   AD采集                   第1个1ms  
  case 2:          
   {
      asm("nop");
      AngleCalculate(); 
      g_nDirectionGyro = (int16)LPLD_ADC_Get(ADC0, AD11); //方向陀螺仪采集
   }; break;
    
    //----------------------------计数:2   直立控制 电机PWM输出     第2个1ms
  case 3:
    { 
       asm("nop");
       AngleControl();
       MotorOutput();
    };  break;
    
    //----------------------------计数:3    车模速度控制            第3个1ms
  case 4:
    {  
       asm("nop");
       g_nSpeedControlCount++;  
      if(g_nSpeedControlCount>=20)        //控制是否进入速度更新环节
      { 
        SpeedControl();
        g_nSpeedControlCount = 0;
        g_nSpeedControlPeriod = 0; 
      }
    };break;
    
    //----------------------------计数:4    车模方向控制           第4个1ms
  case 5:   
    {  
      asm("nop");
      g_nCarCount=0; 
      
      g_nDirectionControlCount ++;
      DirectionVoltageSigma();           //电感采集、滤波                                             
      
      if( g_nDirectionControlCount >= 2)//控制是否进入方向更新环节
       {
          DirectionControl();
          g_nDirectionControlCount = 0;
          g_nDirectionControlPeriod = 0;
       }
    };break;
  }
  
  
  EnableInterrupts;
}