Synchronous time determination method, time synchronization device, time synchronization system, and readable storage medium
By configuring the timer of the local clock source and calculating the synchronization time, the time jitter problem when the GNSS signal is limited is solved, and the stability of the system clock source and the continuity and accuracy of the data timestamp are achieved.
Patent Information
- Application Number
- CN202311556766.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-21
- Publication Date
- 2024-09-27
- Estimated Expiration
- 2043-11-21
AI Technical Summary
When the GNSS signal is limited in scenarios such as occlusion, electromagnetic interference, or indoors, it causes time jitter and time rollback in the system clock source, resulting in discontinuous data timestamps.
By configuring the timer base counting frequency and overflow value of the local clock source, the count is reset when the external clock source synchronization pulse signal is received or the count overflows, and the last counting frequency and time error are obtained. Based on this information, the synchronization time is determined to ensure the system clock source stability.
It realizes adaptive synchronization time calibration when the external clock source is abnormal, avoids time jitter and time rollback, and ensures the continuity and accuracy of timestamps of system data.
Smart Images

Figure CN117675072B
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of time synchronization processing, and in particular to a method for determining synchronization time of an adaptive external clock source, a time synchronization device, a time synchronization system and a readable storage medium. Background Art
[0002] At present, the global navigation satellite system (GNSS) is generally used as an external clock source internationally to provide a unified time reference for each system. GNSS time synchronization technology is a technology that achieves time synchronization between systems by receiving satellite signals from each system. The systems rely on second pulse signals and corresponding timestamps to obtain a unified time with nanosecond accuracy. However, GNSS signals are greatly affected by the environment. In scenarios where satellite signals are limited, such as those with obstructions, electromagnetic interference, and indoors, the quality of satellite signals decreases or is interrupted, which can cause time jitter and time rollback in the system's clock source, resulting in discontinuity in the corresponding timestamps and time rollback in the data of each device in the system. Summary of the invention
[0003] Based on this, it is necessary to provide a method for determining the synchronization time of an adaptive external clock source, a time synchronization device, a time synchronization system and a readable storage medium to address the above-mentioned technical problems, which can calibrate the synchronization time when abnormal conditions such as time jitter occur in the external clock source, so as to avoid time jitter, time rollback and other phenomena in the system's clock source.
[0004] A method for determining the synchronization time of an adaptive external clock source comprises: configuring a reference counting frequency and an overflow value of a timer of a local clock source, and setting the timer to reset the count when it determines that a synchronization pulse signal of an external clock source is received or when the count overflows; receiving a current synchronization pulse signal of an external clock source; when the current synchronization pulse signal is invalid, obtaining the last counting frequency of the timer of the local clock source, the last counting frequency being determined based on the count value of the timer when a valid synchronization pulse signal was received last time and the synchronization pulse period of the external clock source; obtaining the last time error of the local clock source, the last time error being determined based on the difference between the external time of the external clock source and the local time of the local clock source when a valid synchronization pulse signal was received last time; obtaining the local time of the current reset record and the current count value of the timer of the local clock source; and determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value.
[0005] In one of the embodiments, a method for determining the synchronization time of an adaptive external clock source also includes: obtaining the current count value of the current reset record of the timer and the last count value of the timer; if the difference between the current count value and the last count value is greater than a set value, determining that the current synchronization pulse signal is invalid; if the difference between the current count value and the last count value is less than or equal to the set value, determining that the current synchronization pulse signal is valid.
[0006] In one of the embodiments, a method for determining synchronization time of an adaptive external clock source further includes: when a current synchronization pulse signal is valid, determining a current counting frequency according to a current counting value and a synchronization pulse period of the external clock source.
[0007] In one embodiment, determining the current counting frequency according to the current counting value and the synchronization pulse period of the external clock source includes: determining the current counting frequency according to the ratio of the current counting value to the synchronization pulse period of the external clock source.
[0008] In one of the embodiments, a method for determining the synchronization time of an adaptive external clock source also includes: when the current synchronization pulse signal is valid, obtaining the external time of the external clock source and the local time of the local clock source; determining the current time error based on the difference between the obtained external time of the external clock source and the local time of the local clock source.
[0009] In one embodiment, the synchronization time is determined based on the last counting frequency of the timer, the last time error, the local time of the current reset record, and the current count value of the timer, including: obtaining the ratio of the current count value of the timer to the last counting frequency of the timer; obtaining the product value of the ratio and the last time error; and determining the synchronization time based on the local time of the current reset record, the ratio, and the product value.
[0010] In one embodiment, the synchronization time is determined according to the local time, the ratio and the product value of the current reset record, including: determining the synchronization time according to the sum of the local time, the ratio and the product value of the current reset record.
[0011] In one of the embodiments, a method for determining synchronization time of an adaptive external clock source further includes: when a current synchronization pulse signal is valid, acquiring an external time of the external clock source; and using the external time of the external clock source as the synchronization time.
[0012] In one of the embodiments, the overflow value of the timer is greater than an integer number of seconds of an interval of a synchronization pulse signal of an external clock source.
[0013] In one embodiment, the external clock source is a global satellite navigation system, the synchronization pulse signal of the external clock source is a second pulse signal output by the global satellite navigation system, and the external time of the external clock source is a whole second time output by the global satellite navigation system.
[0014] In one embodiment, after the step of determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value, it also includes: if the synchronization pulse signal received again is invalid, obtaining the average of multiple historical counting frequencies, each historical counting frequency is determined based on the count value of the timer when each valid synchronization pulse signal is historically received and the synchronization pulse period of the external clock source, and updating the base counting frequency of the timer based on the average of multiple historical counting frequencies; determining the synchronization time according to the updated base counting frequency of the timer, the local time of the timer's re-reset record and the timer's re-count value until a valid synchronization pulse signal is received.
[0015] In one of the embodiments, a method for determining the synchronization time of an adaptive external clock source also includes: receiving the first pulse signal output by an incremental encoder; if the first pulse signal is a first pulse signal, cumulatively counting multiple first pulse signals output by the incremental encoder, and when reading the count value of the cumulative count, determining the current absolute time based on the synchronization time, and using the current absolute time to timestamp the read count value; if the first pulse signal is a second pulse signal, cumulatively counting multiple second pulse signals output by the incremental encoder, and when reading the count value of the cumulative count, determining the current absolute time based on the synchronization time, and using the current absolute time to timestamp the read count value; wherein, the phase difference between the first pulse signal and the second pulse signal is 90°.
[0016] In one of the embodiments, a method for determining the synchronization time of an adaptive external clock source also includes: when capturing the sampling pulse of a pulse sensor through an input capture channel, determining a third current absolute time based on the synchronization time; if sampling data of the pulse sensor is received, then after confirming that the sampling data has been received, adding the third current absolute time to the data packet of the sampling data; if the sampling data of the pulse sensor is not received, then associating the third current absolute time with the pulse sensor and outputting it.
[0017] In one of the embodiments, a method for determining the synchronization time of an adaptive external clock source also includes: when the current synchronization pulse signal is valid, sending the external time of the external clock source to the second pulse self-synchronizing sensor; when the current synchronization pulse signal is invalid, sending the synchronization time to the second pulse self-synchronizing sensor; the external clock source is a global satellite navigation system, the synchronization pulse signal of the external clock source is a second pulse signal output by the global satellite navigation system, and the external time of the external clock source is the whole second time output by the global satellite navigation system.
[0018] In one of the embodiments, a method for determining synchronization time of an adaptive external clock source further includes: sending synchronization time to a sensor supporting the time synchronization protocol based on a time synchronization protocol, so that the sensor supporting the time synchronization protocol performs timestamp synchronization of data packets based on the synchronization time.
[0019] A time synchronization device with an adaptive external clock source, the time synchronization device includes a local clock source, the local clock source is an MCU clock, the MCU clock is configured with a timer and a temperature compensated crystal oscillator, the clock frequency of the timer peripheral is provided by the temperature compensated crystal oscillator; the MCU clock is also configured with a processor, a memory and an embedded program stored in the memory and executable on the processor, and the processor implements the steps of any one of the methods in the above embodiments when executing the embedded program.
[0020] A readable storage medium stores an embedded program, which implements the steps of any method in the above embodiments when executed by a processor.
[0021] A time synchronization system with an adaptive external clock source includes the time synchronization device of the above embodiment and one or more sensors, and the time synchronization device sends synchronization time to the one or more sensors; wherein the one or more sensors include one or more of an incremental encoder, a pulse sensor, a second pulse self-synchronizing sensor, and a sensor supporting a time synchronization protocol.
[0022] The above-mentioned method for determining the synchronization time of an adaptive external clock source, time synchronization device, time synchronization system and readable storage medium configure the base counting frequency and overflow value of the timer of the local clock source, and set the timer to reset the count when it determines that the synchronization pulse signal of the external clock source is received or when the count overflows; receive the current synchronization pulse signal of the external clock source; when the current synchronization pulse signal is invalid, obtain the last counting frequency of the timer of the local clock source, the last counting frequency is determined based on the count value of the timer when a valid synchronization pulse signal was last received and the synchronization pulse period of the external clock source; obtain the last time error of the local clock source, the last time error is determined based on the difference between the external time of the external clock source and the local time of the local clock source when a valid synchronization pulse signal was last received; obtain the local time of the current reset record of the timer of the local clock source and the current count value; determine the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value.
[0023] Therefore, the time stability of the external clock source is judged by detecting the validity of the synchronization pulse of the external clock source received at each moment. When it is detected that the current synchronization pulse signal is invalid, that is, the time of the external clock source is abnormal, the synchronization time is determined based on the last counting frequency of the timer of the local clock source, the last time error, the local time of the current reset record and the current count value, so as to realize the synchronization time of the adaptive external clock source, ensure the accuracy of the system's synchronization time, and avoid the corresponding timestamp discontinuity and time rollback of the data of each device in the system due to the instability of the external clock source. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 A structural block diagram of a time synchronization system with an adaptive external clock source in one embodiment;
[0025] Figure 2 A structural block diagram of a time synchronization device for an adaptive external clock source in one embodiment;
[0026] Figure 3 A schematic diagram of an application environment of a time synchronization device for an adaptive external clock source in an embodiment;
[0027] Figure 4 A schematic flow chart of a method for determining synchronization time of an adaptive external clock source in one embodiment;
[0028] Figure 5 A timing diagram of a PPS pulse signal and a whole second time output by a GNSS clock source in one embodiment;
[0029] Figure 6A schematic diagram of the relationship between the count of a timer and a PPS signal of a GNSS clock source in one embodiment;
[0030] Figure 7 A schematic diagram of performing synchronous time stamping on a pulse signal output by an incremental encoder in one embodiment;
[0031] Figure 8 It is a schematic diagram of synchronous time stamping of sampled data output by a pulse sensor in one embodiment. DETAILED DESCRIPTION
[0032] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0033] The present application provides a time synchronization system with an adaptive external clock source. In one embodiment, Figure 1 As shown, a time synchronization system of an adaptive external clock source includes a time synchronization device of an adaptive external clock source, and also includes one or more sensors, and the time synchronization device sends synchronization time to the one or more sensors; wherein the one or more sensors include one or more of an incremental encoder, a pulse sensor, a second pulse self-synchronizing sensor, and a sensor supporting a time synchronization protocol.
[0034] In one example, the above-mentioned time synchronization device of the adaptive external clock source includes a local clock source, and the local clock source is an MCU clock. MCU: Microcontroller Unit, also known as a single-chip microcomputer or a single-chip microcomputer. Figure 2 As shown, the MCU clock is configured with a timer and a temperature compensated crystal oscillator, and the clock frequency of the timer peripheral is provided by the temperature compensated crystal oscillator; the MCU clock is also configured with a processor, a memory, and an embedded program stored in the memory and executable on the processor, and when the processor executes the embedded program, a method for determining the synchronization time of an adaptive external clock source of the present application is implemented. Figure 2 As shown, the temperature compensated crystal oscillator provides a unified clock source for the synchronous clock device through the processor.
[0035] Specifically, the time synchronization device is as follows: Figure 3As shown. The external clock source is the GNSS (Global Navigation Satellite System) clock source. The second pulse signal (PPS, theoretically the time interval between two PPS is a standard 1 second) and time information output by the GNSS clock source are used to provide an accurate time source for the time synchronization device. The time synchronization device uses the hardware timer inside the MCU to maintain the local time. The clock frequency of the timer peripheral is provided by the MCU's high-precision temperature-compensated crystal oscillator to achieve high-precision timing. At the same time, the GNSS time is continuously measured and the validity of the GNSS time is determined. When the GNSS time is valid, the local time is corrected with the GNSS time as a reference. When the GNSS time fluctuates or is invalid, the local time corrected by the system at the last moment is used to maintain it. The corrected time is the synchronization time, which is used to maintain the 1588 synchronized master clock and provide a stable synchronization clock source for external sensors.
[0036] The adaptive switching of the clock source of the time synchronization device is specifically as follows: when the GNSS PPS signal is working stably, the GNSS time is used as the clock source of the time synchronization device to provide an accurate time source for its own system and external sensors; when the PPS signal is working unstable, it is switched to using the MCU internal clock to provide an accurate time source. The MCU internal clock runs at the counting frequency of the previous PPS signal stable moment until the PPS signal returns to normal and then switches back to the GNSS clock source. For details, please refer to the specific description of the following method for determining the synchronization time of an adaptive external clock source.
[0037] The above-mentioned time synchronization device with an adaptive external clock source and its time synchronization system can be applied to autonomous driving, mobile mapping and other application scenarios that require the coordinated work of multiple sensors or multiple systems. Since the clock systems of sensors or devices of different categories and models are independent of each other, and their data output delays, data frame rates, data lengths, etc. vary greatly, the organization, processing and coordination of various data must rely on a unified synchronization time. Using a unified synchronization time as a benchmark to synchronize each sensor or device, and outputting timestamped data according to the synchronized timing will greatly reduce the complexity of subsequent matching and coordination work. The above-mentioned time synchronization device with an adaptive external clock source and its time synchronization system can maximize the accuracy of the synchronization time, which helps to improve the data accuracy of each sensor or device or the coordination of the system work.
[0038] In one embodiment, Figure 4 As shown, a method for determining the synchronization time of an adaptive external clock source is provided, and the method is applied to Figure 2 The MCU clock in is used as an example to illustrate the following steps:
[0039] S402, configuring a reference counting frequency and an overflow value of a timer of a local clock source, and setting the timer to reset the count when it determines that a synchronization pulse signal from an external clock source is received or when the count overflows.
[0040] In this embodiment, the reference counting frequency of the timer of the local clock source refers to the count value of the timer within a period, which is used to generate the local time of the local clock source. Overflow refers to the interrupt processing program of the microcontroller being triggered when the count value of the timer reaches a set specific value, and the counting starts again from zero. The overflow value of the timer of the local clock source is the counting threshold of the timer. That is, the timer counts again when the count reaches the overflow value. In addition, the timer is configured to reset the count when it is determined that a synchronization pulse signal from an external clock source is received or when the count overflows. That is, when a synchronization pulse signal from an external clock source is received, the timer performs a reset operation and counts again. Or, when the count of the timer reaches the overflow value, the count overflows, a reset operation is performed, and the count is counted again. In one example, the overflow value of the timer is greater than an integer number of seconds of the synchronization pulse signal interval of the external clock source.
[0041] Specifically, the external clock source is a GNSS clock source, and the synchronization pulse signal is a PPS pulse signal. The counting frequency of the 32-bit timer of the MCU clock in the time synchronization device of the adaptive external clock source is set to 90M, then the base counting frequency of the timer is also 90M by default, that is, the counting resolution is 1 / 90M per second, which means that each count represents 11.1 nanoseconds. The overflow value is set to 180M, then the theoretical timer overflow time is 2 seconds, that is, the full 180M count. The timer is set to reset the count value after the effective synchronization pulse signal from the external clock source arrives or the count overflows. Therefore, each time the synchronization pulse signal from the external clock source is received, the theoretical count value before the timer count is reset should be 90M, but in fact, due to errors in the system crystal oscillator, etc., the actual count of the timer may exceed or be less than 90M when each synchronization pulse signal from the external clock source is received. This counting error is the clock error of the device.
[0042] S404, receiving a current synchronization pulse signal from an external clock source.
[0043] In this embodiment, after configuring the timer of the local clock source, the system configured with the MCU clock starts to receive the synchronization pulse signal of the external clock source. Generally, the synchronization pulse signal of the external clock source is a valid signal, but in the case of interference, the synchronization pulse signal of the external clock source is an invalid signal. The synchronization pulse signal received at the current moment is used as the current synchronization pulse signal of the external clock source.
[0044] S406, when the current synchronization pulse signal is invalid, obtain the last counting frequency of the timer of the local clock source, the last counting frequency is determined based on the count value of the timer when a valid synchronization pulse signal was received last time and the synchronization pulse period of the external clock source.
[0045] In this embodiment, the validity of the current synchronization pulse signal is judged. When the current synchronization pulse signal is invalid, an adaptive external clock source is required to adjust the synchronization time. Specifically, the last counting frequency of the timer of the local clock source is obtained. The last counting frequency refers to the counting frequency calculated based on the count value of the timer and the synchronization pulse period of the external clock source when a valid synchronization pulse signal was received last time.
[0046] In one example, before or after the above step S406, the method further includes: when the current synchronization pulse signal is valid, acquiring the external time of the external clock source; and using the external time of the external clock source as the synchronization time.
[0047] In this example, when the current synchronization pulse signal is valid, it indicates that the time signal of the external clock source is stable. Therefore, the external time of the external clock source can be used as the synchronization time. It can be that the external clock source is a global satellite navigation system, the synchronization pulse signal of the external clock source is a second pulse signal output by the global satellite navigation system, and the external time of the external clock source is the whole second time output by the global satellite navigation system.
[0048] Specifically, the external time of the external clock source is the whole second time information output by the GNSS clock source. The PPS pulse signal and the whole second time information output by the GNSS clock source are introduced into the external input interrupt and UART interface of the time synchronization device respectively. The input interrupt detects the leading edge of the PPS pulse signal, which represents the whole second moment of the GNSS clock source. The time data packet will be sent after each PPS pulse, including the actual time corresponding to the previous PPS. The data packet time format can be UTC, unix or seconds in the week, etc. The timing is as follows Figure 5 shown.
[0049] S408, obtaining the last time error of the local clock source, where the last time error is determined based on the difference between the external time of the external clock source and the local time of the local clock source when a valid synchronization pulse signal is received last time.
[0050] In this embodiment, the last time error of the local clock source is the difference between the external time based on the external clock source and the local time of the local clock source when a valid synchronization pulse signal was received last time. For example, when a global satellite navigation system is used as an external clock source to provide a unified time reference for each sensor or system, the external time of the external clock source refers to the whole second time output by the GNSS clock source. The local time of the local clock source refers to the time determined by the timer of the local clock source.
[0051] S410, obtaining the local time and current count value of the current reset record of the timer of the local clock source.
[0052] In this embodiment, when the current synchronization pulse signal is invalid, an overflow interrupt occurs when the timer count reaches the overflow value, and the count is reset. The timer outputs the local time of the current reset record and the current count value. For example, a global satellite navigation system is used as an external clock source, and the synchronization pulse signal of the external clock source is a second pulse signal output by the GNSS clock source. The relationship between the timer count and the PPS signal of the GNSS clock source is as follows: Figure 6 shown.
[0053] like Figure 6 As shown, cnt_T(overflow) indicates the count of the timer when the timer overflows. cnt_T(0) indicates the count of the timer at time 0. cnt_T(1) indicates the count of the timer at time 1. cnt_T(n) indicates the count of the timer at time n, where n is a positive integer. cnt_T(n+1) indicates the count of the timer at time n+1. t(0) indicates time 0. t(1) indicates time 1. t(n) indicates time n. t(n+1) indicates time n+1. Among them, the GNSS pulse signal is a PPS pulse signal. Figure 6 As shown, when the GNSS pulse signal is invalid, the timer overflows and outputs the count value and the local time.
[0054] S412, determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record, and the current counting value.
[0055] In this embodiment, since the current synchronization pulse signal is invalid, the external clock source cannot be used for time synchronization. In this case, the last counting frequency and last time error of the timer are used in combination with the local time recorded by the current reset of the timer and the current count value to determine the synchronization time.
[0056] In one example, the synchronization time is determined based on the timer's last counting frequency, the last time error, the local time of the current reset record, and the current count value of the timer, including: obtaining the ratio of the current count value of the timer to the timer's last counting frequency; obtaining the product value of the ratio and the last time error; and determining the synchronization time based on the local time of the current reset record, the ratio, and the product value.
[0057] The step of determining the synchronization time according to the local time, the ratio and the product value of the current reset record includes: determining the synchronization time according to the sum of the local time, the ratio and the product value of the current reset record.
[0058] Specifically, the synchronization time is calculated according to the following formula:
[0059]
[0060] Among them, t n(modify) Indicates the local calibrated synchronization time with an external clock source as reference; T (frequency) Indicates the counting frequency of the timer; t (timerstart) Indicates the local time recorded each time the timer is reset; cnt indicates the count value of the timer; time (err) Indicates the time error. When using the above formula to calculate the synchronization time, T (frequency) Use the last counting frequency, time (err) The last time error is used, cnt uses the count value when the current timer is reset, t (timerstart) The time recorded when the current timer is reset is used. Therefore, the local synchronization time can be determined at each moment by the counting frequency and time error of the previous moment, as well as the local time and count value recorded by the timer at the current moment, to solve the problem of abnormal local synchronization time caused by the time abnormality of the external clock source at any moment.
[0061] The above-mentioned method for determining the synchronization time of an adaptive external clock source determines the time stability of the external clock source by detecting the validity of the synchronization pulses of the external clock source received at each moment. When it is detected that the current synchronization pulse signal is invalid, that is, the time of the external clock source is abnormal, the synchronization time is determined based on the last counting frequency of the timer of the local clock source, the last time error, the local time of the current reset record and the current count value, thereby realizing the synchronization time of an adaptive external clock source, ensuring the accuracy of the synchronization time of the system, and avoiding the corresponding timestamp discontinuity, time rollback, etc. of the data of each device in the system due to the instability of the external clock source.
[0062] In one embodiment, before the above step S406, it also includes: obtaining the current count value of the current reset record of the timer and the last count value of the timer; if the difference between the current count value and the last count value is greater than the set value, determining that the current synchronization pulse signal is invalid; if the difference between the current count value and the last count value is less than or equal to the set value, determining that the current synchronization pulse signal is valid.
[0063] In this embodiment, the validity of the synchronization pulse is judged by the count value of the timer between adjacent synchronization pulses. Specifically, if the difference between the current count value and the last count value is greater than the set value, the current synchronization pulse signal is determined to be invalid; if the difference between the current count value and the last count value is less than or equal to the set value, the current synchronization pulse signal is determined to be valid. For example, the synchronization pulse is a PPS pulse, and the actual count value of the timer between the PPS pulses is obtained. If the difference between the current count value and the last count value is ≤2 count values, the current PPS pulse is valid and the GNSS signal is stable; if the difference between the current count value and the last count value exceeds 3 count values, the current PPS pulse is considered invalid, and the GNSS signal is jittering, indicating that the PPS signal is unstable. Therefore, the stability of the external clock source can be identified by the count value of the timer.
[0064] In one example, when the current synchronization pulse signal is valid, the current counting frequency is determined according to the current count value and the synchronization pulse period of the external clock source. Wherein, determining the current counting frequency according to the current count value and the synchronization pulse period of the external clock source includes: determining the current counting frequency according to the ratio of the current count value to the synchronization pulse period of the external clock source.
[0065] Specifically, when the current synchronization pulse signal is valid, the current counting frequency is calculated by the following formula:
[0066] T1 (frequency) =cnt_T(n) / synchronization pulse period;
[0067] Among them, T1 (frequency) Indicates the current counting frequency, cnt_T is the current counting value when the timer is reset.
[0068] Therefore, when the current synchronization pulse signal is valid, the current counting frequency is determined so that when the next synchronization pulse signal is received, if the received synchronization pulse signal is invalid, the current counting frequency can be used to calculate the synchronization time when the next synchronization pulse signal is invalid.
[0069] In one example, when the current synchronization pulse signal is valid, the external time of the external clock source and the local time of the local clock source are obtained; and the current time error is determined according to the difference between the obtained external time of the external clock source and the local time of the local clock source.
[0070] For example, the local time can be captured by the following calculation:
[0071]
[0072] Among them, t (raw) Refers to the local time corresponding to the timer count cnt, t (timerstart)Refers to the local time recorded each time the timer is reset, T (frequency) Indicates the counting frequency of the timer, that is, the number of times the timer actually counts in one second, and cnt refers to the count of the timer.
[0073] For example, the external clock source is GNSS, the external time is the whole second time corresponding to GNSS, and the synchronization pulse is the PPS pulse. When an external input interrupt detects a PPS pulse signal, the following operations are performed:
[0074] (1) Get the local time when the PPS pulse arrives: t (raw) ;
[0075] (2) Obtain the timer count between PPS pulses. If the PPS is stable, that is, the deviation between the current count value and the previous count value is ≤2 count values, then the timer count frequency T is increased. (frequency) Update to the actual measured counting frequency;
[0076] (3) Reset the timer count and update the local time t (timerstart) =t (raw) ;
[0077] (4) Record the whole second time t(n) corresponding to this PPS pulse;
[0078] (5) Calculate the deviation between local time and GNSS time:
[0079] time (err) =t (n) -t (raw) .
[0080] That is, through the formula time (err) =t (n) -t (raw) By determining the time error at each moment, the current time error can be determined. Therefore, when the time signal of the external clock source is stable, the time error between the local time and the external time can be calculated, so that the next time the time signal of the external clock source is unstable, the time error calculated this time can be used to calculate the synchronization time.
[0081] In one embodiment, after the above-mentioned step of determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value, it also includes: if the synchronization pulse signal received again is invalid, obtaining the average of multiple historical counting frequencies, each historical counting frequency is determined based on the count value of the timer when each valid synchronization pulse signal is historically received and the synchronization pulse period of the external clock source, and updating the base counting frequency of the timer based on the average of multiple historical counting frequencies; determining the synchronization time according to the updated base counting frequency of the timer, the local time of the timer's re-reset record and the timer's re-count value until a valid synchronization pulse signal is received.
[0082] In this embodiment, if the synchronization pulse signal received subsequently is also an invalid signal, the base counting frequency of the timer is updated to the average of multiple historical counting frequencies, and the synchronization time is calculated using the updated base counting frequency, thereby avoiding instability in the local synchronization time within the system caused by long-term instability in the time signal of the external clock source.
[0083] For example, the external clock source is the GNSS clock source, and the synchronization pulse signal is the PPS pulse signal. When the PPS pulse signal is unstable, the timer enters the overflow interrupt mode and performs the following operations:
[0084] (1) Record the time t recorded by the timer when the local timer overflows (raw) ;
[0085] (2) Reset timer;
[0086] (3) Update the start time t of the timer in the timer overflow interrupt (timerstart) =t (raw) ;
[0087] (4) Ignore the deviation between local time and GNSS time and update time (err) =0;
[0088] (5) Set the timer counting frequency T (frequency) It is the average value of the 3 seconds before the PPS pulse signal works abnormally, that is, the average value of the 3 seconds before the PPS pulse signal is invalid, and remains unchanged until the synchronization pulse signal is determined to be normal.
[0089] In one embodiment, after the above-mentioned step of determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value, it also includes: receiving the first pulse signal output by the incremental encoder; if the first pulse signal is the first pulse signal, then cumulatively counting the multiple first pulse signals output by the incremental encoder, and when reading the count value of the cumulative count, determining the current absolute time based on the synchronization time, and using the current absolute time to timestamp the read count value; if the first pulse signal is the second pulse signal, then cumulatively counting the multiple second pulse signals output by the incremental encoder, and when reading the count value of the cumulative count, determining the current absolute time based on the synchronization time, and using the current absolute time to timestamp the read count value; wherein, the phase difference between the first pulse signal and the second pulse signal is 90°.
[0090] In this embodiment, for the incremental encoder, after the synchronization time is determined, the time synchronization operation is performed for different types of sensors connected to the time synchronization device. Figure 7 As shown, it outputs two pulse signals with different phases to indicate the direction of rotation. The two pulse signals are simultaneously connected to the timer interface that has been configured as encoder mode in the time synchronization device to accumulate and count the pulses of the incremental encoder. The initial count value is 0, and then the accumulated value increases or decreases one by one according to the actual rotation direction of the incremental encoder. Figure 7 As shown, the first pulse signal is the A-phase pulse, and the second pulse signal is the B-phase pulse. If the A-phase pulse phase leads, the count accumulation increases, and if the B-phase pulse phase leads, the count accumulation decreases. At the same time, the absolute time when the count is cut off, that is, when the count value is read, is recorded to complete the timestamp synchronization of the incremental encoder. The shorter the output time interval, that is, the shorter the interval of reading the count value, the higher the resolution.
[0091] In one embodiment, after the above step of determining the synchronization time based on the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value, it also includes: when capturing the sampling pulse of the pulse sensor through the input capture channel, determining the third current absolute time based on the synchronization time; if the sampling data of the pulse sensor is received, then after confirming that the reception of the sampling data is complete, the third current absolute time is added to the data packet of the sampling data; if the sampling data of the pulse sensor is not received, then the third current absolute time is associated with the pulse sensor and then output.
[0092] In this embodiment, after the synchronization time is determined, time synchronization operations are performed for different types of sensors connected to the time synchronization device. For a pulse sensor, that is, a sampling pulse + sampling data type sensor, this type of sensor sends / receives a sampling pulse and outputs sampling data at the sampling pulse moment. Figure 8 As shown, in order to more accurately calculate the pulse time, the pulse sensor sampling pulse needs to be connected to the input capture channel of the time synchronization device. When the sampling pulse arrives, the synchronization device will automatically capture the current timer count value in the internal register of the time synchronization device. After the sampling data is received, the synchronization time corresponding to the sampling pulse is calculated through the captured timer data and added to the data packet of the sampling data; for sensors with only sampling pulses but no sampling data, the time synchronization device directly outputs the timestamp of the sampling moment to complete the timestamp synchronization of the sampling pulse.
[0093] In one embodiment, after the above-mentioned step of determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value, it also includes: when the current synchronization pulse signal is valid, sending the external time of the external clock source to the second pulse self-synchronous sensor; when the current synchronization pulse signal is invalid, sending the synchronization time to the second pulse self-synchronous sensor; the external clock source is a global satellite navigation system, the synchronization pulse signal of the external clock source is a second pulse signal output by the global satellite navigation system, and the external time of the external clock source is the whole second time output by the global satellite navigation system.
[0094] In this embodiment, after the synchronization time is determined, time synchronization operations are performed for different types of sensors connected to the time synchronization device. Among them, for the second pulse self-synchronization type sensor, this type of sensor can directly connect to the PPS signal of the GNSS and receive the synchronization time for its own time synchronization, but due to the influence of the possible signal instability of the GNSS, there may be an increasing deviation between the clock system of the second pulse self-synchronization type sensor and other sensors when the GNSS signal is lost. When the current synchronization pulse signal is valid, the external time of the external clock source is sent to the second pulse self-synchronization type sensor; when the current synchronization pulse signal is invalid, the synchronization time is sent to the second pulse self-synchronization type sensor. Therefore, for the abnormal GNSS signal, especially for multiple sensors without GNSS, the continuity, reliability and stability of the synchronization signal of the second pulse self-synchronization type sensor is guaranteed.
[0095] In one embodiment, after the above step of determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value, it also includes: sending the synchronization time to the sensor supporting the time synchronization protocol based on the time synchronization protocol, so that the sensor supporting the time synchronization protocol synchronizes the timestamp of the data packet based on the synchronization time.
[0096] In this embodiment, after the synchronization time is determined, time synchronization operations are performed for different types of sensors connected to the time synchronization device. Among them, for sensors that support the time synchronization protocol, such as sensors that support the network port PTP synchronization protocol, a stable PTP master clock is maintained at the same time based on GNSS or internal synchronization time. In this embodiment, the synchronization time is sent to the sensor that supports the time synchronization protocol based on the time synchronization protocol, so that the sensor that supports the time synchronization protocol performs the timestamp synchronization of the data packet based on the synchronization time, and the PTP (Precision Time Protocol, IEEE 1588 Precision Time Protocol) time synchronization protocol is implemented, and the timestamp synchronization of the data packet can be realized for the sensor that supports the PTP protocol, ensuring that the sensor that supports the time synchronization protocol is in the same clock system as other sensors.
[0097] In summary, the above-mentioned method for determining synchronization time of an adaptive external clock source, time synchronization device, time synchronization system and storage medium have the following beneficial effects:
[0098] 1. The overflow value of the timer is set to an integer number of seconds greater than the interval of the synchronization pulse signal of the external clock source, such as greater than the integer number of seconds of the PPS pulse signal. In this way, when the GNSS signal is normal, the timer overflow interrupt will not be triggered, which not only saves the interrupt overhead, but also avoids the interrupt nesting problem of the PPS pulse signal and the timer, and improves the synchronization accuracy of the local clock and the GNSS clock.
[0099] 2. The health and stability of the GNSS clock source is determined by the change of the PPS pulse period. When the GNSS clock source is unstable, the system is adaptively switched to a time system maintained by a clock circuit based on an internal high-precision temperature-compensated crystal oscillator, that is, the synchronization time calculated locally is used. In the absence of GNSS signals, such as indoors and in tunnels, a high time accuracy can be maintained, avoiding the time jitter caused by directly using GNSS to calibrate the time, and solving the problem of GNSS signal interference or failure to a certain extent.
[0100] 3. The existing deviation between local time and GNSS whole second time is uniformly fitted. That is, the actual frequency of the timer is proportionally mapped to the theoretical frequency to solve the error problem between the local clock frequency and the theoretical frequency. Therefore, the continuity and stability of the local time system and the synchronization accuracy of GNSS time are guaranteed. It has been verified that the synchronization accuracy of local time and GNSS time can reach ±200ns.
[0101] 4. The time synchronization device can add timestamps for multiple sensors, and because of the input capture technology, each pulse trigger is independent and does not interfere with each other, which greatly improves the accuracy compared to the general method of adding timestamps in interrupts. In addition, higher performance is achieved at a lower cost.
[0102] 5. It has multiple time synchronization methods such as adding timestamp, PTP network time synchronization, PPS and UTC time synchronization, and has high synchronization accuracy. After testing, the synchronization accuracy of the time synchronization device can reach ns level, which can meet most usage scenarios with high requirements for time synchronization accuracy.
[0103] It should be understood that, although the steps in the flowchart are shown in sequence as indicated by the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.
[0104] In one embodiment, a readable storage medium is provided, on which an embedded program is stored. When the embedded program is executed by a processor, a method for determining synchronization time of an adaptive external clock source described in any of the above embodiments is implemented.
[0105] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through an embedded program, and the embedded program can be stored in a non-volatile computer-readable storage medium. When the embedded program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in the present application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0106] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0107] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A method for determining synchronization time of an adaptive external clock source, characterized in that: The method comprises: Configure the reference counting frequency and overflow value of the timer of the local clock source, and set the timer to reset the count when it is determined that the synchronization pulse signal of the external clock source is received or when the count overflows; Receiving a current synchronization pulse signal from the external clock source; When the current synchronization pulse signal is invalid, obtaining the last counting frequency of the timer of the local clock source, wherein the last counting frequency is determined based on the count value of the timer when a valid synchronization pulse signal was received last time and the synchronization pulse period of the external clock source; Obtaining a last time error of the local clock source, wherein the last time error is determined based on a difference between an external time of the external clock source and a local time of the local clock source when a valid synchronization pulse signal was received last time; Obtaining the local time and current count value of the current reset record of the timer of the local clock source; The synchronization time is determined according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current counting value.
2. The method according to claim 1, characterized in that The method further comprises: Obtaining a current count value of a current reset record of the timer and a previous count value of the timer; If the difference between the current count value and the last count value is greater than a set value, it is determined that the current synchronization pulse signal is invalid; If the difference between the current count value and the previous count value is less than or equal to a set value, it is determined that the current synchronization pulse signal is valid.
3. The method according to claim 2, characterized in that The method further comprises: When the current synchronization pulse signal is valid, the current counting frequency is determined according to the current count value and the synchronization pulse period of the external clock source.
4. The method according to claim 3, characterized in that: The determining the current counting frequency according to the current counting value and the synchronization pulse period of the external clock source includes: The current counting frequency is determined according to a ratio of the current counting value to a synchronization pulse period of the external clock source.
5. The method according to claim 2, characterized in that: The method further comprises: When the current synchronization pulse signal is valid, acquiring the external time of the external clock source and the local time of the local clock source; The current time error is determined according to the difference between the acquired external time of the external clock source and the local time of the local clock source.
6. The method according to claim 1, characterized in that The step of determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record, and the current counting value of the timer includes: Obtaining a ratio of a current count value of the timer to a previous count frequency of the timer; Obtaining a product value of the ratio and the last time error; The synchronization time is determined according to the local time of the current reset record, the ratio and the product value.
7. The method according to claim 6, characterized in that The step of determining the synchronization time according to the local time of the current reset record, the ratio and the product value comprises: The synchronization time is determined according to the local time of the current reset record, the ratio and the sum of the product value.
8. The method according to claim 1, characterized in that The method further comprises: When the current synchronization pulse signal is valid, obtaining the external time of the external clock source; The external time of the external clock source is used as the synchronization time.
9. The method according to claim 1, characterized in that: The overflow value of the timer is greater than an integer number of seconds of an interval of a synchronization pulse signal of an external clock source.
10. The method according to any one of claims 1 to 9, characterized in that The external clock source is a global satellite navigation system, the synchronization pulse signal of the external clock source is a second pulse signal output by the global satellite navigation system, and the external time of the external clock source is a whole second time output by the global satellite navigation system.
11. The method according to claim 1, characterized in that: After the step of determining the synchronization time according to the last counting frequency of the timer, the last time error, the local time of the current reset record and the current count value, the method further includes: If the synchronization pulse signal received again is invalid, then the average of multiple historical counting frequencies is obtained, each historical counting frequency is determined based on the count value of the timer when each valid synchronization pulse signal is received historically and the synchronization pulse period of the external clock source, and the reference counting frequency of the timer is updated based on the average of the multiple historical counting frequencies; The synchronization time is determined according to the updated reference counting frequency of the timer, the local time recorded by the resetting of the timer, and the re-count value of the timer until a valid synchronization pulse signal is received.
12. The method according to claim 1, characterized in that The method further comprises: Receive the first pulse signal output by the incremental encoder; If the first pulse signal is a first pulse signal, cumulatively count the multiple first pulse signals output by the incremental encoder, determine the current absolute time based on the synchronization time when reading the count value of the cumulative count, and use the current absolute time to timestamp the read count value; If the first pulse signal is the second pulse signal, a plurality of second pulse signals output by the incremental encoder are cumulatively counted, and when reading the count value of the cumulative count, the current absolute time is determined based on the synchronization time, and the read count value is timestamped using the current absolute time; The phase difference between the first pulse signal and the second pulse signal is 90°.
13. The method according to claim 1, characterized in that The method further comprises: When capturing the sampling pulse of the pulse sensor through the input capture channel, determining a third current absolute time based on the synchronization time; If the sampling data of the pulse sensor is received, after confirming that the reception of the sampling data is completed, the third current absolute time is added to the data packet of the sampling data; If the sampling data of the pulse sensor is not received, the third current absolute time is associated with the pulse sensor and then output.
14. The method according to claim 1, characterized in that The method further comprises: When the current synchronization pulse signal is valid, sending the external time of the external clock source to the pulse-per-second self-synchronization type sensor; When the current synchronization pulse signal is invalid, sending the synchronization time to the pulse-per-second self-synchronization type sensor; The external clock source is a global satellite navigation system, the synchronization pulse signal of the external clock source is a second pulse signal output by the global satellite navigation system, and the external time of the external clock source is a whole second time output by the global satellite navigation system.
15. The method according to claim 1, characterized in that The method further comprises: The synchronization time is sent to a sensor supporting the time synchronization protocol based on a time synchronization protocol, so that the sensor supporting the time synchronization protocol performs timestamp synchronization of data packets based on the synchronization time.
16. A time synchronization device for an adaptive external clock source, characterized in that: The time synchronization device includes a local clock source, which is an MCU clock. The MCU clock is configured with a timer and a temperature compensated crystal oscillator, and the clock frequency of the timer peripheral is provided by the temperature compensated crystal oscillator; the MCU clock is also configured with a processor, a memory, and an embedded program stored in the memory and executable on the processor, and the processor implements the steps of the method described in any one of claims 1 to 15 when executing the embedded program.
17. A readable storage medium having an embedded program stored thereon, characterized in that: When the embedded program is executed by a processor, the steps of the method described in any one of claims 1 to 15 are implemented.
18. A time synchronization system for an adaptive external clock source, characterized in that: The time synchronization device according to claim 16 further comprises one or more sensors, the time synchronization device sending synchronization time to the one or more sensors; The one or more sensors include one or more of an incremental encoder, a pulse sensor, a pulse-per-second self-synchronous sensor, and a sensor supporting a time synchronization protocol.
