增量式PID的stm32實現(xiàn),整定過程
資料上比較多的是:
還有一種是:
感覺第二種的Kp Ki Kd比較清楚,更好理解,下面介紹的就以第二種來吧。(比例、積分、微分三個環(huán)節(jié)的作用這里就詳細(xì)展開,百度會有很多)
硬件部分:
控制系統(tǒng)的控制對象是4個空心杯直流電機,電機帶光電編碼器,可以反饋轉(zhuǎn)速大小的波形。電機驅(qū)動模塊是普通的L298N模塊。
芯片型號,STM32F103ZET6
軟件部分:
PWM輸出:TIM3,可以直接輸出4路不通占空比的PWM波
PWM捕獲:STM32除了TIM6 TIM7其余的都有捕獲功能,使用TIM1 TIM2 TIM4 TIM5四個定時器捕獲四個反饋信號
PID的采樣和處理:使用了基本定時器TIM6,溢出時間就是我的采樣周期,理論上T越小效果會越好,這里我取20ms,依據(jù)控制對象吧,如果控制水溫什么的采樣周期會是幾秒幾分鐘什么的。
上面的PWM輸出和捕獲關(guān)于定時器的設(shè)置都有例程,我這里是這樣的:
TIM3輸出四路PWM,在引腳 C 的 GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9輸出
四路捕獲分別是TIM4
高級定時器tim1的初始化略不同,它的中斷”名稱“和通用定時器不同,見代碼:
- void TIM3_PWM_Init(u16 arr,u16 psc)
- {
GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外設(shè)和AFIO復(fù)用功能模塊時鐘使能 GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //Timer3全映射 GPIOC-> 6,7,8,9 //用于TIM3的CH2輸出的PWM通過該LED顯示 //設(shè)置該引腳為復(fù)用輸出功能,輸出TIM3 CH1 CH2 CH3 CH4 的PWM脈沖波形 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; //初始化GPIO GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_ResetBits(GPIOC,GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);//默認(rèn)電機使能端狀態(tài):不使能 TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值 這里是72分頻,那么時鐘頻率就是1M TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時間基數(shù)單位 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //選擇定時器模式:TIM脈沖寬度調(diào)制模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能 TIM_OCInitStructure.TIM_Pulse = 0; //設(shè)置待裝入捕獲比較寄存器的脈沖值 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高 TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根據(jù)TIM_OCInitStruct中指定的參數(shù)初始化外設(shè)TIMx TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR1上的預(yù)裝載寄存器 TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根據(jù)TIM_OCInitStruct中指定的參數(shù)初始化外設(shè)TIMx TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR2上的預(yù)裝載寄存器 TIM_OC3Init(TIM3, &TIM_OCInitStructure); //根據(jù)TIM_OCInitStruct中指定的參數(shù)初始化外設(shè)TIMx TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR3上的預(yù)裝載寄存器 TIM_OC4Init(TIM3, &TIM_OCInitStructure); //根據(jù)TIM_OCInitStruct中指定的參數(shù)初始化外設(shè)TIMx TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR4上的預(yù)裝載寄存器 TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的預(yù)裝載寄存器 TIM_Cmd(TIM3, ENABLE); //使能TIMx外設(shè) - }
- void TIM4_PWMINPUT_INIT(u16 arr,u16 psc)
- {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //TIM的初始化結(jié)構(gòu)體 NVIC_InitTypeDef NVIC_InitStructure; //中斷配置 TIM_ICInitTypeDef TIM4_ICInitStructure; //TIM4 PWM配置結(jié)構(gòu)體 GPIO_InitTypeDef GPIO_InitStructure; //IO口配置結(jié)構(gòu)體 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Open TIM4 clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //open gpioB clock GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //GPIO 7 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時間基數(shù)單位 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio rity = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM4_ICInitStructure.TIM_ICFilter = 0x3; //Filter:過濾 TIM_PWMIConfig(TIM4, &TIM4_ICInitStructure); //PWM輸入配置 TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); //選擇有效輸入端 TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset); //配置為主從復(fù)位模式 TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);//啟動定時器的被動觸發(fā) TIM_ITConfig(TIM4, TIM_IT_CC2|TIM_IT_Update, ENABLE); //中斷配置 TIM_ClearITPendingBit(TIM4, TIM_IT_CC2|TIM_IT_Update); //清除中斷標(biāo)志位 TIM_Cmd(TIM4, ENABLE); - }
- void TIM4_IRQHandler(void)
- {
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)//捕獲1發(fā)生捕獲事件 { duty_TIM4 = TIM_GetCapture1(TIM4); //采集占空比 if (TIM_GetCapture2(TIM4)>600) period_TIM4 = TIM_GetCapture2(TIM4);//簡單的處理 CollectFlag_TIM4 = 0; } TIM_ClearITPendingBit(TIM4, TIM_IT_CC2|TIM_IT_Update); //清除中斷標(biāo)志位 - }
- void TIM1_PWMINPUT_INIT(u16 arr,u16 psc)
- {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //TIM的初始化結(jié)構(gòu)體 NVIC_InitTypeDef NVIC_InitStructure; //中斷配置 TIM_ICInitTypeDef TIM1_ICInitStructure; //PWM配置結(jié)構(gòu)體 GPIO_InitTypeDef GPIO_InitStructure; //IO口配置結(jié)構(gòu)體 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //Open TIM1 clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //open gpioE clock GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //Timer1完全重映射 TIM1_CH2->PE11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //GPIO 11 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE, &GPIO_InitStructure); TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時間基數(shù)單位 NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //TIM1捕獲中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio rity = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM1_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM1_ICInitStructure.TIM_ICFilter = 0x03; //Filter:過濾 TIM_PWMIConfig(TIM1, &TIM1_ICInitStructure); //PWM輸入配置 TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2); //選擇有效輸入端 TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset); //配置為主從復(fù)位模式 TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);//啟動定時器的被動觸發(fā) - // TIM_ITConfig(TIM1, TIM_IT_CC2|TIM_IT_Update, ENABLE);
//中斷配置 TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE); //通道2 捕獲中斷打開 //TIM_ClearITPendingBit(TIM1, TIM_IT_CC2|TIM_IT_Update); //清除中斷標(biāo)志位 TIM_Cmd(TIM1, ENABLE); - }
- void TIM1_CC_IRQHandler(void)
- {
{ if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET)//捕獲1發(fā)生捕獲事件 { duty_TIM1 = TIM_GetCapture1(TIM1); //采集占空比 if (TIM_GetCapture2(TIM1)>600) period_TIM1 = TIM_GetCapture2(TIM1); CollectFlag_TIM1 = 0; } } TIM_ClearITPendingBit(TIM1, TIM_IT_CC2|TIM_IT_Update); //清除中斷標(biāo)志位 - }
- void TIM2_PWMINPUT_INIT(u16 arr,u16 psc)
- {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //TIM的初始化結(jié)構(gòu)體 NVIC_InitTypeDef NVIC_InitStructure; //中斷配置 TIM_ICInitTypeDef TIM2_ICInitStructure; //TIM2 PWM配置結(jié)構(gòu)體 GPIO_InitTypeDef GPIO_InitStructure; //IO口配置結(jié)構(gòu)體 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //Open TIM2 clock - // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//open gpioB clock - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB
| RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外設(shè)和AFIO復(fù)用功能模塊時鐘 - GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
//關(guān)閉JTAG GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer2完全重映射 TIM2_CH2->PB3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //GPIO 3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //浮空輸入 上拉輸入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時間基數(shù)單位 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio rity = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM2_ICInitStructure.TIM_ICFilter = 0x3; //Filter:過濾 TIM_PWMIConfig(TIM2, &TIM2_ICInitStructure); //PWM輸入配置 TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); //選擇有效輸入端 TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //配置為主從復(fù)位模式 TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//啟動定時器的被動觸發(fā) TIM_ITConfig(TIM2, TIM_IT_CC2|TIM_IT_Update, ENABLE); //中斷配置 TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //清除中斷標(biāo)志位 TIM_Cmd(TIM2, ENABLE); - }
- void TIM2_IRQHandler(void)
- {
{ if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)//捕獲1發(fā)生捕獲事件 { duty_TIM2 = TIM_GetCapture1(TIM2); //采集占空比 if (TIM_GetCapture2(TIM2)>600) period_TIM2 = TIM_GetCapture2(TIM2); CollectFlag_TIM2 = 0; } } TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //清除中斷標(biāo)志位 - }
-
關(guān)鍵詞:
增量式PIDstm32整定過
評論