您的位置 首页 电路

增量式PID的stm32完成,整定进程

首先说说增量式PID的公式,这个关系到MCU算法公式的书写,实际上两个公式的写法是同一个公式变换来得,不同的是系数的差异。资料上比较多的

首要说说增量式PID的公式,这个关系到MCU算法公式的书写,实际上两个公式的写法是同一个公式改换来得,不同的是系数的差异。
材料上比较多的是:

还有一种是:

感觉第二种的Kp Ki Kd比较清楚,更好了解,下面介绍的就以第二种来吧。(份额、积分、微分三个环节的作用这儿就具体翻开,百度会有许多)

硬件部分:
操控系统的操控目标是4个空心杯直流电机,电机带光电编码器,能够反应转速巨细的波形。电机驱动模块是一般的L298N模块。
芯片型号,STM32F103ZET6

软件部分:
PWM输出:TIM3,能够直接输出4路不通占空比的PWM波
PWM捕获:STM32除了TIM6 TIM7其他的都有捕获功用,运用TIM1 TIM2 TIM4 TIM5四个定时器捕获四个反应信号
PID的采样和处理:运用了根本定时器TIM6,溢出时刻便是我的采样周期,理论上T越小作用会越好,这儿我取20ms,依据操控目标吧,假如操控水温什么的采样周期会是几秒几分钟什么的。

上面的PWM输出和捕获关于定时器的设置都有例程,我这儿是这样的:
TIM3输出四路PWM,在引脚 C 的 GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9输出
四路捕获分别是TIM4TIM1TIM2TIM5,对应引脚是:PB7 PE11 PB3 PA1
高档定时器tim1的初始化略不同,它的中止”称号“和通用定时器不同,见代码:

  1. void TIM3_PWM_Init(u16 arr,u16 psc)
  2. {
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
  5. TIM_OCInitTypeDefTIM_OCInitStructure;
  6. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO, ENABLE);//使能GPIO外设和AFIO复用功用模块时钟使能
  8. GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //Timer3全映射 GPIOC-> 6,7,8,9//用于TIM3的CH2输出的PWM经过该LED显现
  9. //设置该引脚为复用输出功用,输出TIM3 CH1 CH2 CH3 CH4 的PWM脉冲波形
  10. GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; //初始化GPIO
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_Init(GPIOC, &GPIO_InitStructure);
  14. GPIO_ResetBits(GPIOC,GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);//默许电机使能端状况:不使能
  15. TIM_TimeBaseStructure.TIM_Period = arr; //设置鄙人一个更新事情装入活动的主动重装载寄存器周期的值
  16. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值这儿是72分频,那么时钟频率便是1M
  17. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟切割:TDTS = Tck_tim
  18. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数形式
  19. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //依据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时刻基数单位
  20. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //挑选定时器形式:TIM脉冲宽度调制形式1
  21. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  22. TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
  23. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  24. TIM_OC1Init(TIM3, &TIM_OCInitStructure);//依据TIM_OCInitStruct中指定的参数初始化外设TIMx
  25. TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能TIMx在CCR1上的预装载寄存器
  26. TIM_OC2Init(TIM3, &TIM_OCInitStructure);//依据TIM_OCInitStruct中指定的参数初始化外设TIMx
  27. TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能TIMx在CCR2上的预装载寄存器
  28. TIM_OC3Init(TIM3, &TIM_OCInitStructure);//依据TIM_OCInitStruct中指定的参数初始化外设TIMx
  29. TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能TIMx在CCR3上的预装载寄存器
  30. TIM_OC4Init(TIM3, &TIM_OCInitStructure);//依据TIM_OCInitStruct中指定的参数初始化外设TIMx
  31. TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能TIMx在CCR4上的预装载寄存器
  32. TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
  33. TIM_Cmd(TIM3, ENABLE);//使能TIMx外设
  34. }
  35. void TIM4_PWMINPUT_INIT(u16 arr,u16 psc)
  36. {
  37. TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM的初始化结构体
  38. NVIC_InitTypeDef NVIC_InitStructure;//中止装备
  39. TIM_ICInitTypeDefTIM4_ICInitStructure;//TIM4PWM装备结构体
  40. GPIO_InitTypeDef GPIO_InitStructure;//IO口装备结构体
  41. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//Open TIM4 clock
  42. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//open gpioB clock
  43. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//GPIO 7
  44. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
  45. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  46. GPIO_Init(GPIOB, &GPIO_InitStructure);
  47. TIM_TimeBaseStructure.TIM_Period = arr; //设置鄙人一个更新事情装入活动的主动重装载寄存器周期的值
  48. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  49. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟切割:TDTS = Tck_tim
  50. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数形式
  51. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //依据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时刻基数单位
  52. NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  53. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  54. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  55. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  56. NVIC_Init(&NVIC_InitStructure);
  57. TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2;
  58. TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  59. TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  60. TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  61. TIM4_ICInitStructure.TIM_ICFilter = 0x3;//Filter:过滤
  62. TIM_PWMIConfig(TIM4, &TIM4_ICInitStructure);//PWM输入装备
  63. TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);//挑选有用输入端
  64. TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);//装备为主从复位形式
  65. TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);//发动定时器的被迫触发
  66. TIM_ITConfig(TIM4, TIM_IT_CC2|TIM_IT_Update, ENABLE);//中止装备
  67. TIM_ClearITPendingBit(TIM4, TIM_IT_CC2|TIM_IT_Update); //铲除中止标志位
  68. TIM_Cmd(TIM4, ENABLE);
  69. }
  70. void TIM4_IRQHandler(void)
  71. {
  72. if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)//捕获1产生捕获事情
  73. {
  74. duty_TIM4=TIM_GetCapture1(TIM4); //收集占空比
  75. if(TIM_GetCapture2(TIM4)>600)period_TIM4=TIM_GetCapture2(TIM4);//简略的处理
  76. CollectFlag_TIM4 = 0;
  77. }
  78. TIM_ClearITPendingBit(TIM4, TIM_IT_CC2|TIM_IT_Update); //铲除中止标志位
  79. }
  80. void TIM1_PWMINPUT_INIT(u16 arr,u16 psc)
  81. {
  82. TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM的初始化结构体
  83. NVIC_InitTypeDef NVIC_InitStructure;//中止装备
  84. TIM_ICInitTypeDefTIM1_ICInitStructure;//PWM装备结构体
  85. GPIO_InitTypeDef GPIO_InitStructure;//IO口装备结构体
  86. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//Open TIM1 clock
  87. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);//open gpioE clock
  88. GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //Timer1彻底重映射TIM1_CH2->PE11
  89. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//GPIO 11
  90. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
  91. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  92. GPIO_Init(GPIOE, &GPIO_InitStructure);
  93. TIM_TimeBaseStructure.TIM_Period = arr; //设置鄙人一个更新事情装入活动的主动重装载寄存器周期的值
  94. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  95. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟切割:TDTS = Tck_tim
  96. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数形式
  97. TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //依据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时刻基数单位
  98. NVIC_InitStructure.NVIC_IRQChannel =TIM1_CC_IRQn;//TIM1捕获中止
  99. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  100. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  101. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  102. NVIC_Init(&NVIC_InitStructure);
  103. TIM1_ICInitStructure.TIM_Channel = TIM_Channel_2;
  104. TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  105. TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  106. TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  107. TIM1_ICInitStructure.TIM_ICFilter = 0x03;//Filter:过滤
  108. TIM_PWMIConfig(TIM1, &TIM1_ICInitStructure);//PWM输入装备
  109. TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2);//挑选有用输入端
  110. TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);//装备为主从复位形式
  111. TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);//发动定时器的被迫触发
  112. // TIM_ITConfig(TIM1, TIM_IT_CC2|TIM_IT_Update, ENABLE);//中止装备
  113. TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE); //通道2 捕获中止翻开
  114. //TIM_ClearITPendingBit(TIM1, TIM_IT_CC2|TIM_IT_Update); //铲除中止标志位
  115. TIM_Cmd(TIM1, ENABLE);
  116. }
  117. void TIM1_CC_IRQHandler(void)
  118. {
  119. {
  120. if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET)//捕获1产生捕获事情
  121. {
  122. duty_TIM1=TIM_GetCapture1(TIM1); //收集占空比
  123. if(TIM_GetCapture2(TIM1)>600)period_TIM1=TIM_GetCapture2(TIM1);
  124. CollectFlag_TIM1 = 0;
  125. }
  126. }
  127. TIM_ClearITPendingBit(TIM1, TIM_IT_CC2|TIM_IT_Update); //铲除中止标志位
  128. }
  129. void TIM2_PWMINPUT_INIT(u16 arr,u16 psc)
  130. {
  131. TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM的初始化结构体
  132. NVIC_InitTypeDef NVIC_InitStructure;//中止装备
  133. TIM_ICInitTypeDefTIM2_ICInitStructure;//TIM2PWM装备结构体
  134. GPIO_InitTypeDef GPIO_InitStructure;//IO口装备结构体
  135. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//Open TIM2 clock
  136. // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//open gpioB clock
  137. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE);//使能GPIO外设和AFIO复用功用模块时钟
  138. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//封闭JTAG
  139. GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer2彻底重映射TIM2_CH2->PB3
  140. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//GPIO 3
  141. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//浮空输入 上拉输入
  142. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  143. GPIO_Init(GPIOB, &GPIO_InitStructure);
  144. TIM_TimeBaseStructure.TIM_Period = arr; //设置鄙人一个更新事情装入活动的主动重装载寄存器周期的值
  145. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  146. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟切割:TDTS = Tck_tim
  147. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数形式
  148. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //依据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时刻基数单位
  149. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  150. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  151. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  152. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  153. NVIC_Init(&NVIC_InitStructure);
  154. TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2;
  155. TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  156. TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  157. TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  158. TIM2_ICInitStructure.TIM_ICFilter = 0x3;//Filter:过滤
  159. TIM_PWMIConfig(TIM2, &TIM2_ICInitStructure);//PWM输入装备
  160. TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);//挑选有用输入端
  161. TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);//装备为主从复位形式
  162. TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//发动定时器的被迫触发
  163. TIM_ITConfig(TIM2, TIM_IT_CC2|TIM_IT_Update, ENABLE);//中止装备
  164. TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //铲除中止标志位
  165. TIM_Cmd(TIM2, ENABLE);
  166. }
  167. void TIM2_IRQHandler(void)
  168. {
  169. {
  170. if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)//捕获1产生捕获事情
  171. {
  172. duty_TIM2=TIM_GetCapture1(TIM2); //收集占空比
  173. if(TIM_GetCapture2(TIM2)>600)period_TIM2=TIM_GetCapture2(TIM2);
  174. CollectFlag_TIM2 = 0;
  175. }
  176. }
  177. TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //铲除中止标志位
  178. }

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/dianlu/255753.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部