STM32 FOC5.2电机库中中的电流采样改为反向放大

使用STM32 FOC 5.2的电机库,硬件做电流采样时使用的是反向放大,但是电机库中使用的是同向放大,ST的配置上位机中的电流采样参考如下图:
STM32 FOC5.2电机库中中的电流采样改为反向放大_第1张图片

可是我们的电流采样电路如下:
STM32 FOC5.2电机库中中的电流采样改为反向放大_第2张图片

所以要修改ST的FOC电机库源码,路径是:../MCSDK_v5.2.0-Full/MotorControl/MCSDK/MCLib/F1xx/Src/r3_hd2_pwm_curr_fdbk.c 对应函数改动如下:

void R3HD2_GetPhaseCurrents( PWMC_Handle_t * pHdl, Curr_Components* pStator_Currents )
{
  uint8_t bSector;
  int32_t wAux;
  PWMC_R3_HD2_Handle_t * pHandle = (PWMC_R3_HD2_Handle_t *) pHdl;

  /* Deactivate TIMx CH4 to disable next triggers using bit-banding access */
  *(uint32_t*) (pHandle->wTIMxCH4_BB_Addr) = 0u;

  /* Reset the SOFOC flag to indicate the start of FOC algorithm*/
  pHandle->bSoFOC = 0u;

  bSector = (uint8_t) pHdl->hSector;

  switch ( bSector )
  {
  case SECTOR_4:
  case SECTOR_5:
    /* Current on Phase C is not accessible     */
    /* Ia = PhaseAOffset - ADC converted value) */
    wAux = (int32_t)( ADC1->JDR1 );
    wAux *= 2;
    wAux = wAux - (int32_t)( pHandle->wPhaseAOffset ) ;

    /* Saturation of Ia */
    if ( wAux < -INT16_MAX )
    {
      pStator_Currents->qI_Component1 = -INT16_MAX;
    }
    else if ( wAux > INT16_MAX )
    {
      pStator_Currents->qI_Component1 = INT16_MAX;
    }
    else
    {
      pStator_Currents->qI_Component1 = (int16_t) wAux;
    }
    
    /* Ib = PhaseBOffset - ADC converted value) */
    wAux = (int32_t)( pHandle->pParams_str->ADCx2->JDR1 );
    wAux *= 2;
    wAux = wAux - (int32_t)( pHandle->wPhaseBOffset ) ;

    /* Saturation of Ib */
    if ( wAux < -INT16_MAX )
    {
      pStator_Currents->qI_Component2 = -INT16_MAX;
    }
    else if ( wAux > INT16_MAX )
    {
      pStator_Currents->qI_Component2 = INT16_MAX;
    }
    else
    {
      pStator_Currents->qI_Component2 = (int16_t) wAux;
    }
    break;
    
  case SECTOR_6:
  case SECTOR_1:
    /* Current on Phase A is not accessible     */
    /* Ib = PhaseBOffset - ADC converted value) */
    wAux = (int32_t)( ADC1->JDR1 );
    wAux *= 2;
    wAux =  wAux - (int32_t)( pHandle->wPhaseBOffset ) ; //Ib

    /* Saturation of Ib */
    if ( wAux < -INT16_MAX )
    {
      pStator_Currents->qI_Component2 = -INT16_MAX;
    }
    else if ( wAux > INT16_MAX )
    {
      pStator_Currents->qI_Component2 = INT16_MAX;
    }
    else
    {
      pStator_Currents->qI_Component2 = (int16_t) wAux;
    }

    /* Ia = -Ic -Ib */
    wAux = (int32_t)( pHandle->pParams_str->ADCx2->JDR1 );
    wAux *= 2;
    wAux = (int32_t) pHandle->wPhaseCOffset - wAux;  //Ic
    wAux -= (int32_t) pStator_Currents->qI_Component2; //-Ic-Ib wAux = -wAux-pStator_Currents->qI_Component2

    /* Saturation of Ia */
    if ( wAux > INT16_MAX )
    {
      pStator_Currents->qI_Component1 = INT16_MAX;
    }
    else if ( wAux < -INT16_MAX )
    {
      pStator_Currents->qI_Component1 = -INT16_MAX;
    }
    else
    {
      pStator_Currents->qI_Component1 = (int16_t) wAux;
    }
    break;
    
  case SECTOR_2:
  case SECTOR_3:
    /* Current on Phase B is not accessible     */
    /* Ia = PhaseAOffset - ADC converted value) */
    wAux = (int32_t)( ADC1->JDR1 );
    wAux *= 2;
    wAux =  wAux - (int32_t)( pHandle->wPhaseAOffset ) ;

    /* Saturation of Ia */
    if ( wAux < -INT16_MAX )
    {
      pStator_Currents->qI_Component1 = -INT16_MAX;
    }
    else if ( wAux > INT16_MAX )
    {
      pStator_Currents->qI_Component1 = INT16_MAX;
    }
    else
    {
      pStator_Currents->qI_Component1 = (int16_t) wAux;
    }

    /* Ib = -Ic -Ia */
    wAux = (int32_t)( pHandle->pParams_str->ADCx2->JDR1 );
    wAux *= 2;
    wAux = (int32_t) pHandle->wPhaseCOffset - wAux;
    wAux -= (int32_t) pStator_Currents->qI_Component1;

    /* Saturation of Ib */
    if ( wAux > INT16_MAX )
    {
      pStator_Currents->qI_Component2 = INT16_MAX;
    }
    else if ( wAux < -INT16_MAX )
    {
      pStator_Currents->qI_Component2 = -INT16_MAX;
    }
    else
    {
      pStator_Currents->qI_Component2 = (int16_t) wAux;
    }                     
    break;

  default:
    break;
  }

  pHandle->_Super.hIa = pStator_Currents->qI_Component1;
  pHandle->_Super.hIb = pStator_Currents->qI_Component2;
  pHandle->_Super.hIc = -pStator_Currents->qI_Component1 - pStator_Currents->qI_Component2;
}

你可能感兴趣的:(控制算法,stm32)