-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresult.py
More file actions
121 lines (89 loc) · 3.23 KB
/
result.py
File metadata and controls
121 lines (89 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""Result calculation for segments and planner blocks."""
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pyGCodeDecode.planner_block import planner_block
from pyGCodeDecode.utils import segment
# new segment class spanned by pos, rest is "result"
class abstract_result(ABC):
"""Abstract class for result calculation."""
@property
@abstractmethod
def name(self):
"""Name of the result. Has to be set in the derived class."""
pass
@abstractmethod
def calc_pblock(self, pblock: "planner_block", **kwargs):
"""Calculate the result for a planner block."""
pass
@abstractmethod
def calc_segm(self, segm: "segment", **kwargs):
"""Calculate the result for a segment."""
pass
class acceleration_result(abstract_result):
"""The acceleration."""
name = "acceleration"
def calc_segm(self, segm: "segment", **kwargs):
"""Calculate the acceleration for a segment."""
delta_v = segm.vel_end.get_norm() - segm.vel_begin.get_norm()
delta_t = segm.get_segm_duration()
if delta_t > 0:
acc = delta_v / delta_t
else:
acc = 0
segm.result[self.name] = acc
def calc_pblock(self, pblock, **kwargs):
"""Calculate the acceleration for a planner block."""
for segm in pblock.segments:
self.calc_segm(segm, **kwargs)
class velocity_result(abstract_result):
"""The velocity."""
name = "velocity"
def calc_segm(self, segm: "segment", **kwargs):
"""Calculate the velocity for a segment."""
segm.result[self.name] = [
segm.vel_begin.get_norm(),
segm.vel_end.get_norm(),
]
def calc_pblock(self, pblock: "planner_block", **kwargs):
"""Calculate the velocity for a planner block."""
for segm in pblock.segments:
self.calc_segm(segm, **kwargs)
def get_all_result_calculators():
"""Get all results."""
public_results = [
acceleration_result(),
velocity_result(),
]
private_results = []
# Try to import private results if the module exists
try:
from pyGCodeDecode.private_result import get_private_result_calculators
private_results = get_private_result_calculators()
except ImportError:
# Private results module not available, continue with only public results
pass
return public_results + private_results
def has_private_results():
"""Check if private results are available."""
try:
from pyGCodeDecode.private_result import ( # noqa: F401
get_private_result_calculators,
)
return True
except ImportError:
return False
def get_result_info():
"""Get information about available result calculators."""
all_calcs = get_all_result_calculators()
public_count = 2 # acceleration_result and velocity_result
private_count = len(all_calcs) - public_count
return {
"total_count": len(all_calcs),
"public_count": public_count,
"private_count": private_count,
"has_private": has_private_results(),
"result_names": [calc.name for calc in all_calcs],
}
if __name__ == "__main__":
pass