Skip to content

Commit

Permalink
1. adjust_offset不能是负数.
Browse files Browse the repository at this point in the history
2. 增加log和注释.
3. 计算调整频率和时间偏置的算法有问题.频率调整的时间间隔过小.
  • Loading branch information
GuoGeTiertime committed Dec 11, 2023
1 parent 4e47c59 commit fd7c1b0
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions klippy/clocksync.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,30 @@ def calibrate_clock(self, print_time, eventtime):
sync2_main_clock = sync2_print_time * main_mcu_freq
sync2_sys_time = ser_time + (sync2_main_clock - ser_clock) / ser_freq
# Adjust freq so estimated print_time will match at sync2_print_time
sync1_clock = self.print_time_to_clock(sync1_print_time)
sync2_clock = self.get_clock(sync2_sys_time)
#comment: 调整频率用一个很小的时间段估算,造成较大误差,sync1_clock的误差被放大.print_time值远大于(sync2_print_time - sync1_print_time)
#所以这种估算频率和时间偏移的算法对频率有较大误差时会造成无法同步.
sync1_clock = self.print_time_to_clock(sync1_print_time) #int((print_time - adjusted_offset) * adjusted_freq)
sync2_clock = self.get_clock(sync2_sys_time) #int(clock + (eventtime - sample_time) * freq) (clock_est的时钟,频率,估算值,不是标准值)
adjusted_freq = ((sync2_clock - sync1_clock)
/ (sync2_print_time - sync1_print_time))
adjusted_offset = sync1_print_time - sync1_clock / adjusted_freq

# add by guoge, avoid adjust vibration. 20231205
if abs(adjusted_offset) > 0.1 :
# add by guoge, avoid adjust vibration. 20231205. adjust offset can't less 0.
if adjusted_offset > 0.1 or adjusted_offset < 0.0 :
logging.info("\n *** Sync clock adjust too high, offset: %.3f freq:%.3f ", adjusted_offset, adjusted_freq)
adjusted_freq = self.mcu_freq + 1000 if adjusted_freq > self.mcu_freq else self.mcu_freq - 1000
adjusted_offset = 0.011 if adjusted_offset >0 else -0.011
# adjusted_freq = self.mcu_freq + 1000 if adjusted_freq > self.mcu_freq else self.mcu_freq - 1000
# adjusted_offset = 0.011 if adjusted_offset >0 else -0.011
adjusted_freq = self.mcu_freq
adjusted_offset = 0.030
# Apply new values
self.clock_adj = (adjusted_offset, adjusted_freq)
self.last_sync_time = sync2_print_time
logging.info(" *** Sync clock by Second clock sync. print time: %.3f @ event time: %.3f, adj off: %.3f, freq: %.3f", print_time, eventtime, adjusted_offset, adjusted_freq)
if abs(adjusted_offset) > 0.005 :
if abs(adjusted_offset) > 0.020 :
logging.info("ser_time:%.3f, ser_clock:%.3f, ser_freq:%.3f", ser_time, ser_clock, ser_freq )
logging.info("est_main_clock:%.3f, est_print_time:%.3f, sync1_print_time:%.3f, sync2_print_time:%.3f", est_main_clock, est_print_time, sync1_print_time, sync2_print_time )
logging.info("sync2_main_clock:%.3f, sync2_sys_time:%.3f, sync1_clock:%.3f, sync2_clock:%.3f", sync2_main_clock, sync2_sys_time, sync1_clock, sync2_clock )
sample_time, clock, freq = self.clock_est
logging.info("child mcu - sample_time:%.3f, clock:%.3f, freq:%.3f", sample_time, clock, freq )

return self.clock_adj

0 comments on commit fd7c1b0

Please sign in to comment.