你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

查看: 2069|回复: 3

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

[复制链接]

122

主题

129

回帖

0

蝴蝶豆

论坛元老

最后登录
2019-5-28
发表于 2016-8-30 08:58:38 | 显示全部楼层 |阅读模式
【HAL库每天一例】系列例程从今天开始持续更新。。。。。
我们将坚持每天至少发布一个基于YS-F1Pro开发板的HAL库例程,
该系列例程将带领大家从零开始使用HAL库,后面会持续添加模块应用例程。
同样的,我们还程序发布基于HAL库的指导文档和视频教程,欢迎持续关注,并提出改进意见。

参考文档见:

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

百度云盘:https://pan.baidu.com/s/1slN8rIt 密码:u6m1
360云盘: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****/
定位函数
  1. void
  2. TB6600_AxisMoveAbs(int32_t step, uint32_t accel, uint32_t decel,
  3. uint32_t speed)//绝对运动,step是目标位置,accel是加速度,decel是减速度,speed是速度
  4. {
  5.   //! Number of steps before we hit max speed.达到最大速度时的步数
  6.   uint32_t max_s_lim;
  7.   //! Number of steps before we must start deceleration (if accel does not hit max speed).
  8.   //如果加速没有达到最大速度,但是必须要开始减速的步数
  9.   uint32_t accel_lim;
  10.     float ftemp=0.0;

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

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

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

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

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

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

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

  138.     // Reset counter.
  139.     srd.accel_count = 0;
  140.     MotionStatus = 1;
  141.     __HAL_TIM_SET_COMPARE(&htimx_TB6600,TB6600_TIM_CHANNELn,TIMx_pluse);
  142.     __HAL_TIM_SET_AUTORELOAD(&htimx_TB6600,TIMx_pluse);
  143.         __HAL_TIM_ENABLE(&htimx_TB6600);        
  144.   }
  145. }
复制代码


<
回复

使用道具 举报

0

主题

19

回帖

0

蝴蝶豆

初级会员

最后登录
2020-10-10
发表于 2017-4-17 16:47:03 | 显示全部楼层
好呀!谢谢了!正在学习!
回复 支持 反对

使用道具 举报

0

主题

7

回帖

0

蝴蝶豆

初级会员

最后登录
2020-11-6
发表于 2019-7-20 14:10:07 | 显示全部楼层
下来看看
回复 支持 反对

使用道具 举报

1

主题

9

回帖

2

蝴蝶豆

新手上路

最后登录
2020-11-20
发表于 2020-1-3 19:22:21 | 显示全部楼层
最近刚好要用到步进电机相关的资料,感谢大神指点!
回复 支持 反对

使用道具 举报

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版