Xiao Ming was given a task: There is a water leak in the tank (the leak rate isn't necessarily constant), and the goal is to keep the water level at a certain position. Once the water level drops below the required level, he must add water to the tank. After receiving the task, Xiao Ming stayed by the tank. When he got bored after a long time, he went into the room to read a novel. He checked the water level every 30 minutes. However, the leak was too fast, and every time he came back, the water level was almost gone, far from the desired height. He tried checking every 3 minutes, but the result was still the same — the tank was always leaking. Remembering the PID algorithm he used when he was in university for robot control, he decided to try applying it to this problem. Here's the source code he found: #define PID_Uint struct pid_uint /************************************************* ******************* /************************************************* ******************* /************************************************* ******************* After trying different approaches, Xiao Ming realized that adding water too often was not effective. After several trials, he decided to check the water level every 10 minutes. This interval became known as the sampling period. Initially, he used a scoop to add water, but the faucet was more than ten meters away from the tank. He had to run back and forth many times, which wasn’t efficient. So he switched to using buckets. One bucket filled the tank faster, and the number of trips decreased. However, there were instances where the tank overflowed, and his shoes got wet a few times. Xiao Ming thought again and decided to use a pot instead. He could fill it just right without overfilling, reducing the number of trips and avoiding overflow. The size of the watering tool was referred to as the scale factor. But even with the pot, sometimes the water level would go above the target, risking another spill. To solve this, he placed a funnel on the tank. Instead of pouring directly into the tank, he poured into the funnel, allowing the water to flow slowly. This prevented overflow, but it made the filling process slower. Sometimes, the water couldn’t keep up with the leak. He experimented with different funnel sizes to adjust the flow speed until he found one that worked well. The time it took for the water to pass through the funnel was called the integral time. Finally, Xiao Ming felt relieved, but the task requirements suddenly became stricter. The water level needed to be controlled more precisely and quickly. If the level dropped too low, water had to be added immediately to the exact position. Any excess would result in no payment. Xiao Ming was stuck again. He thought hard and finally came up with an idea: he placed a spare bucket next to the tank. When the water level dropped, he would take a pot of water directly, skipping the funnel. This ensured quick response, but sometimes the water level would rise too high. To fix this, he drilled a hole above the water surface and connected a pipe to the spare bucket below. This allowed extra water to leak out, balancing the system. The speed at which this water leaked was called the differential time.
The Aluminum Alloy Creative Notebook Stand is stable and durable, stable and does not shake. Laptop Stand Ergonomic bone is supported by an aluminum alloy triangle-shaped stable structure, which has a strong load-bearing capacity and can easily carry 100 g items, and Laptop Stand Holder is stable and not easy to damage.
Aluminum Alloy Laptop Bracket,Laptop Stand To Keep Cool,Dell Laptop Vertical Stand,Aluminium Alloy Notebook Stand Shenzhen ChengRong Technology Co.,Ltd. , https://www.laptopstandsupplier.com
PID_Uint
{
Int U_kk;
Int ekk;
Int ekkk;
Int Ur; // Limited output value, needs to be initialized
Int Un; // Insensitive area
// int multiple; // The gain of the PID coefficient. In data shaping cases, the precision of PID parameters is fixed to 256.
Int Kp; // Proportional term
Int Ti; // Integral term
Int Td; // Derivative term, set to 0 when using the patrol board
Int k1;
Int k2;
Int k3;
};
Function name: void Init_PID_uint(PID_uint *p)
Function: Initialize PID parameters
Description: Before calling this function, Kp, Ti, Td should be set first, simplifying the formula entry parameters: PID unit parameter structure body
Return value: None
************************************************** *********************/
Void Init_PID_uint(PID_Uint *p)
{
p->k1 = (p->Kp) + (p->Kp)*1024/(p->Ti) + (p->Kp)*(p->Td)/1024;
p->k2 = (p->Kp) + 2*(p->Kp)*(p->Td)/1024;
p->k3 = (p->Kp)*(p->Td)/1024;
}
Function name: void reset_Uk(PID_Uint *p)
Function: Initialize U_kk, ekk, ekkk
Description: Called during initialization, can also be called when changing PID parameters: PID unit parameter structure address
Return value: None
************************************************** *********************/
Void reset_Uk(PID_Uint *p)
{
p->U_kk = 0;
p->ekk = 0;
p->ekkk = 0;
}
Function name: int PID_common(int set, int jiance, PID_Uint *p)
Function: General PID function
Description: Calculate the control quantity of any single PID. Entry parameters: expected value, measured value, PID unit structure
Return value: PID control amount
************************************************** *********************/
Int PID_common(int set, int jiance, PID_Uint *p)
{
Int ek, U_k = 0;
ek = jiance - set;
if ((ek > (p->Un)) || (ek < -(p->Un))) // Integral insensitive area
U_k = (p->U_kk) + (p->k1)*ek - (p->k2)*(p->ekk) + (p->k3)*(p->ekkk);
p->U_kk = U_k;
p->ekkk = p->ekk;
p->ekk = ek;
if (U_k > (p->Ur)) // Limit maximum output
U_k = p->Ur;
if (U_k < -(p->Ur))
U_k = -(p->Ur);
return U_k / 1024;
}