电机的加减速为了,电机启动停止更加稳定,提高匀速速度。S曲线算法优化电机的运行。
整个过程就是,模仿S曲线设置电机的频率
程序采用,通过s_curve()函数映射一张小于1500个点的表, 输出一次pwm方波,中断一次,计数器step计数一次 ,TIM_SetAutoreload()重新设置arr频率。
S曲线函数,加速和减速同理,返回值是表的个数
[plain] view plain copy
- uint16_t _stepmotor_calc(uint16_t min, uint16_t max, float rate, float val[1500])
- {
- uint16_t i;
- float Ainf_v;
- uint16_t AInf_t;
- float end_v;
- float start_v;
- end_v=100000/min;
- start_v=100000/max;
- Ainf_v=(end_v-start_v)/2+start_v;
- AInf_t=sqrt(2*(Ainf_v-start_v)/rate);
- for(i=0;i
代码实现:
电机的配置 :
[plain] view plain copy
- void RCC_Cfg(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
- }
- void GPIO_Cfg_gpio_b(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET); //OE使能
- GPIO_WriteBit(GPIOA, GPIO_Pin_5, Bit_RESET); //细分1
- GPIO_WriteBit(GPIOA, GPIO_Pin_6, Bit_SET); //细分2
- GPIO_WriteBit(GPIOB, GPIO_Pin_10, Bit_RESET); //ATT1衰减
- GPIO_WriteBit(GPIOB, GPIO_Pin_11, Bit_SET); //ATT2衰减
- GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_RESET); //CW方向
- }
- void PWM_Cfg(void)
- {
- TIM_OCInitTypeDef TIM_OCInitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_DeInit(TIM3);
- TIM_InternalClockConfig(TIM3);
- TIM_TimeBaseStructure.TIM_Period = 65535; //arr
- TIM_TimeBaseStructure.TIM_Prescaler = 200; // psc //Tclk/((arr+1)*(psc+1))Tclk;
- TIM_TimeBaseStructure.TIM_ClockDivision =0;// TIM_CKD_DIV1
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OC4Init(TIM3, &TIM_OCInitStructure);
- TIM_CtrlPWMOutputs(TIM3,ENABLE);
-
- }
主函数部分,整个运行的图像是个梯形,先加速在匀速在减速停止
[plain] view plain copy
- #include "stm32f10x.h"
- #include "math.h"
- #include "pwm.h"
-
- #define _ACC_DEC_MAX_COUNT 1500
- #define _STEP_INT 8000
-
- #define mortor_cw(x) x ? GPIO_SetBits(GPIOB,GPIO_Pin_0):GPIO_ResetBits(GPIOB,GPIO_Pin_0)
- void stm32_init(void);
- void delay_ms(u32 i);
- void once_pwm(void);
-
- int acc_i = 0;
- int dec_i = 0;
- int cw_buf=0;
- uint16_t acc_count;
- uint16_t dec_count;
-
- typedef struct
- {
- int StepCount;
- float acc_val[_ACC_DEC_MAX_COUNT];
- float dec_val[_ACC_DEC_MAX_COUNT];
- }str_mot;
-
- str_mot motor_1;
-
- void Delay(u32 nCount)
- {
- do{
- }
- while(nCount--);
- }
-
- int main()
- {
- int i = 0;
- float temp = 0;
- stm32_init();
-
- RCC_Cfg();
- GPIO_Cfg_gpio_b();
- motor_1.StepCount=0;
- acc_count=0;
- dec_count=0;
- PWM_Cfg();
- TIM_SetCompare4(TIM3,(20/2));
-
- acc_count = _stepmotor_calc(60,250,0.09,motor_1.acc_val);
- dec_count = _stepmotor_calc(60,150,0.005,motor_1.dec_val);
-
- for(i = 0;i= (_STEP_INT-dec_count))
- {
- TIM_SetAutoreload(TIM3,motor_1.dec_val[(dec_i)]);
- TIM_SetCompare4(TIM3,(motor_1.dec_val[(dec_i)]/2));
- dec_i++;
- }
- }
- }
|
|
2021-10-19 14:16:17
评论
举报
|
|
|