haohao663 发表于 2016-8-30 08:58:38

【HAL库每天一例】第106例:57&42步进电机梯形加减速实现

【HAL库每天一例】系列例程从今天开始持续更新。。。。。
我们将坚持每天至少发布一个基于YS-F1Pro开发板的HAL库例程,
该系列例程将带领大家从零开始使用HAL库,后面会持续添加模块应用例程。
同样的,我们还程序发布基于HAL库的指导文档和视频教程,欢迎持续关注,并提出改进意见。

参考文档见:
硬石电机控制专题指导手册
例程下载:
资料包括程序、相关说明资料以及软件使用截图
百度云盘:https://pan.baidu.com/s/1slN8rIt 密码:u6m1360云盘:http://yunpan.cn/OcPiRp3wEcA92u密码 cfb6(硬石YS-F1Pro开发板HAL库例程持续更新\5. 软件设计之电机控制(HAL库版本)\步进电机.zip)
/**
******************************************************************************
*                           硬石YS-F1Pro开发板例程功能说明
*
*例程名称: YSF1_HAL_MOTOR-004. 57&42步进电机基本旋转实现
*   
******************************************************************************
* 说明:
* 本例程配套硬石stm32开发板YS-F1Pro使用。
*
* 淘宝:
* 论坛:硬石电子社区
* 版权归硬石嵌入式开发团队所有,请勿商用。
******************************************************************************
*/

【1】例程简介
【2】跳线帽情况
【3】操作及现象
/******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/
定位函数
void
TB6600_AxisMoveAbs(int32_t step, uint32_t accel, uint32_t decel,
uint32_t speed)//绝对运动,step是目标位置,accel是加速度,decel是减速度,speed是速度
{
//! Number of steps before we hit max speed.达到最大速度时的步数
uint32_t max_s_lim;
//! Number of steps before we must start deceleration (if accel does not hit max speed).
//如果加速没有达到最大速度,但是必须要开始减速的步数
uint32_t accel_lim;
    float ftemp=0.0;

    step=step-position;
if(step <0)
{
    if(HAL_GPIO_ReadPin(AXIS_LMTNEG_PORT, AXIS_LMTNEG_PIN))
    {
      if(LmtSnsNeg==0)
      {
      bLmtNeg=FALSE;
      }
      else
      {
      bLmtNeg=TRUE;
      return;
      }
         
    }
    else
    {
      if(LmtSnsNeg==0)
      {
      bLmtNeg=TRUE;
      return;
      }
      else
      {
      bLmtNeg=FALSE;   
      }            
    }
    srd.dir = CCW;
    TB6600_SETDIR_CCW();
    step =-step;
}
else
{
    if(HAL_GPIO_ReadPin(AXIS_LMTPOS_PORT, AXIS_LMTPOS_PIN))
    {
      if(LmtSnsPos==0)
      {
      bLmtPos=FALSE;
      }
      else
      {
      bLmtPos=TRUE;
      return;
      }                  
    }
    else
    {
      if(LmtSnsPos==0)
      {
      bLmtPos=TRUE;
      return;
      }
      else
      {
      bLmtPos=FALSE;
      }            
    }
    srd.dir = CW;
    TB6600_SETDIR_CW();
}

if(step == 1)
{
    srd.accel_count = -1; // Move one step...
    srd.run_state = DECEL;// ...in DECEL state.
    srd.step_delay = 1000;    // Just a short delay so main() can act on 'running'.      
    __HAL_TIM_SET_COMPARE(&htimx_TB6600,TB6600_TIM_CHANNELn,TIMx_pluse);
    __HAL_TIM_SET_AUTORELOAD(&htimx_TB6600,TIMx_pluse);
      MotionStatus = 1;
      __HAL_TIM_ENABLE(&htimx_TB6600);            
}
else if(step != 0)// Only move if number of steps to move is not zero.
{
    // Refer to documentation for detailed information about these calculations.
    // Set max speed limit, by calc min_delay to use in timer.
    // min_delay = (alpha / tt)/ w
    srd.min_delay = T1_FREQ/speed/2;

    // Set accelration by calc the first (c0) step delay .
    // step_delay = 1/tt * axis_sqrt(2*alpha/accel)
    // step_delay = ( tfreq*0.676/100 )*100 * axis_sqrt( (2*alpha*10000000000) / (accel*100) )/10000
    srd.step_delay = ((long)T1_FREQ*0.676* axis_sqrt(2000000 / accel))/1000/2;
    // Find out after how many steps does the speed hit the max speed limit.
    // max_s_lim = speed^2 / (2*alpha*accel)
    max_s_lim = speed*speed/(2*accel);
    // If we hit max speed limit before 0,5 step it will round to 0.
    // But in practice we need to move atleast 1 step to get any speed at all.
    if(max_s_lim == 0){
      max_s_lim = 1;
    }

    // Find out after how many steps we must start deceleration.
    // n1 = (n1+n2)decel / (accel + decel)
    if((accel+decel)>step)
      {
//            accel_lim = step*decel/(accel+decel);
            ftemp=(float)decel/(float)(accel+decel);
            accel_lim = (float)step*ftemp;
      }
      else
      {
            accel_lim = step/(accel+decel)*decel;
      }
    // We must accelrate at least 1 step before we can start deceleration.
    if(accel_lim == 0){
      accel_lim = 1;
    }

    // Use the limit we hit first to calc decel.
    if(accel_lim <= max_s_lim){
      srd.decel_val = accel_lim - step;
    }
    else{
      srd.decel_val =-(int32_t)(max_s_lim*accel/decel);
    }
    // We must decelrate at least 1 step to stop.
    if(srd.decel_val == 0){
      srd.decel_val = -1;
    }

    // Find step to start decleration.
    srd.decel_start = step + srd.decel_val;

    // If the maximum speed is so low that we dont need to go via accelration state.
    if(srd.step_delay <= srd.min_delay)
   {
      srd.step_delay = srd.min_delay;
      srd.run_state = RUN;
    }
    else{
      srd.run_state = ACCEL;
    }

    // Reset counter.
    srd.accel_count = 0;
    MotionStatus = 1;
    __HAL_TIM_SET_COMPARE(&htimx_TB6600,TB6600_TIM_CHANNELn,TIMx_pluse);
    __HAL_TIM_SET_AUTORELOAD(&htimx_TB6600,TIMx_pluse);
      __HAL_TIM_ENABLE(&htimx_TB6600);      
}
}

terryGameMM 发表于 2017-4-17 16:47:03

好呀!谢谢了!正在学习!

koonwood 发表于 2019-7-20 14:10:07

下来看看

huangzongwu 发表于 2020-1-3 19:22:21

最近刚好要用到步进电机相关的资料,感谢大神指点!
页: [1]
查看完整版本: 【HAL库每天一例】第106例:57&42步进电机梯形加减速实现