Closed-Loop Control Principles and Examples - 我的飛鳥集
Closed-Loop Control Principles and Examples
·(edited)·· reads· likes0
AI TranslationSimplified ChineseEnglish
Key Insights
This article explains closed-loop control for motor speed and steering angle in vehicles. It begins by contrasting open-loop control, which fails under varying conditions like slopes or battery voltage, with closed-loop control that uses feedback to correct errors. The core concept is that the controller adjusts output based on the difference (error) between the target and actual output. The article details how to derive a closed-loop transfer function and explains how feedback rejects disturbances. It then focuses on PID controllers, explaining the roles of proportional (P), integral (I) for eliminating steady-state error, and derivative (D) for damping. Practical examples are given for a drive motor speed loop (typically using PI) and a steering angle loop (using PID), including their mathematical models. Key implementation challenges are discussed, such as discrete-time effects, PWM saturation and integral windup, sensor noise, and time delays, which can cause instability in real systems. The article emphasizes that good control requires understanding the physical system, not just tuning PID gains.
Closed-Loop Control Principles and Examples
0. Introduction
In practical device control, a common question arises: if you give the drive motor a target speed v_{\mathrm{ref}}=3\mathrm{m/s}, will the vehicle actually run stably at 3\mathrm{m/s}? If you give the steering motor a target angle \delta_{\mathrm{ref}}=10^\circ, will the front wheels actually hold precisely at 10^\circ?
If you simply supply a fixed PWM to the motor based on experience, the answer is usually no.
Battery voltage fluctuates, motors have internal resistance and back-EMF, mechanical systems have inertia and friction, and the vehicle is also affected by road grade, tire resistance, and self-aligning torque. The same PWM yields completely different speeds depending on whether the vehicle is jacked up, running on level ground, or climbing a slope; likewise, the same steering motor output can correspond to different actual wheel angles when stationary versus at high speed.
Therefore, what a practical control system must truly address is:
\boxed{\text{输出与目标之间存在误差时,系统应该如何自动修正?}}
This is the problem that closed-loop control is meant to solve.
Starting from closed-loop control itself, this article further combines the drive motor speed loop and the steering motor angle loop to analyze how a real control system begins with a physical model, goes through mathematical modeling, controller design, and stability analysis, and ultimately becomes a control algorithm that runs on an MCU.
1. Starting with Open-Loop Control
Suppose we want to control a motor.
The simplest approach is to give the motor a control input u(t) based on experience, e.g., a PWM signal with a certain duty cycle. This control input passes through the motor and mechanical structure and ultimately produces an output y(t).
From a system perspective, this can be written as:
u(t)\longrightarrow G(s)\longrightarrow y(t)
Here G(s) represents the plant — the system formed collectively by the motor, transmission, tires, load, and all other physical processes.
For a drive motor, it can be simply understood as:
\mathrm{PWM}\longrightarrow\text{驱动电路}\longrightarrow\text{电机}\longrightarrow\text{车轮}\longrightarrow v
This method of control is called open-loop control.
The problem with open-loop control is:
The same input produces the same output under all conditions.
But a real system obviously does not satisfy this condition.
For example, an experiment shows that when the PWM duty cycle is 40%, the vehicle runs stably at 3\mathrm{m/s} on flat ground. Can we conclude that as long as we always output 40% PWM, the speed will always be 3\mathrm{m/s}?
No.
When the vehicle enters a slope, the load torque increases; when the battery voltage drops, the average motor voltage for the same duty cycle decreases; tire temperature, ground friction, mechanical transmission efficiency, and even vehicle mass changes all affect the final speed.
So the actual speed might become: v=2.6\mathrm{m/s}, yet the controller still outputs the original PWM
This is the most fundamental problem of open-loop systems:
\boxed{\text{控制器没有利用实际输出的信息}}
2. Closed-Loop Control
If we want the system to actively correct itself, the actual output must be fed back to the controller.
Let the system target be r(t) and the actual output be y(t).
Define the error:
\boxed{e(t)=r(t)-y(t)}
The controller no longer outputs a fixed control value based on experience; instead, it calculates the control input based on the current error e(t):
u(t)=C(s)e(t)
where C(s) represents the controller.
So the entire control process becomes:
r\longrightarrow e\longrightarrow C(s) \longrightarrow G(s) \longrightarrow y
At the same time, the output y is fed back to the input to be compared with the target r. Taking speed control as an example: e_v(t)=v_{\mathrm{ref}}(t)-v(t)
If the actual speed is less than the target speed, then:e_v>0
The controller increases the motor output. If the actual speed exceeds the target speed, then:e_v<0
The controller decreases the motor output. In other words, a closed-loop system continuously corrects the control quantity by observing the error.
This is the most important idea of closed-loop control:
\boxed{\text{不断修正错误直到获得正确的结果}}
3. How the Closed-Loop Transfer Function Is Derived
To further analyze the closed-loop system, we need to write the above relationships in mathematical form.
In a unity negative feedback system:E(s)=R(s)-Y(s) Controller output is:U(s)=C(s)E(s) Plant output is:Y(s)=G(s)U(s) Substituting the controller relationship:
If we define the open-loop transfer function:L(s)=C(s)G(s)
Then the closed-loop transfer function is:
\boxed{T(s)=\frac{L(s)}{1+L(s)}}
The most critical part here is the denominator:1+L(s)
Because the closed-loop poles are determined by: 1+C(s)G(s)=0. The location of the closed-loop poles directly determines whether the system is stable, and whether the response is fast, slow, or oscillatory. So when designing PI and PID controllers later, we are essentially using the controller C(s) to change the closed-loop poles of the entire system.
4. Why Feedback Can Reject Disturbances
Suppose a model car is running stably and suddenly encounters an uphill slope. The slope adds a load disturbance d(t) to the drive system. In an open-loop system, the control input does not change, so the vehicle speed drops. In a closed-loop system, the speed drop immediately causes:v<v_{\mathrm{ref}}
The controller then increases the motor output, raising the current and drive torque to bring the vehicle back to the target speed. From a frequency-domain perspective, we can also define the sensitivity function:
S(s)=\frac{1}{1+L(s)}
where: L(s)=C(s)G(s). If, within the frequency range we care about: |L(j\omega)|\gg1, then: |S(j\omega)|\approx\frac{1}{|L(j\omega)|}\ll1 This means the effect of disturbances on the output is significantly attenuated by closed-loop feedback. So it can be simply understood as:
\boxed{\text{闭环反馈让系统对扰动具有主动纠正能力}}
But this does not mean that higher gain is always better.
If the loop gain is too high, especially in the presence of time delays and high-order dynamics, the system will develop increasingly severe phase lag, potentially going from fast response to oscillation or even instability.
Therefore, what closed-loop control truly needs to balance is:
It consists of three parts: proportional, integral, and derivative.
1. Proportional Term P
Proportional control is:
u_P(t)=K_p e(t)
Its logic is very simple: the larger the error, the stronger the correction.
If the target speed is: v_{\mathrm{ref}}=3\mathrm{m/s} and the actual speed is only: v=2\mathrm{m/s}, then:e_v=1\mathrm{m/s}
The controller generates the corresponding control output based on K_p.
Increasing K_p generally makes the system respond faster, but if the proportional gain is too large, the system may overshoot the target, then correct in the opposite direction, overshoot again, and eventually oscillate.
So proportional control determines:
How aggressively should we correct once we see the error?
2. Integral Term I
Integral control is:
u_I(t)=K_i\int_0^t e(\tau),d\tau
The main problem it addresses is persistent small errors.
Suppose the vehicle target speed is still: v_{\mathrm{ref}}=3\mathrm{m/s}. Due to the slope load, proportional control eventually stabilizes the speed at: v=2.9,\mathrm{m/s}. At this point, the error is only:e_v=0.1,\mathrm{m/s}
The correction produced by proportional control may be insufficient to continue overcoming the load, so the system maintains a fixed error.
After adding integral action, as long as:e_v\neq0
then \int e_v(t),dt will continue to accumulate. Even if the error is very small, as long as it persists long enough, the integral term will keep increasing the control output until the error approaches zero.
Therefore, the most important significance of the integral term is:
\boxed{\text{消除恒定参考和恒定扰动产生的稳态误差}}
3. Derivative Term D
Derivative control is:
u_D(t)=K_d\frac{de(t)}{dt}
It focuses on: how fast is the error changing?
Suppose the system is rapidly approaching the target value. Even if the current error still exists, the derivative term can proactively reduce the control action based on the trend of change. Therefore, the derivative term is often understood as adding system damping, helping to suppress overshoot and oscillation. However, real sensor data always contains noise, and differentiation amplifies high-frequency variations.
Thus, practical controllers generally do not use an ideal derivative:
K_d s
More common is a form with low-pass filtering:
C_D(s)=K_d\frac{Ns}{s+N}
This is also why PI is more common than full PID in real motor speed control.
VI. Example 1: Drive Motor Speed Closed Loop
Having understood basic closed-loop control, let us consider our first real system: the motor speed loop.
The goal of speed control is very clear:
v(t)\rightarrow v_{\mathrm{ref}}(t)
But to design a controller properly, we must first answer one question:
Why does the control voltage change the motor speed?
Therefore, we need to build a motor model first.
To keep the discussion simple, we use the classic DC motor model here. For BLDC or PMSM motors, if a fast current loop already exists at the lower level, the outer speed loop can also be approximated as a similar mechanical system under certain conditions.
VII. Electrical Model of the Motor
For a DC motor, the armature circuit satisfies Kirchhoff's voltage law:
u(t)=Ri(t)+L\frac{di(t)}{dt}+e_b(t)
where R is the winding resistance, L is the winding inductance, and e_b is the back-EMF. The back-EMF is approximately proportional to the motor angular velocity:e_b(t)=K_e\omega(t)
Therefore:
\boxed{L\frac{di}{dt}+Ri+K_e\omega u}
This formula has a very important physical meaning.
When the motor speed increases: \omega\uparrow, the back-EMF also increases: K_e\omega\uparrow, so the effective voltage actually applied across the resistor and inductor decreases.
This shows that the relationship between motor speed and input voltage is not a simple linear proportion.
VIII. Mechanical Model of the Motor
The electromagnetic torque produced by the motor can be approximated as:
\tau_m=K_t i
where K_t is the motor torque constant.
According to Newton's second law for rotational systems:
J\frac{d\omega}{dt}=\tau_mB\omega\tau_L
Substituting the electromagnetic torque:
\boxed{J\frac{d\omega}{dt}+B\omega=K_t i\tau_L}
Here J represents the equivalent moment of inertia reflected to the motor shaft, B represents viscous damping, and \tau_L represents the load torque.
9. From Differential Equations to the Speed Transfer Function
To analyze the system, we temporarily ignore the load disturbance:\tau_L=0
Performing the Laplace transform under zero initial conditions. The electrical equation gives: (Ls+R)I(s)+K_e\Omega(s)=U(s), the mechanical equation gives: (Js+B)\Omega(s)=K_tI(s), therefore:I(s)=\frac{Js+B}{K_t}\Omega(s)
At this point, we have transformed an actual "vehicle speed control problem" into a standard control system problem.
10. Why Speed Loops Typically Use PI
For speed control, the most common controller is not a full PID, but rather a PI:
C_v(s)=K_p+\frac{K_i}{s}
The reason is that the speed loop has two main tasks:
First, quickly track the target speed.
Second, maintain zero steady-state error after being subjected to persistent loads.
The proportional term handles fast response, and the integral term eliminates steady-state error caused by constant disturbances.
Let the speed plant be simplified to:
G(s)=\frac{b_0}{a_2s^2+a_1s+a_0}
The PI controller is:
C(s)=\frac{K_ps+K_i}{s}
The closed-loop characteristic equation satisfies:
1+C(s)G(s)=0
Substituting gives:
s(a_2s^2+a_1s+a_0)+b_0(K_ps+K_i)=0
Rearranging:
\boxed{a_2s^3+a_1s^2+(a_0+b_0K_p)s+b_0K_i=0}
It is easy to observe that after adding the integral term, the original second-order motor plant becomes a third-order closed-loop system.
For a third-order polynomial, according to the Routh-Hurwitz stability criterion, with all coefficients positive, it must also satisfy:
\boxed{a_1(a_0+b_0K_p)>a_2b_0K_i}
This condition illustrates a very important point:
\boxed{K_i\text{ 并不是越大越好}}
If the integral gain is too small, steady-state error is eliminated slowly; if the integral gain is too large, system damping may be significantly reduced, causing oscillation.
So what is called PID tuning is essentially finding suitable closed-loop poles that balance response speed and stability.
11. Why Real Motors Often Use Dual Closed Loops
Looking at the mechanical equation: J\dot{\omega}+B\omega=K_ti-\tau_L, we can see that what truly and directly determines motor torque is current:\tau_m=K_ti
Therefore, in modern motor control, we usually do not directly adopt:\text{速度误差}\rightarrow\mathrm{PWM}
The speed loop calculates "how much torque is needed now" based on the speed error, and this torque is expressed as the target current i_{\mathrm{ref}}.
The current loop is then responsible for quickly making: i\rightarrow i_{\mathrm{ref}}, because:\tau_m=K_ti
So if the current loop is fast enough, the speed loop can approximately consider itself to be controlling an ideal torque source.
The core design principle of this structure is:
\boxed{\text{内环快,外环慢}}
That is, the bandwidth of the current loop is significantly higher than that of the speed loop.
12. Example 2: Steering Motor Closed Loop
Next, let us consider another very typical closed loop: the steering loop.
The speed loop controls the vehicle speed v, while the steering loop controls the actual wheel angle \delta.
Define the target angle: \delta_{\mathrm{ref}}, and the actual angle:\delta
Then the steering angle error is:
\boxed{e_\delta=\delta_{\mathrm{ref}}\delta}
The task of the steering controller is to make: e_\delta\rightarrow0, which is:\delta\rightarrow\delta_{\mathrm{ref}}
It looks identical to the speed closed loop, but real steering actuators are usually more complex.
XIII. Why the Steering System Is More Complex
After the steering motor outputs, the wheel angle is not obtained directly.
The control action typically needs to pass sequentially through:
The mechanical system also contains friction, elasticity, and backlash.
More importantly, while the vehicle is moving, the tires themselves generate a self-aligning torque on the steering mechanism. Therefore, even with a constant control input to the steering motor, the final angle may vary with vehicle speed and road conditions. After reflecting the entire mechanism to the wheel angle \delta, a simplified mechanical model can be established:
It can be seen that in this simplified model, the steering system already manifests as a third-order system.
Compared to the speed loop, it contains more dynamic elements and is therefore generally more sensitive to controller parameters, mechanical resonance, and time delays.
14. Why the Steering Closed Loop Also Needs Integral Action
Suppose the target angle is: \delta_{\mathrm{ref}}=10^\circ. After the vehicle turns, the tires continuously generate a self-aligning torque: \tau_{\mathrm{road}}\neq0. If only proportional control is used: u_\delta=K_p e_\delta, then for the motor to continuously produce control output that counteracts the road's self-aligning torque, it must satisfy: u_\delta\neq0. But proportional control only produces a non-zero control output when: e_\delta\neq0. Therefore, the vehicle may eventually stabilize at: \delta=9^\circ. At this point, although the system is stable and no longer oscillating, the target is:10^\circ
This is the steady-state error.
After adding integral action:
u_\delta=K_pe_\delta+K_i\int e_\delta(t),dt
As long as the angle has not truly reached the target: e_\delta\neq0, the integrator will continue to increase the compensation until it generates a sufficiently large sustained steering torque.
Therefore:
\boxed{\text{稳定不代表准确,稳定性与稳态误差是两个不同的问题}}
15. The Steering Closed Loop and Vehicle Lateral Control Are Not the Same Concept
In vehicle systems, "steering control" and "path tracking" are often conflated. In reality, they operate at different control levels.
The low-level steering servo addresses: \delta_{\mathrm{ref}}\rightarrow\delta. That is, the upper level requests the front wheels to turn by 10^\circ, and the low level is responsible for ensuring the actuator actually rotates to 10^\circ. Vehicle lateral control, on the other hand, addresses:\text{路径误差}\rightarrow\delta_{\mathrm{ref}}
For example, after the vehicle deviates from the center of the track, algorithms such as Pure Pursuit, Stanley, LQR, or MPC calculate, based on lateral error and heading error, what \delta_{\mathrm{ref}} should be.
Therefore, no matter how sophisticated the upper-level algorithm is, if the low-level steering mechanism cannot accurately execute the target angle, the final path tracking performance will be limited.
16. What Should Be Verified in Simulation
After building the model, simulation should be performed first.
For the speed loop, first set a step target, for example:
v_{\mathrm{ref}}:0\rightarrow3\mathrm{m/s}
Observe the vehicle speed response from standstill to the target speed.
A basic simulation should at least record:
Rise time;
Overshoot;
Settling time;
Steady-state error.
For example, the percentage overshoot can be defined as:
For the speed loop, a more important experiment is to suddenly add a load disturbance after the vehicle has been running stably:
\tau_L:0\rightarrow\tau_d
Observe whether the speed drops significantly and how long the controller takes to bring the vehicle back to the target speed.
For the steering loop, we can also let:
\delta_{\mathrm{ref}}:0\rightarrow10^\circ
Then, at a certain time, add the road self-aligning torque:
\tau_{\mathrm{road}}:0\rightarrow\tau_d
Compare the responses of P, PI, and PID controllers.
Typically, it can be observed that P control has a fast response but may leave a steady-state error under constant disturbance; adding integral action eliminates the steady-state error, but too much integral gain may increase oscillation; adding appropriate derivative action can improve damping but increases sensitivity to sensor noise.
The purpose of simulation is to verify:
Whether the behavior of the current controller matches the expectations derived from our theoretical analysis.
17. Why Simulation Is Stable but Oscillates on the Actual Vehicle
One of the most common scenarios in practical control is that MATLAB or Python simulation curves look perfect, but when the controller actually runs on the MCU, it starts oscillating.
The reason is that the earlier models neglected a large number of real-world factors.
First, the actual controller executes discretely.
Let the sampling period be: T_s. Then the control algorithm only executes at these discrete instants: t=kT_s.
The integral term also changes from its continuous form: \int e(t),dt to:T_s\sum e[k]
A simple digital PI can be written as:
u[k]=K_pe[k]+K_iT_s\sum_{j=0}^{k}e[j]
If the control frequency is too low, parameters that were stable in the continuous system may not have the same stability margin in the discrete implementation.
18. PWM Saturation and Integrator Windup
The second issue is that actuators have limitations.
Suppose the motor driver can only provide a maximum of:
|u|\le U_{\max}
The theoretical output calculated by the controller, however, could be:
u^\ast>U_{\max}
At this point, the actual output is already clamped at the maximum, but the integrator may still continue to accumulate the error.
For example, when the vehicle goes uphill and the speed drops, the PI controller keeps increasing the output, but the PWM has already reached 100%.
Continuing to integrate at this point does not produce more voltage but causes the integrator state to grow larger and larger.
When the vehicle returns to level ground, even if the speed is already close to the target, the integrator still retains a large amount of accumulated historical error, causing the motor to maintain an excessively large output, ultimately resulting in severe overshoot.
This phenomenon is called:
\boxed{\text{积分饱和}}
Therefore, real PI/PID controllers typically require Anti-Windup measures, such as integral clamping, conditional integration, or the back-calculation method.
19. The Encoder Is Not a Perfect Sensor
The speed loop needs to measure speed, and real model vehicles typically use encoders.
Suppose the encoder produces N_{\mathrm{enc}} counts per revolution, and within the sampling period T_s, we measure: \Delta N counts. Then the motor angular velocity can be approximately calculated as:
At high speed, a single sample may yield dozens or even hundreds of counts, making the speed calculation relatively smooth.
But at low speed, a situation like \Delta N=0,\ 1,\ 2 may occur, which is discrete and uneven.
The measured speed is then no longer continuously varying but jumps among a few discrete values. If this speed is then directly differentiated, the noise is further amplified.
Therefore, in a real control system, data filtering, speed estimation, and the sampling period are themselves part of the controller design, not peripheral issues that can be completely ignored.
20. Why Time Delays Are Dangerous
The actual control process typically goes through:
Assume the total delay is \tau_d. In the continuous model, it can be approximately represented as: G_d(s)=e^{-s\tau_d} . Letting s=j\omega yields:G_d(j\omega)=e^{-j\omega\tau_d}
Its magnitude is always:
|G_d(j\omega)|=1
But it produces additional phase:
\boxed{\angle G_d(j\omega)=-\omega\tau_d}
In other words, the higher the frequency, the more severe the phase loss caused by delay. This is why a controller cannot pursue bandwidth indefinitely.
Once the system response is tuned to be very fast, delays and unmodeled high-frequency dynamics become increasingly important, and may ultimately push an otherwise stable system into oscillation.
21. Closed-Loop Performance That Truly Matters
When evaluating a closed-loop system, you cannot simply ask:
Did it eventually reach the target value?
If the speed eventually reaches 3\mathrm{m/s}, but first overshoots to ,\mathrm{m/s}, that clearly cannot be called good control.
A complete closed-loop system typically needs to consider at least: Rise time t_r indicates how quickly the system approaches the target.
Overshoot M_p indicates how much the system exceeds the target value.
Settling time t_s indicates how long it takes for the system to truly stabilize near the target.
Steady-state error e_{\mathrm{ss}} indicates whether a deviation still exists between the target and the actual output after prolonged operation.
In addition, disturbance experiments and parameter perturbation experiments should also be conducted.
For example, modify the motor inertia to:
J'=1.2J
Or reduce the battery voltage, then observe whether the control system remains stable.
Because the parameters of a real vehicle are never exactly equal to the parameters used in our modeling.
A good controller should be:
\boxed{\text{面对扰动、参数变化、噪声和延迟时仍然能够稳定工作}}
XXII. Back to the Original Question
Now let us return to the question posed at the very beginning of this article.
If the vehicle is required to run at:v_{\mathrm{ref}}=3\mathrm{m/s}
We should not look for a fixed PWM and then assume it will always correspond to 3\mathrm{m/s}.
A more reasonable approach is:
v_{\mathrm{ref}}\rightarrow e_v\rightarrow\text{速度控制器}\rightarrow\text{电机}\rightarrow v
When the actual speed drops, the controller automatically increases the output; when the actual speed rises, the controller automatically decreases the output.
Similarly, if the front wheels are required to maintain:\delta_{\mathrm{ref}}=10^\circ
We should not simply assume that a certain PWM or a certain voltage will always correspond to 10^\circ.
All the theory discussed above must ultimately be translated into a practical control program. For an MCU, closed-loop control is not executed continuously; rather, it is completed repeatedly within a fixed control interrupt period:
The proportional control action also decreases continuously.
If the tire self-aligning torque causes the steering system to ultimately settle at:
\delta=9.7^\circ
Then there still exists:
e_\delta=0.3^\circ
The integral term will continue to accumulate:
I[k]=I[k-1]+0.3^\circ T_s
Until a sufficiently large sustained control output is produced, such that:
\delta\rightarrow10^\circ
The derivative term:
K_d\frac{e[k]-e[k-1]}{T_s}
primarily provides damping based on the rate of change of the error. When the steering mechanism rapidly approaches the target angle, the derivative term can proactively suppress excessive control output, thereby reducing overshoot and oscillation.
3. Summary
Whether it is the speed PI or the steering PID, the basic logic of the program is essentially:
When external load, road torque, or battery voltage changes, the system does not need to know each disturbance in advance. Instead, it continuously corrects the control quantity using the error between the actual output and the target output.
Therefore, the PID code itself is not complicated.
What truly determines control performance is the understanding of the plant's dynamic characteristics, as well as factors such as the control period, feedback measurement, parameter tuning, saturation handling, and noise management.
24. Conclusion
PID is just one method of implementing feedback. A truly complete control design should start from the physical system:
If you do not know what the plant is, simply tweaking K_p, K_i, and K_d is essentially still tuning by experience.
Conversely, when we understand how voltage generates current, how current generates torque, and how torque changes speed; when we know how the steering motor affects the front wheel angle through the mechanical structure; when we know where disturbances enter, what the sensor measures, and what limitations the actuator has — then the PID parameters are no longer just three numbers that need to be "found by trial and error."
They are changing the closed-loop dynamics of a specific system.
So I prefer to understand closed-loop control as a very simple sentence:
Tip
Allow the control system to make errors, and use the error to continuously correct
For model vehicles, this philosophy runs through the entire control system.
From the lowest-level current loop, to the speed loop and angle loop, and further to vehicle lateral control and trajectory tracking, it is essentially always answering the same question:
How far are we from the target, and what should the next correction be?
0. Introduction
In practical device control, a common question arises: if you give the drive motor a target speed
v_{\mathrm{ref}}=3\mathrm{m/s}, will the vehicle actually run stably at3\mathrm{m/s}? If you give the steering motor a target angle\delta_{\mathrm{ref}}=10^\circ, will the front wheels actually hold precisely at10^\circ?If you simply supply a fixed PWM to the motor based on experience, the answer is usually no.
Battery voltage fluctuates, motors have internal resistance and back-EMF, mechanical systems have inertia and friction, and the vehicle is also affected by road grade, tire resistance, and self-aligning torque. The same PWM yields completely different speeds depending on whether the vehicle is jacked up, running on level ground, or climbing a slope; likewise, the same steering motor output can correspond to different actual wheel angles when stationary versus at high speed.
Therefore, what a practical control system must truly address is:
\boxed{\text{输出与目标之间存在误差时,系统应该如何自动修正?}}This is the problem that closed-loop control is meant to solve.
Starting from closed-loop control itself, this article further combines the drive motor speed loop and the steering motor angle loop to analyze how a real control system begins with a physical model, goes through mathematical modeling, controller design, and stability analysis, and ultimately becomes a control algorithm that runs on an MCU.
1. Starting with Open-Loop Control
Suppose we want to control a motor.
The simplest approach is to give the motor a control input
u(t)based on experience, e.g., a PWM signal with a certain duty cycle. This control input passes through the motor and mechanical structure and ultimately produces an outputy(t).From a system perspective, this can be written as:
u(t)\longrightarrow G(s)\longrightarrow y(t)Here
G(s)represents the plant — the system formed collectively by the motor, transmission, tires, load, and all other physical processes.For a drive motor, it can be simply understood as:
\mathrm{PWM}\longrightarrow\text{驱动电路}\longrightarrow\text{电机}\longrightarrow\text{车轮}\longrightarrow vThis method of control is called open-loop control.
The problem with open-loop control is:
But a real system obviously does not satisfy this condition.
For example, an experiment shows that when the PWM duty cycle is
40%, the vehicle runs stably at3\mathrm{m/s}on flat ground. Can we conclude that as long as we always output40%PWM, the speed will always be3\mathrm{m/s}?No.
When the vehicle enters a slope, the load torque increases; when the battery voltage drops, the average motor voltage for the same duty cycle decreases; tire temperature, ground friction, mechanical transmission efficiency, and even vehicle mass changes all affect the final speed.
So the actual speed might become:
v=2.6\mathrm{m/s}, yet the controller still outputs the original PWMThis is the most fundamental problem of open-loop systems:
\boxed{\text{控制器没有利用实际输出的信息}}2. Closed-Loop Control
If we want the system to actively correct itself, the actual output must be fed back to the controller.
Let the system target be
r(t)and the actual output bey(t).Define the error:
\boxed{e(t)=r(t)-y(t)}The controller no longer outputs a fixed control value based on experience; instead, it calculates the control input based on the current error
e(t):u(t)=C(s)e(t)where
C(s)represents the controller.So the entire control process becomes:
r\longrightarrow e\longrightarrow C(s) \longrightarrow G(s) \longrightarrow yAt the same time, the output
yis fed back to the input to be compared with the targetr. Taking speed control as an example:e_v(t)=v_{\mathrm{ref}}(t)-v(t)If the actual speed is less than the target speed, then:
e_v>0The controller increases the motor output. If the actual speed exceeds the target speed, then:
e_v<0The controller decreases the motor output. In other words, a closed-loop system continuously corrects the control quantity by observing the error.
This is the most important idea of closed-loop control:
\boxed{\text{不断修正错误直到获得正确的结果}}3. How the Closed-Loop Transfer Function Is Derived
To further analyze the closed-loop system, we need to write the above relationships in mathematical form.
In a unity negative feedback system:
E(s)=R(s)-Y(s)Controller output is:
U(s)=C(s)E(s)Plant output is:
Y(s)=G(s)U(s)Substituting the controller relationship:
Y(s)=G(s)C(s)E(s)Then using:
E(s)=R(s)-Y(s)We get:
Y(s)=G(s)C(s)\left[R(s)-Y(s)\right]Collecting all terms containing
Y(s):Y(s)\left[1+C(s)G(s)\right]=C(s)G(s)R(s)Therefore:
\boxed{\frac{Y(s)}{R(s)}=\frac{C(s)G(s)}{1+C(s)G(s)}}If we define the open-loop transfer function:
L(s)=C(s)G(s)Then the closed-loop transfer function is:
\boxed{T(s)=\frac{L(s)}{1+L(s)}}The most critical part here is the denominator:
1+L(s)Because the closed-loop poles are determined by:
1+C(s)G(s)=0. The location of the closed-loop poles directly determines whether the system is stable, and whether the response is fast, slow, or oscillatory. So when designing PI and PID controllers later, we are essentially using the controllerC(s)to change the closed-loop poles of the entire system.4. Why Feedback Can Reject Disturbances
Suppose a model car is running stably and suddenly encounters an uphill slope. The slope adds a load disturbance
d(t)to the drive system. In an open-loop system, the control input does not change, so the vehicle speed drops. In a closed-loop system, the speed drop immediately causes:v<v_{\mathrm{ref}}The controller then increases the motor output, raising the current and drive torque to bring the vehicle back to the target speed. From a frequency-domain perspective, we can also define the sensitivity function:
S(s)=\frac{1}{1+L(s)}where:
L(s)=C(s)G(s). If, within the frequency range we care about:|L(j\omega)|\gg1, then:|S(j\omega)|\approx\frac{1}{|L(j\omega)|}\ll1This means the effect of disturbances on the output is significantly attenuated by closed-loop feedback. So it can be simply understood as:\boxed{\text{闭环反馈让系统对扰动具有主动纠正能力}}But this does not mean that higher gain is always better.
If the loop gain is too high, especially in the presence of time delays and high-order dynamics, the system will develop increasingly severe phase lag, potentially going from fast response to oscillation or even instability.
Therefore, what closed-loop control truly needs to balance is:
\boxed{\text{响应速度}+\text{跟踪精度}+\text{抗扰能力}+\text{稳定性}}5. What the PID Controller Actually Does
The most common closed-loop controller in engineering is the PID:
u(t)=K_p e(t)+K_i\int_0^t e(\tau),d\tau+K_d\frac{de(t)}{dt}It consists of three parts: proportional, integral, and derivative.
1. Proportional Term P
Proportional control is:
u_P(t)=K_p e(t)Its logic is very simple: the larger the error, the stronger the correction.
If the target speed is:
v_{\mathrm{ref}}=3\mathrm{m/s}and the actual speed is only:v=2\mathrm{m/s}, then:e_v=1\mathrm{m/s}The controller generates the corresponding control output based on
K_p.Increasing
K_pgenerally makes the system respond faster, but if the proportional gain is too large, the system may overshoot the target, then correct in the opposite direction, overshoot again, and eventually oscillate.So proportional control determines:
2. Integral Term I
Integral control is:
u_I(t)=K_i\int_0^t e(\tau),d\tauThe main problem it addresses is persistent small errors.
Suppose the vehicle target speed is still:
v_{\mathrm{ref}}=3\mathrm{m/s}. Due to the slope load, proportional control eventually stabilizes the speed at:v=2.9,\mathrm{m/s}. At this point, the error is only:e_v=0.1,\mathrm{m/s}The correction produced by proportional control may be insufficient to continue overcoming the load, so the system maintains a fixed error.
After adding integral action, as long as:
e_v\neq0then
\int e_v(t),dtwill continue to accumulate. Even if the error is very small, as long as it persists long enough, the integral term will keep increasing the control output until the error approaches zero.Therefore, the most important significance of the integral term is:
\boxed{\text{消除恒定参考和恒定扰动产生的稳态误差}}3. Derivative Term D
Derivative control is:
u_D(t)=K_d\frac{de(t)}{dt}It focuses on: how fast is the error changing?
Suppose the system is rapidly approaching the target value. Even if the current error still exists, the derivative term can proactively reduce the control action based on the trend of change. Therefore, the derivative term is often understood as adding system damping, helping to suppress overshoot and oscillation. However, real sensor data always contains noise, and differentiation amplifies high-frequency variations.
Thus, practical controllers generally do not use an ideal derivative:
K_d sMore common is a form with low-pass filtering:
C_D(s)=K_d\frac{Ns}{s+N}This is also why PI is more common than full PID in real motor speed control.
VI. Example 1: Drive Motor Speed Closed Loop
Having understood basic closed-loop control, let us consider our first real system: the motor speed loop.
The goal of speed control is very clear:
v(t)\rightarrow v_{\mathrm{ref}}(t)But to design a controller properly, we must first answer one question:
Therefore, we need to build a motor model first.
To keep the discussion simple, we use the classic DC motor model here. For BLDC or PMSM motors, if a fast current loop already exists at the lower level, the outer speed loop can also be approximated as a similar mechanical system under certain conditions.
VII. Electrical Model of the Motor
For a DC motor, the armature circuit satisfies Kirchhoff's voltage law:
u(t)=Ri(t)+L\frac{di(t)}{dt}+e_b(t)where
Ris the winding resistance,Lis the winding inductance, ande_bis the back-EMF. The back-EMF is approximately proportional to the motor angular velocity:e_b(t)=K_e\omega(t)Therefore:
\boxed{L\frac{di}{dt}+Ri+K_e\omega u}This formula has a very important physical meaning.
When the motor speed increases:
\omega\uparrow, the back-EMF also increases:K_e\omega\uparrow, so the effective voltage actually applied across the resistor and inductor decreases.This shows that the relationship between motor speed and input voltage is not a simple linear proportion.
VIII. Mechanical Model of the Motor
The electromagnetic torque produced by the motor can be approximated as:
\tau_m=K_t iwhere
K_tis the motor torque constant.According to Newton's second law for rotational systems:
J\frac{d\omega}{dt}=\tau_mB\omega\tau_LSubstituting the electromagnetic torque:
\boxed{J\frac{d\omega}{dt}+B\omega=K_t i\tau_L}Here
Jrepresents the equivalent moment of inertia reflected to the motor shaft,Brepresents viscous damping, and\tau_Lrepresents the load torque.From this we obtain:
\text{电压}\rightarrow\text{电流}\rightarrow\text{转矩}\rightarrow\text{加速度}\rightarrow\text{转速}9. From Differential Equations to the Speed Transfer Function
To analyze the system, we temporarily ignore the load disturbance:
\tau_L=0Performing the Laplace transform under zero initial conditions. The electrical equation gives:
(Ls+R)I(s)+K_e\Omega(s)=U(s), the mechanical equation gives:(Js+B)\Omega(s)=K_tI(s), therefore:I(s)=\frac{Js+B}{K_t}\Omega(s)Substituting back into the electrical equation:
(Ls+R)\frac{Js+B}{K_t}\Omega(s)+K_e\Omega(s)=U(s)Rearranging gives:
\boxed{\frac{\Omega(s)}{U(s)}=\frac{K_t}{LJs^2+(LB+RJ)s+RB+K_tK_e}}It can be seen that under this model, from input voltage to motor speed, it is a second-order system.
If the wheel radius is
r_wand the gear reduction ratio isn_g, ignoring longitudinal tire slip, then:v=\frac{r_w}{n_g}\omegaThus the vehicle speed model can be written as:
\boxed{G_v(s)=\frac{V(s)}{U(s)}\frac{\dfrac{r_wK_t}{n_g}}{LJs^2+(LB+RJ)s+RB+K_tK_e}}At this point, we have transformed an actual "vehicle speed control problem" into a standard control system problem.
10. Why Speed Loops Typically Use PI
For speed control, the most common controller is not a full PID, but rather a PI:
C_v(s)=K_p+\frac{K_i}{s}The reason is that the speed loop has two main tasks:
First, quickly track the target speed.
Second, maintain zero steady-state error after being subjected to persistent loads.
The proportional term handles fast response, and the integral term eliminates steady-state error caused by constant disturbances.
Let the speed plant be simplified to:
G(s)=\frac{b_0}{a_2s^2+a_1s+a_0}The PI controller is:
C(s)=\frac{K_ps+K_i}{s}The closed-loop characteristic equation satisfies:
1+C(s)G(s)=0Substituting gives:
s(a_2s^2+a_1s+a_0)+b_0(K_ps+K_i)=0Rearranging:
\boxed{a_2s^3+a_1s^2+(a_0+b_0K_p)s+b_0K_i=0}It is easy to observe that after adding the integral term, the original second-order motor plant becomes a third-order closed-loop system.
For a third-order polynomial, according to the Routh-Hurwitz stability criterion, with all coefficients positive, it must also satisfy:
\boxed{a_1(a_0+b_0K_p)>a_2b_0K_i}This condition illustrates a very important point:
\boxed{K_i\text{ 并不是越大越好}}If the integral gain is too small, steady-state error is eliminated slowly; if the integral gain is too large, system damping may be significantly reduced, causing oscillation.
So what is called PID tuning is essentially finding suitable closed-loop poles that balance response speed and stability.
11. Why Real Motors Often Use Dual Closed Loops
Looking at the mechanical equation:
J\dot{\omega}+B\omega=K_ti-\tau_L, we can see that what truly and directly determines motor torque is current:\tau_m=K_tiTherefore, in modern motor control, we usually do not directly adopt:
\text{速度误差}\rightarrow\mathrm{PWM}Instead, the design is:
v_{\mathrm{ref}}\rightarrow\text{速度 PI}\rightarrow i_{\mathrm{ref}}\rightarrow\text{电流 PI}\rightarrow\mathrm{PWM}\rightarrow\text{电机}The speed loop calculates "how much torque is needed now" based on the speed error, and this torque is expressed as the target current
i_{\mathrm{ref}}.The current loop is then responsible for quickly making:
i\rightarrow i_{\mathrm{ref}}, because:\tau_m=K_tiSo if the current loop is fast enough, the speed loop can approximately consider itself to be controlling an ideal torque source.
The core design principle of this structure is:
\boxed{\text{内环快,外环慢}}That is, the bandwidth of the current loop is significantly higher than that of the speed loop.
12. Example 2: Steering Motor Closed Loop
Next, let us consider another very typical closed loop: the steering loop.
The speed loop controls the vehicle speed
v, while the steering loop controls the actual wheel angle\delta.Define the target angle:
\delta_{\mathrm{ref}}, and the actual angle:\deltaThen the steering angle error is:
\boxed{e_\delta=\delta_{\mathrm{ref}}\delta}The task of the steering controller is to make:
e_\delta\rightarrow0, which is:\delta\rightarrow\delta_{\mathrm{ref}}It looks identical to the speed closed loop, but real steering actuators are usually more complex.
XIII. Why the Steering System Is More Complex
After the steering motor outputs, the wheel angle is not obtained directly.
The control action typically needs to pass sequentially through:
\text{电机}\rightarrow\text{减速器}\rightarrow\text{齿轮或齿条}\rightarrow\text{拉杆}\rightarrow\text{前轮}The mechanical system also contains friction, elasticity, and backlash.
More importantly, while the vehicle is moving, the tires themselves generate a self-aligning torque on the steering mechanism. Therefore, even with a constant control input to the steering motor, the final angle may vary with vehicle speed and road conditions. After reflecting the entire mechanism to the wheel angle
\delta, a simplified mechanical model can be established:J_\delta\ddot{\delta}+B_\delta\dot{\delta}+K_\delta\delta=K_{t,s}i_s\tau_{\mathrm{road}}where:
J_\deltais the equivalent inertia of the steering mechanism;B_\deltais the mechanical damping;K_\deltais the equivalent stiffness or self-aligning characteristic;\tau_{\mathrm{road}}is the disturbance torque from the road and tires.The steering motor itself still satisfies the electrical equation:
L_s\dot{i}*s+R_si_s+K*{e,s}\dot{\delta}=u_\deltaAfter eliminating the current, the approximate transfer function of the steering actuator can be obtained:
\boxed{G_\delta(s)=\frac{\Delta(s)}{U_\delta(s)}\frac{K_{t,s}}{L_sJ_\delta s^3+(R_sJ_\delta+L_sB_\delta)s^2+(R_sB_\delta+L_sK_\delta+K_{e,s}K_{t,s})s+R_sK_\delta}}It can be seen that in this simplified model, the steering system already manifests as a third-order system.
Compared to the speed loop, it contains more dynamic elements and is therefore generally more sensitive to controller parameters, mechanical resonance, and time delays.
14. Why the Steering Closed Loop Also Needs Integral Action
Suppose the target angle is:
\delta_{\mathrm{ref}}=10^\circ. After the vehicle turns, the tires continuously generate a self-aligning torque:\tau_{\mathrm{road}}\neq0. If only proportional control is used:u_\delta=K_p e_\delta, then for the motor to continuously produce control output that counteracts the road's self-aligning torque, it must satisfy:u_\delta\neq0. But proportional control only produces a non-zero control output when:e_\delta\neq0. Therefore, the vehicle may eventually stabilize at:\delta=9^\circ. At this point, although the system is stable and no longer oscillating, the target is:10^\circThis is the steady-state error.
After adding integral action:
u_\delta=K_pe_\delta+K_i\int e_\delta(t),dtAs long as the angle has not truly reached the target:
e_\delta\neq0, the integrator will continue to increase the compensation until it generates a sufficiently large sustained steering torque.Therefore:
\boxed{\text{稳定不代表准确,稳定性与稳态误差是两个不同的问题}}15. The Steering Closed Loop and Vehicle Lateral Control Are Not the Same Concept
In vehicle systems, "steering control" and "path tracking" are often conflated. In reality, they operate at different control levels.
The low-level steering servo addresses:
\delta_{\mathrm{ref}}\rightarrow\delta. That is, the upper level requests the front wheels to turn by10^\circ, and the low level is responsible for ensuring the actuator actually rotates to10^\circ. Vehicle lateral control, on the other hand, addresses:\text{路径误差}\rightarrow\delta_{\mathrm{ref}}For example, after the vehicle deviates from the center of the track, algorithms such as Pure Pursuit, Stanley, LQR, or MPC calculate, based on lateral error and heading error, what
\delta_{\mathrm{ref}}should be.So the complete structure is actually:
\text{目标轨迹}\rightarrow\text{横向控制器}\rightarrow\delta_{\mathrm{ref}}\rightarrow\text{转角闭环}\rightarrow\delta\rightarrow\text{车辆}Therefore, no matter how sophisticated the upper-level algorithm is, if the low-level steering mechanism cannot accurately execute the target angle, the final path tracking performance will be limited.
16. What Should Be Verified in Simulation
After building the model, simulation should be performed first.
For the speed loop, first set a step target, for example:
v_{\mathrm{ref}}:0\rightarrow3\mathrm{m/s}Observe the vehicle speed response from standstill to the target speed.
A basic simulation should at least record:
For example, the percentage overshoot can be defined as:
M_p=\frac{y_{\max}-y_{\mathrm{ref}}}{y_{\mathrm{ref}}}\times100%The steady-state error is defined as:
e_{\mathrm{ss}}=\lim_{t\rightarrow\infty}e(t)A step response alone is not sufficient.
For the speed loop, a more important experiment is to suddenly add a load disturbance after the vehicle has been running stably:
\tau_L:0\rightarrow\tau_dObserve whether the speed drops significantly and how long the controller takes to bring the vehicle back to the target speed.
For the steering loop, we can also let:
\delta_{\mathrm{ref}}:0\rightarrow10^\circThen, at a certain time, add the road self-aligning torque:
\tau_{\mathrm{road}}:0\rightarrow\tau_dCompare the responses of P, PI, and PID controllers.
Typically, it can be observed that P control has a fast response but may leave a steady-state error under constant disturbance; adding integral action eliminates the steady-state error, but too much integral gain may increase oscillation; adding appropriate derivative action can improve damping but increases sensitivity to sensor noise.
The purpose of simulation is to verify:
17. Why Simulation Is Stable but Oscillates on the Actual Vehicle
One of the most common scenarios in practical control is that MATLAB or Python simulation curves look perfect, but when the controller actually runs on the MCU, it starts oscillating.
The reason is that the earlier models neglected a large number of real-world factors.
First, the actual controller executes discretely.
Let the sampling period be:
T_s. Then the control algorithm only executes at these discrete instants:t=kT_s.The integral term also changes from its continuous form:
\int e(t),dtto:T_s\sum e[k]A simple digital PI can be written as:
u[k]=K_pe[k]+K_iT_s\sum_{j=0}^{k}e[j]If the control frequency is too low, parameters that were stable in the continuous system may not have the same stability margin in the discrete implementation.
18. PWM Saturation and Integrator Windup
The second issue is that actuators have limitations.
Suppose the motor driver can only provide a maximum of:
|u|\le U_{\max}The theoretical output calculated by the controller, however, could be:
u^\ast>U_{\max}At this point, the actual output is already clamped at the maximum, but the integrator may still continue to accumulate the error.
For example, when the vehicle goes uphill and the speed drops, the PI controller keeps increasing the output, but the PWM has already reached
100%.Continuing to integrate at this point does not produce more voltage but causes the integrator state to grow larger and larger.
When the vehicle returns to level ground, even if the speed is already close to the target, the integrator still retains a large amount of accumulated historical error, causing the motor to maintain an excessively large output, ultimately resulting in severe overshoot.
This phenomenon is called:
\boxed{\text{积分饱和}}Therefore, real PI/PID controllers typically require Anti-Windup measures, such as integral clamping, conditional integration, or the back-calculation method.
19. The Encoder Is Not a Perfect Sensor
The speed loop needs to measure speed, and real model vehicles typically use encoders.
Suppose the encoder produces
N_{\mathrm{enc}}counts per revolution, and within the sampling periodT_s, we measure:\Delta Ncounts. Then the motor angular velocity can be approximately calculated as:\omega\approx\frac{\Delta N}{N_{\mathrm{enc}}}\frac{2\pi}{T_s}At high speed, a single sample may yield dozens or even hundreds of counts, making the speed calculation relatively smooth.
But at low speed, a situation like
\Delta N=0,\ 1,\ 2may occur, which is discrete and uneven.The measured speed is then no longer continuously varying but jumps among a few discrete values. If this speed is then directly differentiated, the noise is further amplified.
Therefore, in a real control system, data filtering, speed estimation, and the sampling period are themselves part of the controller design, not peripheral issues that can be completely ignored.
20. Why Time Delays Are Dangerous
The actual control process typically goes through:
\text{采样}\rightarrow\text{读取传感器}\rightarrow\text{算法计算}\rightarrow\text{PWM 更新}\rightarrow\text{驱动器}\rightarrow\text{机械响应}Each of these processes introduces delay.
Assume the total delay is
\tau_d. In the continuous model, it can be approximately represented as:G_d(s)=e^{-s\tau_d}. Letting
s=j\omegayields:G_d(j\omega)=e^{-j\omega\tau_d}Its magnitude is always:
|G_d(j\omega)|=1But it produces additional phase:
\boxed{\angle G_d(j\omega)=-\omega\tau_d}In other words, the higher the frequency, the more severe the phase loss caused by delay. This is why a controller cannot pursue bandwidth indefinitely.
Once the system response is tuned to be very fast, delays and unmodeled high-frequency dynamics become increasingly important, and may ultimately push an otherwise stable system into oscillation.
21. Closed-Loop Performance That Truly Matters
When evaluating a closed-loop system, you cannot simply ask:
If the speed eventually reaches
3\mathrm{m/s}, but first overshoots to,\mathrm{m/s}, that clearly cannot be called good control.A complete closed-loop system typically needs to consider at least:
Rise time
t_rindicates how quickly the system approaches the target.Overshoot
M_pindicates how much the system exceeds the target value.Settling time
t_sindicates how long it takes for the system to truly stabilize near the target.Steady-state error
e_{\mathrm{ss}}indicates whether a deviation still exists between the target and the actual output after prolonged operation.In addition, disturbance experiments and parameter perturbation experiments should also be conducted.
For example, modify the motor inertia to:
J'=1.2JOr reduce the battery voltage, then observe whether the control system remains stable.
Because the parameters of a real vehicle are never exactly equal to the parameters used in our modeling.
A good controller should be:
\boxed{\text{面对扰动、参数变化、噪声和延迟时仍然能够稳定工作}}XXII. Back to the Original Question
Now let us return to the question posed at the very beginning of this article.
If the vehicle is required to run at:
v_{\mathrm{ref}}=3\mathrm{m/s}We should not look for a fixed PWM and then assume it will always correspond to
3\mathrm{m/s}.A more reasonable approach is:
v_{\mathrm{ref}}\rightarrow e_v\rightarrow\text{速度控制器}\rightarrow\text{电机}\rightarrow vWhen the actual speed drops, the controller automatically increases the output; when the actual speed rises, the controller automatically decreases the output.
Similarly, if the front wheels are required to maintain:
\delta_{\mathrm{ref}}=10^\circWe should not simply assume that a certain PWM or a certain voltage will always correspond to
10^\circ.Instead, we should construct:
\delta_{\mathrm{ref}}\rightarrow e_\delta\rightarrow\text{转角控制器}\rightarrow\text{转向机构}\rightarrow\deltaAfter the road torque changes, the controller adjusts the steering motor output based on the new error.
Thus, although speed control and steering control deal with completely different physical systems, they ultimately share the same control logic:
\boxed{\text{目标}\rightarrow\text{测量}\rightarrow\text{比较}\rightarrow\text{修正}\rightarrow\text{再次测量}}23. Examples and Explanations
All the theory discussed above must ultimately be translated into a practical control program. For an MCU, closed-loop control is not executed continuously; rather, it is completed repeatedly within a fixed control interrupt period:
\boxed{\text{读取传感器}\rightarrow\text{计算误差}\rightarrow\text{计算控制量}\rightarrow\text{驱动执行器}}1. PI Controller Implementation
The continuous-form PI controller is:
u(t)=K_p e(t)+K_i\int_0^t e(\tau),d\tauIn a microcontroller, the controller runs periodically with a sampling period
T_s, so the error can be written as:e[k]=r[k]-y[k]The integral term can be approximately discretized as:
I[k]=I[k-1]+e[k]T_sThus the discrete PI controller is:
u[k]=K_p e[k]+K_i I[k]For the vehicle speed loop:
e_v[k]=v_{\mathrm{ref}}[k]-v[k]The controller calculates the PWM for the next control cycle based on the target speed and the actual speed measured by the encoder.
The core of the code is actually just:
Corresponding respectively to:
e_v=v_{\mathrm{ref}}-vI=\int e_v(t)\,dtAnd:
u=K_p e_v+K_iI2. Steering PID Control and Implementation
The steering closed loop can also be implemented following the same discrete control concept.
Define the angle error:
e_\delta[k]=\delta_{\mathrm{ref}}[k]-\delta[k]The discrete PID can be simply written as:
u[k]=K_p e[k]+K_i I[k]+K_d\frac{e[k]-e[k-1]}{T_s}where:
I[k]=I[k-1]+e[k]T_sAssume the target angle is:
\delta_{\mathrm{ref}}=10^\circThe current actual angle is:
\delta=6^\circThen:
$e_\delta=10\circ-6\circ=4^\circ$
The proportional term first generates the main control output based on the current error:
u_P=K_p e_\deltaAs the steering mechanism gradually approaches the target angle:
4^\circ\rightarrow2^\circ\rightarrow1^\circ\rightarrow0The proportional control action also decreases continuously.
If the tire self-aligning torque causes the steering system to ultimately settle at:
\delta=9.7^\circThen there still exists:
e_\delta=0.3^\circThe integral term will continue to accumulate:
I[k]=I[k-1]+0.3^\circ T_sUntil a sufficiently large sustained control output is produced, such that:
\delta\rightarrow10^\circThe derivative term:
K_d\frac{e[k]-e[k-1]}{T_s}primarily provides damping based on the rate of change of the error. When the steering mechanism rapidly approaches the target angle, the derivative term can proactively suppress excessive control output, thereby reducing overshoot and oscillation.
3. Summary
Whether it is the speed PI or the steering PID, the basic logic of the program is essentially:
But these few lines of code correspond to a complete closed-loop control theory:
\boxed{\text{目标}\rightarrow\text{误差}\rightarrow\text{控制器}\rightarrow\text{被控对象}\rightarrow\text{反馈}}When external load, road torque, or battery voltage changes, the system does not need to know each disturbance in advance. Instead, it continuously corrects the control quantity using the error between the actual output and the target output.
Therefore, the PID code itself is not complicated.
What truly determines control performance is the understanding of the plant's dynamic characteristics, as well as factors such as the control period, feedback measurement, parameter tuning, saturation handling, and noise management.
24. Conclusion
PID is just one method of implementing feedback. A truly complete control design should start from the physical system:
\text{物理建模}\rightarrow\text{数学模型}\rightarrow\text{控制器设计}\rightarrow\text{稳定性分析}\rightarrow\text{仿真}\rightarrow\text{离散实现}\rightarrow\text{实际器件验证}If you do not know what the plant is, simply tweaking
K_p,K_i, andK_dis essentially still tuning by experience.Conversely, when we understand how voltage generates current, how current generates torque, and how torque changes speed; when we know how the steering motor affects the front wheel angle through the mechanical structure; when we know where disturbances enter, what the sensor measures, and what limitations the actuator has — then the PID parameters are no longer just three numbers that need to be "found by trial and error."
They are changing the closed-loop dynamics of a specific system.
So I prefer to understand closed-loop control as a very simple sentence:
Allow the control system to make errors, and use the error to continuously correct
For model vehicles, this philosophy runs through the entire control system.
From the lowest-level current loop, to the speed loop and angle loop, and further to vehicle lateral control and trajectory tracking, it is essentially always answering the same question:
This is closed-loop control.