1
项目中用到步进电机,控制两个机械单元。要求动作速度快。我们知道给步进电机突然设置比较大的运行频率,可能会发生失步。而失步属于比较严重的事故,意味着程序再也不知道步进电机的准确位置。所以为了获得比较快的转动速度,又不会失步,可以用S曲线对步进电机启停进行加减速。这里我暂时用梯形加速,运行过程分为加速阶段、匀速阶段、减速阶段。S曲线同理,不过得根据电机性能和负载计算一个合适的加速曲线。
用到的STM32F411 Nucleo开发板的外设:定时器、DMA。
查看用户手册定时器4的通道1在PB6引脚上
查看数据手册可知定时器4通道1使用 DMA1流0通道2,与我们之前使用的ModBus发送数据的DMA通道无冲突。
查看STM32F411 Nucleo开发板开发板原理图,可以看到PB6引脚可用。
接下来我们初始化定时器4通道1作为PWM输出口。定时器预分频设置为100,由于我们使用的100MHz主频,定时器频率为1MHz。初始化代码如下。初始化完关闭定时器。
- /************************************************************************************
- º¯ÊýÃû³Æ£º
- ÊäÈë²ÎÊý£º
- Êä³ö²ÎÊý£º
- ¹¦ÄÜÃèÊö£º
- ************************************************************************************/
- void tiM4_CH1_PWM_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);////
- //------------------------------------------------------------------------------------
- GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- //------------------------------------------------------------------------------------
- TIM_DeInit(TIM4);
- TIM_TimeBaseStructure.TIM_Prescaler = 100-1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = 50;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
-
- TIM_OCStructInit(&TIM_OCInitStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;////
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- // TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 20;
- // TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- // TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
- // TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- // TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC1Init(TIM4, &TIM_OCInitStructure);
- //------------------------------------------------------------------------------------
- // TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
- // TIM_ARRPreloadConfig(TIM4,ENABLE);
-
- // TIM_DMAConfig(TIM4,TIM_DMABase_ARR,TIM_DMABurstLength_3Transfers);
- TIM_DMACmd(TIM4,TIM_DMA_CC1,DISABLE);
- TIM4 -> CCER &= ~(1<<0); //¹Ø±ÕTME4 PWMÊä³ö
- TIM_Cmd(TIM4,DISABLE);
- }
复制代码
接着我们初始化DMA通道,初始化的时候暂时把数据源地址、发送数据数量、DMA递增方式用参数来传递。因为我们要变速调节步进电机。这里使用梯形加速,运行过程分为加速阶段、匀速阶段、减速阶段。每个阶段数据提前在内存中存储。其起始地址,和数据长度不固定。
- /************************************************************************************
- º¯ÊýÃû³Æ£º
- ÊäÈë²ÎÊý£º
- Êä³ö²ÎÊý£º
- ¹¦ÄÜÃèÊö£º
- ************************************************************************************/
- void TIM4_PWMDMA_Config(uint16_t *DataBuf,int32_t BufSize,int32_t MemoryInc)
- {
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
-
- DMA_InitStructure.DMA_Channel = DMA_Channel_2;
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM4->ARR;
- DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DataBuf;
- DMA_InitStructure.DMA_BufferSize = BufSize;
- DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = MemoryInc;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
- DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_Init(DMA1_Stream0,&DMA_InitStructure);
-
- DMA_ITConfig(DMA1_Stream0,DMA_IT_TC,ENABLE);
- }
复制代码
启动RTX系统之前,我们先调用初始化步进电机函数。包含对硬件的初始化,以及加减速数据表的初始化。
- /************************************************************************************
- º¯ÊýÃû³Æ£º
- ÊäÈë²ÎÊý£º
- Êä³ö²ÎÊý£º
- ¹¦ÄÜÃèÊö£º
- ************************************************************************************/
- void Stepper_Init(void)
- {
- //-----------------------------------------------------------------------------------
- uint16_t i = 0,j = SetStartT; //100Hz //¼ÓËÙÇúÏß±í¸ñ³õʼ»¯
- for(;i
- Stepper_Start_Buffer[i] = j-i*((j-SetMaxT)/Stepper_BufLen);
-
- for(i=0;i
- Stepper_Stop_Buffer[i] = Stepper_Start_Buffer[Stepper_BufLen-1-i];
- //-----------------------------------------------------------------------------------
- DMA1_Stream0_CH2Init();
- TIM4_CH1_PWM_Config();
- }
复制代码
上位机对步进电机的控制:上位机通过ModBus协议向STM32F411 Nucleo开发板写入转动角度数据。数据类型为浮点型。比如上位机可以输入100°、101.1°。接收到上位机转动角度的指令之后,进行DMA数据表的计算。计算加速步数、匀速步数、减速步数。
- /************************************************************************************
- ÈÎÎñÃû³Æ£º²½½øµç»úÔËÐгÌÐò
- ÊäÈë²ÎÊý£º
- Êä³ö²ÎÊý£º
- ¹¦ÄÜÃèÊö£º
- ************************************************************************************/
- void Stepper(void)
- {
- if((!flag_DMA1_Stream0_CH2)&&(Writable_Structure.Stepper_Angles))
- {
- osDelay(1000);
- Stepper_Angle(Writable_Structure.Stepper_Angles);
- Writable_Structure.Stepper_Angles = 0;
- }
- }
- /************************************************************************************
- º¯ÊýÃû³Æ£º
- ÊäÈë²ÎÊý£º
- Êä³ö²ÎÊý£º
- ¹¦ÄÜÃèÊö£º
- ************************************************************************************/
- void Stepper_Angle(float Angle)
- {
- int32_t stepnum;
- static float wucha;
-
- stepnum = (int32_t)((Angle*1600)/360);
-
- wucha = (Angle*1600)/360 - stepnum + wucha;//Îó²îÐÞÕý
- if(wucha>1)
- {
- stepnum++;
- wucha -= 1;
- }
-
- Stepper_Steps_a(stepnum);
- }
- /************************************************************************************
- º¯ÊýÃû³Æ£º
- ÊäÈë²ÎÊý£º
- Êä³ö²ÎÊý£º
- ¹¦ÄÜÃèÊö£º
- ************************************************************************************/
- void Stepper_Steps_a(int32_t steps)//// ÓàÊý¼Óµ½ÔÈËÙÀï
- {
- int32_t Start_Steps = 0;
- Stepper_Period = 3;
-
- if((steps/2)>Stepper_BufLen)
- {
- Start_Steps = Stepper_BufLen;
- Stepper_Run = Stepper_Start_Buffer[Stepper_BufLen-1]; //ÔÈËÙʱµÄÔËÐÐƵÂÊ
- Stepper_Mid_Steps = steps - Stepper_BufLen*2 + steps%2; //ÔÈËٽ׶β½Êý
- }
- else
- {
- Start_Steps = steps/2 + steps%2;
- Stepper_Run = Stepper_Start_Buffer[steps/2-1];
- Stepper_Mid_Steps = -(steps/2);
- }
- DMA1_Stream0_CH2_Cmd(&TIM4_PWMDMA_Config,Stepper_Start_Buffer,Start_Steps,DMA_MemoryInc_Enable);
- TIM_DMACmd(TIM4,TIM_DMA_CC1,ENABLE);
- TIM4->CCER |= 1<<0; //¿ªTME4 PWMÊä³ö
- TIM_Cmd(TIM4,ENABLE);
- }
复制代码
其中有一段误差修正。因为我是用的步进电机步数为1600步360°。当上位机输入1°的时候,程序控制转动4.44步。只能取整数。如果每次都转动4步,误差会累积,最后变得十分不准确。加入误差修正程序,可以使误差始终小于1°。
- void DMA1_Stream0_IRQHandler(void)
- {
- if(DMA_GetITStatus(DMA1_Stream0,DMA_IT_TCIF0)==SET)
- {
- DMA_ClearFlag(DMA1_Stream0,DMA_IT_TCIF0);
-
- if(--Stepper_Period)//¸ù¾Ýµ±Ç°µ÷Ëٽ׶ÎÖØÐÂÅäÖÃDMA
- {
- if(Stepper_Mid_Steps<0)
- {
- Stepper_Mid_Steps = 0-Stepper_Mid_Steps;
- DMA1_Stream0_CH2_Cmd(&TIM4_PWMDMA_Config,&Stepper_Stop_Buffer[Stepper_BufLen-Stepper_Mid_Steps],Stepper_Mid_Steps,DMA_MemoryInc_Enable);
- Stepper_Period = 1;
- }
- else
- {
- if(2 == Stepper_Period)
- DMA1_Stream0_CH2_Cmd(&TIM4_PWMDMA_Config,&Stepper_Run,Stepper_Mid_Steps,DMA_MemoryInc_Disable);
- if(1 == Stepper_Period)
- DMA1_Stream0_CH2_Cmd(&TIM4_PWMDMA_Config,Stepper_Stop_Buffer,Stepper_BufLen,DMA_MemoryInc_Enable);
- }
- }
- else
- {
- DMA_Cmd(DMA1_Stream0,DISABLE);
- TIM_DMACmd(TIM4,TIM_DMA_CC1,DISABLE);
- TIM4 -> CCER &= ~(1<<0); //¹Ø±ÕTME4 PWMÊä³ö
- flag_DMA1_Stream0_CH2 = 0;
- }
- }
- }
复制代码
中断里进行调速阶段的切换。具体程序的细节请看附件吧。
硬件的连线。如前面文章中更新的,我是用的仍然是ModBus通讯。图中的黄绿线是和上位机的通讯线。我使用的步进电机有4根控制线,分别是脉冲+、脉冲-、转向+、转向-。
|
|