CN120263328A - A time synchronization method based on IEEE 1588 protocol - Google Patents
A time synchronization method based on IEEE 1588 protocol Download PDFInfo
- Publication number
- CN120263328A CN120263328A CN202510491046.6A CN202510491046A CN120263328A CN 120263328 A CN120263328 A CN 120263328A CN 202510491046 A CN202510491046 A CN 202510491046A CN 120263328 A CN120263328 A CN 120263328A
- Authority
- CN
- China
- Prior art keywords
- clock
- time
- timestamp
- time offset
- synchronization
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Landscapes
- Synchronisation In Digital Transmission Systems (AREA)
Abstract
The invention provides a time synchronization method based on an IEEE 1588 protocol, and relates to the technical field of time synchronization. First, a network-based master-slave clock model is built to characterize the clock frequency. And secondly, acquiring a time stamp set of the master-slave clock of the current wheel, and calculating average transmission delay and time offset. Then, a Kalman filtering clock synchronization equation is constructed based on the clock model, time offset and drift are taken as input, an optimal time offset value is obtained, and a local clock is adjusted to complete current round synchronization. Subsequently, for the next round a new set of time stamps is acquired, the average transmission delay and time offset are calculated, and the optimal offset value is determined using the kalman filter equation. Meanwhile, the number of periods is calculated from the offset value and the reception time stamp, and the slave clock is frequency-compensated. The invention effectively solves the problem of low time synchronization precision in the prior art.
Description
Technical Field
The invention relates to the technical field of time synchronization, in particular to a time synchronization method based on an IEEE 1588 protocol.
Background
With the rapid development of fields such as industrial automation, robot technology and the internet of things, bus technology is widely applied to various industrial automation and control fields, so the requirements on bus performance become higher and higher. In view of the fact that time synchronization techniques are the basis for reliable operation of buses, it is very urgent to improve time synchronization performance.
Currently, the main time synchronization technologies include GPS-based clock synchronization, network Time Protocol (NTP), IEEE 1588 (PTP), and the like. The research shows that the GPS clock synchronization equipment has high precision, but can not be used in indoor environment without GPS, has larger limitation, NTP is based on clock synchronization of a client/server mode, the clock synchronization precision only reaches millisecond level, PTP is based on clock synchronization protocol of a master node/slave node mode, and can realize synchronization between slave node and master node clock. Compared with the NTP protocol, the PTP protocol has higher clock synchronization precision and can reach the sub microsecond level, so the PTP protocol has wider applicability in most bus networks.
The IEEE 1588 is commonly known as the precision clock synchronization Protocol standard for networked measurement and control systems, commonly known as the precision time Protocol (PTP, precisionTime Protocol). The PTP adopts a master-slave architecture, and a plurality of devices can realize synchronization by transmitting time stamp information in a network, and the precision can reach the level of millisecond. The core of PTP is that it exchanges time information between a master clock and a slave clock by exchanging synchronization messages, thereby ensuring that the slave clock adjusts its clock according to the network delay, keeping it consistent with the master clock.
Along with the development of IEEE 1588 protocol, especially IEEE 1588v2 version published in 2008, the protocol is obviously improved in expansibility, precision and reliability, is suitable for more complex network environment, and is widely applied to the fields of electric power, communication, finance and the like. In some high precision demanding applications, IEEE 1588 provides a network-based clock synchronization scheme that provides a stable, accurate clock source for a variety of modern industrial and communication networks. But time synchronization accuracy on the order of milliseconds has been difficult to meet future demands.
Disclosure of Invention
In order to overcome the defects of the prior art, the invention aims to provide a time synchronization method based on an IEEE 1588 protocol.
In order to achieve the above object, the present invention provides the following solutions:
A time synchronization method based on IEEE 1588 protocol, comprising:
based on the working characteristics of a master clock and a slave clock of a network, a clock model is established aiming at the characteristics of clock frequency;
Acquiring a time stamp set of time synchronization of a master clock and a slave clock of a current round, wherein the time stamp set comprises a first sending time stamp, a second sending time stamp, a first receiving time stamp and a second receiving time stamp;
calculating the average transmission delay and time offset of the current wheel according to the time stamp set;
Based on the clock model, constructing a Kalman filtering clock synchronization equation;
Taking the time offset and the clock drift as input values of Kalman filtering to obtain an optimal solution of the current round of time offset;
The slave clock modifies the local clock according to the optimal time offset value to complete the clock synchronization of the current round;
Acquiring a time stamp set of time synchronization of a master clock and a slave clock of the next round, wherein the time stamp set comprises a third receiving time stamp, a fourth receiving time stamp, a third sending time stamp, a fourth sending time stamp and a first receiving time stamp of the previous round;
calculating according to the third receiving time stamp, the fourth receiving time stamp, the third sending time stamp and the fourth sending time stamp to obtain average transmission delay and time offset of the next round, and determining the optimal time offset of the next round according to a clock synchronization equation of Kalman filtering;
Calculating a cycle number value according to the optimal time offset of the next round and the first receiving time stamp and the third receiving time stamp;
and frequency compensating the slave clock according to the cycle number value.
Preferably, the calculation expression of the average transmission delay is:
Wherein C m1、Cs1、Cs2、Cm2 is a first transmit timestamp, a first receive timestamp, a second transmit timestamp, and a second receive timestamp, respectively, d ms is a transmission delay from the master clock to the slave clock, and d sm is a transmission delay from the slave clock to the master clock.
Preferably, the calculation expression of the time offset is:
Where θ offset is the time offset.
Preferably, the calculation expression of the clock drift is:
Where s is clock drift, T cycle is a time synchronization message transmission interval, θ=θ offset is a slave clock time offset, and T is a first preset time.
Preferably, the expression of the clock model is:
wherein u θ(k)、us(k)、uγ (k) represents the correction values of the time offset, the clock drift and the clock drift change rate, k is the second preset time, and ω θ(k),ωs(k),ωγ(k) is the gaussian white noise of the time offset, the clock drift and the clock drift change rate, respectively.
Preferably, the calculation expression of the cycle number value is:
Where C s3 is the third receive timestamp, θ koffset is the optimal time offset for the next round, cnt is the number of cycles.
The invention discloses the following technical effects:
The invention provides a time synchronization method based on an IEEE 1588 protocol, which comprises the steps of establishing a clock model aiming at the characteristics of clock frequency based on the working characteristics of a master clock and a slave clock of a network, obtaining a time stamp set for synchronizing the time of the master clock and the slave clock of a current round, wherein the time stamp set comprises a first sending time stamp, a second sending time stamp, a first receiving time stamp and a second receiving time stamp, calculating average transmission delay and time offset of the current round according to the time stamp set, constructing a Kalman filter clock synchronization equation based on the clock model, taking the time offset and the clock drift as input values of Kalman filter to obtain an optimal solution of the time offset of the current round, modifying the local clock according to an optimal time offset value by the slave clock to complete the clock synchronization of the current round, obtaining a time stamp set for synchronizing the master clock and the slave clock of the next round, wherein the time stamp set comprises a third receiving time stamp, a fourth receiving time stamp, a third sending time stamp, a fourth sending time stamp and a first receiving time stamp of the previous round, calculating the average transmission delay and the time offset of the current round according to the time stamp set, calculating the time offset of the third receiving time stamp, the fourth receiving time stamp and the optimal time offset of the current round, calculating the time offset of the current round according to the time offset value of the optimal clock and the optimal clock. The time synchronization algorithm based on Kalman filtering is based on the comprehensive consideration of a plurality of factors such as local clock drift, path delay and the like, can effectively reduce the influence of asymmetric links, packet delay changes and frequency drift on clock synchronization, improves the accuracy of time offset calculated by using PTP, and effectively improves the time synchronization accuracy, and under the condition of ensuring the accuracy of calculated time offset, the frequency compensation algorithm provided by the invention can timely compensate clock drift generated by a slave clock by taking one clock cycle as a basis, ensures that the frequency of the slave clock can be effectively compensated within 1 transmission interval, and greatly improves the time synchronization accuracy of a system. Under the condition that the time stamp and the asymmetry adopted by PTP calculation are accurate by adopting various measures, the frequency compensation algorithm can keep the time deviation between the master clock and the slave clock within 1 clock, and the Kalman filtering algorithm and the frequency compensation algorithm are combined, so that modeling errors and time stamp measurement noise existing in the original system can be tolerated, the time synchronization algorithm can work more stably in an actual network, and the time synchronization precision of the system is improved.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions of the prior art, the drawings that are needed in the embodiments will be briefly described below, it being obvious that the drawings in the following description are only some embodiments of the present invention, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
Fig. 1 is a flowchart of a time synchronization method based on IEEE 1588 protocol according to an embodiment of the present invention;
FIG. 2 is a timing chart of a time synchronization method according to an embodiment of the present invention;
Fig. 3 is a diagram of simulation results of a time synchronization algorithm based on the shack bus protocol according to an embodiment of the present invention.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present invention, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
In order that the above-recited objects, features and advantages of the present invention will become more readily apparent, a more particular description of the invention will be rendered by reference to the appended drawings and appended detailed description.
As shown in fig. 1, the present invention provides a time synchronization method based on IEEE 1588 protocol, which is characterized by comprising:
Step 100, based on the working characteristics of a master clock and a slave clock of a network, establishing a clock model aiming at the characteristics of clock frequency;
Step 200, acquiring a time stamp set of time synchronization of a master clock and a slave clock of a current round, wherein the time stamp set comprises a first sending time stamp, a second sending time stamp, a first receiving time stamp and a second receiving time stamp;
Step 300, calculating the average transmission delay and time offset of the current wheel according to the time stamp set;
step 400, constructing a Kalman filtering clock synchronization equation based on the clock model;
Step 500, taking the time offset and the clock drift as input values of Kalman filtering to obtain an optimal solution of the current round of time offset;
step 600, modifying the local clock according to the optimal time offset value by the slave clock to complete the clock synchronization of the current round;
Step 700, acquiring a time stamp set of time synchronization of a master clock and a slave clock of the next round, wherein the time stamp set comprises a third receiving time stamp, a fourth receiving time stamp, a third sending time stamp, a fourth sending time stamp and a first receiving time stamp of the previous round;
step 800, calculating according to the third receiving time stamp, the fourth receiving time stamp, the third sending time stamp and the fourth sending time stamp to obtain the average transmission delay and the time offset of the next round, and determining the optimal time offset of the next round according to a clock synchronization equation of Kalman filtering;
Step 900, calculating a cycle number value according to the optimal time offset of the next round and the first receiving time stamp and the third receiving time stamp;
and step 1000, performing frequency compensation on the slave clock according to the cycle number value.
Specifically, the method mentioned in this embodiment may be further expressed by the following steps:
step1, based on the working characteristics of a master clock and a slave clock of a network, establishing a clock model aiming at the characteristics of clock frequency;
And 2, judging whether the network exchanges time information for the first time. If not, step 8 is performed;
And 3, according to an IEEE 1588 protocol, the master clock exchanges time information with the slave clock, and the slave clock acquires 4 pieces of time stamp information, namely C m1、Cs1、Cs2、Cm2.
Step 4, calculating average transmission delay d delay and time offset theta offset according to the obtained time stamp information by the slave clock;
Step 5, calculating a state transition matrix, an observation matrix, an initialization error vector and an error covariance matrix based on a clock model established from a clock, and then establishing a clock synchronization equation of Kalman filtering;
step 6, taking the time offset and the clock drift as input values of Kalman filtering, and obtaining an optimal solution theta koffset of the time offset in the current synchronous period through updating and iteration of a Kalman filtering equation;
Step 7, modifying the local clock according to the optimal time offset value by the slave clock to complete the first clock synchronization of the system;
Step 8, the slave clock needs to record the time stamp C s1 of the previous time synchronization process clock, then the master clock exchanges time information with the slave clock, and the slave clock contains 5 pieces of time stamp information C m3、Cs3、Cs4、Cm4、Cs1 at the moment;
Step 9, calculating average transmission delay d delay and time offset theta offset according to the obtained time stamp information by the slave clock;
step 10, taking time offset and clock drift as input values of Kalman filtering, and obtaining an optimal solution theta koffset of the time offset in the current synchronous period through updating and iteration of a Kalman filtering equation;
Step 11, calculating a cnt value according to a frequency compensation algorithm from a clock according to the optimal time offset theta koffset and the two time stamps C s1 and C s3;
Step 12, in the next round of synchronization period, the slave clock increases or decreases by 1 clock period according to the cnt clock period to compensate the clock frequency of the slave clock.
Further, as shown in fig. 2-3, taking SharkNet bus as an example, master-slave clocks are adopted to obtain time deviation and transmission delay through time stamp exchange, and the synchronization process mainly comprises 3 steps of calculating time offset, filtering and transmission delay, 3 messages are used for handshake communication (1) 0xA1 message (2) 0x51 message (3) 0xA2 message. These synchronization messages are all 10bytes and the message type is determined by the pattern field in the data frame structure of the SharkNet message packet. Specifically, the mode field of the A1 message is 0x A1, the mode field of the 51 message is 0x51, and the mode field of the a2 message is 0xA2. The method comprises the following specific steps:
First, the network selects the master clock of the system using the best master clock algorithm. After the master clock is selected, the master clock periodically sends a 0xA1 message to the slave clock. In the 0xA1 message, the time count C m1 when leaving the port is recorded, the time count C s1 when entering the slave clock port is recorded after the slave clock receives the 0xA1 message, then the slave clock sends the 0x51 message to the master clock and records the time count C s2 when leaving the port, the master clock receives the 0x51 message and records the time count C m2 when entering the master clock port, then the master clock sends the 0xA2 message with the time stamp C m2 to the slave clock, and finally, the slave clock receives the 0xA2 message and obtains 4 time stamps which are C m1、Cs1、Cs2、Cm2 respectively. The following equation can be obtained according to the PTP algorithm:
Cs1-Cm1=θoffset+dms;
Cm2-Cs2=dsm-θoffset;
Assuming that the path delays are symmetrical, i.e., d ms=dsm, the average propagation delay between the master and slave clocks, d delay, and the time offset, θ offset, can be calculated:
Wherein C m1、Cs1、Cs2、Cm2 is a first transmit timestamp, a first receive timestamp, a second transmit timestamp, and a second receive timestamp, respectively.
The calculated d delay contains noise components, so a Kalman filtering algorithm is needed to estimate clock deviation, clock drift and clock drift change rate, and the value of d delay is filtered to obtain the optimal solution θ koffset of time deviation.
The clock model equation needs to be established according to the working characteristics of the master clock and the slave clock:
assuming that C (t) represents the time of the slave clock node at the reference time t (t is the master clock node time), the time offset θ (t) of the slave clock can be expressed as:
θ(t)=C(t)-t;
θoffset=θ(t);
the rate of change of the time offset is defined as the clock drift s (t), expressed as:
Where T cycle denotes a time synchronization message transmission interval.
Gamma (t) is the rate of change of clock drift (also called clock aging rate), which is related to s (t) as follows:
From the above, a basic model of the clock can be derived:
Wherein ω θ(k),ωs(k),ωγ(k) is the gaussian white noise of the time offset, clock drift and rate of change of clock drift, respectively, their variances are expressed as:
In the IEEE 1588 synchronization process, the clock is adjusted according to the estimated values of the time offset and the clock drift. In the kth synchronization iteration, the time offset, clock drift, and clock drift rate of change u θ(k),uθ(k),uθ (k) are adjusted using the input parameters. Assuming that the correction input for synchronization period T cycle is valid in real time, the IEEE 1588 three-state clock model can be expressed as:
Wherein u θ(k),us(k),uγ (k) represents a correction value of time offset, clock drift and clock drift change rate respectively, k is a second preset time, ωθ (k),ωs(k),ωγ(k) is gaussian white noise of time offset, clock drift and clock drift change rate respectively, and t is a first preset time;
and then, adopting Kalman filtering to estimate clock deviation, clock drift and clock drift change rate, and realizing clock state correction.
The predictive equation:
P(k|k-1)=AP(k-1|k-1)AT+Q;
Updating the equation:
K(k)=P(k|k-1)HT[HP(k|k-1)HT+R]-1;
P(k|k)=(I-K(k)H)P(k|k-1);
the above is the five basic formulas of the kalman filter algorithm. Wherein, the Represents the posterior state estimate at time (k-1),Representing an a priori state estimate at time k,Representing a posterior state estimate at time k, P (k|k-1) represents an a priori 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.
The kalman filtering algorithm is applied to time synchronization, namely, the time offset and clock drift calculated by using the PTP algorithm are taken as observation values, and the state vector x (k|k) in the update equation is taken as the optimal solution of filtering. Wherein the state vectorThe first component θ (k) of (a) is the optimal solution of the time offset calculated by kalman filtering (θ koffset), denoted θ koffset =θ (k).
In the kalman filter equation, the state transition matrix a is:
The control input matrix B is:
Posterior state estimation The method comprises the following steps:
the clock state equation input vector u (k) is:
u(k)=[uθ(k)us(k)uγ(k)]T;
The observation vector z (k) at time k is:
z(k)=[θM(k) sM(k)]T;
θ 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, namely:
θM(k)=θoffset;
The observation matrix H is:
The covariance matrix of the process noise Q is:
The matrix P (k|k-1) may be initialized to Q, namely:
P(1|0)=Q;
the observed noise covariance matrix R is:
and when the synchronization message interval set by the system is ended, starting the second round of clock synchronization. In this round, steps 1 and 2 are repeated, obtaining four time stamps, i.e., C m3、Cs3、Cs4、Cm4. Thus, the slave clock has 5 time stamps, C m3、Cs3、Cs4、Cm4、Cs1, and a new time offset θ koffset (Kalman filtered value) is obtained. The time offset may accumulate when the slave clock runs at a slightly faster or slower frequency during the time interval of the synchronization message. To solve this problem, a clock drift compensation algorithm is used to correct clock drift. The method comprises the following steps:
Clock frequency drift compensation algorithm the slave clock is compensated using the cnt value in the following equation after the second time the time offset (θ koffset) is obtained from the clock. This means that the slave clock will increase or decrease by one clock cycle after every 1 clock cycle. The method uses the time offset generated by the previous round to predict the time error that may occur in the next round of master-slave clocks. The method avoids continuous accumulation of time errors in the transmission interval period and improves the time synchronization precision of the network.
The calculation expression of the cycle number value is as follows:
Wherein C s3 is the third reception time stamp, θ koffset is the optimal time offset for the next round, cnt is the number of cycles
When the transmission interval of the synchronization message is completed again, the master clock transmits an A1 message to start the third time synchronization, and then repeats step 3. Except that theta koffset、Cs1、Cs3 is required to be updated.
Further, fig. 3 is a simulation performed on a symmetric link by the algorithm of the present invention, and it can be seen from the result that, in the case of a master-slave clock with a frequency of 80MHz, the time offset of the master-slave clock is substantially maintained within ±12.5ns.
In the present specification, each embodiment is described in a progressive manner, and each embodiment is mainly described in a different point from other embodiments, and identical and similar parts between the embodiments are all enough to refer to each other.
The principles and embodiments of the present invention have been described herein with reference to specific examples, which are intended to facilitate an understanding of the principles and concepts of the invention and are to be varied in scope and detail by persons of ordinary skill in the art based on the teachings herein. In view of the foregoing, this description should not be construed as limiting the invention.
Claims (6)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202510491046.6A CN120263328A (en) | 2025-04-18 | 2025-04-18 | A time synchronization method based on IEEE 1588 protocol |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202510491046.6A CN120263328A (en) | 2025-04-18 | 2025-04-18 | A time synchronization method based on IEEE 1588 protocol |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| CN120263328A true CN120263328A (en) | 2025-07-04 |
Family
ID=96179212
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202510491046.6A Pending CN120263328A (en) | 2025-04-18 | 2025-04-18 | A time synchronization method based on IEEE 1588 protocol |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN120263328A (en) |
-
2025
- 2025-04-18 CN CN202510491046.6A patent/CN120263328A/en active Pending
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US9288037B2 (en) | Method for time synchronization in a communications network | |
| US9344981B2 (en) | Method for synchronizing clocks in a communication network | |
| US10164762B2 (en) | Method, system and device for clock synchronization over time-varying and lossy networks | |
| CN109392074B (en) | Clock calibration method and device | |
| CN103812595B (en) | A kind of TPS time synchronized innovatory algorithm based on IEEE1588 synchronization mechanism | |
| CN110460553B (en) | Implicit node clock frequency offset estimation method free of timestamp interaction | |
| CN106452650A (en) | Clock synchronizing frequency deviation estimation method applicable to multi-hop wireless sensor network | |
| CN114205045B (en) | TTE network clock calibration method and system | |
| CN102754369B (en) | Method for time synchronization in a communication network | |
| CN102377558B (en) | For the system and method for time synchronized in communication network | |
| CN114389735A (en) | A Clock Synchronization Method Based on IEEE802.1AS Redundant Master Clock | |
| CN117176285A (en) | PTP time synchronization method and device based on FPGA | |
| CN119402125B (en) | A cross-domain clock synchronization method in time-sensitive networks | |
| CN113141225A (en) | Time synchronization method, device, storage medium and terminal | |
| CN106612152A (en) | Method for accurately evaluating lower bound of time offset estimator of IEEE1588 synchronous clock | |
| CN119011058A (en) | IEEE 802.1AS clock synchronization method for smart grid | |
| CN120263328A (en) | A time synchronization method based on IEEE 1588 protocol | |
| CN115865249B (en) | A method for improving clock synchronization accuracy and stability of distributed clock synchronization system | |
| CN114520703B (en) | Clock drift compensation method and circuit for time synchronization between industrial network devices | |
| CN107343314B (en) | Time synchronization method and device of wireless sensor network | |
| CN114301561A (en) | Clock synchronization device and method and clock servo system and method for improving IEEE1588 protocol | |
| CN110289929B (en) | Network synchronization method | |
| CN115987438A (en) | A PTP Precise Time Synchronization Method for Predicting Asymmetric Delay | |
| JP2023003969A (en) | Slave device, time synchronization method, and time synchronization program | |
| EP3732806B1 (en) | Method, device and system for estimating offset skew and drift |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination |