When we really want to use
the PID algorithm, we find that it is not so easy to implement the code in the book on our 51. Simply put, it cannot be called directly. A careful analysis will reveal that the C language code for PID implementation in the textbooks and on the Internet is almost all done with floating-point data. You can imagine how painful it would be if our calculations use floating-point data, and how painful it would be for our 51 single-chip microcomputer to run it.
Therefore, I figured out how to use an integer variable to implement the PID algorithm. Since it is done with integers, it is not very accurate, but for many occasions, this accuracy is enough. The coefficients and sampling voltages are all magnified 10 times. So the accuracy is not very high, but it is not so low, and it is enough for most occasions. If you really feel that the accuracy is not enough, you can magnify it 10 times or 100 times, but be careful not to exceed the range of the entire data type.
What I did is a PID algorithm with dead zone control.
For specific reference code, please see the following:
typedef struct PIDValue
{
uint32 Ek_Uint32[3]; //Difference storage, the difference between given and feedback
uint8 EkFlag_Uint8[3]; //Sign, 1 corresponds to Ek[i] is a negative number, 0 corresponds to Ek[i] is a positive number
uint8 KP_Uint8;
uint8 KI_Uint8;
uint8 KD_Uint8;
uint8 B_Uint8; //Dead zone voltage
uint8 KP; //Use when displaying modification
uint8 KI; //
uint8 KD; //
uint8 B; //
uint16 Uk_Uint16; //Control voltage at the last moment
}PIDValueStr;
PIDValueStr xdata PID;
/*******************************
**PID = Uk + (KP*E(k) - KI*E(k-1) + KD*E(k-2));
********************************/
void PIDProcess(void)
{
uint32 idata Temp[3]; //
uint32 idata PostSum; //Positive sum
uint32 idata NegSum; //Negative sum
Temp[0] = 0;
Temp[1] = 0;
Temp[2] = 0;
PostSum = 0;
NegSum = 0;
if( ADPool.Value_Uint16[UINADCH] > ADPool.Value_Uint16[UFADCH] ) //Given is greater than feedback, then EK is a positive number
{
Temp[0] = ADPool.Value_Uint16[UINADCH] - ADPool.Value_Uint16[UFADCH]; //Calculate Ek[0]
if( Temp[0] > PID.B_Uint8 )
{
//Value shift
PID.Ek_Uint32[2] = PID.Ek_Uint32[1];
PID.Ek_Uint32[1] = PID.Ek_Uint32[0];
PID.Ek_Uint32[0] = Temp[0];
//Sign shift
PID.EkFlag_Uint8[2] = PID.EkFlag_Uint8[1];
PID.EkFlag_Uint8[1] = PID.EkFlag_Uint8[0];
PID.EkFlag_Uint8[0] = 0; //Current EK is a positive number
Temp[0] = (uint32)PID.KP_Uint8 * PID.Ek_Uint32[0]; // KP*EK0
Temp[1] = (uint32)PID.KI_Uint8 * PID.Ek_Uint32[1]; // KI*EK1
Temp[2] = (uint32)PID.KD_Uint8 * PID.Ek_Uint32[2]; // KD*EK2
} }
else
//Feedback is greater than given
{
Temp[0] = ADPool.Value_Uint16[UFADCH] - ADPool.Value_Uint16[UINADCH]; //Calculate Ek[0]
if( Temp[0] > PID.B_Uint8 )
{
//Value shift
PID.Ek_Uint32[2] = PID.Ek_Uint32[1];
PID.Ek_Uint32[1] = PID.Ek_Uint32[0];
PID.Ek_Uint32[0] = Temp[0];
//Sign shift
PID.EkFlag_Uint8[2] = PID.EkFlag_Uint8[1];
PID.EkFlag_Uint8[1] = PID.EkFlag_Uint8[0];
PID.EkFlag_Uint8[0] = 1; //The current EK is a negative number
Temp[0] = (uint32 )PID.KP_Uint8 * PID.Ek_Uint32[0]; // KP*EK0
Temp[1] = (uint32)PID.KI_Uint8 * PID.Ek_Uint32[1]; // KI*EK1
Temp[2] = (uint32)PID .KD_Uint8 * PID.Ek_Uint32[2]; // KD*EK2
}
}[page]
if(PID.EkFlag_Uint8[0]==0)
{
PostSum += Temp[0]; //Positive sum
}
else
{
NegSum += Temp[0]; //Negative sum
} // KP*EK0
if(PID.EkFlag_Uint8[1]!=0)
{
PostSum += Temp[1]; //Positive sum
}
else
{
NegSum += Temp[1]; //Negative sum
} // - kI * EK1
if(PID.EkFlag_Uint8[2]==0)
{
PostSum += Temp[2]; //Positive sum
}
else
{
NegSum += Temp[2]; //Negative sum
} // KD * EK2
PostSum += (uint32)PID.Uk_Uint16; //
if( PostSum > NegSum ) // Is the control amount positive
?{
Temp[0] = PostSum - NegSum;
if( Temp[0] < (uint32)ADPool.Value_Uint16[UMAXADCH] ) //If it is less than the limit value, it is the calculated value output
{
PID.Uk_Uint16 = (uint16)Temp[0];
}
else
{
PID.Uk_Uint16 = ADPool.Value_Uint16[UMAXADCH]; //Otherwise, it is the limit value output
}
}
else //If the control quantity output is a negative number, it will output 0
{
PID.Uk_Uint16 = 0;
}
Previous article:Design of a handheld electronic nose based on STC89C516RD+ single chip microcomputer
Next article:16X2 character LCD display module driver
Recommended ReadingLatest update time:2024-11-16 16:46
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- China's first batch of autonomous driving unmanned commercial licenses issued! Do you dare to sit in an unmanned car? ?
- GDB debugging and DSP
- The IO pin of MSP430 is set as input, but it receives the output signal of the Hall sensor, and the chip-side signal does not switch.
- [Serial] [Starlight Lightning STM32F407 Development Board] Chapter 10 Serial Communication Experiment
- Where do errors in vector network analyzers come from?
- The relationship between serial port communication and interruption
- How to use a wiring harness tester to measure small resistance? Here's a method
- Those who don't know about RF MEMS, please take a look. This is what the technology is like.
- General Design Considerations for Embedded Systems
- Download the NI white paper "Overcoming the Challenges of Production Test for Complex Devices Under Test" for free and get a chance to win prizes!