Coverage for src / zooc / data / calibration_data.py: 74%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-11 21:45 +0000

1"""Calibration data for the ZOOC plugin.""" 

2from __future__ import annotations 

3 

4import textwrap 

5from dataclasses import dataclass, field 

6 

7from klipper_utils.klipper_utils import ConfigWrapper, PrinterConfig 

8 

9from .offset_data import OffsetData 

10 

11 

12@dataclass(kw_only=True, frozen=True) 

13class CalibrationData: 

14 """Calibration data of the Z-offsets and auxiliary data. 

15 

16 Data can be loaded and stored as Klipper configuration. 

17 

18 :param offset_data: Z-offset data for various bed and extruder temperatures. 

19 :param time_constant_bed: Bed's heat expansion time constant [s]. 

20 :param time_constant_extruder: Extruder's heat expansion time constant [s]. 

21 :param frame_expansion_coef: Frame expansion value. Reserved for future use. 

22 """ 

23 

24 @staticmethod 

25 def klipper_config(config: ConfigWrapper) -> CalibrationData: 

26 """Load and parse data from the configuration file. 

27 

28 Example:: 

29 

30 offset_data= 

31 bed_temp=30.0, extruder_temp=140.0, z_offset=3.4 

32 bed_temp=30.0, extruder_temp=180.0, z_offset=3.5 

33 ... 

34 time_constant_bed=100.0 

35 time_constant_extruder=30.0 

36 

37 :param config: Klipper configuration object to load the data from. 

38 :return: Calibration data. 

39 """ 

40 return CalibrationData( 

41 offset_data=OffsetData.build_text(config.getlists("offset_data", seps='\n', default=None)), 

42 time_constant_bed=config.getfloat("time_constant_bed", default=0.0), 

43 time_constant_extruder=config.getfloat("time_constant_extruder", default=0.0), 

44 frame_expansion_coef=config.getfloat("frame_expansion_coef", default=0.0), 

45 ) 

46 

47 offset_data: OffsetData = field(repr=True) 

48 time_constant_bed: float 

49 time_constant_extruder: float 

50 frame_expansion_coef: float 

51 

52 def to_config(self, configfile: PrinterConfig, name: str) -> None: 

53 """Save the configuration to a file. 

54 

55 :param configfile: Klipper configfile object. 

56 :param name: Name of the main section in configuration: [<name>]. 

57 """ 

58 configfile.set(name, "offset_data", f"\n{self.offset_data.to_string()}") 

59 configfile.set(name, "time_constant_bed", self.time_constant_bed) 

60 configfile.set(name, "time_constant_extruder", self.time_constant_extruder) 

61 configfile.set(name, "frame_expansion_coef", self.frame_expansion_coef) 

62 

63 def is_valid(self) -> bool: 

64 """Check whether the calibration data is valid. 

65 

66 :return: True when valid. 

67 """ 

68 return self.offset_data.is_valid() 

69 

70 def __str__(self) -> str: 

71 """Get data values in a user-friendly format. 

72 

73 :return: String representation of the calibration data. 

74 """ 

75 return (f"offset_data=\n{textwrap.indent(f"{self.offset_data}", '\t')}\n" 

76 f"time_constant_bed={self.time_constant_bed}\n" 

77 f"time_constant_extruder={self.time_constant_extruder}\n" 

78 # f"frame_expansion_coef={self.frame_expansion_coef}") 

79 )