|
100黑币
自己写了一个四轮的小车,主控是stm32f103rct6
目前的问题是,不论怎么设置初值,车轮总是全速转动,并且拔掉编码器的线,车轮转速依旧没啥变化,不知道是哪里除了问题
单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "encoder.h"
- #include "timer.h"
- #include "pwm.h"
- #include "pid.h"
- #include "motor.h"
- #include <stdio.h>
- #include "usart.h"
- int FleftSpeedNow =0;
- int FrightSpeedNow =0;
- int BleftSpeedNow =0;
- int BrightSpeedNow =0;
- int FleftSpeeSet =400;//mm/s
- int FrightSpeedSet = 2000;//mm/s
- int BleftSpeeSet = 600;//mm/s
- int BrightSpeedSet = 600;//mm/s
- int main(void)
- {
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//禁用JTAG 启用 SWD
-
- MY_NVIC_PriorityGroupConfig(2); //=====设置中断分组
- delay_init(); //=====延时函数初始化
- Motor_Init(); //电机初始化
- Encoder_Init_TIM2(); //=====初始化编码器
- Encoder_Init_TIM1();
- Encoder_Init_TIM3();
- Encoder_Init_TIM4(); //=====初始化编码器
- PWM_Init(7199,0); //=====初始化PWM
- TIM5_Int_Init(50-1,7200-1); //=====定时器初始化 5ms一次中断
- PID_Init(); //=====PID参数初始化
- uart_init(9600);
- FrontmotorLeft=400;
- //闭环速度控制
- while(1)
- {
- //给速度设定值,想修改速度,就更该leftSpeeSet、rightSpeedSet变量的值
- Fpid_Task_Letf.speedSet = FleftSpeeSet;
- Fpid_Task_Right.speedSet = FrightSpeedSet;
- Bpid_Task_Letf.speedSet = BleftSpeeSet;
- Bpid_Task_Right.speedSet = BrightSpeedSet;
-
- //给定速度实时值
- Fpid_Task_Letf.speedNow = FleftSpeedNow;
- Fpid_Task_Right.speedNow = FrightSpeedNow;
- Bpid_Task_Letf.speedNow = BleftSpeedNow;
- Bpid_Task_Right.speedNow = BrightSpeedNow;
-
- //执行PID控制函数
- Pid_Ctrl(&FrontmotorLeft ,&FrontmotorRight,&BackmotorLeft ,&BackmotorRight);
-
- //根据PID计算的PWM数据进行设置PWM
- Set_Pwm_Front(FrontmotorLeft,FrontmotorRight);
- Set_Pwm_Back(BackmotorLeft,BackmotorRight);
- delay_ms(10);
- }
- }
- //5ms 定时器中断服务函数 --> 计算速度实时值,运行该程序之前,确保自己已经能获得轮速,如果不懂,可看之前电机测速的文章
- void TIM5_IRQHandler(void) //TIM7中断
- {
- if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否
- {
- TIM_ClearITPendingBit(TIM5, TIM_IT_Update); //清除TIMx的中断待处理位
-
- Get_Motor_Speed(&FleftSpeedNow ,&FrightSpeedNow,&BleftSpeedNow,&BrightSpeedNow);//计算电机速度
-
-
- }
- }
- #include "sys.h"
- #include "delay.h"
- #include "encoder.h"
- #include "timer.h"
- #include "pwm.h"
- #include "pid.h"
- #include "motor.h"
- #include <stdio.h>
- #include "usart.h"
- int FleftSpeedNow =0;
- int FrightSpeedNow =0;
- int BleftSpeedNow =0;
- int BrightSpeedNow =0;
- int FleftSpeeSet =400;//mm/s
- int FrightSpeedSet = 2000;//mm/s
- int BleftSpeeSet = 600;//mm/s
- int BrightSpeedSet = 600;//mm/s
- int main(void)
- {
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//禁用JTAG 启用 SWD
-
- MY_NVIC_PriorityGroupConfig(2); //=====设置中断分组
- delay_init(); //=====延时函数初始化
- Motor_Init(); //电机初始化
- Encoder_Init_TIM2(); //=====初始化编码器
- Encoder_Init_TIM1();
- Encoder_Init_TIM3();
- Encoder_Init_TIM4(); //=====初始化编码器
- PWM_Init(7199,0); //=====初始化PWM
- TIM5_Int_Init(50-1,7200-1); //=====定时器初始化 5ms一次中断
- PID_Init(); //=====PID参数初始化
- uart_init(9600);
- FrontmotorLeft=400;
- //闭环速度控制
- while(1)
- {
- //给速度设定值,想修改速度,就更该leftSpeeSet、rightSpeedSet变量的值
- Fpid_Task_Letf.speedSet = FleftSpeeSet;
- Fpid_Task_Right.speedSet = FrightSpeedSet;
- Bpid_Task_Letf.speedSet = BleftSpeeSet;
- Bpid_Task_Right.speedSet = BrightSpeedSet;
-
- //给定速度实时值
- Fpid_Task_Letf.speedNow = FleftSpeedNow;
- Fpid_Task_Right.speedNow = FrightSpeedNow;
- Bpid_Task_Letf.speedNow = BleftSpeedNow;
- Bpid_Task_Right.speedNow = BrightSpeedNow;
-
- //执行PID控制函数
- Pid_Ctrl(&FrontmotorLeft ,&FrontmotorRight,&BackmotorLeft ,&BackmotorRight);
-
- //根据PID计算的PWM数据进行设置PWM
- Set_Pwm_Front(FrontmotorLeft,FrontmotorRight);
- Set_Pwm_Back(BackmotorLeft,BackmotorRight);
- delay_ms(10);
- }
- }
- //5ms 定时器中断服务函数 --> 计算速度实时值,运行该程序之前,确保自己已经能获得轮速,如果不懂,可看之前电机测速的文章
- void TIM5_IRQHandler(void) //TIM7中断
- {
- if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否
- {
- TIM_ClearITPendingBit(TIM5, TIM_IT_Update); //清除TIMx的中断待处理位
-
- Get_Motor_Speed(&FleftSpeedNow ,&FrightSpeedNow,&BleftSpeedNow,&BrightSpeedNow);//计算电机速度
-
-
- }
- }
复制代码
|
|