Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 75 additions & 19 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1856,13 +1856,35 @@ struct IntentSession {
state: camlib::post_intent::IntentChecker,
initial: Vec<u8>,
}
/// The decoder's reading of each event's blocks (`Line.holds_axes`), a JSON
/// list of boolean lists; omitted when it held none.
fn held_blocks(held_json: Option<&str>) -> PyResult<Vec<Vec<bool>>> {
held_json
.map(serde_json::from_str)
.transpose()
.map_err(|e| PyValueError::new_err(e.to_string()))
.map(Option::unwrap_or_default)
}
#[pymethods]
impl IntentSession {
/// `held_json`: the decoder's reading of each initialization block, a
/// JSON list of booleans (`Line.holds_axes`); omitted when it held none.
#[new]
fn new(header_json: &str, settings_json: &str, lines_json: &str) -> PyResult<Self> {
if [header_json.len(), settings_json.len(), lines_json.len()]
.iter()
.any(|n| *n > 512 * 1024)
#[pyo3(signature=(header_json,settings_json,lines_json,held_json=None))]
fn new(
header_json: &str,
settings_json: &str,
lines_json: &str,
held_json: Option<&str>,
) -> PyResult<Self> {
if [
header_json.len(),
settings_json.len(),
lines_json.len(),
held_json.map_or(0, str::len),
]
.iter()
.any(|n| *n > 512 * 1024)
{
return Err(PyValueError::new_err("generic post header byte cap"));
}
Expand All @@ -1872,8 +1894,14 @@ impl IntentSession {
.map_err(|e| PyValueError::new_err(e.to_string()))?;
let lines: Vec<String> =
serde_json::from_str(lines_json).map_err(|e| PyValueError::new_err(e.to_string()))?;
let (state, first) = camlib::post_intent::IntentChecker::begin(&header, settings, &lines)
.map_err(PyValueError::new_err)?;
let held: Vec<bool> = held_json
.map(serde_json::from_str)
.transpose()
.map_err(|e| PyValueError::new_err(e.to_string()))?
.unwrap_or_default();
let (state, first) =
camlib::post_intent::IntentChecker::begin_held(&header, settings, &lines, &held)
.map_err(PyValueError::new_err)?;
let initial =
serde_json::to_vec(&first).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(Self { state, initial })
Expand Down Expand Up @@ -1985,19 +2013,25 @@ impl IntentSession {
let bytes = serde_json::to_vec(&out).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(PyBytes::new(py, &bytes))
}
#[pyo3(signature=(ranges_json,decoded_json,blocks_json,cancel=None))]
#[pyo3(signature=(ranges_json,decoded_json,blocks_json,cancel=None,held_json=None))]
fn check_lowered<'py>(
&mut self,
py: Python<'py>,
ranges_json: &str,
decoded_json: &str,
blocks_json: &str,
cancel: Option<PyRef<'_, PostCancellation>>,
held_json: Option<&str>,
) -> PyResult<Bound<'py, PyBytes>> {
if !self.initial.is_empty()
|| [ranges_json.len(), decoded_json.len(), blocks_json.len()]
.iter()
.any(|n| *n > 512 * 1024)
|| [
ranges_json.len(),
decoded_json.len(),
blocks_json.len(),
held_json.map_or(0, str::len),
]
.iter()
.any(|n| *n > 512 * 1024)
{
return Err(PyValueError::new_err(
"generic lowered lifecycle/input byte cap",
Expand All @@ -2009,26 +2043,36 @@ impl IntentSession {
serde_json::from_str(decoded_json).map_err(|e| PyValueError::new_err(e.to_string()))?;
let blocks: Vec<Vec<String>> =
serde_json::from_str(blocks_json).map_err(|e| PyValueError::new_err(e.to_string()))?;
let held = held_blocks(held_json)?;
let flag = cancel.map(|c| c.flag.clone()).unwrap_or_default();
let out = py
.detach(|| self.state.check_lowered(&ranges, &decoded, &blocks, &*flag))
.detach(|| {
self.state
.check_lowered_held(&ranges, &decoded, &blocks, &held, &*flag)
})
.map_err(PyValueError::new_err)?;
let bytes = serde_json::to_vec(&out).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(PyBytes::new(py, &bytes))
}
#[pyo3(signature=(events_json,decoded_json,blocks_json,cancel=None))]
#[pyo3(signature=(events_json,decoded_json,blocks_json,cancel=None,held_json=None))]
fn consume<'py>(
&mut self,
py: Python<'py>,
events_json: &str,
decoded_json: &str,
blocks_json: &str,
cancel: Option<PyRef<'_, PostCancellation>>,
held_json: Option<&str>,
) -> PyResult<Bound<'py, PyBytes>> {
if !self.initial.is_empty()
|| [events_json.len(), decoded_json.len(), blocks_json.len()]
.iter()
.any(|n| *n > 512 * 1024)
|| [
events_json.len(),
decoded_json.len(),
blocks_json.len(),
held_json.map_or(0, str::len),
]
.iter()
.any(|n| *n > 512 * 1024)
{
return Err(PyValueError::new_err(
"generic post lifecycle/input byte cap",
Expand All @@ -2040,10 +2084,13 @@ impl IntentSession {
serde_json::from_str(decoded_json).map_err(|e| PyValueError::new_err(e.to_string()))?;
let blocks: Vec<Vec<String>> =
serde_json::from_str(blocks_json).map_err(|e| PyValueError::new_err(e.to_string()))?;
let held = held_blocks(held_json)?;
let flag = cancel.map(|c| c.flag.clone()).unwrap_or_default();
let bytes = py
.detach(|| {
let out = self.state.consume(&events, &decoded, &blocks, &*flag)?;
let out = self
.state
.consume_held(&events, &decoded, &blocks, &held, &*flag)?;
serde_json::to_vec(&out).map_err(|e| e.to_string())
})
.map_err(PyValueError::new_err)?;
Expand Down Expand Up @@ -2143,21 +2190,30 @@ impl IntentSession {
(placed[0], placed[1], placed[2]),
))
}
#[pyo3(signature=(lines_json,cancel=None))]
#[pyo3(signature=(lines_json,cancel=None,held_json=None))]
fn finish<'py>(
&mut self,
py: Python<'py>,
lines_json: &str,
cancel: Option<PyRef<'_, PostCancellation>>,
held_json: Option<&str>,
) -> PyResult<Bound<'py, PyBytes>> {
if !self.initial.is_empty() || lines_json.len() > 32768 {
if !self.initial.is_empty()
|| lines_json.len() > 32768
|| held_json.is_some_and(|h| h.len() > 32768)
{
return Err(PyValueError::new_err("generic finish byte cap"));
}
let lines: Vec<String> =
serde_json::from_str(lines_json).map_err(|e| PyValueError::new_err(e.to_string()))?;
let held: Vec<bool> = held_json
.map(serde_json::from_str)
.transpose()
.map_err(|e| PyValueError::new_err(e.to_string()))?
.unwrap_or_default();
let flag = cancel.map(|c| c.flag.clone()).unwrap_or_default();
let out = py
.detach(|| self.state.finish(&lines, &*flag))
.detach(|| self.state.finish_held(&lines, &held, &*flag))
.map_err(PyValueError::new_err)?;
let bytes = serde_json::to_vec(&out).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(PyBytes::new(py, &bytes))
Expand Down
4 changes: 3 additions & 1 deletion docs/fanuc-0imb-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ are retained privately; the manual is not redistributed in the library.
G43/H and G49 can cause axis movement. The reference therefore uses explicit
G1 compensation blocks whose spindle-axis endpoints preserve the declared tip
position. G52 clears only the local shift; a zero G92 shift remains a setup
premise. Units agree with startup offset-table units. Decimal points avoid DPI
premise. G52 moves no axis: the decoder reads its X/Y/Z words as the shift
(`hold_axes`), so the machine check keeps a start pose on the travel limits
exactly there (#369). Units agree with startup offset-table units. Decimal points avoid DPI
parameter ambiguity. G61 selects exact-stop mode without asserting zero physical
servo error. M-code functions and ATC routes depend on the builder.

Expand Down
3 changes: 3 additions & 0 deletions docs/haas-ngc-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ length geometry+wear, G92 and machine setup remain external measured premises.
Initialization: framed `%`/O program, explicit units/G90/G17/G94/G40/G49/G80,
G52 X0 Y0 Z0, conditional G50/G69, G100 X0 Y0 Z0, plain G187 reset, G61 exact stop,
M5/M9. Decoder requires the closing percent after M30 before file completion.
G52 and G100 set a shift and a mode and move no axis: both decoders read their
X/Y/Z words as parameters (`hold_axes`), so the machine check keeps a start
pose on the travel limits exactly there (#369).
G61 supplies the documented nominal stop at every commanded endpoint. No G64,
G187 P/E blending model or automatic corner rounding/chamfer words. This proves
nominal paths under the declared model, not servo/acceleration/physical accuracy.
Expand Down
4 changes: 3 additions & 1 deletion docs/machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ start pose or a waypoint set it is still exactly there, at the start of every
move and along any move that does not command it. A mill homed on its limits
(X 0 of travel [0, 508], say) therefore starts its first moves inside. A block
commands an axis by writing its word, even one equal to the current position,
or by moving along it; the exact rule is in
or by moving along it, but not by naming it as a parameter of an offset or
mode setting its decoder reads as moving nothing (the Haas and FANUC header's
`G52 X0 Y0 Z0`); the exact rule is in
[the travel rule](python.md#the-travel-rule). A decoded arc
whose words leave an endpoint gap is known to that gap either side of its
nominal circle.
Expand Down
5 changes: 5 additions & 0 deletions docs/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ while that axis is still where the controller placed it itself (#367):
controller post writes itself (a FANUC `G1 G43 H.. Z..`, a Carvera
`G53 G0 Z-3`). A probing cycle leaves every axis written, since its macros
move axes their blocks need not name.
- A block that only sets an offset or a mode, naming axis letters as its
parameters (`G52 X0 Y0 Z0`, Haas `G100 X0 Y0 Z0`), writes no axis word:
the decoder reads it as moving no axis (`GCodeDecoder.hold_axes`), and
the post and the machine check both leave the axes it names placed. A
block no decoder classifies so counts every axis it names.

An axis word commands the axis to that work coordinate through the
controller's *actual* work offset, even at the value it already has. Where
Expand Down
9 changes: 9 additions & 0 deletions docs/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ and route waypoints.
and an absent word is the only way to leave an axis where it is;
- a block that writes an axis word and decodes to no move (its words equal
the decoded position) commands that axis all the same, for the next move;
- a block the independent decoder reads as setting an offset or a mode
without moving any axis commands none of the axes it names: there the
letters are parameters, not positions. The controller decoders read so
the local-shift reset `G52 X0 Y0 Z0` (Haas NGC, FANUC 0i-MB, Centroid
CNC12 mill, plasma and lathe, LNC), the Haas mirror-image cancel `G100
X0 Y0 Z0` and the Siemens 808D ISO `G50.1`/`G92.1 X0. Y0. Z0.` resets,
each from its manual. The decoder says so itself
(`GCodeDecoder.hold_axes`, recorded as `holds_axes` on the posted line);
a block no decoder classifies counts every axis it names;
- a route waypoint makes the axes it names exact again; an axis it retains
stays as it was.

Expand Down
3 changes: 3 additions & 0 deletions examples/centroid_lathe_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def read_line(self, line, context=None):
return [CutterCompensation(self.compensation, 'xz')]
if text == 'G52 X0 Z0':
self.ready.add('frame_reset')
# 9.17: G52 with zero shifts cancels any local offset; it moves
# no axis, the X/Z words are the shift.
self.hold_axes()
return []
words = text.split(' ')
if words[0] == d.code('rapid'):
Expand Down
5 changes: 4 additions & 1 deletion examples/centroid_plasma_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def read_line(self, line, context=None):
self.length = None
return super().read_line(line, context)
if text == 'G52 X0 Y0 Z0':
# Cancels any local offset: the frame reset.
# Cancels any local offset: the frame reset. CNC12 G52 (Mill
# Operator's Manual 12.20, the same CNC12 software) moves no axis:
# the X/Y/Z words are the shift.
self.hold_axes()
return super().read_line('G52', context)
return super().read_line(line, context)
3 changes: 3 additions & 0 deletions examples/centroid_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def read_line(self, line, context=None):
if set(values) != {'G', 'X', 'Y', 'Z'} or any(values[a] != 0 for a in 'XYZ'):
raise ValueError('only an explicit zero XYZ G52 reset is supported')
self.ready.add('frame_reset')
# 12.20: G52 shifts the local coordinate system (zero cancels the
# shift); it moves no axis, the X/Y/Z words are the shift.
self.hold_axes()
return []
if values.get('G') in (73, 83, 84) and any('.' not in text for w, text in pieces if w == 'Q'):
# Parameters 245/246 may reinterpret an undotted Q (implied decimals).
Expand Down
4 changes: 4 additions & 0 deletions examples/fanuc_0imb_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ def read_line(self, line, context=None):
if set(values) != {'X', 'Y', 'Z'} or any(values[a] != 0 for a in 'XYZ'):
raise ValueError('only explicit all-axis local zero supported')
self.ready.add('frame_reset')
# B-63844EN, local coordinate system (printed pp. 79-89): G52 IP_
# sets the local shift, G52 with zero cancels it; the block moves
# no axis, its X/Y/Z words are the shift.
self.hold_axes()
return []
if values.get('M') in (0, 1, int(self.machine.dialect.code('tool_change')[1:])):
if self.rpm or self.coolant != 'off':
Expand Down
5 changes: 5 additions & 0 deletions examples/haas_ngc_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ def read_line(self, line, context=None):
self.ready.add('frame_reset')
else:
self.mirror_off = True
# 96-8210 Rev N, G-codes: G52 sets a coordinate shift (zero
# clears it) and G100 cancels mirror image on the axes it names.
# Neither commands motion: the X/Y/Z words are the shift and the
# axes selected, not positions.
self.hold_axes()
return []
if g in (50, 69, 187):
if set(values) != {'G'} or g in (50, 69) and not self.machine.rotation_scaling:
Expand Down
3 changes: 3 additions & 0 deletions examples/lnc_mill_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ def read_line(self, line, context=None):
if set(values) != {'X', 'Y', 'Z'} or any(values[a] != 0 for a in 'XYZ'):
raise ValueError('only an explicit all-axis G52 zero is supported')
self.ready.add('frame_reset')
# p.48: G52 X0 Y0 Z0 clears the local coordinate system; it moves
# no axis, the X/Y/Z words are the shift.
self.hold_axes()
return []
tool_change = int(self.machine.dialect.code('tool_change')[1:])
if values.get('M') in (0, 1, tool_change):
Expand Down
4 changes: 4 additions & 0 deletions examples/siemens808d_iso_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ def read_line(self, line, context=None):
if self.resets != {50, 69, 50.1}:
raise ValueError('cancel transforms before coordinate reset')
self.ready.add('frame_reset')
# ISO pp104-113: G50.1 cancels mirroring on the axes it names and
# G92.1 X0. Y0. Z0. resets the G92/G52 shifts of those axes;
# neither moves an axis, the X/Y/Z words select the axes.
self.hold_axes()
return []
if (m in (0, 1, 6) or 'T' in values) and (self.rpm or self.coolant != 'off'):
raise ValueError('explicit spindle/coolant off required before OEM stop/tool change')
Expand Down
Loading
Loading