Clock drift compensation method, device, terminal equipment and storage medium

By receiving the synchronization frame and calculating the time deviation, rapid and periodic compensation is performed, which solves the problem of clock drift compensation in the distributed clock mechanism, achieves synchronization between the equipment system time and the reference time, and improves the consistent task execution of industrial Ethernet equipment. sex.

CN114640415BActive Publication Date: 2024-09-03SHANGHAI ANLOGIC INFOTECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210190191.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-02-28
Publication Date
2024-09-03
Estimated Expiration
2042-02-28

AI Technical Summary

Technical Problem

In the distributed clock mechanism, clock drift compensation is difficult to achieve, resulting in synchronization differences between the device system time and the reference time, affecting the synchronous execution of device tasks on the industrial Ethernet.

Method used

By receiving the current synchronization frame, extracting the reference time, calculating the time deviation, and calculating the fast compensation value and periodic compensation value of each timing cycle of the local clock based on the time deviation, fast compensation and periodic compensation are performed to eliminate clock drift.

Benefits of technology

Effective compensation for clock drift is achieved, ensuring the synchronization of the slave system time and the reference time, preventing possible time deviations in the future, and improving the clock synchronization consistency of the entire system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114640415B
    Figure CN114640415B
Patent Text Reader

Abstract

The present invention discloses a clock drift compensation method, device, terminal equipment and storage medium, the method comprising: calculating the time deviation according to the current slave station system time, the current reference time and the transmission delay of the current synchronization frame; calculating the fast compensation value and the periodic compensation value of the cycle increment of each timing cycle of the local clock of the synchronization frame receiving slave station in the current synchronization cycle according to the time deviation, and then performing fast compensation and periodic compensation on the cycle increment of the local clock of the synchronization frame receiving slave station according to the fast compensation value and the periodic compensation value of the cycle increment of each timing cycle. By implementing the embodiment of the present invention, the final clock drift compensation can be achieved through fast compensation and periodic compensation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of time synchronization technology, and in particular to a clock drift compensation method, device, terminal equipment and storage medium. Background Art

[0002] Industrial Ethernet single-line topology such as Figure 1 As shown, the master station sends Ethernet frames to each slave station. The slave station can directly process the received message and extract or insert relevant user data from the message, and then transmit the message to the next slave station. The last slave station sends back the fully processed message. The Ethernet frame finally returns to the master station after passing through all slave stations.

[0003] The slave stations in industrial Ethernet generally have a local clock with a period of 10 nanoseconds embedded. According to the distributed clock mechanism, the first slave station with distributed clock function connected to the master station is used as the reference slave station (i.e. slave station 1 in the figure), and the time of the reference slave station is used as the reference time to synchronize the time of other slave stations, thereby controlling the synchronous execution of tasks of each device.

[0004] When the system starts, there is a certain difference between the local time of the remaining slave stations and the reference time of the reference slave station. This difference is called the initial time offset. The delay when the data frame propagates between the slave stations is called the transmission delay. During operation, the local crystal oscillator used by each slave station will have a slight difference in the timing cycle, which is called clock drift. In the distributed clock mechanism, these factors must be compensated so that the system time in each device is synchronized with the reference time after compensation, thereby achieving synchronization of the entire system. It can be seen that the synchronization of the system time in the distributed clock mechanism mainly includes two parts: one is the initial time offset and transmission delay, and the other is the clock drift compensation of each slave station. The present invention mainly solves the compensation of clock drift of slave stations in the distributed clock mechanism. Summary of the invention

[0005] The embodiments of the present invention provide a clock drift compensation method, an apparatus, a terminal device and a storage medium, which can compensate for the clock drift of a slave station in a distributed clock mechanism.

[0006] An embodiment of the present invention provides a clock drift compensation method, comprising: receiving a current synchronization frame, and extracting a reference time from the current synchronization frame; wherein the reference time is the time displayed by the reference slave station when the reference slave station receives the current synchronization frame;

[0007] Calculate the current slave station system time when receiving the current synchronization frame according to the current local time when receiving the current synchronization frame and the initial time offset; calculate the time deviation according to the current slave station system time, the reference time and the transmission delay of the current synchronization frame;

[0008] Calculate the fast compensation value of the period increment of each timing cycle of the local clock in the current synchronization cycle according to the time deviation; wherein the sum of the fast compensation values ​​of the period increment of all timing cycles of the local clock in the current synchronization cycle is the inverse of the time deviation;

[0009] According to the time deviation, the synchronization time interval and the compensation accumulation of the previous synchronization cycle, the periodic compensation value of the period increment of each timing cycle of the local clock in the current synchronization cycle is calculated; wherein the synchronization time interval is the time interval between the reception time of the current synchronization frame and the reception time of the previous synchronization frame; the compensation accumulation of a synchronization cycle is the sum of the periodic compensation values ​​of the period increment of all timing cycles of the local clock in its synchronization cycle;

[0010] According to the fast compensation value and the periodic compensation value of the period increment of each timing cycle in the current synchronization cycle, the period increment of the local clock in the current synchronization cycle is quickly compensated and periodically compensated.

[0011] Further, calculating a fast compensation value of a period increment of a current timing period of a local clock includes:

[0012] If the current timing cycle is the initial timing cycle, the absolute value of the time deviation is used as the residual deviation of the current timing cycle; otherwise, the difference between the residual deviation of the previous timing cycle and the absolute value of the fast compensation value of the previous timing cycle is used as the residual deviation of the current timing cycle;

[0013] Determine whether the residual deviation of the current timing cycle is greater than a first preset compensation value; if so, then when the time deviation is greater than zero, use the opposite of the first preset compensation value as the quick compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, use the first preset compensation value as the quick compensation value of the cycle increment of the current timing cycle; if not, then when the time deviation is greater than zero, use the opposite of the residual deviation of the current timing cycle as the quick compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, use the residual deviation of the current timing cycle as the quick compensation value of the cycle increment of the current timing cycle; wherein, the first preset compensation value is greater than zero.

[0014] Further, calculating the periodic compensation value of the period increment of the current timing cycle of the local clock specifically includes:

[0015] Determine whether the current timing cycle needs periodic compensation based on the compensation period. If so, when the cumulative offset is greater than zero, the opposite of the second preset compensation value is used as the periodic compensation value of the current timing cycle. When the cumulative offset is not greater than zero, the second preset compensation value is used as the periodic compensation value of the current timing cycle. If not, the periodic compensation value of the current timing cycle is 0; wherein the cumulative offset is the difference between the compensation accumulation of the previous synchronization cycle and the time deviation; the compensation period is calculated based on the synchronization time interval, the local timing cycle, the second preset compensation value and the cumulative offset, and the second preset compensation value is greater than zero.

[0016] Furthermore, the first preset compensation value is smaller than the difference between the timing period and the second preset compensation value, and the second preset compensation value is smaller than the timing period.

[0017] Based on the above method embodiment, the present invention provides a corresponding device embodiment;

[0018] An embodiment of the present invention provides a clock drift compensation device, comprising: a current reference time extraction module, a time deviation calculation module, a fast compensation value calculation module, a periodic compensation value calculation module and a drift compensation module;

[0019] The current reference time extraction module is used to receive the current synchronization frame and extract the reference time from the current synchronization frame; wherein the reference time is the time displayed by the reference slave station when the reference slave station receives the current synchronization frame;

[0020] The time deviation calculation module is used to calculate the current slave station system time when receiving the current synchronization frame according to the current local time when receiving the current synchronization frame and the initial time offset; calculate the time deviation according to the current slave station system time, the reference time and the transmission delay of the current synchronization frame;

[0021] The fast compensation value calculation module is used to calculate the fast compensation value of the cycle increment of each timing cycle of the local clock in the current synchronization cycle according to the time deviation; wherein the sum of the fast compensation values ​​of the cycle increments of all timing cycles of the local clock in the current synchronization cycle is the inverse of the time deviation;

[0022] The periodic compensation value calculation module is used to calculate the periodic compensation value of the period increment of each timing cycle of the local clock in the current synchronization cycle according to the time deviation, the synchronization time interval and the compensation accumulation of the previous synchronization cycle; wherein the synchronization time interval is the time interval between the reception time of the current synchronization frame and the reception time of the previous synchronization frame; the compensation accumulation of a synchronization cycle is the sum of the periodic compensation values ​​of the period increments of all timing cycles of the local clock in its synchronization cycle;

[0023] The drift compensation module is used to perform rapid compensation and periodic compensation on the period increment of the local clock in the current synchronization period according to the rapid compensation value and periodic compensation value of the period increment of each timing period in the current synchronization period.

[0024] Furthermore, the fast compensation value calculation module calculates the fast compensation value of the cycle increment of the current timing cycle of the local clock, specifically including:

[0025] If the current timing cycle is the initial timing cycle, the absolute value of the time deviation is used as the residual deviation of the current timing cycle; otherwise, the difference between the residual deviation of the previous timing cycle and the absolute value of the fast compensation value of the previous timing cycle is used as the residual deviation of the current timing cycle;

[0026] Determine whether the residual deviation of the current timing cycle is greater than a first preset compensation value; if so, then when the time deviation is greater than zero, use the opposite of the first preset compensation value as the quick compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, use the first preset compensation value as the quick compensation value of the cycle increment of the current timing cycle; if not, then when the time deviation is greater than zero, use the opposite of the residual deviation of the current timing cycle as the quick compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, use the residual deviation of the current timing cycle as the quick compensation value of the cycle increment of the current timing cycle; wherein, the first preset compensation value is greater than zero.

[0027] Furthermore, the periodic compensation value calculation module calculates the periodic compensation value of the period increment of the current timing cycle of the local clock, specifically including:

[0028] Determine whether the current timing cycle needs periodic compensation based on the compensation period. If so, when the cumulative offset is greater than zero, the opposite of the second preset compensation value is used as the periodic compensation value of the current timing cycle. When the cumulative offset is not greater than zero, the second preset compensation value is used as the periodic compensation value of the current timing cycle. If not, the periodic compensation value of the current timing cycle is 0; wherein the cumulative offset is the difference between the compensation accumulation of the previous synchronization cycle and the time deviation; the compensation period is calculated based on the synchronization time interval, the local timing cycle, the second preset compensation value and the cumulative offset, and the second preset compensation value is greater than zero.

[0029] Furthermore, the first preset compensation value is smaller than the difference between the timing period and the second preset compensation value, and the second preset compensation value is smaller than the timing period.

[0030] Based on the above method embodiment, the present invention provides a terminal device embodiment;

[0031] An embodiment of the present invention provides a terminal device including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor; wherein, when the processor executes the computer program, the clock drift compensation method described in any one of the present invention is implemented.

[0032] Based on the above method embodiment, the present invention provides a storage medium embodiment;

[0033] An embodiment of the present invention provides a storage medium, wherein the storage medium includes a stored computer program, wherein when the computer program is running, the device where the storage medium is located is controlled to execute any one of the clock drift compensation methods described in the present invention.

[0034] The following beneficial effects are achieved by implementing the embodiments of the present invention:

[0035] An embodiment of the present invention provides a clock drift compensation method, apparatus, terminal equipment and storage medium. The method calculates a time deviation based on the current slave system time, the current reference time and the transmission delay of the current synchronization frame. The calculated time deviation is the deviation caused by the clock drift between the local clock of the slave station receiving the current synchronization frame (hereinafter referred to as the synchronization frame receiving slave station) and the local clock of the reference slave station; then, according to the time deviation, a fast compensation value and a periodic compensation value of the period increment of each timing cycle of the local clock of the synchronization frame receiving slave station are calculated in the current synchronization cycle, and then, according to the fast compensation value and the periodic compensation value of the period increment of each timing cycle, the period increment of the local clock of the synchronization frame receiving slave station is quickly compensated and periodically compensated; the sum of the fast compensation values ​​of the period increment of all timing cycles of the local clock of the synchronization frame receiving slave station is the inverse of the time deviation, so that the time deviation that has been formed between the synchronization frame receiving slave station and the reference slave station when receiving the current synchronization frame can be compensated, thereby eliminating the existing deviation between the slave system time of the synchronization frame receiving slave station and the reference time. In addition to fast compensation, periodic compensation is also performed to compensate for the time deviation that may be formed between the synchronization frame receiving slave station and the reference slave station from receiving the current synchronization frame to receiving the next synchronization frame, thereby preventively compensating for the possible deviation between the slave station system time and the reference time of the synchronization frame receiving slave station before the next synchronization frame arrives. The final clock drift compensation is achieved through fast compensation and periodic compensation. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] Figure 1 It is a single-line topology diagram of Industrial Ethernet.

[0037] Figure 2 It is a flowchart of a clock drift compensation method provided by an embodiment of the present invention.

[0038] Figure 3 It is a schematic diagram of a calculation flow of a rapid compensation value provided by an embodiment of the present invention.

[0039] Figure 4 It is a schematic diagram of a calculation process of a periodic compensation value provided by an embodiment of the present invention.

[0040] Figure 5 It is a structural schematic diagram of a clock drift compensation device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0041] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0042] like Figure 2 As shown, an embodiment of the present invention provides a clock drift compensation method, which at least includes the following steps:

[0043] Step S101: receiving a current synchronization frame, and extracting a reference time from the current synchronization frame; wherein the reference time is the time displayed by the reference slave station when the reference slave station receives the current synchronization frame.

[0044] Step S102: Calculate the current slave station system time when receiving the current synchronization frame according to the current local time when receiving the current synchronization frame and the initial time offset; calculate the time deviation according to the current slave station system time, the reference time and the transmission delay of the current synchronization frame.

[0045] Step S103: Calculate the fast compensation value of the period increment of each timing cycle of the local clock in the current synchronization cycle according to the time deviation; wherein, the sum of the fast compensation values ​​of the period increment of all timing cycles of the local clock in the current synchronization cycle is the inverse of the time deviation.

[0046] Step S104: Calculate the periodic compensation value of the period increment of each timing cycle of the local clock in the current synchronization cycle according to the time deviation, the synchronization time interval and the compensation accumulation of the previous synchronization cycle; wherein the synchronization time interval is the time interval between the receiving time of the current synchronization frame and the receiving time of the previous synchronization frame; the compensation accumulation of a synchronization cycle is the sum of the periodic compensation values ​​of the period increment of all timing cycles of the local clock in its synchronization cycle.

[0047] Step S105: Perform fast compensation and periodic compensation on the period increment of the local clock in the current synchronization cycle according to the fast compensation value and periodic compensation value of the period increment of each timing cycle in the current synchronization cycle.

[0048] When performing clock drift compensation, the master station reads the Ethernet port status of each slave station and uses it to deduce the topology of the entire communication network; then the master station sends a broadcast frame to each slave station, records the local time when each slave station receives the broadcast frame leader, and then calculates the transmission delay between the slave stations (the transmission delay at this time refers to the transmission delay of the synchronization frame from the first slave station (i.e., the reference slave station) to the currently receiving slave station); the master station calculates the initial time offset of each slave station based on the reference time, the transmission delay, and the local time of each slave station; then the master station periodically sends Ethernet frames, and sends the local time in the reference slave station as the reference time to all downstream slave stations (hereinafter referred to as such Ethernet frames as synchronization frames), so as to assist the downstream slave stations in clock drift compensation. The clock drift compensation method provided by the present invention is described in detail below, taking one of the slave stations as a synchronization frame receiving slave station.

[0049] For step S101, after receiving the current synchronization frame, the synchronization frame receiving slave station extracts the reference time from the current synchronization frame. The reference time is the reference time when the reference slave station receives the current synchronization frame (such as Figure 1 As shown, in the present invention, the time displayed by the local clock of the first slave station connected to the master station, slave station 1 in the figure, is used as the reference slave station.

[0050] For step S102, when receiving the current synchronization frame, the synchronization frame receiving slave station records the time displayed by the local clock at this time to obtain the above-mentioned current local time; then the sum of the current local time and the initial time offset is calculated to obtain the system time of the synchronization frame receiving slave station when receiving the current synchronization frame, that is, the above-mentioned current slave station system time. Next, the time deviation is calculated based on the current slave station system time, the current reference time, and the transmission delay of the current synchronization frame. Specifically, time deviation = current slave station system time - transmission delay - current reference time.

[0051] It should be noted that the method of measuring and calculating the initial time offset and transmission delay in the distributed clock mechanism has been described in the industrial Ethernet protocol as prior art content and will not be described in detail here. Before performing clock drift compensation, the master station first transmits the initial time offset and transmission delay of each slave station to each slave station.

[0052] For step S103, in a preferred embodiment, calculating the fast compensation value of the cycle increment of the local clock-current timing cycle specifically includes:

[0053] If the current timing cycle is the initial timing cycle, the absolute value of the time deviation is used as the residual deviation of the current timing cycle; otherwise, the difference between the residual deviation of the previous timing cycle and the absolute value of the fast compensation value of the previous timing cycle is used as the residual deviation of the current timing cycle;

[0054] Determine whether the residual deviation of the current timing cycle is greater than a first preset compensation value; if so, then when the time deviation is greater than zero, use the opposite of the first preset compensation value as the quick compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, use the first preset compensation value as the quick compensation value of the cycle increment of the current timing cycle; if not, then when the time deviation is greater than zero, use the opposite of the residual deviation of the current timing cycle as the quick compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, use the residual deviation of the current timing cycle as the quick compensation value of the cycle increment of the current timing cycle; wherein, the first preset compensation value is greater than zero.

[0055] For a better understanding of the calculation of the fast compensation value, please refer to Figure 3 , taking the first preset compensation value of 8 nanoseconds as an example:

[0056] First, the absolute value of the time deviation is used as the initial residual deviation, that is, as the residual deviation of the initial counting cycle, and then it is judged whether the residual deviation is greater than 8. If the residual deviation is greater than 8, when the time deviation is greater than 0, the fast compensation value is set to -8; when the time deviation is not greater than 0, the fast compensation value is set to 8; if the residual deviation value is not greater than 8, when the time deviation is greater than 0, the opposite of the residual deviation is the fast compensation value; when the time deviation is not greater than 0, the residual deviation is used as the fast compensation value; then the residual deviation is updated, and the difference between the current residual deviation and the absolute value of the currently calculated fast compensation value is used as the updated residual deviation, and then the fast compensation value of the next counting cycle is repeatedly calculated based on the updated residual deviation. When the next synchronization frame is received, the time deviation is recalculated to update the time deviation, and then the calculation of the fast compensation value of the period increment of each timing cycle of the local clock of the synchronization frame receiving slave station in the next synchronization cycle is started.

[0057] According to the above diagram, it is assumed that the time interval between adjacent synchronization frames is 1 microsecond, that is, the synchronization frame receiving slave station receives a synchronization frame every 1 microsecond for synchronization, the time deviation calculated after receiving the current synchronization frame is -100 nanoseconds, the timing cycle of the local clock of the synchronization frame receiving slave station is 10 nanoseconds, and the first preset compensation value is 8. Then, one synchronization cycle includes 100 timing cycles of the local clock. During fast compensation, the fast compensation values ​​of the first 12 timing cycles are 8, the fast compensation value of the 13th timing cycle is 4, and the fast compensation values ​​of subsequent timing cycles are all 0.

[0058] For step S104: in a preferred embodiment, calculating the periodic compensation value of the period increment of the current timing cycle of the local clock specifically includes:

[0059] Determine whether the current timing cycle needs periodic compensation based on the compensation period. If so, when the cumulative offset is greater than zero, the opposite of the second preset compensation value is used as the periodic compensation value of the current timing cycle. When the cumulative offset is not greater than zero, the second preset compensation value is used as the periodic compensation value of the current timing cycle. If not, the periodic compensation value of the current timing cycle is 0; wherein the cumulative offset is the difference between the compensation accumulation of the previous synchronization cycle and the time deviation; the compensation period is calculated based on the synchronization time interval, the local timing cycle, the second preset compensation value and the cumulative offset, and the second preset compensation value is greater than zero.

[0060] The local timing cycle refers to the timing cycle of the local clock of the current receiving slave station, which is preferably 10 nanoseconds in the present invention.

[0061] For compensation accumulation, the compensation accumulation of a synchronization period is equal to the sum of the periodic compensation values ​​of the cycle increments of all timing cycles of the local clock within the synchronization period.

[0062] For the cumulative offset, the cumulative offset in a synchronization cycle is equal to the sum of the time deviation of this cycle and the compensation accumulation of the previous synchronization cycle.

[0063] As for the compensation period, in the present application, the compensation period refers to the interval of performing a periodic compensation within a synchronization period. Compensation period = synchronization time interval ÷ local timing period ÷ absolute value of accumulated offset * second preset compensation value.

[0064] After learning the above concepts, the following Figure 4 The calculation of the periodic compensation value is described in detail, assuming that the second compensation value is 1;

[0065] like Figure 4As shown, firstly, a time deviation is obtained, and the cumulative offset is calculated according to the cumulative offset = compensation accumulation - time deviation, and the initial compensation accumulation is 0; according to: compensation period = synchronization time interval ÷ local timing period ÷ | cumulative offset | * second preset compensation value, the compensation period within this synchronization period is calculated, and then the compensation accumulation is set to 0; then a cycle count is set, and the cycle count is set to the compensation period, and then the periodic compensation value of each timing period is calculated; when calculating, firstly, it is determined whether the cycle count is equal to 1, if not, it means that the time for periodic compensation has not yet arrived, that is, it is determined that the current timing period does not need periodic compensation, at this time, the periodic compensation value of the current timing period is set to 0, the cycle count is reduced by 1, and it is returned to determine whether the cycle count is equal to 1 to determine whether the next timing period needs periodic compensation value, and the periodic compensation value of the next timing period is calculated; if it is determined that the cycle count value is equal to 1, it means that the current timing period needs periodic compensation, and at this time, it is determined whether the cumulative offset is greater than 0, and if so, the opposite number of the second preset compensation value -1 is used as the periodic compensation value of the current timing period. If not, the second preset compensation value 1 is used as the periodic compensation value of the current timing cycle, and then the compensation accumulation is updated according to compensation accumulation = compensation accumulation + periodic compensation value; the cycle count is reset to the compensation cycle, and the calculation of the periodic compensation values ​​of other timing cycles is continued. When the next synchronization frame arrives, the time deviation is updated, and then the compensation accumulation of the previous synchronization cycle is returned to the cumulative offset calculation step to calculate the cumulative offset of the next synchronization cycle.

[0066] For step S105: in a preferred embodiment, according to the fast compensation value and the periodic compensation value of the period increment of each timing cycle in the current synchronization cycle, the period increment of the local clock in the current synchronization cycle is quickly compensated and periodically compensated, specifically including:

[0067] When the fast compensation value and the periodic compensation value of the cycle increment of each timing cycle are calculated, the cycle increment of the timing cycle is determined based on the fast compensation value and the periodic compensation value, and then the local clock is controlled to adjust the count based on the cycle increment of the timing cycle; this operation is repeated until the count of the local clock is adjusted according to the cycle increments of all timing cycles within the current synchronization cycle.

[0068] Specifically, through the above steps S102 and S103, it can be learned that within the current synchronization cycle, the fast compensation value and the periodic compensation value of the period increment of each timing cycle of the local clock of the synchronization frame receiving slave station are obtained. After the fast compensation value and the periodic compensation value of the period increment of each timing cycle are calculated, the period increment of the timing cycle is determined based on the fast compensation value and the periodic compensation value, and then the local clock is controlled to perform counting adjustment based on the period increment.

[0069] In schematic form, assume that the synchronization frame time interval is 1000 nanoseconds, the time deviation calculated after receiving the initial synchronization frame is -50 nanoseconds, the local timing cycle of the local clock of the synchronization frame receiving slave station is 10 nanoseconds, the first preset compensation value is 8, and the second preset compensation value is 1; then one synchronization cycle includes 100 timing cycles of the local clock, and during fast compensation, it can be calculated that the fast compensation values ​​of the first 6 timing cycles are 8, the fast compensation value of the 7th timing cycle is 2, and the fast compensation values ​​of the subsequent other timing cycles are all 0. Since it is an initial synchronization frame, the compensation accumulation is 0, and the cumulative offset of the initial synchronization cycle is calculated to be -50 at this time, and then the compensation period of the initial synchronization cycle is calculated to be 1000÷10÷|-50|*1=2; that is, in the initial synchronization cycle, periodic compensation is performed every 2 timing cycles. Since the accumulated offset is -50, which is less than 0, when a periodic compensation value is required, the periodic compensation value is 1; the periodic compensation value of the period increment in the 2nd, 4th, 6th, 8th, 10th, ... and 100th timing cycles is 1, and the periodic compensation value of other timing cycles is 0; the comprehensive fast compensation and periodic compensation can be obtained, and within the initial synchronization cycle, the period increment of the 7th timing cycle is 10+2=12 nanoseconds (where 10 is the local timing cycle of the local clock). The period increment of the 2nd, 4th and 6th timing cycles is 10+8+1=19 nanoseconds; the period increment of the 1st, 3rd and 5th timing cycles is 10+8=18 nanoseconds; the period increment of the 8th, 10th, 12th, 14th, 16th, 18th, ... and other subsequent even-numbered timing cycles is 10+1=11 nanoseconds; the period increment of the 9th, 11th, 13th, 15th, 17th, ... and other subsequent odd-numbered timing cycles is 10 nanoseconds. By controlling the local clock to time according to the period increment of each timing cycle, the 50 nanosecond deviation caused by clock drift between the synchronization frame receiving slave and the reference slave when receiving the initial synchronization frame can be quickly eliminated through fast compensation. By periodic compensation, an additional 50 nanosecond deviation can be compensated to preventively compensate for the deviation that may occur before the second synchronization frame arrives. Assuming that the second synchronization frame is received after the initial synchronization cycle is synchronized, the time deviation of the second synchronization frame is calculated to be 30 nanoseconds; then during the fast compensation of the second synchronization cycle, it can be calculated that the fast compensation values ​​of the first three timing cycles are -8, the fast compensation value of the fourth timing cycle is -6, and the fast compensation values ​​of the subsequent other timing cycles are all 0.At this time, the cumulative offset of the second synchronization cycle is calculated, 50-30=20, and then the compensation period of the initial synchronization cycle is calculated to be 1000÷10÷20*1=5; (Preferably, in the present invention, when calculating the compensation period, if the calculation result is less than 1, 1 is directly used as the compensation period. If it is greater than 1 and is a decimal, the compensation period is obtained according to the principle of rounding) That is to say, in this second synchronization period, periodic compensation is performed every 5 timing cycles. Since the cumulative offset is 20, which is greater than 0, when a periodic compensation value is required, the periodic compensation value is -1; the periodic compensation value of the periodic increments of the 5th, 10th, 15th, 20th, 25th, 30th, 35th, 40th, 45th, 50th, 55th, 60th, 65th, 70th, 75th, 80th, 85th, 90th, 95th and 100th timing cycles is -1, and the periodic compensation value of other timing cycles is 0; the comprehensive fast compensation and periodic compensation can be obtained, In the second synchronization cycle, the cycle increment in the 1st to 3rd timing cycles is 10-8=2 nanoseconds, the cycle increment in the 4th timing cycle is 10-6=4 nanoseconds, the cycle increment in the 5th, 10th, 15th, 20th, 25th, 30th, 35th, 40th, 45th, 50th, 55th, 60th, 65th, 70th, 75th, 80th, 85th, 90th, 95th and 100th timing cycles is 10-1=9 nanoseconds, and the cycle increments in the remaining timing cycles are all 10 nanoseconds.

[0070] In a preferred embodiment, the first preset compensation value is less than the difference between the timing cycle and the second preset compensation value, and the second preset compensation value is less than the timing cycle. By setting this condition, it is possible to ensure that the timing of the local timer remains in a strictly increasing state, thereby preventing certain timed tasks from being repeatedly executed. For example, assuming that the second preset compensation value is 1 and the timing cycle of the local clock is 10, then the first preset compensation value needs to be less than 9. Indicatively, it can be set to any value from 1 to 8. If the first preset compensation value is set to 9 and the second preset compensation value is set to 1, then there may be a situation where, in a certain timing cycle, the fast compensation value is -9, and the periodic compensation value at this time happens to be -1; then the period increment of this timing cycle is 10-9-1=0, that is, the period increment is 0, resulting in no increase in time, and the same timed task will be executed twice, causing system confusion. If the first preset compensation value is set to 10 and the second preset compensation value is set to 1, a situation may occur where, in a certain timing cycle, the fast compensation value is -10, and the periodic compensation value at this time happens to be -1; then the period increment of this timing cycle is 10-9-1=-1, that is, the period increment is -1, which causes time to flow backwards. At this time, the scheduled task executed at the previous time point will also be executed twice, causing system confusion.

[0071] Based on the above method embodiment, the present invention provides a corresponding device embodiment;

[0072] like Figure 5 As shown, an embodiment of the present invention provides a clock drift compensation device, including: a current reference time extraction module, a time deviation calculation module, a fast compensation value calculation module, a periodic compensation value calculation module and a drift compensation module;

[0073] The current reference time extraction module is used to receive the current synchronization frame and extract the reference time from the current synchronization frame; wherein the reference time is the time displayed by the reference slave station when the reference slave station receives the current synchronization frame;

[0074] The time deviation calculation module is used to calculate the current slave station system time when receiving the current synchronization frame according to the current local time when receiving the current synchronization frame and the initial time offset; calculate the time deviation according to the current slave station system time, the reference time and the transmission delay of the current synchronization frame;

[0075] The fast compensation value calculation module is used to calculate the fast compensation value of the cycle increment of each timing cycle of the local clock in the current synchronization cycle according to the time deviation; wherein the sum of the fast compensation values ​​of the cycle increments of all timing cycles of the local clock in the current synchronization cycle is the inverse of the time deviation;

[0076] The periodic compensation value calculation module is used to calculate the periodic compensation value of the period increment of each timing cycle of the local clock in the current synchronization cycle according to the time deviation, the synchronization time interval and the compensation accumulation of the previous synchronization cycle; wherein the synchronization time interval is the time interval between the reception time of the current synchronization frame and the reception time of the previous synchronization frame; the compensation accumulation of a synchronization cycle is the sum of the periodic compensation values ​​of the period increments of all timing cycles of the local clock in its synchronization cycle;

[0077] The drift compensation module is used to perform rapid compensation and periodic compensation on the period increment of the local clock in the current synchronization period according to the rapid compensation value and periodic compensation value of the period increment of each timing period in the current synchronization period.

[0078] In a preferred embodiment, the fast compensation value calculation module calculates the fast compensation value of the cycle increment of the local clock-current timing cycle, specifically including:

[0079] If the current timing cycle is the initial timing cycle, the absolute value of the time deviation is used as the residual deviation of the current timing cycle; otherwise, the difference between the residual deviation of the previous timing cycle and the absolute value of the fast compensation value of the previous timing cycle is used as the residual deviation of the current timing cycle;

[0080] Determine whether the residual deviation of the current timing cycle is greater than a first preset compensation value; if so, then when the time deviation is greater than zero, use the opposite of the first preset compensation value as the quick compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, use the first preset compensation value as the quick compensation value of the cycle increment of the current timing cycle; if not, then when the time deviation is greater than zero, use the opposite of the residual deviation of the current timing cycle as the quick compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, use the residual deviation of the current timing cycle as the quick compensation value of the cycle increment of the current timing cycle; wherein, the first preset compensation value is greater than zero.

[0081] In a preferred embodiment, the periodic compensation value calculation module calculates the periodic compensation value of the period increment of the current timing cycle of the local clock, specifically including:

[0082] Determine whether the current timing cycle needs periodic compensation according to the compensation period. If so, when the cumulative offset is greater than zero, the opposite of the second preset compensation value is used as the periodic compensation value of the current timing cycle. When the cumulative offset is not greater than zero, the second preset compensation value is used as the periodic compensation value of the current timing cycle. If not, the periodic compensation value of the current timing cycle is 0. The cumulative offset is the accumulated sum of the time deviation and the compensation of the previous synchronization cycle. The compensation period is calculated based on the synchronization time interval, the local timing cycle, the second preset compensation value and the cumulative offset, and the second preset compensation value is greater than zero.

[0083] In a preferred embodiment, the first preset compensation value is smaller than the difference between the timing period and the second preset compensation value, and the second preset compensation value is smaller than the timing period.

[0084] It should be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the accompanying drawings of the device embodiments provided by the present invention, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines. A person of ordinary skill in the art may understand and implement it without paying any creative effort.

[0085] Based on the above method embodiment, the present invention provides a corresponding terminal device embodiment;

[0086] An embodiment of the present invention provides a terminal device, comprising a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor; wherein, when the processor executes the computer program, the clock drift compensation method described in any one of the present invention is implemented.

[0087] Based on the above method embodiment, the present invention provides a storage medium embodiment;

[0088] An embodiment of the present invention provides a storage medium, wherein the storage medium includes a stored computer program, wherein when the computer program is executed, the device where the storage medium is located is controlled to execute any one of the clock drift compensation methods described in the present invention.

[0089] The processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0090] The memory may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile phone (such as audio data, a phone book, etc.), etc. In addition, the memory may include a high-speed random access memory, and may also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.

[0091] The above storage medium is a computer-readable storage medium, which may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal, and software distribution medium, etc. It should be noted that the content contained in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.

[0092] The above is a preferred embodiment of the present invention. It should be pointed out that a person skilled in the art can make several improvements and modifications without departing from the principle of the present invention. These improvements and modifications are also considered to be within the scope of protection of the present invention.

Claims

1. A clock drift compensation method, characterized in that: include: Receive a current synchronization frame, and extract a reference time from the current synchronization frame; wherein the reference time is the time displayed by the reference slave station when the reference slave station receives the current synchronization frame; Calculate the current slave station system time when receiving the current synchronization frame according to the current local time when receiving the current synchronization frame and the initial time offset; calculate the time deviation according to the current slave station system time, the reference time and the transmission delay of the current synchronization frame; Traversing each timing cycle of the local clock in the current synchronization cycle, if the current timing cycle is the initial timing cycle, taking the absolute value of the time deviation as the residual deviation of the current timing cycle, otherwise, taking the difference between the residual deviation of the previous timing cycle and the absolute value of the fast compensation value of the previous timing cycle as the residual deviation of the current timing cycle; Determine whether the residual deviation of the current timing cycle is greater than a first preset compensation value; if so, when the time deviation is greater than zero, use the opposite of the first preset compensation value as a rapid compensation value of the cycle increment of the current timing cycle, and when the time deviation is not greater than zero, use the first preset compensation value as a rapid compensation value of the cycle increment of the current timing cycle; If not, when the time deviation is greater than zero, the opposite number of the remaining deviation of the current timing cycle is used as the rapid compensation value of the cycle increment of the current timing cycle; when the time deviation is not greater than zero, the remaining deviation of the current timing cycle is used as the rapid compensation value of the cycle increment of the current timing cycle; wherein, the first preset compensation value is greater than zero, and within the current synchronization period, the sum of the rapid compensation values ​​of the cycle increments of all timing cycles of the local clock is the opposite number of the time deviation; Traversing each timing cycle of the local clock in the current synchronization cycle, determining whether the current timing cycle needs to be periodically compensated according to the compensation cycle, if so, when the accumulated offset is greater than zero, using the opposite of the second preset compensation value as the periodic compensation value of the current timing cycle, and when the accumulated offset is not greater than zero, using the second preset compensation value as the periodic compensation value of the current timing cycle; if not, the periodic compensation value of the current timing cycle is 0; The accumulated offset is the difference between the compensation accumulation of the previous synchronization cycle and the time deviation; the compensation cycle is calculated according to the synchronization time interval, the local timing cycle, the second preset compensation value and the accumulated offset, the second preset compensation value is greater than zero, and the synchronization time interval is the time interval between the reception time of the current synchronization frame and the reception time of the previous synchronization frame; the compensation accumulation of a synchronization cycle is the sum of the periodic compensation values ​​of the period increments of all timing cycles of the local clock within the synchronization cycle; According to the fast compensation value and the periodic compensation value of the period increment of each timing cycle in the current synchronization cycle, the period increment of the local clock in the current synchronization cycle is quickly compensated and periodically compensated.

2. The clock drift compensation method according to claim 1, characterized in that: The first preset compensation value is smaller than a difference between a local timing cycle and a second preset compensation value, and the second preset compensation value is smaller than the local timing cycle.

3. A clock drift compensation device, characterized in that: include: Current reference time extraction module, time deviation calculation module, fast compensation value calculation module, periodic compensation value calculation module and drift compensation module; The current reference time extraction module is used to receive the current synchronization frame and extract the reference time from the current synchronization frame; wherein the reference time is the time displayed by the reference slave station when the reference slave station receives the current synchronization frame; The time deviation calculation module is used to calculate the current slave station system time when receiving the current synchronization frame according to the current local time when receiving the current synchronization frame and the initial time offset; calculate the time deviation according to the current slave station system time, the reference time and the transmission delay of the current synchronization frame; The fast compensation value calculation module is used to traverse each timing cycle of the local clock in the current synchronization cycle. If the current timing cycle is an initial timing cycle, the absolute value of the time deviation is used as the residual deviation of the current timing cycle. Otherwise, the difference between the residual deviation of the previous timing cycle and the absolute value of the fast compensation value of the previous timing cycle is used as the residual deviation of the current timing cycle; determine whether the residual deviation of the current timing cycle is greater than a first preset compensation value; if so, when the time deviation is greater than zero, the opposite of the first preset compensation value is used as the fast compensation value of the cycle increment of the current timing cycle, and when the time deviation is not greater than zero, the first preset compensation value is used as the fast compensation value of the cycle increment of the current timing cycle; if not, when the time deviation is greater than zero, the opposite of the residual deviation of the current timing cycle is used as the fast compensation value of the cycle increment of the current timing cycle, and when the time deviation is not greater than zero, the residual deviation of the current timing cycle is used as the fast compensation value of the cycle increment of the current timing cycle; wherein, the first preset compensation value is greater than zero, and within the current synchronization cycle, the sum of the fast compensation values ​​of the cycle increments of all timing cycles of the local clock is the opposite of the time deviation; The periodic compensation value calculation module is used to traverse each timing cycle of the local clock in the current synchronization cycle, and determine whether the current timing cycle needs periodic compensation according to the compensation cycle. If so, when the cumulative offset is greater than zero, the opposite of the second preset compensation value is used as the periodic compensation value of the current timing cycle, and when the cumulative offset is not greater than zero, the second preset compensation value is used as the periodic compensation value of the current timing cycle; if not, the periodic compensation value of the current timing cycle is 0; wherein the cumulative offset is the difference between the compensation accumulation of the previous synchronization cycle and the time deviation, the compensation cycle is calculated according to the synchronization time interval, the local timing cycle, the second preset compensation value and the cumulative offset, the second preset compensation value is greater than zero, the synchronization time interval is the time interval between the reception time of the current synchronization frame and the reception time of the previous synchronization frame, and the compensation accumulation of a synchronization cycle is the sum of the periodic compensation values ​​of the period increments of all timing cycles of the local clock within its synchronization cycle; The drift compensation module is used to perform rapid compensation and periodic compensation on the period increment of the local clock in the current synchronization period according to the rapid compensation value and periodic compensation value of the period increment of each timing period in the current synchronization period.

4. The clock drift compensation device according to claim 3, characterized in that: The first preset compensation value is smaller than a difference between the timing period and the second preset compensation value, and the second preset compensation value is smaller than the timing period.

5. A terminal device, characterized in that: The method comprises a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor; wherein, when the processor executes the computer program, the clock drift compensation method according to any one of claims 1 to 2 is implemented.

6. A storage medium, characterized in that: The storage medium includes a stored computer program, wherein when the computer program is executed, the device where the storage medium is located is controlled to execute the clock drift compensation method according to any one of claims 1 to 2.