当前位置:网站首页>[untitled] PID control TT encoder motor
[untitled] PID control TT encoder motor
2022-04-23 13:02:00 【ZLP~】
Take notes of my study
Begin to learn PID It's hard for him to , It's not . First, we need to understand PID The basic process of controlling the motor , Then match it with your theoretical knowledge .
Let's talk about the specific implementation , First, set the timer of MCU to read AB Number of pulses in phase ( The first thing to note is that the channels for acquiring pulses can only be the first two channels of the timer ), And then in PID Compare the number of pulses you set with the measured number of pulses . Generally speaking, we set the speed , So we need to convert the speed into the number of pulses . for instance :TT The motor 13 The of the line makes the frequency quadruple , There are 2496 Pulse , We set 50 Turn every minute ( There's only one wheel speed here , If it is the internal speed of the motor, it needs to be divided by your reduction ratio ), Then we set the timer 10ms Read value once . that 10ms The number of pulses should be 50*2496/6000( One minute has 6000 individual 10ms), Therefore, the set pulse and the currently measured pulse are brought into PID Calculate the value according to the calculation formula , In the abstract, it is a basis deviation , The control effect . Generate a by calculating the value PWM The signal .
Here is part of the code , You can have a look at , Basically, that's all .
1. Set the timer
#include "encoder.h"
#include "delay.h"
#include "usart.h"
//PA0
//PA1
void TIM5_Encoder_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x00; // No prescaling
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(TIM5, &TIM_ICInitStructure);
TIM_SetCounter(TIM5,0);
TIM_Cmd(TIM5, ENABLE);
}
2.PID Calculation part
There are two calculation formulas here, which are written differently , You can use either ,PID The calculation formula uses a lot to see your own needs .
#include "pid.h"
static double Kp=10;
static double Ki=0;
static double Kd=0;
int shouxie_PID_ZLP(int now , int set)
{
int PWM,Bias,Bias_D;
static int error,last_Bias;
Bias = now - set;
error +=Bias;
Bias_D=Bias-last_Bias;
PWM = Kp*Bias + Ki*error +Kd*Bias_D;
last_Bias = Bias;
return PWM;
}
static double ProportionA=70;
static double IntegralA=25;
static double DerivativeA=35;
int SPEED_PID_A(int now,int set)
{
static int LastError; //Error[-1]
static int PrevError; //Error[-2]
int iError,Output;
iError=set-now;
Output = ((ProportionA * (iError - LastError)) + (IntegralA * iError) + (DerivativeA * (iError - 2*LastError + PrevError)));
PrevError=LastError;
LastError=iError;
return Output;
}
3. Control the output
#include "control.h"
#include "pid.h"
#include "datascope.h"
#include "motor.h"
#include "usart.h"
int para_A;
int Motor_A;
int SetRPMA=100;
#define SetPointA SetRPMA*2496/6000
int myabs(int a){
int temp;
if(a < 0) temp = -a;
else temp = a;
return temp;
}
int Read_Encoder(u8 x)
{
int Encoder_TIM;
switch(x)
{
case 5: Encoder_TIM= (short)TIM5 -> CNT; TIM5 -> CNT=0;break;
default: Encoder_TIM=0;
}
return Encoder_TIM;
}
int Encoder_A;
int Send_Count,i;
extern int adc_data;
int position_out;
int ac_speed_b;
int bigout_B;
void TIM6_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM6, TIM_IT_Update) != RESET)
{
Encoder_A=Read_Encoder(5); // Read the value of the encoder
para_A=SPEED_PID_A(Encoder_A,SetPointA);
/* Bring the set pulse and measured pulse into PID The controller */
printf("%d\r\n",Encoder_A);
if((para_A<-3)||(para_A>3))
{
Motor_A +=para_A;
}
if(Motor_A>7000)Motor_A=7000;// Limiting
if(Motor_A<-7000)Motor_A=-7000;
if(Motor_A > 0){
MotorA1 = 0;
MotorA2 = 1;
}else{
MotorA1 = 1;
MotorA2 = 0;
}TIM8->CCR1 = myabs(Motor_A);
/* Timer 8 I use a counting cycle of 10000 ,CCR1 The value of is myabs(Motor_A)*/
}
TIM_ClearITPendingBit(TIM6, TIM_FLAG_Update);
}
If you don't understand or I'm wrong, you can write it in the comment area .
版权声明
本文为[ZLP~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230613177293.html
边栏推荐
- HQL statement tuning
- STD:: shared of smart pointer_ ptr、std::unique_ ptr
- 98. Error s.e.errormvcautoconfiguration $staticview reported by freemaker framework: cannot render error page for request
- JMeter operation redis
- leetcode:437. Path sum III [DFS selected or not selected?]
- pyqt5 将opencv图片存入内置SQLlite数据库,并查询
- (1) Openjuterpyrab comparison scheme
- Idea的src子文件下无法创建servlet
- 在线计算过往日期天数,计算活了多少天
- Golang implements MD5, sha256 and bcrypt encryption
猜你喜欢
R语言中dcast 和 melt的使用 简单易懂
22. 括号生成
[csnote] ER diagram
Servlet监听器&过滤器介绍
No idle servers? Import OVF image to quickly experience smartx super fusion community version
ZigBee CC2530 minimum system and register configuration (1)
数据库中的日期时间类型
Melt reshape decast long data short data length conversion data cleaning row column conversion
云原生KubeSphere部署Redis
Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
随机推荐
STM32 is connected to the motor drive, the DuPont line supplies power, and then the back burning problem
Teach you to quickly develop a werewolf killing wechat applet (with source code)
Learning materials
Date time type in database
Golang implements MD5, sha256 and bcrypt encryption
22. 括号生成
Customize classloader and implement hot deployment - use loadclass
产品开发都应该知道的8个网站,增强工作体验
Introduction to servlet listener & filter
(personal) sorting out system vulnerabilities after recent project development
Use compressorjs to compress pictures, optimize functions, and compress pictures in all formats
7_ The cell type scores obtained by addmodule and gene addition method are compared in space
ZigBee CC2530 minimum system and register configuration (1)
拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇
Recovering data with MySQL binlog
The quill editor image zooms, multiple rich text boxes are used on one page, and the quill editor upload image address is the server address
HQL find the maximum value in a range
Hanlp word splitter (via spark)
Proteus 8.10 installation problem (personal test is stable and does not flash back!)
Golang implements a five insurance and one gold calculator with web interface