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

使用STM32 FOC 5.2的电机库,硬件做电流采样时使用的是反向放大,但是电机库中使用的是同向放大,ST的配置上位机中的电流采样参考如下图:
在这里插入图片描述

可是我们的电流采样电路如下:
在这里插入图片描述

所以要修改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;
}
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值