Time synchronization method and apparatus based on real-time clock (RTC)
Through the time-based method based on real-time clock RTC, the time and frequency deviation are adjusted using the PID controller and the Kalman filtering equation, the time inaccuracy problem caused by GPS signal loss is solved, and the real-time time-based and time accuracy is guaranteed.
Patent Information
- Application Number
- CN202411939712.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-26
- Publication Date
- 2025-04-01
AI Technical Summary
The GPS signal is lost intermittently due to weather or geographical location, resulting in inaccurate time information of the clock server.
The time-based timing method based on the real-time clock RTC is adopted, by obtaining the time deviation and frequency deviation between the RTC and the external clock to be adjusted, the proportional integral differential PID controller and the Kalman filtering equation are adjusted to provide real-time timing information.
Real-time alignment of the external clock to be adjusted is achieved, avoiding the problem of inaccurate time information and ensuring the accuracy of time.
Smart Images

Figure CN119743225A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of RTC time synchronization, and in particular to a time synchronization method and device based on a real-time clock (RTC). Background Art
[0002] As a universal timing device, the clock server has become an indispensable time reference device in many fields with its high-precision time source and accurate time information, and is widely used in the fields of electricity, communication, finance, transportation, meteorology, etc. Specifically, for GPS (Global Positioning System) timing, the GPS timing module can simultaneously receive signals from 4 to 8 satellites within its field of view at any time. Its internal hardware circuit and software can extract and output two time signals from it by encoding and processing the received information: one is a synchronization pulse signal 1PPS with an interval of 1 second, and the synchronization error accuracy of its pulse leading edge with UTC is at the nanosecond level; the other is the UTC absolute time (year, month, day, hour, minute, second) included in the serial port output information, which corresponds to the 1PPS pulse. However, the GPS signal may be intermittently lost due to weather, geographical location, etc., resulting in inaccurate timing information.
[0003] There is currently no effective solution to the above-mentioned problems in the related art. Summary of the invention
[0004] The present application provides a time synchronization method and device based on a real-time clock (RTC) to solve the problem in the related art that GPS signals may be intermittently lost due to weather or geographical location, resulting in inaccurate time synchronization information.
[0005] In the first aspect, the present application provides a time synchronization method based on a real-time clock (RTC), comprising: obtaining the time deviation and frequency deviation between the RTC and an external clock to be adjusted; determining the adjusted frequency deviation through a proportional integral differential (PID) controller and the frequency deviation; predicting the time deviation at the next moment based on a Kalman filter equation and a current time deviation, and obtaining a compensated time deviation based on the predicted time deviation and the RTC time; providing time synchronization information to the external clock to be adjusted based on the adjusted frequency deviation and the compensated time deviation, wherein the time synchronization information is used to perform real-time time synchronization on the external clock to be adjusted.
[0006] In the second aspect, the present application provides a time synchronization device based on a real-time clock RTC, including: an acquisition module, used to acquire the time deviation and frequency deviation between the RTC and an external clock to be adjusted; a determination module, used to determine the adjusted frequency deviation through a proportional integral differential PID controller and the frequency deviation; a first processing module, used to predict the time deviation at the next moment based on the Kalman filter equation and the current time deviation, and obtain the compensated time deviation based on the predicted time deviation and the RTC time; a second processing module, used to provide synchronization information to the external clock to be adjusted based on the adjusted frequency deviation and the compensated time deviation, wherein the synchronization information is used to perform real-time synchronization on the external clock to be adjusted.
[0007] In a third aspect, the present application provides an electronic device comprising: at least one communication interface; at least one bus connected to the at least one communication interface; at least one processor connected to the at least one bus; and at least one memory connected to the at least one bus, wherein the processor is configured to execute the time synchronization method based on the real-time clock RTC described in the first aspect of the present application.
[0008] In a fourth aspect, the present application further provides a computer storage medium storing computer executable instructions, wherein the computer executable instructions are used to execute the time synchronization method based on the real-time clock RTC described in the first aspect of the present application.
[0009] The above technical solution provided by the embodiment of the present application has the following advantages compared with the prior art: after obtaining the time deviation and frequency deviation between the RTC and the external clock to be adjusted through the method provided by the embodiment of the present application, the adjusted frequency deviation can be determined by the proportional integral differential PID controller and the frequency deviation, and the time deviation of the next moment is predicted based on the Kalman filter equation and the current time deviation, and the compensated time deviation is obtained based on the predicted time deviation and the time of the RTC, that is, the adjusted frequency deviation and the compensated time deviation can be obtained. Then, based on the adjusted frequency deviation and the compensated time deviation, the timing information is provided to the external clock to be adjusted, and the external clock to be adjusted can be timely synchronized in real time, avoiding the problem that the external clock to be adjusted causes inaccurate time information due to some factors, such as the GPS timing module, which may cause intermittent loss of GPS signals due to weather or geographical location, resulting in inaccurate timing information. Through the method of the embodiment of the present application, it can be adjusted in time to ensure the time accuracy of the external clock to be adjusted. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0011] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0012] One or more embodiments are exemplarily described by pictures in the corresponding drawings, and these exemplified descriptions do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and unless otherwise stated, the figures in the drawings do not constitute proportional limitations.
[0013] Figure 1 A flowchart of a time synchronization method based on a real-time clock RTC provided in an embodiment of the present application;
[0014] Figure 2 An optional flow chart of a time synchronization method based on a real-time clock RTC provided in an embodiment of the present application;
[0015] Figure 3 A schematic diagram describing each state of each state machine in a specific implementation manner provided in an embodiment of the present application;
[0016] Figure 4 A schematic diagram of a frequency adjustment trend provided in an embodiment of the present application;
[0017] Figure 5 A schematic diagram of the deviation adjustment trend provided in the embodiment of the present application;
[0018] Figure 6 A schematic diagram of the structure of a time synchronization device based on a real-time clock RTC provided in an embodiment of the present application;
[0019] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0020] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0021] The disclosure below provides many different embodiments or examples to implement different structures of the present invention. In order to simplify the disclosure of the present invention, the parts and settings of specific examples are described below. Of course, they are only examples, and the purpose is not to limit the present invention. In addition, the present invention can repeat reference numbers and / or letters in different examples. This repetition is for the purpose of simplification and clarity, and does not itself indicate the relationship between the various embodiments and / or settings discussed.
[0022] In order to solve the problem in the related art that the GPS signal may be intermittently lost due to weather or geographical location, resulting in inaccurate time information of the clock server, the present application provides a time synchronization method based on a real-time clock RTC, such as Figure 1 As shown, the steps of the method include:
[0023] Step 101, obtaining the time deviation and frequency deviation between the RTC (Real_Time Clock) and the external clock to be adjusted;
[0024] In the embodiment of the present application, RTC is an integrated circuit and also a clock chip, which provides an accurate time reference for the electronic system. Most real-time clock chips use a crystal oscillator with high precision as a clock source. Therefore, the RTC in the embodiment of the present application can be a GPS timing module in a clock server in a specific example. The external clock to be adjusted is the clock module currently used in various application scenarios such as electricity, communication, transportation, and meteorology.
[0025] Step 102, determining the adjusted frequency deviation through a proportional-integral-derivative (PID) controller and the frequency deviation;
[0026] PID control (Proportional-Integral-Derivative Control) is a control strategy based on three parameters: proportion (P), integration (I) and differentiation (D), that is, by comparing the deviation between the system output value and the expected value, the control quantity is adjusted using proportional, integral and differential operations to eliminate the deviation and achieve the control target. In the embodiment of the present application, the main purpose of adaptive PID control is to enable the PID parameters to be automatically adjusted according to the dynamic changes of the system, such as continuously optimizing the PID parameters based on quadratic performance indicators. The adjustment algorithm can quickly and effectively reduce the system error, improve the stability and response speed of the system, and quickly obtain a stable frequency error. In addition, among the three parameters in the PID controller, Kp is the proportional gain used to speed up the response speed of the system, Ki is the integral gain used to eliminate steady-state errors, and Kd is the differential gain used to reduce overshoot and adjustment time.
[0027] Step 103, predicting the time deviation of the next moment based on the Kalman filter equation and the current time deviation, and obtaining the compensated time deviation based on the predicted time deviation and the RTC time;
[0028] In this regard, Kalman filtering is an algorithm that uses the linear system state equation to optimally estimate the system state through system input and output observation data. In the embodiment of the present application, the state equation and covariance equation in the Kalman filter equation are continuously updated by comparing the deviation at this moment with the deviation predicted by the Kalman filter at the previous moment, so that the predicted deviation will be more accurate. For example, if the deviation is predicted at the next moment, the deviation will be adjusted to the current moment.
[0029] Step 104: providing timing information to the external clock to be adjusted based on the adjusted frequency deviation and the compensated time deviation, wherein the timing information is used to perform real-time timing on the external clock to be adjusted.
[0030] After obtaining the time deviation and frequency deviation between the RTC and the external clock to be adjusted through the above steps 101 to 104, the adjusted frequency deviation can be determined through the proportional integral differential PID controller and the frequency deviation, and the time deviation of the next moment is predicted based on the Kalman filter equation and the current time deviation, and the compensated time deviation is obtained based on the predicted time deviation and the time of the RTC, that is, the adjusted frequency deviation and the compensated time deviation can be obtained. Then, based on the adjusted frequency deviation and the compensated time deviation, the timing information is provided to the external clock to be adjusted, and the external clock to be adjusted can be timely synchronized in real time to avoid the problem of inaccurate time information of the external clock to be adjusted due to some factors, such as the GPS timing module, the GPS signal will be intermittently lost due to weather or geographical location, resulting in inaccurate timing information. Through the method of the embodiment of the present application, it can be adjusted in time to ensure the time accuracy of the external clock to be adjusted.
[0031] In the embodiment of the present application, in the process of determining the target frequency by the PID controller, it is necessary to continuously adjust so that the output of the PID controller at multiple consecutive moments does not exceed the first preset threshold value, indicating that the final frequency for adjustment is obtained at this time. Based on this, the method of determining the adjusted frequency deviation by the proportional integral differential PID controller and the frequency deviation involved in step 102 in the embodiment of the present application can further include:
[0032] Step 11, using the frequency and frequency deviation of the external clock to be adjusted as inputs of a PID controller to obtain a first frequency deviation;
[0033] Step 12, adjusting the parameters in the PID controller, and using the frequency of the external clock to be adjusted and the first frequency deviation as inputs to the PID controller after adjusting the parameters; wherein the parameters include: proportional gain, integral gain and differential gain;
[0034] Step 13, repeating the above steps 11 and 12 until N frequency deviations are successively smaller than the first preset threshold value among the output M frequency deviations, and determining the adjusted frequency deviation from the N frequency deviations; wherein M and N are positive integers and M is greater than N.
[0035] In a specific example, the values of M and N can be set accordingly according to actual needs. For example, if the value of N is 10, then M is a positive integer greater than N. In addition, in the embodiment of the present application, obtaining the frequency deviation through the PID controller is a continuous process, because the frequency of the external clock to be adjusted is continuously adjusted through the obtained frequency deviation, that is, the frequency deviation between the frequency of the external clock to be adjusted and the frequency of the RTC should be gradually reduced until it is reduced to a certain extent. When the frequency deviation obtained by the PID controller is basically less than the first preset threshold, it indicates that the adjustment of the frequency deviation at this time has been completed, that is, the frequency of the external clock to be adjusted can be finally adjusted through the determined adjusted frequency deviation.
[0036] In the embodiment of the present application, after the frequency deviation adjustment is completed, the time deviation needs to be adjusted. Because the variation of the deviation between the RTC and the external clock to be adjusted over time can be described by a first-order linear equation (y=ax+b), wherein the slope a is mainly determined by the deviation between the RTC clock frequency and the frequency of the external clock to be adjusted, and the intercept b is determined by the fixed time deviation, so it is divided into the calibration of the frequency deviation and the time deviation. Based on this, in the embodiment of the present application, the time deviation needs to be adjusted, that is, the method of predicting the time deviation of the next moment based on the Kalman filter equation and the current time deviation involved in the above step 103, and the method of obtaining the time deviation after compensation based on the predicted time deviation and the time of the RTC, can further include:
[0037] Step 21, predicting the time deviation of the next moment based on the Kalman filter and the current time deviation, and continuing to predict the time deviation based on the time deviation of the next moment and the Kalman filter, and continuing to predict until the predicted time deviations of L consecutive moments are less than the second preset threshold;
[0038] Step 22, determining a target time deviation from the predicted time deviations at L moments, and obtaining a compensated time deviation based on the target time deviation and the RTC time.
[0039] In the specific example, the value of L can be set accordingly according to actual needs, such as the value of L is 10. In addition, in the specific example of the embodiment of the present application, the time deviation at this moment can be compared with the time deviation predicted by the Kalman filter at the previous moment, and then the state equation and the covariance equation can be continuously updated, so that a more accurate time deviation can be predicted, such as predicting the time deviation of the next moment, and then adjusting the time deviation to the current moment.
[0040] Further, in the process of continuous prediction based on the Kalman filter equation, if there is no prediction time deviation less than the second preset threshold value for L consecutive moments, the frequency deviation is adjusted again. Therefore, in actual application scenarios, the local clock frequency may be affected by external factors (such as temperature, etc.), causing the deviation to exceed the threshold value continuously, and then the frequency deviation needs to be re-determined. Only after the frequency deviation and the time deviation meet the preset conditions, can the timing information be provided to the external clock to be adjusted based on the adjusted frequency deviation and the compensated time deviation to ensure the accuracy of the time.
[0041] In the embodiment of the present application, after obtaining the adjusted frequency deviation and the compensated time deviation, it is necessary to fine-tune them to obtain the timing information that can be finally provided to the external clock to be adjusted. Based on this, the method of providing the timing information to the external clock to be adjusted based on the adjusted frequency deviation and the compensated time deviation involved in step 103 in the embodiment of the present application may further include:
[0042] Step 31, comparing the adjusted frequency deviation and the compensated time deviation with the corresponding preset step lengths respectively;
[0043] Step 32, when the adjusted frequency deviation or the compensated time deviation exceeds the preset step length, providing timing information to the external clock to be adjusted based on the preset step length;
[0044] Step 33: When the adjusted frequency deviation or the compensated time deviation does not exceed the preset step length, provide timing information to the external clock to be adjusted based on the adjusted frequency deviation or the compensated time deviation.
[0045] In a specific example, the adjusted frequency deviation may be compared with the first preset step length, and the compensated time deviation may be compared with the second preset step length. If the adjusted frequency deviation is less than the first preset step length, and the compensated time deviation is less than the second preset step length, the timing information is provided to the external clock to be adjusted according to the adjusted frequency deviation and the compensated time deviation. If the adjusted frequency deviation is less than the first preset step length, and the compensated time deviation is greater than the second preset step length, the timing information is provided to the external clock to be adjusted according to the adjusted frequency deviation and the second step length. If the adjusted frequency deviation is greater than the first preset step length, and the compensated time deviation is greater than the second preset step length, the timing information is provided to the external clock to be adjusted according to the first preset step length and the second preset step length. It can be seen that in the embodiment of the present application, the smaller of the comparison results of the adjusted frequency deviation and the compensated time deviation with the corresponding preset step lengths is taken to provide the timing information to the external clock to be adjusted.
[0046] In the embodiment of the present application, specifically, the timing information is provided to the external clock to be adjusted according to the corresponding timing protocol. Based on this, the method of providing the timing information to the external clock to be adjusted based on the adjusted frequency deviation or the compensated time deviation involved in step 103 in the embodiment of the present application may further include:
[0047] Step 41, providing timing information to the external clock to be adjusted based on the adjusted frequency deviation or the compensated time deviation and in combination with a timing protocol, wherein the timing protocol includes a Network Time Protocol (NTP) and a Precision Time Protocol (PTP).
[0048] NTP uses a hierarchical architecture, and servers are organized into a tree structure called an NTP server stratum. The top layer is stratum 0, which is usually a high-precision clock connected to an atomic clock or GPS device. Servers in other layers (stratum 1, stratum 2, etc.) obtain time from servers in the upper layer and provide services to the lower layer. PTP achieves high-precision synchronization through hardware timestamps in the local area network. Its basic working units are master clocks and slave clocks. The master clock broadcasts time information, and the slave clock adjusts its own clock based on this information.
[0049] In the embodiment of the present application, the adjusted frequency deviation and the compensated time deviation can be recorded so that if a power failure and restart occurs later, the record can still be obtained to continue to provide time synchronization information for the external clock to be adjusted. Therefore, after the time deviation is compensated based on the predicted time deviation and the RTC time, Figure 2 As shown, the method of the embodiment of the present application also includes:
[0050] Step 201, recording the adjusted frequency deviation and the compensated time deviation;
[0051] Step 202, after the RTC is powered off and restarted, the record is read and the record is set as a parameter in the RTC.
[0052] It can be seen that in the embodiment of the present application, the adjustment of the frequency deviation and the time deviation can be recorded in real time to ensure that the adjusted frequency deviation and time deviation can still be obtained after an unexpected situation occurs, so as to ensure the time accuracy of the external clock to be adjusted.
[0053] The present application is explained below in conjunction with the specific implementation of the embodiment of the present application. The specific implementation provides a fast time synchronization method based on an adjustable high-precision RTC, which includes the following stages: initialization stage, frequency adjustment stage, deviation adjustment stage, and fine-tuning stage.
[0054] Among them, the initialization stage refers to the initialization of the system RTC, initial time setting, initial frequency setting, etc. The frequency adjustment stage refers to the use of PID feedback adjustment algorithm to achieve rapid frequency locking. The deviation adjustment stage refers to the use of Kalman filter algorithm to predict the deviation and predict compensation in advance. The fine-tuning stage refers to the use of the above algorithm to fine-tune the frequency and deviation to avoid large-scale jitter in time. Specific examples Figure 3 As shown, the INIT state refers to the initialization state, the FREQ-ADJ state refers to the frequency adjustment (Frequency Adjust) stage, the DRIFT-ADJ state refers to the deviation adjustment (DriftAdjust) stage, and the LOCK state refers to the locking state.
[0055] Combination Figure 4 Frequency adjustment trend chart and Figure 5 The deviation adjustment trend diagram, the method steps in this specific implementation manner include:
[0056] Step 301, initialize the internal RTC, initial time setting, initial frequency setting, etc.
[0057] Step 302, when there is an external time source (such as GPS), obtain the real-time deviation (the deviation between local time and external time), calculate the first-order inverse of the deviation, and use F = (d2-d1) / (t2-t1) to approximate the slope a of the deviation curve, where the deviation d1 at time t1 and the deviation d2 at time t2 are approximated by the first-order derivative. In addition, an adaptive PID controller is required to dynamically adjust the three PID parameters (Kp, Kd, Ki) using error, integration and differentiation, and finally calculate the frequency that needs to be adjusted. This process ensures fast response and stability through feedback regulation.
[0058] The frequency that needs to be adjusted refers to the deviation between the frequency of the local clock and the frequency of the external source, and the deviation needs to be increased or decreased to the local clock frequency.
[0059] Step 303: When the frequency is lower than the set threshold (the threshold is determined by the actual allowable fluctuation range and the system clock accuracy) for 10 consecutive times, it can be determined that the frequency has been locked, and the phase switches to the deviation adjustment phase.
[0060] Step 304, Kalman filtering and real-time deviation are used to predict the deviation at the next moment and correct it in advance. When the real-time deviation is less than the threshold for 10 consecutive times, the deviation adjustment is completed and the state is switched to the locked state. When the deviation exceeds the threshold continuously, the state switches to the frequency adjustment stage and the frequency needs to be recalibrated.
[0061] Using Kalman filtering and real-time deviation to predict the deviation at the next moment means comparing the deviation at this moment with the deviation predicted by the Kalman filter at the previous moment, so as to continuously update the state equation and covariance equation. In this way, the predicted deviation will be more accurate. For example, if the deviation at the next moment is predicted, the deviation will be adjusted to the current moment.
[0062] Step 305, in the locking stage, the frequency and deviation are fine-tuned using the above algorithm (setting the maximum adjustment range). When deviations exceeding the threshold value appear continuously, such as when the local clock frequency is affected by external factors (such as temperature, etc.), causing the deviation to exceed the threshold value continuously, the frequency needs to be re-locked and the system switches to the deviation adjustment stage.
[0063] Fine-tuning the algorithm as above means setting an adjustment step size. If the deviation exceeds the step size, it is adjusted according to the step size, and if it is less than the step size, it is adjusted according to the deviation.
[0064] Step 306, when entering the locked state, the corresponding frequency and deviation are recorded in real time, and when the power is cut off and restarted, the value is read and the parameter is set to the RTC during the initialization phase.
[0065] Specifically, the records are saved in the local flash and will not be lost during a power outage. After restart, the records are read from the flash and set to the RTC.
[0066] Step 307: At the same time, the device synchronizes time with the outside world (external clock to be adjusted) through the PTP / NTP protocol.
[0067] It can be seen that in the embodiment of the present application, when external GPS provides time information, the local RTC clock system can be tamed in a short time and the stable operation of the system clock can be maintained for a long time, and high-precision time synchronization services (such as NTP, PTP, etc.) can be provided to the outside.
[0068] Corresponding to the above Figure 1 The embodiment of the present application provides a time synchronization device based on a real-time clock RTC, such as Figure 6 As shown, the device comprises:
[0069] An acquisition module 602 is used to acquire a time deviation and a frequency deviation between the RTC and an external clock to be adjusted;
[0070] A determination module 604 is used to determine the adjusted frequency deviation through a proportional integral derivative (PID) controller and the frequency deviation;
[0071] A first processing module 606 is used to predict the time deviation of the next moment based on the Kalman filter equation and the current time deviation, and obtain the compensated time deviation based on the predicted time deviation and the RTC time;
[0072] The second processing module 608 is used to provide timing information to the external clock to be adjusted based on the adjusted frequency deviation and the compensated time deviation, wherein the timing information is used to perform real-time timing on the external clock to be adjusted.
[0073] Through the device of the embodiment of the present application, after obtaining the time deviation and frequency deviation between the RTC and the external clock to be adjusted, the adjusted frequency deviation can be determined by the proportional integral differential PID controller and the frequency deviation, and the time deviation of the next moment is predicted based on the Kalman filter equation and the current time deviation, and the compensated time deviation is obtained based on the predicted time deviation and the time of the RTC, that is, the adjusted frequency deviation and the compensated time deviation can be obtained. Then, based on the adjusted frequency deviation and the compensated time deviation, the timing information is provided to the external clock to be adjusted, and the external clock to be adjusted can be timely synchronized in real time to avoid the problem of inaccurate time information of the external clock to be adjusted due to some factors, such as the GPS timing module, which may cause intermittent loss of GPS signals due to weather or geographical location, resulting in inaccurate timing information. Through the method of the embodiment of the present application, it can be adjusted in time to ensure the time accuracy of the external clock to be adjusted.
[0074] In an optional implementation manner of an embodiment of the present application, the determination module in the embodiment of the present application may further include: an input unit, used to take the frequency and frequency deviation of the external clock to be adjusted as inputs of the PID controller to obtain a first frequency deviation; a first processing unit, used to adjust the parameters in the PID controller, and take the frequency of the external clock to be adjusted and the first frequency deviation as inputs of the PID controller after adjusting the parameters; wherein the parameters include: proportional gain, integral gain and differential gain; a second processing unit, used to repeatedly execute the above steps until N frequency deviations appear continuously among the output M frequency deviations that are less than the first preset threshold, and determine the adjusted frequency deviation from the N frequency deviations; wherein M and N are positive integers and M is greater than N.
[0075] In an optional implementation manner of an embodiment of the present application, the first processing module in the embodiment of the present application may further include: a third processing unit, used to predict the time deviation of the next moment based on the Kalman filter equation and the current time deviation, and continue to predict the time deviation based on the time deviation of the next moment and the Kalman filter, and continue to predict until the predicted time deviation of L consecutive moments is less than the second preset threshold; a fourth processing unit, used to determine the target time deviation from the predicted time deviations of L moments, and the time deviation after compensation based on the target time deviation and the RTC time.
[0076] In an optional implementation manner of the embodiment of the present application, the device in the embodiment of the present application may also include: a third processing module, which is used to adjust the frequency deviation again if the predicted time deviation is not less than the second preset threshold for L consecutive moments during the continuous prediction process.
[0077] In an optional implementation manner of an embodiment of the present application, the second processing module in the embodiment of the present application may further include: a comparison unit, used to compare the adjusted frequency deviation and the compensated time deviation with the corresponding preset step sizes respectively; a fifth processing unit, used to provide timing information to the external clock to be adjusted based on the preset step size when the adjusted frequency deviation or the compensated time deviation exceeds the preset step size; a sixth processing unit, used to provide timing information to the external clock to be adjusted based on the adjusted frequency deviation or the compensated time deviation when the adjusted frequency deviation or the compensated time deviation does not exceed the preset step size.
[0078] In an optional implementation manner of an embodiment of the present application, the sixth processing unit in the embodiment of the present application is also used to provide timing information to the external clock to be adjusted based on the adjusted frequency deviation or the compensated time deviation and in combination with the timing protocol, wherein the timing protocol includes the Network Time Protocol NTP and the Precision Time Protocol PTP.
[0079] In an optional implementation manner of an embodiment of the present application, the device in the embodiment of the present application also includes: a recording module, used to record the adjusted frequency deviation and the compensated time deviation after the time deviation is compensated based on the predicted time deviation and the RTC time; a third processing module, used to read the record after the RTC is powered off and restarted, and set the record as a parameter in the RTC.
[0080] like Figure 7 As shown, an embodiment of the present application provides an electronic device, including a processor 711, a communication interface 712, a memory 713 and a communication bus 714, wherein the processor 711, the communication interface 712, and the memory 713 communicate with each other through the communication bus 714.
[0081] Memory 713, used for storing computer programs;
[0082] In one embodiment of the present application, the processor 711 is used to execute the program stored in the memory 713 to implement the time synchronization method based on the real-time clock RTC provided by any of the aforementioned method embodiments, and its role is similar and will not be repeated here.
[0083] An embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the steps of a time synchronization method based on a real-time clock (RTC) as provided in any of the aforementioned method embodiments are implemented.
[0084] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0085] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0086] It should be understood that the terms used herein are only for the purpose of describing specific example embodiments and are not intended to be limiting. Unless the context clearly indicates otherwise, the singular forms "one", "an" and "said" as used herein may also be meant to include plural forms. The terms "include", "comprise", "contain", and "have" are inclusive, and therefore specify the existence of stated features, steps, operations, elements and / or parts, but do not exclude the existence or addition of one or more other features, steps, operations, elements, parts, and / or combinations thereof. The method steps, processes, and operations described herein are not interpreted as necessarily requiring them to be performed in the specific order described or illustrated, unless the execution order is clearly indicated. It should also be understood that additional or alternative steps may be used.
[0087] The foregoing is merely a specific embodiment of the present invention, which enables those skilled in the art to understand or implement the present invention. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features claimed herein.
Claims
1. A time synchronization method based on a real-time clock (RTC), characterized in that: include: Obtaining a time deviation and a frequency deviation between the RTC and an external clock to be adjusted; Determine the adjusted frequency deviation by using a proportional-integral-derivative (PID) controller and the frequency deviation; The time deviation of the next moment is predicted based on the Kalman filter equation and the current time deviation, and the time deviation after compensation is obtained based on the predicted time deviation and the RTC time; Based on the adjusted frequency deviation and the compensated time deviation, timing information is provided to the external clock to be adjusted, wherein the timing information is used to perform real-time timing on the external clock to be adjusted.
2. The method according to claim 1, characterized in that The adjusted frequency deviation is determined by a proportional-integral-derivative (PID) controller and the frequency deviation, including: Using the frequency of the external clock to be adjusted and the frequency deviation as inputs of the PID controller to obtain a first frequency deviation; Adjusting the parameters in the PID controller, and using the frequency of the external clock to be adjusted and the first frequency deviation as inputs to the PID controller after adjusting the parameters; wherein the parameters include: proportional gain, integral gain and differential gain; Repeat the above steps until N frequency deviations are successively smaller than the first preset threshold value among the output M frequency deviations, and determine the adjusted frequency deviation from the N frequency deviations; wherein M and N are positive integers and M is greater than N.
3. The method according to claim 1, characterized in that The time deviation of the next moment is predicted based on the Kalman filter and the current time deviation, and the time deviation after compensation is obtained based on the predicted time deviation and the RTC time, including: Predicting the time deviation of the next moment based on the Kalman filter equation and the current time deviation, and continuing to predict the time deviation based on the time deviation of the next moment and the Kalman filter, and continuing to predict until the predicted time deviations of L consecutive moments are less than a second preset threshold; A target time deviation is determined from the predicted time deviations at the L moments, and a compensated time deviation is obtained based on the target time deviation and the RTC time.
4. The method according to claim 3, characterized in that The method further comprises: In the continuous prediction process, if the predicted time deviation is not smaller than the second preset threshold value for L consecutive moments, the frequency deviation is adjusted again.
5. The method according to claim 1, characterized in that Providing timing information to the external clock to be adjusted based on the adjusted frequency deviation and the compensated time deviation, including: Compare the adjusted frequency deviation and the compensated time deviation with the corresponding preset step lengths respectively; When the adjusted frequency deviation or the compensated time deviation exceeds a preset step length, providing timing information to the external clock to be adjusted based on the preset step length; When the adjusted frequency deviation or the compensated time deviation does not exceed the preset step length, timing information is provided to the external clock to be adjusted based on the adjusted frequency deviation or the compensated time deviation.
6. The method according to claim 5, characterized in that Providing timing information to the external clock to be adjusted based on the adjusted frequency deviation or the compensated time deviation, including: Based on the adjusted frequency deviation or the compensated time deviation and in combination with a time synchronization protocol, time synchronization information is provided to the external clock to be adjusted, wherein the time synchronization protocol includes a network time protocol NTP and a precision time protocol PTP.
7. The method according to claim 1, characterized in that After the time deviation is compensated based on the predicted time deviation and the time of the RTC, the method further includes: Record the adjusted frequency deviation and compensated time deviation; After the RTC is powered off and restarted, the record is read and the record is set as a parameter in the RTC.
8. A time synchronization device based on a real-time clock (RTC), characterized in that: include: An acquisition module, used for acquiring a time deviation and a frequency deviation between the RTC and an external clock to be adjusted; A determination module, used for determining an adjusted frequency deviation through a proportional-integral-derivative (PID) controller and the frequency deviation; A first processing module is used to predict the time deviation of the next moment based on the Kalman filter equation and the current time deviation, and obtain the compensated time deviation based on the predicted time deviation and the RTC time; The second processing module is used to provide timing information to the external clock to be adjusted based on the adjusted frequency deviation and the compensated time deviation, wherein the timing information is used to perform real-time timing on the external clock to be adjusted.
9. An electronic device, comprising: at least one communication interface; at least one bus connected to the at least one communication interface; at least one processor coupled to the at least one bus; At least one memory connected to the at least one bus, wherein the processor is configured to execute the time synchronization method based on the real-time clock (RTC) as described in any one of claims 1 to 7.
10. A computer storage medium storing computer executable instructions, wherein the computer executable instructions are used to execute the time synchronization method based on a real-time clock (RTC) as claimed in any one of claims 1 to 7.