Time synchronization method based on IEEE 1588 protocol
By establishing a clock model and Kalman filtering equation combined with a frequency compensation algorithm, the problem of insufficient time synchronization accuracy of the IEEE 1588 protocol in complex network environments is solved, and higher time synchronization accuracy and stability are achieved.
Patent Information
- Application Number
- CN202510491046.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-18
- Publication Date
- 2025-07-04
AI Technical Summary
The existing IEEE 1588 protocol is difficult to meet future needs in terms of high-precision time synchronization, especially in complex network environments, the time synchronization accuracy is insufficient and cannot effectively reduce the impact of asymmetric links, packet delay changes and frequency drift.
The time synchronization method based on the IEEE 1588 protocol is adopted, and the clock synchronization equation for Kalman filter is built by establishing a clock model, obtaining the time stamp set, and the clock synchronization equation for Kalman filtering is constructed. Time offset and clock drift are used as input values for Kalman filtering, and combined with the frequency compensation algorithm, the time synchronization accuracy is improved.
It effectively improves the time synchronization accuracy, reduces the impact of asymmetric links, packet delay changes and frequency drift on clock synchronization, ensures that the time deviation between master and slave clocks remains consistent within 1 clock cycle, and improves the system's time synchronization stability and accuracy.
Smart Images

Figure CN120263328A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of time synchronization, and particularly to a time synchronization method based on the IEEE 1588 protocol. Background Art
[0002] With the rapid development of fields such as industrial automation, robotics, and the Internet of Things, bus technology is widely used in various industrial automation and control fields, so the requirements for bus performance are becoming increasingly high. Considering that time synchronization technology is the basis for the reliable operation of the bus, it is very urgent to improve time synchronization performance.
[0003] Currently, the mainstream time synchronization technologies include: GPS-based clock synchronization, Network Time Protocol (NTP), IEEE 1588 (PTP), etc. Research shows that GPS clock synchronization devices have high accuracy, but they cannot be used in indoor environments without GPS, and there are significant limitations; NTP is a client / server mode-based clock synchronization, and the clock synchronization accuracy only reaches the millisecond level; in contrast, PTP is a master / slave mode-based clock synchronization protocol, which is widely used in distributed networks and can achieve the synchronization of slave nodes to the master node clock. Compared with the NTP protocol, the PTP protocol has higher clock synchronization accuracy, which can reach the sub-microsecond level, so the PTP protocol has wider applicability in most bus networks.
[0004] The full name of IEEE 1588 is the Precision Clock Synchronization Protocol Standard for Networked Measurement and Control Systems, usually referred to as the Precision Time Protocol (PTP). PTP adopts a master-slave architecture. By transmitting timestamp information in the network, multiple devices can achieve synchronization, and the accuracy can reach the millisecond level. The core of PTP is that it exchanges time information between the master clock and the slave clock by exchanging synchronization messages, so as to ensure that the slave clock adjusts its clock according to the network delay and keeps consistent with the master clock.
[0005] With the development of the IEEE 1588 protocol, especially the IEEE 1588v2 version released in 2008, the protocol has been significantly improved in terms of scalability, accuracy, and reliability, adapting to more complex network environments and being widely used in fields such as power, communication, and finance. In some applications with high-precision requirements, IEEE 1588 provides a network-based clock synchronization solution, providing a stable and accurate clock source for various modern industrial and communication networks. However, the time synchronization accuracy at the millisecond level is already difficult to meet future requirements. Summary of the Invention
[0006] To overcome the deficiencies of the prior art, the objective of the present invention is to provide a time synchronization method based on the IEEE 1588 protocol.
[0007] To achieve the above objective, the present invention provides the following solution:
[0008] A time synchronization method based on the IEEE 1588 protocol, comprising:
[0009] Based on the working characteristics of the master clock and slave clock in the network, and in view of the characteristics of the clock frequency, a clock model is established;
[0010] Obtain the timestamp set for the time synchronization between the master clock and the slave clock in the current round, wherein the timestamp set includes: the first transmission timestamp, the second transmission timestamp, the first reception timestamp, and the second reception timestamp;
[0011] Calculate the average transmission delay and time offset in the current round according to the timestamp set;
[0012] Based on the clock model, construct a clock synchronization equation for Kalman filtering;
[0013] Use the time offset and clock drift as the input values of Kalman filtering to obtain the optimal solution of the time offset in the current round;
[0014] The slave clock modifies the local clock according to the optimal time offset value to complete the clock synchronization in the current round;
[0015] Obtain the timestamp set for the time synchronization between the master clock and the slave clock in the next round, the timestamp set including the third reception timestamp, the fourth reception timestamp, the third transmission timestamp, the fourth transmission timestamp, and the first reception timestamp of the previous round;
[0016] Calculate the average transmission delay and time offset in the next round according to the third reception timestamp, the fourth reception timestamp, the third transmission timestamp, and the fourth transmission timestamp, and determine the optimal time offset in the next round according to the clock synchronization equation of Kalman filtering;
[0017] Calculate the cycle number value according to the optimal time offset in the next round, the first reception timestamp, and the third reception timestamp;
[0018] Perform frequency compensation on the slave clock according to the cycle number value.
[0019] Preferably, the calculation expression for the average transmission delay is:
[0020] wherein, C m1 , C s1 , C s2 , C m2They are the first transmission timestamp, the first reception timestamp, the second transmission timestamp, and the second reception timestamp, respectively, d ms refers to the transmission delay from the master clock to the slave clock, d sm refers to the transmission delay from the slave clock to the master clock.
[0021] Preferably, the calculation expression for the time offset is:
[0022]
[0023] where θ offset is the time offset.
[0024] Preferably, the calculation expression for the clock drift is:
[0025]
[0026] where s is the clock drift, T cycle represents the time synchronization message sending interval, θ = θ offset is the slave clock time offset, and t is the first preset moment.
[0027] Preferably, the expression of the clock model is:
[0028]
[0029] where u θ (k), u s (k), u γ (k) represent the correction values of the time offset, clock drift, and clock drift change rate respectively, k is the second preset moment, ω θ(k) , ω s(k) , ω γ(k) are the Gaussian white noises of the time offset, clock drift, and clock drift change rate respectively.
[0030] Preferably, the calculation expression for the number of cycle values is:
[0031]
[0032] where C s3 is the third reception timestamp, θ koffset is the optimal time offset for the next round, and cnt is the number of cycle values.
[0033] The present invention discloses the following technical effects:
[0034] The present invention provides a time synchronization method based on the IEEE 1588 protocol, including: establishing a clock model according to the working characteristics of the master clock and slave clock based on the network and the characteristics of the clock frequency; obtaining a timestamp set for the time synchronization between the master clock and slave clock in the current round, where the timestamp set includes: a first transmission timestamp, a second transmission timestamp, a first reception timestamp, and a second reception timestamp; calculating the average transmission delay and time offset in the current round according to the timestamp set; constructing a clock synchronization equation for Kalman filtering based on the clock model; using the time offset and clock drift as the input values of Kalman filtering to obtain the optimal solution of the time offset in the current round; the slave clock modifies the local clock according to the optimal time offset value to complete the clock synchronization in the current round; obtaining a timestamp set for the time synchronization between the master clock and slave clock in the next round, where the timestamp set includes a third reception timestamp, a fourth reception timestamp, a third transmission timestamp, a fourth transmission timestamp, and the first reception timestamp of the previous round; calculating the average transmission delay and time offset in the next round according to the third reception timestamp, fourth reception timestamp, third transmission timestamp, and fourth transmission timestamp and determining the optimal time offset in the next round according to the clock synchronization equation of Kalman filtering; calculating a cycle number value according to the optimal time offset in the next round, the first reception timestamp, and its third reception timestamp; performing frequency compensation on the slave clock according to the cycle number value. The time synchronization algorithm based on Kalman filtering in the present invention comprehensively considers multiple factors such as local clock drift and path delay, which can effectively reduce the influence of asymmetric links, packet delay variation, and frequency drift on clock synchronization, improve the accuracy of the time offset calculated using PTP, and effectively improve the time synchronization accuracy; while ensuring the accuracy of the calculated time offset, the frequency compensation algorithm proposed in the present invention can timely compensate the clock drift generated by the slave clock based on a clock cycle as the basic unit, ensuring that within one transmission interval, the frequency of the slave clock can be effectively compensated, greatly improving the time synchronization accuracy of the system. When multiple measures are taken to ensure that the timestamps and asymmetry used in PTP calculation are accurate, using the frequency compensation algorithm of the present invention can keep the time deviation between the master and slave clocks within one clock; combining the Kalman filtering algorithm with the frequency compensation algorithm can tolerate some modeling errors and timestamp measurement noises existing in the original system, which makes the time synchronization algorithm work more stably in the actual network and improves the time synchronization accuracy of the system. Description of the Drawings
[0035] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the accompanying drawings required in the embodiments. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other accompanying drawings can also be obtained based on these drawings.
[0036] Figure 1 Flowchart of a time synchronization method based on the IEEE 1588 protocol provided by an embodiment of the present invention;
[0037] Figure 2 Timing diagram of the time synchronization method provided by an embodiment of the present invention;
[0038] Figure 3 Simulation result diagram of the time synchronization algorithm based on the Shaker bus protocol provided by an embodiment of the present invention. Specific implementation manners
[0039] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, rather than all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present invention.
[0040] To make the above objects, features, and advantages of the present invention more obvious and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific implementation manners.
[0041] As Figure 1 shown, the present invention provides a time synchronization method based on the IEEE 1588 protocol, which is characterized by including:
[0042] Step 100: Based on the working characteristics of the master clock and the slave clock in the network, establish a clock model according to the characteristics of the clock frequency;
[0043] Step 200: Obtain the timestamp set for the time synchronization of the master clock and the slave clock in the current round, where the timestamp set includes: the first transmission timestamp, the second transmission timestamp, the first reception timestamp, and the second reception timestamp;
[0044] Step 300: Calculate the average transmission delay and time offset in the current round according to the timestamp set;
[0045] Step 400: Based on the clock model, construct a clock synchronization equation for Kalman filtering;
[0046] Step 500: Use the time offset and clock drift as the input values of the Kalman filter to obtain the optimal solution of the current round of time offset;
[0047] Step 600: The slave clock modifies its local clock according to the optimal time offset value to complete the clock synchronization of the current round;
[0048] Step 700: Obtain the timestamp set for the time synchronization between the master clock and the slave clock in the next round. The timestamp set includes the third received timestamp, the fourth received timestamp, the third sent timestamp, the fourth sent timestamp, and the first received timestamp of the previous round;
[0049] Step 800: Calculate the average transmission delay and time offset in the next round based on the third received timestamp, the fourth received timestamp, the third sent timestamp, and the fourth sent timestamp, and determine the optimal time offset in the next round according to the clock synchronization equation of the Kalman filter;
[0050] Step 900: Calculate the cycle number value according to the optimal time offset in the next round, the first received timestamp, and its third received timestamp;
[0051] Step 1000: Perform frequency compensation on the slave clock according to the cycle number value.
[0052] Specifically, the method mentioned in this embodiment can also be expressed in the following steps:
[0053] Step 1: Based on the working characteristics of the master clock and the slave clock in the network and the characteristics of the clock frequency, establish a clock model;
[0054] Step 2: Determine whether the network is exchanging time information for the first time. If so, proceed to Step 3; if not, proceed to Step 8;
[0055] Step 3: According to the IEEE 1588 protocol, the master clock and the slave clock exchange time information, and the slave clock obtains 4 timestamp information, namely C m1 、C s1 、C s2 、C m2 。
[0056] Step 4: The slave clock calculates the average transmission delay d delay and the time offset θ offset ;
[0057] Step 5: Based on the clock model established by the slave clock, calculate the state transition matrix, the observation matrix, the initial error vector, and the error covariance matrix, and then establish the clock synchronization equation of the Kalman filter;
[0058] Step 6: Use the time offset and clock drift as the input values of the Kalman filter, and obtain the optimal solution θ of the time offset within the current synchronization period through the update iteration of the Kalman filter equation koffset ;
[0059] Step 7: The slave clock modifies the local clock according to the optimal time offset value to complete the first clock synchronization of the system;
[0060] Step 8: The slave clock needs to record the timestamp C during the previous time synchronization process s1 , and then the master clock and the slave clock exchange time information. At this time, the slave clock contains a total of 5 timestamp information C m3 、C s3 、C s4 、C m4 、C s1 ;
[0061] Step 9: The slave clock calculates the average transmission delay d delay and the time offset θ offset ;
[0062] Step 10: Use the time offset and clock drift as the input values of the Kalman filter, and obtain the optimal solution θ of the time offset within the current synchronization period through the update iteration of the Kalman filter equation koffset ;
[0063] Step 11: The slave clock calculates the cnt value according to the optimal time offset θ koffset and the two timestamps C s1 and C s3 according to the frequency compensation algorithm;
[0064] Step 12: In the next synchronization period, the slave clock increases or decreases 1 clock cycle every cnt clock cycles to compensate for the clock frequency of the slave clock.
[0065] Furthermore, as Figures 2 - 3 shown, taking the SharkNet bus as an example, the master-slave clock exchanges timestamps to obtain the time deviation and transmission delay. The synchronization process mainly includes 3 steps: calculating the time offset, filtering, and calculating the transmission delay, and uses 3 types of messages for handshake communication: (1) 0xA1 message; (2) 0x51 message; (3) 0xA2 message. These synchronization messages are all 10 bytes, and the message type is determined by the mode field in the data frame structure of the SharkNet message packet. Specifically, the mode field of the A1 message is 0xA1, the mode field of the 51 message is 0x51, and the mode field of the A2 message is 0xA2. The specific steps are as follows:
[0066] First, the system's master clock is selected by the network using the Best Master Clock algorithm. After the master clock is selected, it periodically sends a 0xA1 message to the slave clock. In the 0xA1 message, the time count C at the time of leaving the port is recorded. m1 After the slave clock receives the 0xA1 message, it records the time count C when entering the slave clock port. s1 Subsequently, the slave clock sends a 0x51 message to the master clock and records the time count C when leaving the port. s2 After the master clock receives the 0x51 message, it records the time count C when the 0x51 message enters the master clock port. m2 Then the master clock sends a 0xA2 message containing the timestamp C m2 to the slave clock; finally, after the slave clock receives the 0xA2 message, it obtains four timestamps, namely C m1 , C s1 , C s2 , C m2 . According to the PTP algorithm, the following equations can be obtained:
[0067] C s1 -C m1 = θ offset +d ms ;
[0068] C m2 -C s2 = d sm -θ offset ;
[0069] Assume that the path delay is symmetric, that is, d ms = d sm . In this way, the average transmission delay d delay between the master and slave clocks and the time offset θ offset can be calculated:
[0070]
[0071] where C m1 , C s1 , C s2 , C m2 are the first transmission timestamp, the first reception timestamp, the second transmission timestamp, and the second reception timestamp respectively.
[0072] The calculated d delay contains noise components, so the Kalman filter algorithm needs to be used to estimate the clock deviation, clock drift, and clock drift rate of change, filter the value of d delay to obtain the optimal solution θ koffset of the time deviation.
[0073] A clock model equation needs to be established based on the working characteristics of the master clock and the slave clock:
[0074] Assume that C(t) represents the time of the slave clock node at the reference time t (t is the time of the master clock node). Then the time offset θ(t) of the slave clock can be expressed as:
[0075] θ(t) = C(t) - t;
[0076] θ offset = θ(t);
[0077] The change rate of the time offset is defined as the clock drift s(t), which is expressed as:
[0078]
[0079] where T cycle represents the time synchronization message sending interval.
[0080] γ(t) is the change rate of the clock drift (also known as the clock aging rate), and its relationship with s(t) is:
[0081]
[0082] The basic model of the clock can be derived from the above formula:
[0083]
[0084] where ω θ(k) , ω s(k) , ω γ(k) are the Gaussian white noises of the time offset, clock drift, and clock drift change rate respectively, and their variances are expressed as:
[0085] In the IEEE 1588 synchronization process, the clock is adjusted according to the estimated values of the time offset and clock drift. In the k-th synchronization iteration, the input parameters are used to adjust the time offset, clock drift, and clock drift change rate u θ (k), u θ (k), u θ (k). Assume that the correction input of the synchronization period T cycle is real-time effective. The IEEE 1588 three-state clock model can be expressed as:
[0086]
[0087] where u θ (k), u s (k), u γ (k) represent the correction values of the time offset, clock drift, and clock drift change rate respectively, k is the second preset time, ωθ(k) , ω s(k) , ω γ(k) are Gaussian white noises of time offset, clock drift, and clock drift rate of change respectively, and t is the first preset moment;
[0088] Next, the Kalman filter is used to estimate the clock deviation, clock drift, and clock drift rate of change, and to correct the clock state.
[0089] Prediction equation:
[0090]
[0091] P(k|k - 1) = AP(k - 1|k - 1)A T + Q;
[0092] Update equation:
[0093] K(k) = P(k|k - 1)H T [HP(k|k - 1)H T + R] -1 ;
[0094]
[0095] P(k|k) = (I - K(k)H)P(k|k - 1);
[0096] The above are the five basic formulas of the Kalman filter algorithm. Among them, represents the posterior state estimate at time (k - 1), represents the prior state estimate at time k, represents the posterior state estimate at time k, P(k|k - 1) represents the prior error covariance matrix at time k. P(k - 1|k - 1) represents the posterior error covariance matrix at time (k - 1). P(k|k) represents the posterior error covariance matrix at time k, and K(k) represents the Kalman gain at time k.
[0097] Applying the Kalman filter algorithm in time synchronization means using the time offset and clock drift calculated by the PTP algorithm as the observed values, and using the state vector x(k|k) in the update equation as the optimal solution of the filter. Among them, the state vector The first component θ(k) of is the optimal solution of the time offset calculated by the Kalman filter (θ koffset ), denoted as θ koffset = θ(k).
[0098] In the above Kalman filter equation, the state transition matrix A is:
[0099]
[0100] The control input matrix B is as follows:
[0101]
[0102] Posterior state estimation is as follows:
[0103]
[0104] The input vector u(k) of the clock state equation is as follows:
[0105] u(k) = [u θ (k) u s (k) u γ (k)] T ;
[0106] The observation vector z(k) at time k is as follows:
[0107] z(k) = [θ M (k) s M (k)] T ;
[0108] θ M (k) is the time offset calculated by the PTP algorithm at time k, and s M (k) is the clock drift at time k, that is:
[0109] θ M (k) = θ offset ;
[0110]
[0111] The observation matrix H is as follows:
[0112]
[0113] The covariance matrix of the process noise Q is as follows:
[0114]
[0115] The matrix P(k|k - 1) can be initialized to Q, that is:
[0116] P(1|0) = Q;
[0117] The covariance matrix R of the observation noise is as follows:
[0118]
[0119] When the synchronization message interval set by the system ends, the second round of clock synchronization is started. In this round, steps 1 and 2 are repeated to obtain four timestamps, namely C m3 、Cs3 , C s4 , C m4 . In this way, there are 5 timestamps in the slave clock, namely C m3 , C s3 , C s4 , C m4 , C s1 , and at the same time, a new time offset θ koffset (the value after Kalman filtering) is obtained. When the slave clock runs at a slightly faster or slower frequency within the time interval of the synchronization message, the time offset will accumulate. To solve this problem, a clock drift compensation algorithm is used to correct the clock drift. Specifically as follows:
[0120] Clock frequency drift compensation algorithm: After the slave clock obtains the time offset (θ koffset ) for the second time, the slave clock is compensated using the cnt value in the following formula. This means that after each 1 clock cycle, the slave clock will increase or decrease by one clock cycle. This method uses the time offset generated in the previous round to predict the possible time error in the master-slave clocks in the next round. This method avoids the continuous accumulation of time errors within the transmission interval period and improves the time synchronization accuracy of the network.
[0121] The calculation expression of the number of cycle values is:
[0122]
[0123] where C s3 is the third received timestamp, θ koffset is the optimal time offset in the next round, and cnt is the number of cycle values
[0124] When the transmission interval of the synchronization message is completed again, the master clock sends an A1 message to start the third round of time synchronization, and then repeats step 3. The difference is that θ koffset , C s1 , C s3 need to be updated.
[0125] Furthermore, Figure 3 is the simulation of the algorithm of the present invention under a symmetric link. It can be seen from the results that when the crystal oscillator frequencies of the master and slave clocks are 80 MHz, the time offset between the master and slave clocks basically remains within ±12.5 ns.
[0126] The various embodiments in this specification are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. For the same and similar parts between the various embodiments, reference can be made to each other.
[0127] In this article, specific examples are used to elaborate on the principles and implementation manners of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manners and application scopes. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A time synchronization method based on the IEEE 1588 protocol, characterized in that, Including: Based on the working characteristics of the network-based master clock and slave clock, a clock model is established according to the characteristics of the clock frequency; Obtain the timestamp set for the time synchronization of the master clock and slave clock in the current round, where the timestamp set includes: the first transmission timestamp, the second transmission timestamp, the first reception timestamp, and the second reception timestamp; Calculate the average transmission delay and time offset in the current round according to the timestamp set; Based on the clock model, construct a clock synchronization equation for Kalman filtering; Use the time offset and clock drift as the input values of Kalman filtering to obtain the optimal solution of the time offset in the current round; The slave clock modifies the local clock according to the optimal time offset value to complete the clock synchronization in the current round; Obtain the timestamp set for the time synchronization of the master clock and slave clock in the next round, where the timestamp set includes the third reception timestamp, the fourth reception timestamp, the third transmission timestamp, the fourth transmission timestamp, and the first reception timestamp of the previous round; Calculate the average transmission delay and time offset in the next round according to the third reception timestamp, the fourth reception timestamp, the third transmission timestamp, and the fourth transmission timestamp, and determine the optimal time offset in the next round according to the clock synchronization equation of Kalman filtering; Calculate the cycle number value according to the optimal time offset in the next round, the first reception timestamp, and its third reception timestamp; Perform frequency compensation on the slave clock according to the cycle number value.
2. The time synchronization method based on the IEEE 1588 protocol according to claim 1, wherein The calculation expression for the average transmission delay is: Among them, C m1 , C s1 , C s2 , C m2 are the first transmission timestamp, the first reception timestamp, the second transmission timestamp, and the second reception timestamp respectively. d ms refers to the transmission delay from the master clock to the slave clock, and d sm refers to the transmission delay from the slave clock to the master clock.
3. The time synchronization method based on the IEEE 1588 protocol according to claim 2, characterized in that, The calculation expression for the time offset is: where θ offset is the time offset.
4. A time synchronization method based on the IEEE 1588 protocol according to claim 3, characterized in that The calculation expression for the clock drift is: Among them, s is the clock drift, and T cycle represents the time synchronization message sending interval, and θ = θ offset is the slave clock time offset, and t is the first preset moment.
5. A time synchronization method based on the IEEE 1588 protocol according to claim 4, characterized in that, The expression for the clock model is: Among them, u θ (k), u s (k), u γ (k) represent the correction values of time offset, clock drift, and clock drift change rate respectively, k is the second preset time, ω θ(k) , ω s(k) , ω γ(k) are the Gaussian white noises of time offset, clock drift, and clock drift change rate respectively.
6. A time synchronization method based on the IEEE 1588 protocol according to claim 5, characterized in that, The calculation expression for the cycle number value is: Among them, C s3 is the third reception timestamp, θ koffset is the optimal time offset for the next round, and cnt is the cycle quantity value.
Citation Information
Cited By
AGV (Automatic Guided Vehicle) cooperative blockage avoidance path planning method for real-time road condition
CN121165742A
Real-time traffic-oriented agv cooperative avoidance path planning method
CN121165742B