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

[复制链接]
2465|5
 楼主| 亼亽 发表于 2016-8-30 08:55 | 显示全部楼层 |阅读模式
本帖最后由 亼亽 于 2016-8-31 09:22 编辑

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

参考文档见:

硬石电机控制专题指导手册
例程下载:
资料包括程序、相关说明资料以及软件使用截图

百度云盘:https://pan.baidu.com/s/1slN8rIt 密码:u6m1
360云盘:https://yunpan.cn/OcPiRp3wEcA92u密码 cfb6
(硬石YS-F1Pro开发板HAL库例程持续更新\5. 软件设计之电机控制(HAL库版本)\步进电机.zip
/**
  ******************************************************************************
  *                           硬石YS-F1Pro开发板例程功能说明
  *
  *  例程名称: YSF1_HAL_MOTOR-004. 57&42步进电机基本旋转实现
  *   
  ******************************************************************************
  * 说明:
  * 本例程配套硬石stm32开发板YS-F1Pro使用。
  *
  * 淘宝:
  * 论坛:http://www.ing10bbs.com
  * 版权归硬石嵌入式开发团队所有,请勿商用。
  ******************************************************************************
  */

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

  9.     step=step-position;
  10.   if(step <0)
  11.   {
  12.     if(HAL_GPIO_ReadPin(AXIS_LMTNEG_PORT, AXIS_LMTNEG_PIN))
  13.     {
  14.       if(LmtSnsNeg==0)
  15.       {
  16.         bLmtNeg=FALSE;
  17.       }
  18.       else
  19.       {
  20.         bLmtNeg=TRUE;
  21.         return;
  22.       }
  23.          
  24.     }
  25.     else
  26.     {
  27.       if(LmtSnsNeg==0)
  28.       {
  29.         bLmtNeg=TRUE;
  30.         return;
  31.       }
  32.       else
  33.       {
  34.         bLmtNeg=FALSE;   
  35.       }            
  36.     }
  37.     srd.dir = CCW;
  38.     TB6600_SETDIR_CCW();
  39.     step =-step;
  40.   }
  41.   else
  42.   {
  43.     if(HAL_GPIO_ReadPin(AXIS_LMTPOS_PORT, AXIS_LMTPOS_PIN))
  44.     {
  45.       if(LmtSnsPos==0)
  46.       {
  47.         bLmtPos=FALSE;
  48.       }
  49.       else
  50.       {
  51.         bLmtPos=TRUE;
  52.         return;
  53.       }                    
  54.     }
  55.     else
  56.     {
  57.       if(LmtSnsPos==0)
  58.       {
  59.         bLmtPos=TRUE;
  60.         return;
  61.       }
  62.       else
  63.       {
  64.         bLmtPos=FALSE;
  65.       }            
  66.     }
  67.     srd.dir = CW;
  68.     TB6600_SETDIR_CW();
  69.   }

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

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

  98.     // Find out after how many steps we must start deceleration.
  99.     // n1 = (n1+n2)decel / (accel + decel)
  100.     if((accel+decel)>step)
  101.         {
  102. //            accel_lim = step*decel/(accel+decel);
  103.             ftemp=(float)decel/(float)(accel+decel);
  104.             accel_lim = (float)step*ftemp;
  105.         }
  106.         else
  107.         {
  108.             accel_lim = step/(accel+decel)*decel;
  109.         }
  110.     // We must accelrate at least 1 step before we can start deceleration.
  111.     if(accel_lim == 0){
  112.       accel_lim = 1;
  113.     }

  114.     // Use the limit we hit first to calc decel.
  115.     if(accel_lim <= max_s_lim){
  116.       srd.decel_val = accel_lim - step;
  117.     }
  118.     else{
  119.       srd.decel_val =-(int32_t)(max_s_lim*accel/decel);
  120.     }
  121.     // We must decelrate at least 1 step to stop.
  122.     if(srd.decel_val == 0){
  123.       srd.decel_val = -1;
  124.     }

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

  127.     // If the maximum speed is so low that we dont need to go via accelration state.
  128.     if(srd.step_delay <= srd.min_delay)
  129.      {
  130.       srd.step_delay = srd.min_delay;
  131.       srd.run_state = RUN;
  132.     }
  133.     else{
  134.       srd.run_state = ACCEL;
  135.     }

  136.     // Reset counter.
  137.     srd.accel_count = 0;
  138.     MotionStatus = 1;
  139.     __HAL_TIM_SET_COMPARE(&htimx_TB6600,TB6600_TIM_CHANNELn,TIMx_pluse);
  140.     __HAL_TIM_SET_AUTORELOAD(&htimx_TB6600,TIMx_pluse);
  141.         __HAL_TIM_ENABLE(&htimx_TB6600);        
  142.   }
  143. }




mmuuss586 发表于 2016-8-30 10:05 | 显示全部楼层
谢谢分享;
yiyigirl2014 发表于 2016-11-27 11:45 | 显示全部楼层
搜索加减速驱动呢,找到这里了。谢谢。
yiyigirl2014 发表于 2016-11-27 12:15 | 显示全部楼层
不错,资料泰拳了。
zair 发表于 2020-8-5 09:14 | 显示全部楼层
链接失效了,可以补吗
zair 发表于 2020-8-5 09:14 | 显示全部楼层
链接失效了,可以补吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

122

主题

216

帖子

48

粉丝
快速回复 在线客服 返回列表 返回顶部